connect function
- UserId? userId
Obtains a database connection for running drift
in a Dart VM.
Implementation
QueryExecutor connect([UserId? userId]) {
return LazyDatabase(() async {
final Directory dbFolder;
if (PlatformUtils.isIOS) {
dbFolder = Directory(await IosUtils.getSharedDirectory());
} else {
dbFolder = await PlatformUtils.libraryDirectory;
}
Log.debug(
'connect() -> `drift` will place its files to `${dbFolder.path}`.',
);
final File file = File(
p.join(dbFolder.path, '${userId?.val ?? 'common'}.sqlite'),
);
// Workaround limitations on old Android versions.
if (Platform.isAndroid) {
await applyWorkaroundToOpenSqlite3OnOldAndroidVersions();
}
// Make `sqlite3` pick a more suitable location for temporary files - the
// one from the system may be inaccessible due to sandboxing.
final String cache = (await getTemporaryDirectory()).path;
// We can't access `/tmp` on Android, which `sqlite3` would try by default.
// Explicitly tell it about the correct temporary directory.
sqlite3.tempDirectory = cache;
return NativeDatabase.createInBackground(
file,
setup: (db) => db.execute('PRAGMA journal_mode = wal'),
);
});
}