@syncular/dialect-expo-sqlite 0.0.1-118 → 0.0.1-122
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +3 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +35 -4
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/src/index.test.ts +141 -1
- package/src/index.ts +38 -4
package/dist/index.d.ts
CHANGED
|
@@ -6,7 +6,9 @@
|
|
|
6
6
|
* SQLite-compatible — use with @syncular/server-dialect-sqlite.
|
|
7
7
|
*
|
|
8
8
|
* Implements a custom Kysely Driver that wraps expo-sqlite's sync API
|
|
9
|
-
* into the promise-based interface Kysely expects.
|
|
9
|
+
* into the promise-based interface Kysely expects. All operations are
|
|
10
|
+
* serialized through a single connection to prevent "database is locked"
|
|
11
|
+
* errors from concurrent access on the native handle.
|
|
10
12
|
*/
|
|
11
13
|
import { SerializePlugin } from '@syncular/core';
|
|
12
14
|
import type { DatabaseIntrospector, Dialect, DialectAdapter, Driver, QueryCompiler } from 'kysely';
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAC;AACjD,OAAO,KAAK,EAEV,oBAAoB,EACpB,OAAO,EACP,cAAc,EACd,MAAM,EACN,aAAa,EAGd,MAAM,QAAQ,CAAC;AAChB,OAAO,EAEL,MAAM,EAIP,MAAM,QAAQ,CAAC;AAEhB,MAAM,MAAM,mBAAmB,GAAG,IAAI,GAAG,MAAM,GAAG,MAAM,GAAG,UAAU,CAAC;AAEtE,MAAM,MAAM,oBAAoB,GAAG,mBAAmB,EAAE,CAAC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IAED,aAAa,IAAI,cAAc,CAE9B;IAED,YAAY,IAAI,MAAM,CAErB;IAED,mBAAmB,IAAI,aAAa,CAEnC;IAED,kBAAkB,CAAC,EAAE,EAAE,MAAM,CAAC,OAAO,CAAC,GAAG,oBAAoB,CAE5D;CACF"}
|
package/dist/index.js
CHANGED
|
@@ -6,7 +6,9 @@
|
|
|
6
6
|
* SQLite-compatible — use with @syncular/server-dialect-sqlite.
|
|
7
7
|
*
|
|
8
8
|
* Implements a custom Kysely Driver that wraps expo-sqlite's sync API
|
|
9
|
-
* into the promise-based interface Kysely expects.
|
|
9
|
+
* into the promise-based interface Kysely expects. All operations are
|
|
10
|
+
* serialized through a single connection to prevent "database is locked"
|
|
11
|
+
* errors from concurrent access on the native handle.
|
|
10
12
|
*/
|
|
11
13
|
import { SerializePlugin } from '@syncular/core';
|
|
12
14
|
import { CompiledQuery, Kysely, SqliteAdapter, SqliteIntrospector, SqliteQueryCompiler, } from 'kysely';
|
|
@@ -44,6 +46,31 @@ export function createSerializePlugin() {
|
|
|
44
46
|
return new SerializePlugin();
|
|
45
47
|
}
|
|
46
48
|
// ---------------------------------------------------------------------------
|
|
49
|
+
// Simple async mutex — serializes all DB access on a single native handle.
|
|
50
|
+
// ---------------------------------------------------------------------------
|
|
51
|
+
class Mutex {
|
|
52
|
+
#queue = [];
|
|
53
|
+
#locked = false;
|
|
54
|
+
async acquire() {
|
|
55
|
+
if (!this.#locked) {
|
|
56
|
+
this.#locked = true;
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
return new Promise((resolve) => {
|
|
60
|
+
this.#queue.push(resolve);
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
release() {
|
|
64
|
+
const next = this.#queue.shift();
|
|
65
|
+
if (next) {
|
|
66
|
+
next();
|
|
67
|
+
}
|
|
68
|
+
else {
|
|
69
|
+
this.#locked = false;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
// ---------------------------------------------------------------------------
|
|
47
74
|
// Kysely Dialect implementation for expo-sqlite
|
|
48
75
|
// ---------------------------------------------------------------------------
|
|
49
76
|
class ExpoSqliteDialect {
|
|
@@ -67,6 +94,8 @@ class ExpoSqliteDialect {
|
|
|
67
94
|
class ExpoSqliteDriver {
|
|
68
95
|
#options;
|
|
69
96
|
#db;
|
|
97
|
+
#connection;
|
|
98
|
+
#mutex = new Mutex();
|
|
70
99
|
constructor(options) {
|
|
71
100
|
this.#options = options;
|
|
72
101
|
}
|
|
@@ -77,12 +106,14 @@ class ExpoSqliteDriver {
|
|
|
77
106
|
this.#db.runSync('PRAGMA journal_mode = WAL', []);
|
|
78
107
|
// Wait up to 5s for locks to clear instead of failing immediately.
|
|
79
108
|
this.#db.runSync('PRAGMA busy_timeout = 5000', []);
|
|
109
|
+
this.#connection = new ExpoSqliteConnection(this.#db);
|
|
80
110
|
}
|
|
81
111
|
async acquireConnection() {
|
|
82
|
-
|
|
112
|
+
await this.#mutex.acquire();
|
|
113
|
+
return this.#connection;
|
|
83
114
|
}
|
|
84
115
|
async beginTransaction(connection, _settings) {
|
|
85
|
-
await connection.executeQuery(CompiledQuery.raw('begin'));
|
|
116
|
+
await connection.executeQuery(CompiledQuery.raw('begin immediate'));
|
|
86
117
|
}
|
|
87
118
|
async commitTransaction(connection) {
|
|
88
119
|
await connection.executeQuery(CompiledQuery.raw('commit'));
|
|
@@ -91,7 +122,7 @@ class ExpoSqliteDriver {
|
|
|
91
122
|
await connection.executeQuery(CompiledQuery.raw('rollback'));
|
|
92
123
|
}
|
|
93
124
|
async releaseConnection(_connection) {
|
|
94
|
-
|
|
125
|
+
this.#mutex.release();
|
|
95
126
|
}
|
|
96
127
|
async destroy() {
|
|
97
128
|
if (this.#db) {
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","s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AAG,CAAC,GAAG,UAAU,CAAyB,CAAC;QAEvD,MAAM,YAAY,GAAG,gBAAgB,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;QAChD,MAAM,YAAY,GAAG,qCAAqC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;QAErE,IAAI,YAAY,IAAI,YAAY,EAAE,CAAC;YACjC,MAAM,IAAI,GAAG,IAAI,CAAC,GAAG,CAAC,UAAU,CAAI,GAAG,EAAE,MAAM,CAAC,CAAC;YACjD,MAAM,cAAc,GAAG,IAAI,IAAI,EAAE,CAAC;YAClC,OAAO;gBACL,IAAI,EAAE,cAAc;gBACpB,GAAG,CAAC,YAAY;oBACd,CAAC,CAAC,EAAE,eAAe,EAAE,MAAM,CAAC,cAAc,CAAC,MAAM,CAAC,EAAE;oBACpD,CAAC,CAAC,EAAE,CAAC;aACR,CAAC;QACJ,CAAC;QAED,gFAA8E;QAC9E,MAAM,MAAM,GAAwB,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,GAAG,EAAE,MAAM,CAAC,CAAC;QAClE,OAAO;YACL,IAAI,EAAE,EAAE;YACR,eAAe,EAAE,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC;YACvC,QAAQ,EAAE,MAAM,CAAC,MAAM,CAAC,eAAe,CAAC;SACzC,CAAC;IAAA,CACH;IAED,WAAW,CACT,cAA6B,EAC7B,UAAmB,EACoB;QACvC,MAAM,IAAI,KAAK,CAAC,+CAA+C,CAAC,CAAC;IAAA,CAClE;CACF"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@syncular/dialect-expo-sqlite",
|
|
3
|
-
"version": "0.0.1-
|
|
3
|
+
"version": "0.0.1-122",
|
|
4
4
|
"description": "Expo SQLite dialect for the Syncular client",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Benjamin Kniffler",
|
|
@@ -43,7 +43,7 @@
|
|
|
43
43
|
"release": "bunx syncular-publish"
|
|
44
44
|
},
|
|
45
45
|
"dependencies": {
|
|
46
|
-
"@syncular/core": "0.0.1-
|
|
46
|
+
"@syncular/core": "0.0.1-122"
|
|
47
47
|
},
|
|
48
48
|
"peerDependencies": {
|
|
49
49
|
"expo-sqlite": "^16.0.10",
|
package/src/index.test.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
+
import { Database } from 'bun:sqlite';
|
|
1
2
|
import { afterEach, beforeEach, describe, expect, it } from 'bun:test';
|
|
2
|
-
import type
|
|
3
|
+
import { type Kysely, sql } from 'kysely';
|
|
3
4
|
import type {
|
|
4
5
|
ExpoSqliteBindParams,
|
|
5
6
|
ExpoSqliteBindValue,
|
|
@@ -103,3 +104,142 @@ describe('expo sqlite dialect RETURNING behavior', () => {
|
|
|
103
104
|
await db.destroy();
|
|
104
105
|
});
|
|
105
106
|
});
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Adapter that wraps bun:sqlite Database to match ExpoSqliteDatabaseLike,
|
|
110
|
+
* so we can run real SQLite concurrency tests without expo-sqlite.
|
|
111
|
+
*/
|
|
112
|
+
class BunSqliteAdapter implements ExpoSqliteDatabaseLike {
|
|
113
|
+
readonly #db: Database;
|
|
114
|
+
|
|
115
|
+
constructor(path = ':memory:') {
|
|
116
|
+
this.#db = new Database(path);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
getAllSync<R>(sqlStr: string, params: ExpoSqliteBindParams): R[];
|
|
120
|
+
getAllSync<R>(sqlStr: string, ...params: readonly ExpoSqliteBindValue[]): R[];
|
|
121
|
+
getAllSync<R>(
|
|
122
|
+
sqlStr: string,
|
|
123
|
+
...paramsOrFirst:
|
|
124
|
+
| readonly [ExpoSqliteBindParams]
|
|
125
|
+
| readonly ExpoSqliteBindValue[]
|
|
126
|
+
): R[] {
|
|
127
|
+
const p = this.#resolveParams(paramsOrFirst);
|
|
128
|
+
return this.#db.prepare(sqlStr).all(...p) as R[];
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
runSync(sqlStr: string, params: ExpoSqliteBindParams): ExpoSqliteRunResult;
|
|
132
|
+
runSync(
|
|
133
|
+
sqlStr: string,
|
|
134
|
+
...params: readonly ExpoSqliteBindValue[]
|
|
135
|
+
): ExpoSqliteRunResult;
|
|
136
|
+
runSync(
|
|
137
|
+
sqlStr: string,
|
|
138
|
+
...paramsOrFirst:
|
|
139
|
+
| readonly [ExpoSqliteBindParams]
|
|
140
|
+
| readonly ExpoSqliteBindValue[]
|
|
141
|
+
): ExpoSqliteRunResult {
|
|
142
|
+
const p = this.#resolveParams(paramsOrFirst);
|
|
143
|
+
const result = this.#db.run(sqlStr, ...p);
|
|
144
|
+
return {
|
|
145
|
+
changes: result.changes,
|
|
146
|
+
lastInsertRowId: Number(result.lastInsertRowid),
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
closeSync(): void {
|
|
151
|
+
this.#db.close();
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
#resolveParams(
|
|
155
|
+
paramsOrFirst:
|
|
156
|
+
| readonly [ExpoSqliteBindParams]
|
|
157
|
+
| readonly ExpoSqliteBindValue[]
|
|
158
|
+
): ExpoSqliteBindValue[] {
|
|
159
|
+
if (paramsOrFirst.length === 1 && Array.isArray(paramsOrFirst[0])) {
|
|
160
|
+
return paramsOrFirst[0] as ExpoSqliteBindValue[];
|
|
161
|
+
}
|
|
162
|
+
return paramsOrFirst as ExpoSqliteBindValue[];
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
interface ConcurrencyDb {
|
|
167
|
+
items: { id: number; value: string };
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
describe('expo sqlite dialect concurrency (real SQLite)', () => {
|
|
171
|
+
let adapter: BunSqliteAdapter;
|
|
172
|
+
let db: Kysely<ConcurrencyDb>;
|
|
173
|
+
|
|
174
|
+
beforeEach(async () => {
|
|
175
|
+
adapter = new BunSqliteAdapter();
|
|
176
|
+
db = createExpoSqliteDb<ConcurrencyDb>({ database: adapter });
|
|
177
|
+
await sql`create table items (id integer primary key, value text)`.execute(
|
|
178
|
+
db
|
|
179
|
+
);
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
afterEach(async () => {
|
|
183
|
+
await db.destroy();
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
it('concurrent transaction + read does not deadlock', async () => {
|
|
187
|
+
// Seed data
|
|
188
|
+
for (let i = 0; i < 100; i++) {
|
|
189
|
+
await db
|
|
190
|
+
.insertInto('items')
|
|
191
|
+
.values({ id: i, value: `v${i}` })
|
|
192
|
+
.execute();
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
// Simulate sync engine: long-running write transaction
|
|
196
|
+
const writeTx = db.transaction().execute(async (trx) => {
|
|
197
|
+
for (let i = 100; i < 200; i++) {
|
|
198
|
+
await trx
|
|
199
|
+
.insertInto('items')
|
|
200
|
+
.values({ id: i, value: `tx-${i}` })
|
|
201
|
+
.execute();
|
|
202
|
+
}
|
|
203
|
+
// Yield to allow the concurrent read to interleave
|
|
204
|
+
await new Promise((r) => setTimeout(r, 10));
|
|
205
|
+
for (let i = 200; i < 300; i++) {
|
|
206
|
+
await trx
|
|
207
|
+
.insertInto('items')
|
|
208
|
+
.values({ id: i, value: `tx-${i}` })
|
|
209
|
+
.execute();
|
|
210
|
+
}
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
// Simulate React hook: concurrent read while transaction is open
|
|
214
|
+
const readResult = db.selectFrom('items').select(['id', 'value']).execute();
|
|
215
|
+
|
|
216
|
+
// Both should complete without "database is locked"
|
|
217
|
+
const [, rows] = await Promise.all([writeTx, readResult]);
|
|
218
|
+
expect(rows.length).toBeGreaterThanOrEqual(100);
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
it('sequential write transactions complete without error', async () => {
|
|
222
|
+
await db.transaction().execute(async (trx) => {
|
|
223
|
+
for (let i = 0; i < 50; i++) {
|
|
224
|
+
await trx
|
|
225
|
+
.insertInto('items')
|
|
226
|
+
.values({ id: i, value: `a-${i}` })
|
|
227
|
+
.execute();
|
|
228
|
+
}
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
await db.transaction().execute(async (trx) => {
|
|
232
|
+
for (let i = 1000; i < 1050; i++) {
|
|
233
|
+
await trx
|
|
234
|
+
.insertInto('items')
|
|
235
|
+
.values({ id: i, value: `b-${i}` })
|
|
236
|
+
.execute();
|
|
237
|
+
}
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
const { rows } = await sql<{
|
|
241
|
+
cnt: number;
|
|
242
|
+
}>`select count(*) as cnt from items`.execute(db);
|
|
243
|
+
expect(rows[0].cnt).toBe(100);
|
|
244
|
+
});
|
|
245
|
+
});
|
package/src/index.ts
CHANGED
|
@@ -6,7 +6,9 @@
|
|
|
6
6
|
* SQLite-compatible — use with @syncular/server-dialect-sqlite.
|
|
7
7
|
*
|
|
8
8
|
* Implements a custom Kysely Driver that wraps expo-sqlite's sync API
|
|
9
|
-
* into the promise-based interface Kysely expects.
|
|
9
|
+
* into the promise-based interface Kysely expects. All operations are
|
|
10
|
+
* serialized through a single connection to prevent "database is locked"
|
|
11
|
+
* errors from concurrent access on the native handle.
|
|
10
12
|
*/
|
|
11
13
|
|
|
12
14
|
import { SerializePlugin } from '@syncular/core';
|
|
@@ -105,6 +107,34 @@ export function createSerializePlugin(): SerializePlugin {
|
|
|
105
107
|
return new SerializePlugin();
|
|
106
108
|
}
|
|
107
109
|
|
|
110
|
+
// ---------------------------------------------------------------------------
|
|
111
|
+
// Simple async mutex — serializes all DB access on a single native handle.
|
|
112
|
+
// ---------------------------------------------------------------------------
|
|
113
|
+
|
|
114
|
+
class Mutex {
|
|
115
|
+
#queue: Array<() => void> = [];
|
|
116
|
+
#locked = false;
|
|
117
|
+
|
|
118
|
+
async acquire(): Promise<void> {
|
|
119
|
+
if (!this.#locked) {
|
|
120
|
+
this.#locked = true;
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
return new Promise<void>((resolve) => {
|
|
124
|
+
this.#queue.push(resolve);
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
release(): void {
|
|
129
|
+
const next = this.#queue.shift();
|
|
130
|
+
if (next) {
|
|
131
|
+
next();
|
|
132
|
+
} else {
|
|
133
|
+
this.#locked = false;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
108
138
|
// ---------------------------------------------------------------------------
|
|
109
139
|
// Kysely Dialect implementation for expo-sqlite
|
|
110
140
|
// ---------------------------------------------------------------------------
|
|
@@ -136,6 +166,8 @@ class ExpoSqliteDialect implements Dialect {
|
|
|
136
166
|
class ExpoSqliteDriver implements Driver {
|
|
137
167
|
readonly #options: ExpoSqliteOptions;
|
|
138
168
|
#db: ExpoSqliteDatabaseLike | undefined;
|
|
169
|
+
#connection: ExpoSqliteConnection | undefined;
|
|
170
|
+
readonly #mutex = new Mutex();
|
|
139
171
|
|
|
140
172
|
constructor(options: ExpoSqliteOptions) {
|
|
141
173
|
this.#options = options;
|
|
@@ -148,17 +180,19 @@ class ExpoSqliteDriver implements Driver {
|
|
|
148
180
|
this.#db.runSync('PRAGMA journal_mode = WAL', []);
|
|
149
181
|
// Wait up to 5s for locks to clear instead of failing immediately.
|
|
150
182
|
this.#db.runSync('PRAGMA busy_timeout = 5000', []);
|
|
183
|
+
this.#connection = new ExpoSqliteConnection(this.#db);
|
|
151
184
|
}
|
|
152
185
|
|
|
153
186
|
async acquireConnection(): Promise<DatabaseConnection> {
|
|
154
|
-
|
|
187
|
+
await this.#mutex.acquire();
|
|
188
|
+
return this.#connection!;
|
|
155
189
|
}
|
|
156
190
|
|
|
157
191
|
async beginTransaction(
|
|
158
192
|
connection: DatabaseConnection,
|
|
159
193
|
_settings: TransactionSettings
|
|
160
194
|
): Promise<void> {
|
|
161
|
-
await connection.executeQuery(CompiledQuery.raw('begin'));
|
|
195
|
+
await connection.executeQuery(CompiledQuery.raw('begin immediate'));
|
|
162
196
|
}
|
|
163
197
|
|
|
164
198
|
async commitTransaction(connection: DatabaseConnection): Promise<void> {
|
|
@@ -170,7 +204,7 @@ class ExpoSqliteDriver implements Driver {
|
|
|
170
204
|
}
|
|
171
205
|
|
|
172
206
|
async releaseConnection(_connection: DatabaseConnection): Promise<void> {
|
|
173
|
-
|
|
207
|
+
this.#mutex.release();
|
|
174
208
|
}
|
|
175
209
|
|
|
176
210
|
async destroy(): Promise<void> {
|