pinchbv / floor

The typesafe, reactive, and lightweight SQLite abstraction for your Flutter applications
https://pinchbv.github.io/floor/
Apache License 2.0
947 stars 190 forks source link

feat: add external path support #822

Open lucasdidur opened 5 months ago

lucasdidur commented 5 months ago

Add support for Floor to set a external path to load/save database. App need to have storage permission managed by Permissions Manager.

  Future<AppDatabase> get appDatabase async {
    String externalPath = await ExternalPath.getExternalStoragePublicDirectory(
      ExternalPath.DIRECTORY_DOCUMENTS,
    );
    var dir = Directory(p.join(externalPath, 'My App'));
    if (!dir.existsSync()) {
      dir.create(recursive: true);
    }

    return $FloorAppDatabase.databaseBuilder(p.join(dir.path, DATABASE_NAME)).build();
  }
ryanheise commented 2 months ago

I like this PR. For most apps, the database should probably not be stored where this package wants to store it - path_provider.getApplicationSupportDirectory() would be better in most cases. The sqflite documentation says as much, and suggests not using getDatabasesPath() in favour of using path_provider directly, and constructing your own database path to pass into the API.