react-native-nitro-sqlite-kysely 0.0.0-stage → 10.5.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/README.md +17 -2
- package/package.json +37 -4
- package/src/index.ts +117 -0
package/README.md
CHANGED
|
@@ -1,3 +1,18 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Nitro SQLite for Kysely
|
|
2
2
|
|
|
3
|
-
|
|
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
|
+
```ts
|
|
6
|
+
import { Kysely } from 'kysely'
|
|
7
|
+
import { createNitroSQLiteDialect } from 'react-native-nitro-sqlite-kysely'
|
|
8
|
+
|
|
9
|
+
interface Database {
|
|
10
|
+
notes: { id: number; title: string }
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const db = new Kysely<Database>({
|
|
14
|
+
dialect: createNitroSQLiteDialect({ name: 'notes.sqlite' }),
|
|
15
|
+
})
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
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,39 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-native-nitro-sqlite-kysely",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "10.5.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
|
+
"README.md"
|
|
12
|
+
],
|
|
13
|
+
"scripts": {
|
|
14
|
+
"typecheck": "tsc --build",
|
|
15
|
+
"lint": "eslint \"src/**/*.ts\" --fix",
|
|
16
|
+
"prepublishOnly": "bun typecheck"
|
|
17
|
+
},
|
|
18
|
+
"repository": {
|
|
19
|
+
"type": "git",
|
|
20
|
+
"url": "git+https://github.com/margelo/react-native-nitro-sqlite.git"
|
|
21
|
+
},
|
|
22
|
+
"author": "Christoph Pader",
|
|
23
|
+
"license": "MIT",
|
|
24
|
+
"publishConfig": {
|
|
25
|
+
"registry": "https://registry.npmjs.org/",
|
|
26
|
+
"access": "public"
|
|
27
|
+
},
|
|
28
|
+
"dependencies": {
|
|
29
|
+
"kysely-generic-sqlite": "^2.0.1"
|
|
30
|
+
},
|
|
31
|
+
"peerDependencies": {
|
|
32
|
+
"kysely": "^0.29.9",
|
|
33
|
+
"react-native-nitro-sqlite": ">=10.4.0"
|
|
34
|
+
},
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"kysely": "0.29.9",
|
|
37
|
+
"react-native-nitro-sqlite": "10.5.0"
|
|
38
|
+
}
|
|
39
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
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 NitroSQLiteConnectionOptions,
|
|
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<
|
|
15
|
+
NitroSQLiteConnectionOptions,
|
|
16
|
+
'connection'
|
|
17
|
+
>
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Create a Kysely SQLite dialect for React Native. The first query opens an
|
|
21
|
+
* independent Nitro SQLite handle. Kysely serializes queries and holds the
|
|
22
|
+
* handle for the duration of a transaction or connection callback.
|
|
23
|
+
* Queries reject on open, bind or SQLite errors. Streaming is unsupported.
|
|
24
|
+
* Await outstanding work, then call `Kysely.destroy()` to close the handle.
|
|
25
|
+
* @param options Database name and optional Nitro SQLite file options.
|
|
26
|
+
* @returns A dialect for the `dialect` option of a new Kysely instance.
|
|
27
|
+
* @see [Kysely guide](https://sqlite.margelo.com/docs/integrations/orms/kysely)
|
|
28
|
+
*/
|
|
29
|
+
export function createNitroSQLiteDialect(
|
|
30
|
+
options: NitroSQLiteDialectOptions,
|
|
31
|
+
): Dialect {
|
|
32
|
+
return new BaseSqliteDialect(
|
|
33
|
+
() => new StandaloneDriver(() => openNitroSQLiteExecutor(options)),
|
|
34
|
+
)
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Open the async executor used by the Kysely dialect and MikroORM. Custom
|
|
39
|
+
* kysely-generic-sqlite drivers can use it to manage their own connection leases.
|
|
40
|
+
* @param options Database name and optional Nitro SQLite file options.
|
|
41
|
+
* @returns An executor that owns an independent handle. Its owner must serialize
|
|
42
|
+
* transactions and call `close()` after the last operation.
|
|
43
|
+
* @throws If the file cannot be opened. Queries reject on unsupported binds and SQLite errors.
|
|
44
|
+
*/
|
|
45
|
+
export function openNitroSQLiteExecutor(
|
|
46
|
+
options: NitroSQLiteDialectOptions,
|
|
47
|
+
): IGenericSqlite {
|
|
48
|
+
const db = open({ ...options, connection: 'independent' })
|
|
49
|
+
return {
|
|
50
|
+
db,
|
|
51
|
+
close: () => db.close(),
|
|
52
|
+
query: async (_isSelect, sql, parameters) => {
|
|
53
|
+
const result = await db.executeAsync(sql, parameters.map(bindValue))
|
|
54
|
+
const rows = result.results.map((row) =>
|
|
55
|
+
Object.fromEntries(
|
|
56
|
+
Object.entries(row).map(([column, value]) => [
|
|
57
|
+
column,
|
|
58
|
+
value instanceof ArrayBuffer ? new Uint8Array(value) : value,
|
|
59
|
+
]),
|
|
60
|
+
),
|
|
61
|
+
)
|
|
62
|
+
// SELECT, PRAGMA and RETURNING can leave SQLite's change counters untouched.
|
|
63
|
+
if (result.metadata) return { rows }
|
|
64
|
+
return {
|
|
65
|
+
rows,
|
|
66
|
+
numAffectedRows: BigInt(result.rowsAffected),
|
|
67
|
+
insertId: result.insertId == null ? undefined : BigInt(result.insertId),
|
|
68
|
+
}
|
|
69
|
+
},
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
class StandaloneDriver extends GenericSqliteDriver {
|
|
74
|
+
#acquired = false
|
|
75
|
+
|
|
76
|
+
override async acquireConnection(): Promise<DatabaseConnection> {
|
|
77
|
+
// Kysely already holds its SQLite mutex here. If destroy won during init,
|
|
78
|
+
// executeQuery rejects on the closed handle and Kysely releases the lease.
|
|
79
|
+
this.#acquired = true
|
|
80
|
+
return super.acquireConnection()
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
override async releaseConnection(): Promise<void> {
|
|
84
|
+
this.#acquired = false
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
override async destroy(): Promise<void> {
|
|
88
|
+
if (this.#acquired)
|
|
89
|
+
throw new Error(
|
|
90
|
+
'Cannot close the Nitro SQLite connection while it is busy.',
|
|
91
|
+
)
|
|
92
|
+
await super.destroy()
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function bindValue(value: unknown): SQLiteValue {
|
|
97
|
+
if (
|
|
98
|
+
value == null ||
|
|
99
|
+
typeof value === 'boolean' ||
|
|
100
|
+
typeof value === 'number' ||
|
|
101
|
+
typeof value === 'string' ||
|
|
102
|
+
value instanceof ArrayBuffer
|
|
103
|
+
) {
|
|
104
|
+
return value
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
if (ArrayBuffer.isView(value)) {
|
|
108
|
+
return new Uint8Array(
|
|
109
|
+
value.buffer,
|
|
110
|
+
value.byteOffset,
|
|
111
|
+
value.byteLength,
|
|
112
|
+
).slice().buffer
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const type = value instanceof Date ? 'Date' : typeof value
|
|
116
|
+
throw new TypeError(`Unsupported Nitro SQLite query parameter type: ${type}`)
|
|
117
|
+
}
|