react-native-nitro-sqlite-kysely 0.0.0-stage → 11.0.0-alpha.0

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/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Marc Rousavy & Christoph Pader
4
+ Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ of this software and associated documentation files (the "Software"), to deal
6
+ in the Software without restriction, including without limitation the rights
7
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ copies of the Software, and to permit persons to whom the Software is
9
+ furnished to do so, subject to the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be included in all
12
+ copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,27 @@
1
- # Temporary Holding Version
1
+ # Nitro SQLite for Kysely
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Use Kysely's typed SQL builder with a local Nitro SQLite database on React Native. Install `react-native-nitro-sqlite-kysely`, `kysely@^0.29.9`, and the [Nitro SQLite native dependencies](https://sqlite.margelo.com/docs).
4
+
5
+ Install the 11.0.0 alpha from the `alpha` tag. Save exact versions: npm otherwise saves `^11.0.0-alpha.0`, which also matches the older `11.0.0-nitro-v1.x` builds.
6
+
7
+ ```sh
8
+ npm install --save-exact react-native-nitro-sqlite@alpha react-native-nitro-sqlite-kysely@alpha
9
+ npm install 'kysely@^0.29.9'
10
+ ```
11
+
12
+ See the [companion installation guide](../../../../docs/content/docs/integrations/orms/index.mdx) for peer dependencies.
13
+
14
+ ```ts
15
+ import { Kysely } from 'kysely'
16
+ import { createNitroSQLiteDialect } from 'react-native-nitro-sqlite-kysely'
17
+
18
+ interface Database {
19
+ notes: { id: number; title: string }
20
+ }
21
+
22
+ const db = new Kysely<Database>({
23
+ dialect: createNitroSQLiteDialect({ name: 'notes.sqlite' }),
24
+ })
25
+ ```
26
+
27
+ The first query opens an independent native handle. Await your queries and transactions before closing it with `await db.destroy()`. See the [Kysely quick start](https://sqlite.margelo.com/docs/integrations/orms#kysely) for table creation and queries, and the [Kysely guide](https://sqlite.margelo.com/docs/integrations/orms/kysely) for transactions and migrations.
package/package.json CHANGED
@@ -1,6 +1,57 @@
1
1
  {
2
2
  "name": "react-native-nitro-sqlite-kysely",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "11.0.0-alpha.0",
4
+ "description": "Kysely SQLite dialect for React Native using Nitro SQLite.",
5
+ "source": "./src/index.ts",
6
+ "react-native": "./src/index.ts",
7
+ "main": "./src/index.ts",
8
+ "types": "./src/index.ts",
9
+ "files": [
10
+ "src",
11
+ "scripts",
12
+ "README.md",
13
+ "LICENSE",
14
+ "tsconfig.json"
15
+ ],
16
+ "scripts": {
17
+ "materialize": "node scripts/materialize.mjs",
18
+ "typecheck": "node scripts/materialize.mjs && tsc --build",
19
+ "prepack": "bun run typecheck",
20
+ "prepublishOnly": "bun run typecheck",
21
+ "test": "node scripts/materialize.mjs && node --experimental-strip-types --test tests/*.test.ts",
22
+ "pack": "bun pm pack",
23
+ "lint": "eslint \"src/**/*.ts\" --fix"
24
+ },
25
+ "repository": {
26
+ "type": "git",
27
+ "url": "git+https://github.com/margelo/nitro-sqlite.git",
28
+ "directory": "packages/integrations/react-native/kysely"
29
+ },
30
+ "author": "Christoph Pader",
31
+ "license": "MIT",
32
+ "publishConfig": {
33
+ "registry": "https://registry.npmjs.org/",
34
+ "access": "public"
35
+ },
36
+ "dependencies": {
37
+ "kysely-generic-sqlite": "^2.0.1"
38
+ },
39
+ "peerDependencies": {
40
+ "kysely": "^0.29.9",
41
+ "react-native-nitro-sqlite": ">=11.0.0-0"
42
+ },
43
+ "devDependencies": {
44
+ "kysely": "0.29.9",
45
+ "react-native-nitro-sqlite": "11.0.0-alpha.0",
46
+ "better-sqlite3": "^12.11.1",
47
+ "@types/node": "^25.0.0",
48
+ "typescript": "^6.0.3"
49
+ },
50
+ "exports": {
51
+ ".": {
52
+ "types": "./src/index.ts",
53
+ "default": "./src/index.ts"
54
+ },
55
+ "./package.json": "./package.json"
56
+ }
57
+ }
@@ -0,0 +1,19 @@
1
+ import { existsSync } from 'node:fs'
2
+ import { dirname, join, resolve } from 'node:path'
3
+ import { fileURLToPath, pathToFileURL } from 'node:url'
4
+
5
+ const packageDirectory = resolve(dirname(fileURLToPath(import.meta.url)), '..')
6
+ const sharedHelper = resolve(
7
+ packageDirectory,
8
+ '../../../../shared/orm/materialize-sources.mjs',
9
+ )
10
+ if (existsSync(sharedHelper)) {
11
+ const { materializeOrmSources } = await import(
12
+ pathToFileURL(sharedHelper).href
13
+ )
14
+ materializeOrmSources(packageDirectory)
15
+ } else if (!existsSync(join(packageDirectory, 'src/index.ts'))) {
16
+ throw new Error(
17
+ 'ORM source is missing. Prepare the package from the workspace before packing it.',
18
+ )
19
+ }
package/src/index.ts ADDED
@@ -0,0 +1,114 @@
1
+ import type { DatabaseConnection, Dialect } from 'kysely'
2
+ import {
3
+ BaseSqliteDialect,
4
+ GenericSqliteDriver,
5
+ type IGenericSqlite,
6
+ } from 'kysely-generic-sqlite'
7
+ import {
8
+ open,
9
+ type ConnectionOptions,
10
+ type SQLiteValue,
11
+ } from 'react-native-nitro-sqlite'
12
+
13
+ /** SQLite file, location, access mode and optional SEE key for one dedicated native handle. */
14
+ export type NitroSQLiteDialectOptions = Omit<ConnectionOptions, 'connection'>
15
+
16
+ /**
17
+ * Create a Kysely SQLite dialect for React Native and NitroUI. The first query opens an
18
+ * independent Nitro SQLite handle. Kysely serializes queries and holds the
19
+ * handle for the duration of a transaction or connection callback.
20
+ * Queries reject on open, bind or SQLite errors. Streaming is unsupported.
21
+ * Await outstanding work, then call `Kysely.destroy()` to close the handle.
22
+ * @param options Database name and optional Nitro SQLite file options.
23
+ * @returns A dialect for the `dialect` option of a new Kysely instance.
24
+ * @see [Kysely guide](https://sqlite.margelo.com/docs/integrations/orms/kysely)
25
+ */
26
+ export function createNitroSQLiteDialect(
27
+ options: NitroSQLiteDialectOptions,
28
+ ): Dialect {
29
+ return new BaseSqliteDialect(
30
+ () => new StandaloneDriver(() => openNitroSQLiteExecutor(options)),
31
+ )
32
+ }
33
+
34
+ /**
35
+ * Open the async executor used by the Kysely dialect and MikroORM. Custom
36
+ * kysely-generic-sqlite drivers can use it to manage their own connection leases.
37
+ * @param options Database name and optional Nitro SQLite file options.
38
+ * @returns An executor that owns an independent handle. Its owner must serialize
39
+ * transactions and call `close()` after the last operation.
40
+ * @throws If the file cannot be opened. Queries reject on unsupported binds and SQLite errors.
41
+ */
42
+ export function openNitroSQLiteExecutor(
43
+ options: NitroSQLiteDialectOptions,
44
+ ): IGenericSqlite {
45
+ const db = open({ ...options, connection: 'independent' })
46
+ return {
47
+ db,
48
+ close: () => db.close(),
49
+ query: async (_isSelect, sql, parameters) => {
50
+ const result = await db.executeAsync(sql, parameters.map(bindValue))
51
+ const rows = result.results.map((row) =>
52
+ Object.fromEntries(
53
+ Object.entries(row).map(([column, value]) => [
54
+ column,
55
+ value instanceof ArrayBuffer ? new Uint8Array(value) : value,
56
+ ]),
57
+ ),
58
+ )
59
+ // SELECT, PRAGMA and RETURNING can leave SQLite's change counters untouched.
60
+ if (result.metadata) return { rows }
61
+ return {
62
+ rows,
63
+ numAffectedRows: BigInt(result.rowsAffected),
64
+ insertId: result.insertId == null ? undefined : BigInt(result.insertId),
65
+ }
66
+ },
67
+ }
68
+ }
69
+
70
+ class StandaloneDriver extends GenericSqliteDriver {
71
+ #acquired = false
72
+
73
+ override async acquireConnection(): Promise<DatabaseConnection> {
74
+ // Kysely already holds its SQLite mutex here. If destroy won during init,
75
+ // executeQuery rejects on the closed handle and Kysely releases the lease.
76
+ this.#acquired = true
77
+ return super.acquireConnection()
78
+ }
79
+
80
+ override async releaseConnection(): Promise<void> {
81
+ this.#acquired = false
82
+ }
83
+
84
+ override async destroy(): Promise<void> {
85
+ if (this.#acquired)
86
+ throw new Error(
87
+ 'Cannot close the Nitro SQLite connection while it is busy.',
88
+ )
89
+ await super.destroy()
90
+ }
91
+ }
92
+
93
+ function bindValue(value: unknown): SQLiteValue {
94
+ if (
95
+ value == null ||
96
+ typeof value === 'boolean' ||
97
+ typeof value === 'number' ||
98
+ typeof value === 'string' ||
99
+ value instanceof ArrayBuffer
100
+ ) {
101
+ return value
102
+ }
103
+
104
+ if (ArrayBuffer.isView(value)) {
105
+ return new Uint8Array(
106
+ value.buffer,
107
+ value.byteOffset,
108
+ value.byteLength,
109
+ ).slice().buffer
110
+ }
111
+
112
+ const type = value instanceof Date ? 'Date' : typeof value
113
+ throw new TypeError(`Unsupported Nitro SQLite query parameter type: ${type}`)
114
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,15 @@
1
+ {
2
+ "extends": "../../../../config/tsconfig.json",
3
+ "include": ["src"],
4
+ "references": [{ "path": "../../../react-native-nitro-sqlite" }],
5
+ "compilerOptions": {
6
+ "rootDir": "src",
7
+ "outDir": ".tsbuild",
8
+ "emitDeclarationOnly": true,
9
+ "noEmit": false,
10
+ "tsBuildInfoFile": ".tsbuild/tsconfig.tsbuildinfo",
11
+ "paths": {
12
+ "react-native-nitro-sqlite": ["../../../react-native-nitro-sqlite/src/index.ts"]
13
+ }
14
+ }
15
+ }