@syncular/dialect-react-native-nitro-sqlite 0.0.1-60

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.
@@ -0,0 +1,63 @@
1
+ /**
2
+ * @syncular/dialect-react-native-nitro-sqlite - React Native Nitro SQLite dialect for sync
3
+ *
4
+ * Provides a Kysely dialect for React Native Nitro SQLite (react-native-nitro-sqlite)
5
+ * with SerializePlugin for automatic JSON serialization/deserialization.
6
+ * SQLite-compatible — use with @syncular/server-dialect-sqlite.
7
+ *
8
+ * Implements a custom Kysely Driver that wraps react-native-nitro-sqlite's API
9
+ * into the promise-based interface Kysely expects.
10
+ */
11
+ import { SerializePlugin } from '@syncular/core';
12
+ import type { DatabaseIntrospector, Dialect, DialectAdapter, Driver, QueryCompiler } from 'kysely';
13
+ import { Kysely } from 'kysely';
14
+ import type { NitroSQLiteConnection, NitroSQLiteConnectionOptions } from 'react-native-nitro-sqlite';
15
+ /** Function type for open from react-native-nitro-sqlite */
16
+ type OpenNitroSqlite = (options: NitroSQLiteConnectionOptions) => NitroSQLiteConnection;
17
+ interface NitroSqliteNameOptions {
18
+ /** Database name (will be stored in app's document directory) */
19
+ name: string;
20
+ /** The open function from react-native-nitro-sqlite */
21
+ open: OpenNitroSqlite;
22
+ /** Optional database location */
23
+ location?: string;
24
+ }
25
+ interface NitroSqliteInstanceOptions {
26
+ /** An existing nitro-sqlite database instance */
27
+ database: NitroSQLiteConnection;
28
+ }
29
+ export type NitroSqliteOptions = NitroSqliteNameOptions | NitroSqliteInstanceOptions;
30
+ /**
31
+ * Create a Kysely instance with React Native Nitro SQLite dialect and SerializePlugin.
32
+ *
33
+ * @example
34
+ * import { open } from 'react-native-nitro-sqlite';
35
+ *
36
+ * const db = createNitroSqliteDb<MyDb>({
37
+ * name: 'myapp.db',
38
+ * open,
39
+ * });
40
+ *
41
+ * // Or with an existing database instance:
42
+ * const database = open({ name: 'myapp.db' });
43
+ * const db = createNitroSqliteDb<MyDb>({ database });
44
+ */
45
+ export declare function createNitroSqliteDb<T>(options: NitroSqliteOptions): Kysely<T>;
46
+ /**
47
+ * Create the Nitro SQLite dialect directly (without SerializePlugin).
48
+ */
49
+ export declare function createNitroSqliteDialect(options: NitroSqliteOptions): NitroSqliteDialect;
50
+ /**
51
+ * Create a SerializePlugin instance.
52
+ */
53
+ export declare function createSerializePlugin(): SerializePlugin;
54
+ declare class NitroSqliteDialect implements Dialect {
55
+ #private;
56
+ constructor(options: NitroSqliteOptions);
57
+ createAdapter(): DialectAdapter;
58
+ createDriver(): Driver;
59
+ createQueryCompiler(): QueryCompiler;
60
+ createIntrospector(db: Kysely<unknown>): DatabaseIntrospector;
61
+ }
62
+ export {};
63
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;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;AAChB,OAAO,KAAK,EACV,qBAAqB,EACrB,4BAA4B,EAE7B,MAAM,2BAA2B,CAAC;AAEnC,4DAA4D;AAC5D,KAAK,eAAe,GAAG,CACrB,OAAO,EAAE,4BAA4B,KAClC,qBAAqB,CAAC;AAE3B,UAAU,sBAAsB;IAC9B,iEAAiE;IACjE,IAAI,EAAE,MAAM,CAAC;IACb,uDAAuD;IACvD,IAAI,EAAE,eAAe,CAAC;IACtB,iCAAiC;IACjC,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB;AAED,UAAU,0BAA0B;IAClC,iDAAiD;IACjD,QAAQ,EAAE,qBAAqB,CAAC;CACjC;AAED,MAAM,MAAM,kBAAkB,GAC1B,sBAAsB,GACtB,0BAA0B,CAAC;AAE/B;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,mBAAmB,CAAC,CAAC,EAAE,OAAO,EAAE,kBAAkB,GAAG,MAAM,CAAC,CAAC,CAAC,CAK7E;AAED;;GAEG;AACH,wBAAgB,wBAAwB,CACtC,OAAO,EAAE,kBAAkB,GAC1B,kBAAkB,CAEpB;AAED;;GAEG;AACH,wBAAgB,qBAAqB,IAAI,eAAe,CAEvD;AAMD,cAAM,kBAAmB,YAAW,OAAO;;IAGzC,YAAY,OAAO,EAAE,kBAAkB,EAEtC;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 ADDED
@@ -0,0 +1,132 @@
1
+ /**
2
+ * @syncular/dialect-react-native-nitro-sqlite - React Native Nitro SQLite dialect for sync
3
+ *
4
+ * Provides a Kysely dialect for React Native Nitro SQLite (react-native-nitro-sqlite)
5
+ * with SerializePlugin for automatic JSON serialization/deserialization.
6
+ * SQLite-compatible — use with @syncular/server-dialect-sqlite.
7
+ *
8
+ * Implements a custom Kysely Driver that wraps react-native-nitro-sqlite's API
9
+ * into the promise-based interface Kysely expects.
10
+ */
11
+ import { SerializePlugin } from '@syncular/core';
12
+ import { CompiledQuery, Kysely, SqliteAdapter, SqliteIntrospector, SqliteQueryCompiler, } from 'kysely';
13
+ /**
14
+ * Create a Kysely instance with React Native Nitro SQLite dialect and SerializePlugin.
15
+ *
16
+ * @example
17
+ * import { open } from 'react-native-nitro-sqlite';
18
+ *
19
+ * const db = createNitroSqliteDb<MyDb>({
20
+ * name: 'myapp.db',
21
+ * open,
22
+ * });
23
+ *
24
+ * // Or with an existing database instance:
25
+ * const database = open({ name: 'myapp.db' });
26
+ * const db = createNitroSqliteDb<MyDb>({ database });
27
+ */
28
+ export function createNitroSqliteDb(options) {
29
+ return new Kysely({
30
+ dialect: createNitroSqliteDialect(options),
31
+ plugins: [new SerializePlugin()],
32
+ });
33
+ }
34
+ /**
35
+ * Create the Nitro SQLite dialect directly (without SerializePlugin).
36
+ */
37
+ export function createNitroSqliteDialect(options) {
38
+ return new NitroSqliteDialect(options);
39
+ }
40
+ /**
41
+ * Create a SerializePlugin instance.
42
+ */
43
+ export function createSerializePlugin() {
44
+ return new SerializePlugin();
45
+ }
46
+ // ---------------------------------------------------------------------------
47
+ // Kysely Dialect implementation for react-native-nitro-sqlite
48
+ // ---------------------------------------------------------------------------
49
+ class NitroSqliteDialect {
50
+ #options;
51
+ constructor(options) {
52
+ this.#options = options;
53
+ }
54
+ createAdapter() {
55
+ return new SqliteAdapter();
56
+ }
57
+ createDriver() {
58
+ return new NitroSqliteDriver(this.#options);
59
+ }
60
+ createQueryCompiler() {
61
+ return new SqliteQueryCompiler();
62
+ }
63
+ createIntrospector(db) {
64
+ return new SqliteIntrospector(db);
65
+ }
66
+ }
67
+ class NitroSqliteDriver {
68
+ #options;
69
+ #db;
70
+ constructor(options) {
71
+ this.#options = options;
72
+ }
73
+ async init() {
74
+ this.#db = this.#resolveDatabase();
75
+ }
76
+ async acquireConnection() {
77
+ return new NitroSqliteConnection(this.#db);
78
+ }
79
+ async beginTransaction(connection, _settings) {
80
+ await connection.executeQuery(CompiledQuery.raw('begin'));
81
+ }
82
+ async commitTransaction(connection) {
83
+ await connection.executeQuery(CompiledQuery.raw('commit'));
84
+ }
85
+ async rollbackTransaction(connection) {
86
+ await connection.executeQuery(CompiledQuery.raw('rollback'));
87
+ }
88
+ async releaseConnection(_connection) {
89
+ // Single-connection model — nothing to release.
90
+ }
91
+ async destroy() {
92
+ if (this.#db) {
93
+ this.#db.close();
94
+ }
95
+ }
96
+ #resolveDatabase() {
97
+ if ('database' in this.#options) {
98
+ return this.#options.database;
99
+ }
100
+ return this.#options.open({
101
+ name: this.#options.name,
102
+ location: this.#options.location,
103
+ });
104
+ }
105
+ }
106
+ class NitroSqliteConnection {
107
+ #db;
108
+ constructor(db) {
109
+ this.#db = db;
110
+ }
111
+ async executeQuery(compiledQuery) {
112
+ const { sql, parameters } = compiledQuery;
113
+ const params = parameters;
114
+ // Determine if this is a SELECT / RETURNING query
115
+ const isSelect = /^\s*(select|pragma|explain|with)\b/i.test(sql);
116
+ // Execute the query
117
+ const result = this.#db.execute(sql, params);
118
+ if (isSelect) {
119
+ return { rows: (result.results ?? []) };
120
+ }
121
+ // For INSERT, UPDATE, DELETE — return affected rows info
122
+ return {
123
+ rows: [],
124
+ numAffectedRows: BigInt(result.rowsAffected ?? 0),
125
+ insertId: BigInt(result.insertId ?? 0),
126
+ };
127
+ }
128
+ streamQuery(_compiledQuery, _chunkSize) {
129
+ throw new Error('react-native-nitro-sqlite driver does not support streaming');
130
+ }
131
+ }
132
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,OAAO,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAC;AAWjD,OAAO,EACL,aAAa,EACb,MAAM,EACN,aAAa,EACb,kBAAkB,EAClB,mBAAmB,GACpB,MAAM,QAAQ,CAAC;AA8BhB;;;;;;;;;;;;;;GAcG;AACH,MAAM,UAAU,mBAAmB,CAAI,OAA2B,EAAa;IAC7E,OAAO,IAAI,MAAM,CAAI;QACnB,OAAO,EAAE,wBAAwB,CAAC,OAAO,CAAC;QAC1C,OAAO,EAAE,CAAC,IAAI,eAAe,EAAE,CAAC;KACjC,CAAC,CAAC;AAAA,CACJ;AAED;;GAEG;AACH,MAAM,UAAU,wBAAwB,CACtC,OAA2B,EACP;IACpB,OAAO,IAAI,kBAAkB,CAAC,OAAO,CAAC,CAAC;AAAA,CACxC;AAED;;GAEG;AACH,MAAM,UAAU,qBAAqB,GAAoB;IACvD,OAAO,IAAI,eAAe,EAAE,CAAC;AAAA,C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E,EAAE;YACR,eAAe,EAAE,MAAM,CAAC,MAAM,CAAC,YAAY,IAAI,CAAC,CAAC;YACjD,QAAQ,EAAE,MAAM,CAAC,MAAM,CAAC,QAAQ,IAAI,CAAC,CAAC;SACvC,CAAC;IAAA,CACH;IAED,WAAW,CACT,cAA6B,EAC7B,UAAmB,EACoB;QACvC,MAAM,IAAI,KAAK,CACb,6DAA6D,CAC9D,CAAC;IAAA,CACH;CACF"}
package/package.json ADDED
@@ -0,0 +1,61 @@
1
+ {
2
+ "name": "@syncular/dialect-react-native-nitro-sqlite",
3
+ "version": "0.0.1-60",
4
+ "description": "React Native Nitro SQLite dialect for the Syncular client",
5
+ "license": "MIT",
6
+ "author": "Benjamin Kniffler",
7
+ "homepage": "https://syncular.dev",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "https://github.com/syncular/syncular.git",
11
+ "directory": "packages/dialect-react-native-nitro-sqlite"
12
+ },
13
+ "bugs": {
14
+ "url": "https://github.com/syncular/syncular/issues"
15
+ },
16
+ "keywords": [
17
+ "sync",
18
+ "offline-first",
19
+ "realtime",
20
+ "database",
21
+ "typescript",
22
+ "react-native",
23
+ "nitro",
24
+ "sqlite"
25
+ ],
26
+ "private": false,
27
+ "publishConfig": {
28
+ "access": "public"
29
+ },
30
+ "type": "module",
31
+ "exports": {
32
+ ".": {
33
+ "bun": "./src/index.ts",
34
+ "import": {
35
+ "types": "./dist/index.d.ts",
36
+ "default": "./dist/index.js"
37
+ }
38
+ }
39
+ },
40
+ "scripts": {
41
+ "tsgo": "tsgo --noEmit",
42
+ "build": "rm -rf dist && tsgo",
43
+ "release": "bun pm pack --destination . && npm publish ./*.tgz --tag latest && rm -f ./*.tgz"
44
+ },
45
+ "dependencies": {
46
+ "@syncular/core": "0.0.1"
47
+ },
48
+ "peerDependencies": {
49
+ "kysely": "^0.28.0",
50
+ "react-native-nitro-sqlite": ">=0.1.0"
51
+ },
52
+ "devDependencies": {
53
+ "@syncular/config": "0.0.0",
54
+ "kysely": "*",
55
+ "react-native-nitro-sqlite": "^9.3.0"
56
+ },
57
+ "files": [
58
+ "dist",
59
+ "src"
60
+ ]
61
+ }
package/src/index.ts ADDED
@@ -0,0 +1,214 @@
1
+ /**
2
+ * @syncular/dialect-react-native-nitro-sqlite - React Native Nitro SQLite dialect for sync
3
+ *
4
+ * Provides a Kysely dialect for React Native Nitro SQLite (react-native-nitro-sqlite)
5
+ * with SerializePlugin for automatic JSON serialization/deserialization.
6
+ * SQLite-compatible — use with @syncular/server-dialect-sqlite.
7
+ *
8
+ * Implements a custom Kysely Driver that wraps react-native-nitro-sqlite's API
9
+ * into the promise-based interface Kysely expects.
10
+ */
11
+
12
+ import { SerializePlugin } from '@syncular/core';
13
+ import type {
14
+ DatabaseConnection,
15
+ DatabaseIntrospector,
16
+ Dialect,
17
+ DialectAdapter,
18
+ Driver,
19
+ QueryCompiler,
20
+ QueryResult,
21
+ TransactionSettings,
22
+ } from 'kysely';
23
+ import {
24
+ CompiledQuery,
25
+ Kysely,
26
+ SqliteAdapter,
27
+ SqliteIntrospector,
28
+ SqliteQueryCompiler,
29
+ } from 'kysely';
30
+ import type {
31
+ NitroSQLiteConnection,
32
+ NitroSQLiteConnectionOptions,
33
+ SQLiteValue,
34
+ } from 'react-native-nitro-sqlite';
35
+
36
+ /** Function type for open from react-native-nitro-sqlite */
37
+ type OpenNitroSqlite = (
38
+ options: NitroSQLiteConnectionOptions
39
+ ) => NitroSQLiteConnection;
40
+
41
+ interface NitroSqliteNameOptions {
42
+ /** Database name (will be stored in app's document directory) */
43
+ name: string;
44
+ /** The open function from react-native-nitro-sqlite */
45
+ open: OpenNitroSqlite;
46
+ /** Optional database location */
47
+ location?: string;
48
+ }
49
+
50
+ interface NitroSqliteInstanceOptions {
51
+ /** An existing nitro-sqlite database instance */
52
+ database: NitroSQLiteConnection;
53
+ }
54
+
55
+ export type NitroSqliteOptions =
56
+ | NitroSqliteNameOptions
57
+ | NitroSqliteInstanceOptions;
58
+
59
+ /**
60
+ * Create a Kysely instance with React Native Nitro SQLite dialect and SerializePlugin.
61
+ *
62
+ * @example
63
+ * import { open } from 'react-native-nitro-sqlite';
64
+ *
65
+ * const db = createNitroSqliteDb<MyDb>({
66
+ * name: 'myapp.db',
67
+ * open,
68
+ * });
69
+ *
70
+ * // Or with an existing database instance:
71
+ * const database = open({ name: 'myapp.db' });
72
+ * const db = createNitroSqliteDb<MyDb>({ database });
73
+ */
74
+ export function createNitroSqliteDb<T>(options: NitroSqliteOptions): Kysely<T> {
75
+ return new Kysely<T>({
76
+ dialect: createNitroSqliteDialect(options),
77
+ plugins: [new SerializePlugin()],
78
+ });
79
+ }
80
+
81
+ /**
82
+ * Create the Nitro SQLite dialect directly (without SerializePlugin).
83
+ */
84
+ export function createNitroSqliteDialect(
85
+ options: NitroSqliteOptions
86
+ ): NitroSqliteDialect {
87
+ return new NitroSqliteDialect(options);
88
+ }
89
+
90
+ /**
91
+ * Create a SerializePlugin instance.
92
+ */
93
+ export function createSerializePlugin(): SerializePlugin {
94
+ return new SerializePlugin();
95
+ }
96
+
97
+ // ---------------------------------------------------------------------------
98
+ // Kysely Dialect implementation for react-native-nitro-sqlite
99
+ // ---------------------------------------------------------------------------
100
+
101
+ class NitroSqliteDialect implements Dialect {
102
+ readonly #options: NitroSqliteOptions;
103
+
104
+ constructor(options: NitroSqliteOptions) {
105
+ this.#options = options;
106
+ }
107
+
108
+ createAdapter(): DialectAdapter {
109
+ return new SqliteAdapter();
110
+ }
111
+
112
+ createDriver(): Driver {
113
+ return new NitroSqliteDriver(this.#options);
114
+ }
115
+
116
+ createQueryCompiler(): QueryCompiler {
117
+ return new SqliteQueryCompiler();
118
+ }
119
+
120
+ createIntrospector(db: Kysely<unknown>): DatabaseIntrospector {
121
+ return new SqliteIntrospector(db);
122
+ }
123
+ }
124
+
125
+ class NitroSqliteDriver implements Driver {
126
+ readonly #options: NitroSqliteOptions;
127
+ #db: NitroSQLiteConnection | undefined;
128
+
129
+ constructor(options: NitroSqliteOptions) {
130
+ this.#options = options;
131
+ }
132
+
133
+ async init(): Promise<void> {
134
+ this.#db = this.#resolveDatabase();
135
+ }
136
+
137
+ async acquireConnection(): Promise<DatabaseConnection> {
138
+ return new NitroSqliteConnection(this.#db!);
139
+ }
140
+
141
+ async beginTransaction(
142
+ connection: DatabaseConnection,
143
+ _settings: TransactionSettings
144
+ ): Promise<void> {
145
+ await connection.executeQuery(CompiledQuery.raw('begin'));
146
+ }
147
+
148
+ async commitTransaction(connection: DatabaseConnection): Promise<void> {
149
+ await connection.executeQuery(CompiledQuery.raw('commit'));
150
+ }
151
+
152
+ async rollbackTransaction(connection: DatabaseConnection): Promise<void> {
153
+ await connection.executeQuery(CompiledQuery.raw('rollback'));
154
+ }
155
+
156
+ async releaseConnection(_connection: DatabaseConnection): Promise<void> {
157
+ // Single-connection model — nothing to release.
158
+ }
159
+
160
+ async destroy(): Promise<void> {
161
+ if (this.#db) {
162
+ this.#db.close();
163
+ }
164
+ }
165
+
166
+ #resolveDatabase(): NitroSQLiteConnection {
167
+ if ('database' in this.#options) {
168
+ return this.#options.database;
169
+ }
170
+ return this.#options.open({
171
+ name: this.#options.name,
172
+ location: this.#options.location,
173
+ });
174
+ }
175
+ }
176
+
177
+ class NitroSqliteConnection implements DatabaseConnection {
178
+ readonly #db: NitroSQLiteConnection;
179
+
180
+ constructor(db: NitroSQLiteConnection) {
181
+ this.#db = db;
182
+ }
183
+
184
+ async executeQuery<R>(compiledQuery: CompiledQuery): Promise<QueryResult<R>> {
185
+ const { sql, parameters } = compiledQuery;
186
+ const params = parameters as SQLiteValue[];
187
+
188
+ // Determine if this is a SELECT / RETURNING query
189
+ const isSelect = /^\s*(select|pragma|explain|with)\b/i.test(sql);
190
+
191
+ // Execute the query
192
+ const result = this.#db.execute(sql, params);
193
+
194
+ if (isSelect) {
195
+ return { rows: (result.results ?? []) as R[] };
196
+ }
197
+
198
+ // For INSERT, UPDATE, DELETE — return affected rows info
199
+ return {
200
+ rows: [],
201
+ numAffectedRows: BigInt(result.rowsAffected ?? 0),
202
+ insertId: BigInt(result.insertId ?? 0),
203
+ };
204
+ }
205
+
206
+ streamQuery<R>(
207
+ _compiledQuery: CompiledQuery,
208
+ _chunkSize?: number
209
+ ): AsyncIterableIterator<QueryResult<R>> {
210
+ throw new Error(
211
+ 'react-native-nitro-sqlite driver does not support streaming'
212
+ );
213
+ }
214
+ }