-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathauth_database.dart
More file actions
84 lines (75 loc) · 2.16 KB
/
auth_database.dart
File metadata and controls
84 lines (75 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import 'package:celest_cloud_auth/src/database/auth_database.drift.dart';
import 'package:celest_cloud_auth/src/database/auth_database_accessors.dart';
import 'package:drift/drift.dart' hide Component;
import 'package:drift/native.dart';
import 'package:file/file.dart';
@Deprecated('Use CloudAuthDatabase instead')
typedef AuthDatabase = CloudAuthDatabase;
@DriftDatabase(include: CloudAuthDatabaseMixin.includes)
class CloudAuthDatabase extends $CloudAuthDatabase with CloudAuthDatabaseMixin {
CloudAuthDatabase(
super.e, {
ResolvedProject? project,
}) : _project = project;
factory CloudAuthDatabase.localDir(
Directory dir, {
ResolvedProject? project,
bool verbose = false,
}) {
return CloudAuthDatabase(
_openDirConnection(dir, verbose: verbose),
project: project,
);
}
factory CloudAuthDatabase.memory({
bool verbose = false,
ResolvedProject? project,
}) {
final nativeDb = NativeDatabase.memory(
logStatements: verbose,
cachePreparedStatements: true,
);
return CloudAuthDatabase(nativeDb, project: project);
}
static QueryExecutor _openDirConnection(
Directory dir, {
required bool verbose,
}) {
return LazyDatabase(() async {
final file = dir.childFile('auth.celest.db');
if (!file.existsSync()) {
await file.create(recursive: true);
}
return NativeDatabase(
file,
logStatements: verbose,
cachePreparedStatements: true,
enableMigrations: true,
);
});
}
final ResolvedProject? _project;
@override
int get schemaVersion => 5;
@override
MigrationStrategy get migration {
return MigrationStrategy(
onCreate: (m) async {
await m.createAll();
},
onUpgrade: (m, from, to) async {
await cloudAuth.onUpgrade(m, from: from, to: to);
},
beforeOpen: (details) async {
if (details.versionNow != schemaVersion) {
return;
}
await cloudAuth.onBeforeOpen(details, project: _project);
},
);
}
/// Pings the database to ensure a valid connection.
Future<void> ping() async {
await customSelect('SELECT 1').get();
}
}