@syncular/dialect-sqlite3 0.0.6-159 → 0.0.6-165
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 -15
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +41 -70
- package/dist/index.js.map +1 -1
- package/package.json +3 -2
- package/src/index.ts +49 -95
package/dist/index.d.ts
CHANGED
|
@@ -1,13 +1,10 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @syncular/dialect-sqlite3 - node-sqlite3 dialect for sync
|
|
3
3
|
*
|
|
4
|
-
* Provides a Kysely dialect for the callback-based `sqlite3` npm package
|
|
4
|
+
* Provides a Kysely dialect for the callback-based `sqlite3` npm package.
|
|
5
5
|
* SQLite-compatible — use with @syncular/server-dialect-sqlite.
|
|
6
|
-
*
|
|
7
|
-
* Implements a custom Kysely Driver that wraps sqlite3's callback API
|
|
8
|
-
* into the promise-based interface Kysely expects.
|
|
9
6
|
*/
|
|
10
|
-
import type {
|
|
7
|
+
import type { Dialect } from 'kysely';
|
|
11
8
|
import sqlite3 from 'sqlite3';
|
|
12
9
|
export interface Sqlite3PathOptions {
|
|
13
10
|
/** Path to SQLite database file, or ':memory:' for in-memory */
|
|
@@ -21,14 +18,5 @@ export type Sqlite3Options = Sqlite3PathOptions | Sqlite3InstanceOptions;
|
|
|
21
18
|
/**
|
|
22
19
|
* Create the sqlite3 dialect directly.
|
|
23
20
|
*/
|
|
24
|
-
export declare function createSqlite3Dialect(options: Sqlite3Options):
|
|
25
|
-
declare class Sqlite3Dialect implements Dialect {
|
|
26
|
-
#private;
|
|
27
|
-
constructor(options: Sqlite3Options);
|
|
28
|
-
createAdapter(): DialectAdapter;
|
|
29
|
-
createDriver(): Driver;
|
|
30
|
-
createQueryCompiler(): QueryCompiler;
|
|
31
|
-
createIntrospector(db: Kysely<any>): DatabaseIntrospector;
|
|
32
|
-
}
|
|
33
|
-
export {};
|
|
21
|
+
export declare function createSqlite3Dialect(options: Sqlite3Options): Dialect;
|
|
34
22
|
//# sourceMappingURL=index.d.ts.map
|
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;;;;;GAKG;AAEH,OAAO,KAAK,EAGV,OAAO,EAER,MAAM,QAAQ,CAAC;AAEhB,OAAO,OAAO,MAAM,SAAS,CAAC;AAE9B,MAAM,WAAW,kBAAkB;IACjC,gEAAgE;IAChE,IAAI,EAAE,MAAM,CAAC;CACd;AAED,MAAM,WAAW,sBAAsB;IACrC,4CAA4C;IAC5C,QAAQ,EAAE,OAAO,CAAC,QAAQ,CAAC;CAC5B;AAED,MAAM,MAAM,cAAc,GAAG,kBAAkB,GAAG,sBAAsB,CAAC;AAEzE;;GAEG;AACH,wBAAgB,oBAAoB,CAAC,OAAO,EAAE,cAAc,GAAG,OAAO,CAErE"}
|
package/dist/index.js
CHANGED
|
@@ -1,87 +1,55 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @syncular/dialect-sqlite3 - node-sqlite3 dialect for sync
|
|
3
3
|
*
|
|
4
|
-
* Provides a Kysely dialect for the callback-based `sqlite3` npm package
|
|
4
|
+
* Provides a Kysely dialect for the callback-based `sqlite3` npm package.
|
|
5
5
|
* SQLite-compatible — use with @syncular/server-dialect-sqlite.
|
|
6
|
-
*
|
|
7
|
-
* Implements a custom Kysely Driver that wraps sqlite3's callback API
|
|
8
|
-
* into the promise-based interface Kysely expects.
|
|
9
6
|
*/
|
|
10
|
-
import {
|
|
7
|
+
import { BaseSqliteDialect, BaseSqliteDriver } from 'kysely-generic-sqlite';
|
|
11
8
|
import sqlite3 from 'sqlite3';
|
|
12
9
|
/**
|
|
13
10
|
* Create the sqlite3 dialect directly.
|
|
14
11
|
*/
|
|
15
12
|
export function createSqlite3Dialect(options) {
|
|
16
|
-
return new
|
|
17
|
-
}
|
|
18
|
-
// ---------------------------------------------------------------------------
|
|
19
|
-
// Kysely Dialect implementation for node-sqlite3
|
|
20
|
-
// ---------------------------------------------------------------------------
|
|
21
|
-
class Sqlite3Dialect {
|
|
22
|
-
#options;
|
|
23
|
-
constructor(options) {
|
|
24
|
-
this.#options = options;
|
|
25
|
-
}
|
|
26
|
-
createAdapter() {
|
|
27
|
-
return new SqliteAdapter();
|
|
28
|
-
}
|
|
29
|
-
createDriver() {
|
|
30
|
-
return new Sqlite3Driver(this.#options);
|
|
31
|
-
}
|
|
32
|
-
createQueryCompiler() {
|
|
33
|
-
return new SqliteQueryCompiler();
|
|
34
|
-
}
|
|
35
|
-
createIntrospector(db) {
|
|
36
|
-
return new SqliteIntrospector(db);
|
|
37
|
-
}
|
|
13
|
+
return new BaseSqliteDialect(() => new Sqlite3Driver(options));
|
|
38
14
|
}
|
|
39
|
-
class Sqlite3Driver {
|
|
40
|
-
#options;
|
|
15
|
+
class Sqlite3Driver extends BaseSqliteDriver {
|
|
41
16
|
#db;
|
|
42
17
|
constructor(options) {
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
}
|
|
48
|
-
async acquireConnection() {
|
|
49
|
-
return new Sqlite3Connection(this.#db);
|
|
50
|
-
}
|
|
51
|
-
async beginTransaction(connection, _settings) {
|
|
52
|
-
await connection.executeQuery(CompiledQuery.raw('begin'));
|
|
53
|
-
}
|
|
54
|
-
async commitTransaction(connection) {
|
|
55
|
-
await connection.executeQuery(CompiledQuery.raw('commit'));
|
|
56
|
-
}
|
|
57
|
-
async rollbackTransaction(connection) {
|
|
58
|
-
await connection.executeQuery(CompiledQuery.raw('rollback'));
|
|
59
|
-
}
|
|
60
|
-
async releaseConnection(_connection) {
|
|
61
|
-
// Single-connection model — nothing to release.
|
|
18
|
+
super(async () => {
|
|
19
|
+
this.#db = await resolveSqlite3Database(options);
|
|
20
|
+
this.conn = new Sqlite3Connection(this.#db);
|
|
21
|
+
});
|
|
62
22
|
}
|
|
63
23
|
async destroy() {
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
if ('database' in this.#options) {
|
|
72
|
-
return this.#options.database;
|
|
73
|
-
}
|
|
74
|
-
const path = this.#options.path;
|
|
75
|
-
return new Promise((resolve, reject) => {
|
|
76
|
-
const db = new sqlite3.Database(path, (err) => {
|
|
77
|
-
if (err)
|
|
24
|
+
const db = this.#db;
|
|
25
|
+
this.#db = undefined;
|
|
26
|
+
if (!db)
|
|
27
|
+
return;
|
|
28
|
+
await new Promise((resolve, reject) => {
|
|
29
|
+
db.close((err) => {
|
|
30
|
+
if (err) {
|
|
78
31
|
reject(err);
|
|
79
|
-
|
|
80
|
-
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
resolve();
|
|
81
35
|
});
|
|
82
36
|
});
|
|
83
37
|
}
|
|
84
38
|
}
|
|
39
|
+
async function resolveSqlite3Database(options) {
|
|
40
|
+
if ('database' in options) {
|
|
41
|
+
return options.database;
|
|
42
|
+
}
|
|
43
|
+
return new Promise((resolve, reject) => {
|
|
44
|
+
const db = new sqlite3.Database(options.path, (err) => {
|
|
45
|
+
if (err) {
|
|
46
|
+
reject(err);
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
resolve(db);
|
|
50
|
+
});
|
|
51
|
+
});
|
|
52
|
+
}
|
|
85
53
|
class Sqlite3Connection {
|
|
86
54
|
#db;
|
|
87
55
|
constructor(db) {
|
|
@@ -96,8 +64,10 @@ class Sqlite3Connection {
|
|
|
96
64
|
if (isSelectLike || hasReturning) {
|
|
97
65
|
return new Promise((resolve, reject) => {
|
|
98
66
|
this.#db.all(sql, params, (err, rows) => {
|
|
99
|
-
if (err)
|
|
100
|
-
|
|
67
|
+
if (err) {
|
|
68
|
+
reject(err);
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
101
71
|
const normalizedRows = rows ?? [];
|
|
102
72
|
resolve({
|
|
103
73
|
rows: normalizedRows,
|
|
@@ -108,11 +78,12 @@ class Sqlite3Connection {
|
|
|
108
78
|
});
|
|
109
79
|
});
|
|
110
80
|
}
|
|
111
|
-
// For INSERT, UPDATE, DELETE — use run() to get lastID and changes
|
|
112
81
|
return new Promise((resolve, reject) => {
|
|
113
|
-
this.#db.run(sql, params, function (err) {
|
|
114
|
-
if (err)
|
|
115
|
-
|
|
82
|
+
this.#db.run(sql, params, function onRun(err) {
|
|
83
|
+
if (err) {
|
|
84
|
+
reject(err);
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
116
87
|
resolve({
|
|
117
88
|
rows: [],
|
|
118
89
|
numAffectedRows: BigInt(this.changes),
|
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":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAQH,OAAO,EAAE,iBAAiB,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AAC5E,OAAO,OAAO,MAAM,SAAS,CAAC;AAc9B;;GAEG;AACH,MAAM,UAAU,oBAAoB,CAAC,OAAuB,EAAW;IACrE,OAAO,IAAI,iBAAiB,CAAC,GAAG,EAAE,CAAC,IAAI,aAAa,CAAC,OAAO,CAAC,CAAC,CAAC;AAAA,CAChE;AAED,MAAM,aAAc,SAAQ,gBAAgB;IAC1C,GAAG,CAA+B;IAElC,YAAY,OAAuB,EAAE;QACnC,KAAK,CAAC,KAAK,IAAI,EAAE,CAAC;YAChB,IAAI,CAAC,GAAG,GAAG,MAAM,sBAAsB,CAAC,OAAO,CAAC,CAAC;YACjD,IAAI,CAAC,IAAI,GAAG,IAAI,iBAAiB,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;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I,CAAC,OAAO,CAAC;oBACrC,GAAG,CAAC,QAAQ,CAAC,CAAC,CAAC,EAAE,QAAQ,EAAE,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;iBACvD,CAAC,CAAC;YAAA,CACJ,CAAC,CAAC;QAAA,CACJ,CAAC,CAAC;IAAA,CACJ;IAED,WAAW,CACT,cAA6B,EAC7B,UAAmB,EACoB;QACvC,MAAM,IAAI,KAAK,CAAC,2CAA2C,CAAC,CAAC;IAAA,CAC9D;CACF"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@syncular/dialect-sqlite3",
|
|
3
|
-
"version": "0.0.6-
|
|
3
|
+
"version": "0.0.6-165",
|
|
4
4
|
"description": "node-sqlite3 dialect for the Syncular client",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"author": "Benjamin Kniffler",
|
|
@@ -42,6 +42,7 @@
|
|
|
42
42
|
"release": "bunx syncular-publish"
|
|
43
43
|
},
|
|
44
44
|
"dependencies": {
|
|
45
|
+
"kysely-generic-sqlite": "^1.2.1",
|
|
45
46
|
"sqlite3": "^5.1.7"
|
|
46
47
|
},
|
|
47
48
|
"peerDependencies": {
|
|
@@ -49,7 +50,7 @@
|
|
|
49
50
|
},
|
|
50
51
|
"devDependencies": {
|
|
51
52
|
"@syncular/config": "0.0.0",
|
|
52
|
-
"@syncular/core": "0.0.6-
|
|
53
|
+
"@syncular/core": "0.0.6-165",
|
|
53
54
|
"kysely": "*"
|
|
54
55
|
},
|
|
55
56
|
"files": [
|
package/src/index.ts
CHANGED
|
@@ -1,30 +1,17 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @syncular/dialect-sqlite3 - node-sqlite3 dialect for sync
|
|
3
3
|
*
|
|
4
|
-
* Provides a Kysely dialect for the callback-based `sqlite3` npm package
|
|
4
|
+
* Provides a Kysely dialect for the callback-based `sqlite3` npm package.
|
|
5
5
|
* SQLite-compatible — use with @syncular/server-dialect-sqlite.
|
|
6
|
-
*
|
|
7
|
-
* Implements a custom Kysely Driver that wraps sqlite3's callback API
|
|
8
|
-
* into the promise-based interface Kysely expects.
|
|
9
6
|
*/
|
|
10
7
|
|
|
11
8
|
import type {
|
|
9
|
+
CompiledQuery,
|
|
12
10
|
DatabaseConnection,
|
|
13
|
-
DatabaseIntrospector,
|
|
14
11
|
Dialect,
|
|
15
|
-
DialectAdapter,
|
|
16
|
-
Driver,
|
|
17
|
-
Kysely,
|
|
18
|
-
QueryCompiler,
|
|
19
12
|
QueryResult,
|
|
20
|
-
TransactionSettings,
|
|
21
|
-
} from 'kysely';
|
|
22
|
-
import {
|
|
23
|
-
CompiledQuery,
|
|
24
|
-
SqliteAdapter,
|
|
25
|
-
SqliteIntrospector,
|
|
26
|
-
SqliteQueryCompiler,
|
|
27
13
|
} from 'kysely';
|
|
14
|
+
import { BaseSqliteDialect, BaseSqliteDriver } from 'kysely-generic-sqlite';
|
|
28
15
|
import sqlite3 from 'sqlite3';
|
|
29
16
|
|
|
30
17
|
export interface Sqlite3PathOptions {
|
|
@@ -42,93 +29,53 @@ export type Sqlite3Options = Sqlite3PathOptions | Sqlite3InstanceOptions;
|
|
|
42
29
|
/**
|
|
43
30
|
* Create the sqlite3 dialect directly.
|
|
44
31
|
*/
|
|
45
|
-
export function createSqlite3Dialect(options: Sqlite3Options):
|
|
46
|
-
return new
|
|
32
|
+
export function createSqlite3Dialect(options: Sqlite3Options): Dialect {
|
|
33
|
+
return new BaseSqliteDialect(() => new Sqlite3Driver(options));
|
|
47
34
|
}
|
|
48
35
|
|
|
49
|
-
|
|
50
|
-
// Kysely Dialect implementation for node-sqlite3
|
|
51
|
-
// ---------------------------------------------------------------------------
|
|
52
|
-
|
|
53
|
-
class Sqlite3Dialect implements Dialect {
|
|
54
|
-
readonly #options: Sqlite3Options;
|
|
55
|
-
|
|
56
|
-
constructor(options: Sqlite3Options) {
|
|
57
|
-
this.#options = options;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
createAdapter(): DialectAdapter {
|
|
61
|
-
return new SqliteAdapter();
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
createDriver(): Driver {
|
|
65
|
-
return new Sqlite3Driver(this.#options);
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
createQueryCompiler(): QueryCompiler {
|
|
69
|
-
return new SqliteQueryCompiler();
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
createIntrospector(db: Kysely<any>): DatabaseIntrospector {
|
|
73
|
-
return new SqliteIntrospector(db);
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
class Sqlite3Driver implements Driver {
|
|
78
|
-
readonly #options: Sqlite3Options;
|
|
36
|
+
class Sqlite3Driver extends BaseSqliteDriver {
|
|
79
37
|
#db: sqlite3.Database | undefined;
|
|
80
38
|
|
|
81
39
|
constructor(options: Sqlite3Options) {
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
this.#db = await this.#resolveDatabase();
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
async acquireConnection(): Promise<DatabaseConnection> {
|
|
90
|
-
return new Sqlite3Connection(this.#db!);
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
async beginTransaction(
|
|
94
|
-
connection: DatabaseConnection,
|
|
95
|
-
_settings: TransactionSettings
|
|
96
|
-
): Promise<void> {
|
|
97
|
-
await connection.executeQuery(CompiledQuery.raw('begin'));
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
async commitTransaction(connection: DatabaseConnection): Promise<void> {
|
|
101
|
-
await connection.executeQuery(CompiledQuery.raw('commit'));
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
async rollbackTransaction(connection: DatabaseConnection): Promise<void> {
|
|
105
|
-
await connection.executeQuery(CompiledQuery.raw('rollback'));
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
async releaseConnection(_connection: DatabaseConnection): Promise<void> {
|
|
109
|
-
// Single-connection model — nothing to release.
|
|
40
|
+
super(async () => {
|
|
41
|
+
this.#db = await resolveSqlite3Database(options);
|
|
42
|
+
this.conn = new Sqlite3Connection(this.#db);
|
|
43
|
+
});
|
|
110
44
|
}
|
|
111
45
|
|
|
112
46
|
async destroy(): Promise<void> {
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
47
|
+
const db = this.#db;
|
|
48
|
+
this.#db = undefined;
|
|
49
|
+
if (!db) return;
|
|
50
|
+
|
|
51
|
+
await new Promise<void>((resolve, reject) => {
|
|
52
|
+
db.close((err) => {
|
|
53
|
+
if (err) {
|
|
54
|
+
reject(err);
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
resolve();
|
|
116
58
|
});
|
|
117
|
-
}
|
|
59
|
+
});
|
|
118
60
|
}
|
|
61
|
+
}
|
|
119
62
|
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
return new Promise<sqlite3.Database>((resolve, reject) => {
|
|
126
|
-
const db = new sqlite3.Database(path, (err) => {
|
|
127
|
-
if (err) reject(err);
|
|
128
|
-
else resolve(db);
|
|
129
|
-
});
|
|
130
|
-
});
|
|
63
|
+
async function resolveSqlite3Database(
|
|
64
|
+
options: Sqlite3Options
|
|
65
|
+
): Promise<sqlite3.Database> {
|
|
66
|
+
if ('database' in options) {
|
|
67
|
+
return options.database;
|
|
131
68
|
}
|
|
69
|
+
|
|
70
|
+
return new Promise<sqlite3.Database>((resolve, reject) => {
|
|
71
|
+
const db = new sqlite3.Database(options.path, (err) => {
|
|
72
|
+
if (err) {
|
|
73
|
+
reject(err);
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
resolve(db);
|
|
77
|
+
});
|
|
78
|
+
});
|
|
132
79
|
}
|
|
133
80
|
|
|
134
81
|
class Sqlite3Connection implements DatabaseConnection {
|
|
@@ -149,7 +96,11 @@ class Sqlite3Connection implements DatabaseConnection {
|
|
|
149
96
|
if (isSelectLike || hasReturning) {
|
|
150
97
|
return new Promise<QueryResult<R>>((resolve, reject) => {
|
|
151
98
|
this.#db.all(sql, params, (err: Error | null, rows: R[]) => {
|
|
152
|
-
if (err)
|
|
99
|
+
if (err) {
|
|
100
|
+
reject(err);
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
|
|
153
104
|
const normalizedRows = rows ?? [];
|
|
154
105
|
resolve({
|
|
155
106
|
rows: normalizedRows,
|
|
@@ -161,10 +112,13 @@ class Sqlite3Connection implements DatabaseConnection {
|
|
|
161
112
|
});
|
|
162
113
|
}
|
|
163
114
|
|
|
164
|
-
// For INSERT, UPDATE, DELETE — use run() to get lastID and changes
|
|
165
115
|
return new Promise<QueryResult<R>>((resolve, reject) => {
|
|
166
|
-
this.#db.run(sql, params, function (err: Error | null) {
|
|
167
|
-
if (err)
|
|
116
|
+
this.#db.run(sql, params, function onRun(err: Error | null) {
|
|
117
|
+
if (err) {
|
|
118
|
+
reject(err);
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
|
|
168
122
|
resolve({
|
|
169
123
|
rows: [],
|
|
170
124
|
numAffectedRows: BigInt(this.changes),
|