openDatabase abstract method
Future<SdbDatabase>
openDatabase(
- String name, {
- SdbOpenDatabaseOptions? options,
- int? version,
- SdbOnVersionChangeCallback? onVersionChange,
- SdbDatabaseSchema? schema,
inherited
Open a database.
name is the path of the database.
version is the version of the database. If the existing database has a
lower version, onVersionChange will be called.
onVersionChange is called when the database is created or upgraded.
schema provides an automatic way to handle version changes.
Either onVersionChange or schema should be provided for schema
definition and migration.
Example:
class SchoolDb {
final schoolStore = SdbStoreRef<String, SdbModel>('school');
final studentStore = SdbStoreRef<int, SdbModel>('student');
/// Index on studentStore for field 'schoolId'
late final studentSchoolIndex = studentStore.index<String>(
'school',
); // On field 'schoolId'
late final schoolDbSchema = SdbDatabaseSchema(
stores: [
schoolStore.schema(),
studentStore.schema(
autoIncrement: true,
indexes: [studentSchoolIndex.schema(keyPath: 'schoolId')],
),
],
);
Future<SdbDatabase> open(SdbFactory factory, String dbName) async {
return factory.openDatabase(
dbName,
options: SdbOpenDatabaseOptions(
version: 1,
schema: schoolDbSchema,
),
);
}
}
Implementation
Future<SdbDatabase> openDatabase(
String name, {
/// Options for opening a Sdb database (prefer options over version and schema).
SdbOpenDatabaseOptions? options,
/// The version of the database, prefer options
int? version,
/// Either provide onVersionChange to handle schema changes
/// manually...
SdbOnVersionChangeCallback? onVersionChange,
/// ...or provide a schema to have it applied automatically.
/// Prefer options
SdbDatabaseSchema? schema,
});