Skip to content

Commit 302b5d8

Browse files
committed
chore(core): Add setup parameter to connect
Allow passing a `setup` parameter to the DB `connect` functon.
1 parent 831a283 commit 302b5d8

File tree

3 files changed

+25
-8
lines changed

3 files changed

+25
-8
lines changed

packages/celest/lib/src/runtime/data/connect.dart

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import 'package:celest/src/runtime/data/connect.io.dart'
66
import 'package:drift/drift.dart';
77
import 'package:drift_hrana/drift_hrana.dart';
88
import 'package:logging/logging.dart';
9+
import 'package:sqlite3/common.dart' as sqlite3;
910

1011
final Logger _logger = Logger('Celest.Data');
1112

@@ -25,12 +26,17 @@ Future<Database> connect<Database extends GeneratedDatabase>(
2526
required Database Function(QueryExecutor) factory,
2627
required env hostnameVariable,
2728
required secret tokenSecret,
29+
void Function(sqlite3.CommonDatabase)? setup,
2830
String? path,
2931
}) async {
3032
final host = context.get(hostnameVariable);
3133
if (host == null) {
3234
if (context.environment == Environment.local) {
33-
final executor = await localExecutor(name: name, path: path);
35+
final executor = await localExecutor(
36+
name: name,
37+
path: path,
38+
setup: setup,
39+
);
3440
return _checkConnection(factory(executor));
3541
}
3642
throw StateError(
@@ -48,9 +54,9 @@ Future<Database> connect<Database extends GeneratedDatabase>(
4854
final QueryExecutor connector;
4955
switch (hostUri) {
5056
case Uri(scheme: 'file', path: '/:memory:'):
51-
connector = await inMemoryExecutor();
57+
connector = await inMemoryExecutor(setup: setup);
5258
case Uri(scheme: 'file', :final path):
53-
connector = await localExecutor(name: name, path: path);
59+
connector = await localExecutor(name: name, path: path, setup: setup);
5460
case Uri(scheme: 'ws' || 'wss' || 'http' || 'https' || 'libsql'):
5561
final token = context.get(tokenSecret);
5662
if (token == null) {

packages/celest/lib/src/runtime/data/connect.io.dart

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,21 @@ import 'package:drift/drift.dart';
55
import 'package:drift/native.dart';
66
import 'package:logging/logging.dart';
77
import 'package:path/path.dart' as p;
8+
import 'package:sqlite3/common.dart' as sqlite3;
89

910
final Logger _logger = Logger('Celest.Data');
1011

1112
/// A [QueryExecutor] for an in-memory database.
12-
Future<QueryExecutor> inMemoryExecutor() async => NativeDatabase.memory();
13+
Future<QueryExecutor> inMemoryExecutor({
14+
void Function(sqlite3.CommonDatabase)? setup,
15+
}) async {
16+
return NativeDatabase.memory(setup: setup);
17+
}
1318

1419
/// A [QueryExecutor] with local persistence.
1520
Future<QueryExecutor> localExecutor({
1621
required String name,
22+
void Function(sqlite3.CommonDatabase)? setup,
1723
String? path,
1824
}) async {
1925
if (path == null) {
@@ -28,7 +34,7 @@ Future<QueryExecutor> localExecutor({
2834
'Failed to determine package config path. '
2935
'Falling back to in-memory database.',
3036
);
31-
return inMemoryExecutor();
37+
return inMemoryExecutor(setup: setup);
3238
}
3339
path = p.join(
3440
p.dirname(p.fromUri(packageConfig)),
@@ -45,5 +51,6 @@ Future<QueryExecutor> localExecutor({
4551
databaseFile,
4652
cachePreparedStatements: true,
4753
enableMigrations: true,
54+
setup: setup,
4855
);
4956
}
Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
import 'package:drift/drift.dart';
22
import 'package:drift/wasm.dart';
3+
import 'package:sqlite3/common.dart' as sqlite3;
34
import 'package:sqlite3/wasm.dart';
45

56
/// A [QueryExecutor] for an in-memory database.
6-
Future<QueryExecutor> inMemoryExecutor() async {
7+
Future<QueryExecutor> inMemoryExecutor({
8+
void Function(sqlite3.CommonDatabase)? setup,
9+
}) async {
710
final sqlite3 = await WasmSqlite3.loadFromUrl(Uri.parse('./sqlite3.wasm'));
811
sqlite3.registerVirtualFileSystem(InMemoryFileSystem(), makeDefault: true);
9-
return WasmDatabase.inMemory(sqlite3);
12+
return WasmDatabase.inMemory(sqlite3, setup: setup);
1013
}
1114

1215
/// A [QueryExecutor] with local persistence.
1316
Future<QueryExecutor> localExecutor({
1417
required String name,
18+
void Function(sqlite3.CommonDatabase)? setup,
1519
String? path,
1620
}) async {
1721
// TODO(dnys1): We don't have a use case for this yet.
18-
return inMemoryExecutor();
22+
return inMemoryExecutor(setup: setup);
1923
}

0 commit comments

Comments
 (0)