initialize static method

Future<QuickeyDBImpl> initialize({
  1. required String dbName,
  2. required int dbVersion,
  3. required List<DataAccessObject>? dataAccessObjects,
  4. String? dbPath,
  5. bool debugging = true,
  6. bool importDB = false,
  7. bool? persist = false,
})

Implementation

static Future<QuickeyDBImpl> initialize({
  ///set a database name i.e 'tascan_db'
  required final String dbName,
  required final int dbVersion,
  required final List<DataAccessObject>? dataAccessObjects,
  final String? dbPath,
  final bool debugging = true,
  final bool importDB = false,
  final bool? persist = false,
}) async {
  assert(dbVersion > 0);
  assert(dataAccessObjects != null && dataAccessObjects.isNotEmpty);
  assert(persist != null);

  final databasePath = persist! == false
      ? inMemoryDatabasePath
      : '${dbPath ?? await databaseFactoryFfiWeb.getDatabasesPath()}/$dbName.db';

  return _getInstance = QuickeyDBImpl(
    logger: debugging,
    dataAccessObjects: {
      for (var dao in dataAccessObjects!) dao.runtimeType: dao
    },
    database: await databaseFactoryFfiWeb.openDatabase(databasePath,
        options: OpenDatabaseOptions(
            version: dbVersion,
            onCreate: (Database? database, _) async {
              await Future.forEach(
                  dataAccessObjects.map((dao) => dao.schema.sql),
                  database!.execute);
            },
            onUpgrade: (Database database, int from, int to) async {
              if (debugging) {
                if (kDebugMode) {
                  print('Upgrading from $from to $to');
                }
              }
              final migration =
                  Migration(database: database, logger: debugging);
              await Future.forEach(dataAccessObjects, migration.force);
            },
            onDowngrade: (Database database, int from, int to) async {
              if (debugging) {
                if (kDebugMode) {
                  print('Downgrading from $from to $to');
                }
              }
              final migration =
                  Migration(database: database, logger: debugging);
              await Future.forEach(dataAccessObjects, migration.force);
            })),
  );
}