@sqb/sqlite 6.0.9 → 6.0.10
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 +1 -1
- package/constants.d.ts +3 -0
- package/constants.js +3 -0
- package/drivers/bun-driver.d.ts +1 -0
- package/drivers/bun-driver.js +5 -0
- package/drivers/index.d.ts +6 -0
- package/drivers/index.js +7 -0
- package/drivers/node-driver.d.ts +1 -0
- package/drivers/node-driver.js +5 -0
- package/drivers/types.d.ts +5 -0
- package/index.d.ts +11 -0
- package/index.js +11 -0
- package/package.json +8 -6
- package/sqlite-adapter.d.ts +20 -0
- package/sqlite-adapter.js +32 -0
- package/sqlite-connection.d.ts +28 -0
- package/sqlite-connection.js +30 -0
- package/sqlite-cursor.d.ts +8 -0
- package/sqlite-cursor.js +8 -0
package/README.md
CHANGED
|
@@ -60,5 +60,5 @@ SQB is available under [MIT](LICENSE) license.
|
|
|
60
60
|
[downloads-url]: https://npmjs.org/package/@sqb/sqlite
|
|
61
61
|
[ci-test-image]: https://github.com/panates/sqb/actions/workflows/test.yml/badge.svg
|
|
62
62
|
[ci-test-url]: https://github.com/panates/sqb/actions/workflows/test.yml
|
|
63
|
-
[coveralls-image]: https://coveralls.io/repos/github/
|
|
63
|
+
[coveralls-image]: https://coveralls.io/repos/github/panates/sqb/badge.svg?branch=dev
|
|
64
64
|
[coveralls-url]: https://coveralls.io/github/sqbjs/sqb?branch=master
|
package/constants.d.ts
ADDED
package/constants.js
ADDED
package/drivers/bun-driver.d.ts
CHANGED
package/drivers/bun-driver.js
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
// This module is only ever loaded via a dynamic import() from a Bun
|
|
2
2
|
// runtime (see ./index.ts), so a static import of 'bun:sqlite' is safe.
|
|
3
3
|
import { Database } from 'bun:sqlite';
|
|
4
|
+
/** {@link NativeStatement} implementation wrapping `bun:sqlite`'s `Statement`. */
|
|
4
5
|
class BunStatement {
|
|
5
6
|
_stmt;
|
|
6
7
|
constructor(_stmt) {
|
|
7
8
|
this._stmt = _stmt;
|
|
8
9
|
}
|
|
10
|
+
/** Runs the statement, normalizing the result to {@link NativeRunResult}'s shape. */
|
|
9
11
|
run(params) {
|
|
10
12
|
const r = params ? this._stmt.run(params) : this._stmt.run();
|
|
11
13
|
return {
|
|
@@ -22,6 +24,7 @@ class BunStatement {
|
|
|
22
24
|
iterate(params) {
|
|
23
25
|
return params ? this._stmt.iterate(params) : this._stmt.iterate();
|
|
24
26
|
}
|
|
27
|
+
/** Converts `bun:sqlite`'s column metadata into {@link NativeColumnInfo}. */
|
|
25
28
|
columns() {
|
|
26
29
|
const names = this._stmt.columnNames;
|
|
27
30
|
let types = [];
|
|
@@ -38,6 +41,7 @@ class BunStatement {
|
|
|
38
41
|
}));
|
|
39
42
|
}
|
|
40
43
|
}
|
|
44
|
+
/** {@link NativeDatabase} implementation wrapping `bun:sqlite`'s `Database`. */
|
|
41
45
|
class BunDatabase {
|
|
42
46
|
_db;
|
|
43
47
|
constructor(_db) {
|
|
@@ -56,6 +60,7 @@ class BunDatabase {
|
|
|
56
60
|
this._db.close();
|
|
57
61
|
}
|
|
58
62
|
}
|
|
63
|
+
/** {@link SqliteDriver} backed by Bun's built-in `bun:sqlite` module. */
|
|
59
64
|
export const bunDriver = {
|
|
60
65
|
open(filename) {
|
|
61
66
|
return new BunDatabase(new Database(filename));
|
package/drivers/index.d.ts
CHANGED
|
@@ -1,2 +1,8 @@
|
|
|
1
1
|
import type { NativeDatabase } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Opens `filename` with the driver matching the current JS runtime -
|
|
4
|
+
* `bun:sqlite` under Bun, `node:sqlite` otherwise - dynamically importing
|
|
5
|
+
* only the matching driver module, so the other runtime's native module
|
|
6
|
+
* (unavailable outside its own runtime) is never loaded.
|
|
7
|
+
*/
|
|
2
8
|
export declare function openDatabase(filename: string): Promise<NativeDatabase>;
|
package/drivers/index.js
CHANGED
|
@@ -1,5 +1,12 @@
|
|
|
1
|
+
/** Detects a Bun runtime by the presence of the global `Bun` object and its `version` string. */
|
|
1
2
|
const isBun = typeof globalThis.Bun !== 'undefined' &&
|
|
2
3
|
typeof globalThis.Bun.version === 'string';
|
|
4
|
+
/**
|
|
5
|
+
* Opens `filename` with the driver matching the current JS runtime -
|
|
6
|
+
* `bun:sqlite` under Bun, `node:sqlite` otherwise - dynamically importing
|
|
7
|
+
* only the matching driver module, so the other runtime's native module
|
|
8
|
+
* (unavailable outside its own runtime) is never loaded.
|
|
9
|
+
*/
|
|
3
10
|
export async function openDatabase(filename) {
|
|
4
11
|
if (isBun) {
|
|
5
12
|
const { bunDriver } = await import('./bun-driver.js');
|
package/drivers/node-driver.d.ts
CHANGED
package/drivers/node-driver.js
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
// This module is only ever loaded via a dynamic import() from a Node.js
|
|
2
2
|
// runtime (see ./index.ts), so a static import of 'node:sqlite' is safe.
|
|
3
3
|
import { DatabaseSync, } from 'node:sqlite';
|
|
4
|
+
/** {@link NativeStatement} implementation wrapping `node:sqlite`'s `StatementSync`. */
|
|
4
5
|
class NodeStatement {
|
|
5
6
|
_stmt;
|
|
6
7
|
constructor(_stmt) {
|
|
7
8
|
this._stmt = _stmt;
|
|
8
9
|
}
|
|
10
|
+
/** Runs the statement, converting `node:sqlite`'s `bigint` result fields to plain `number`s (see {@link NativeRunResult}). */
|
|
9
11
|
run(params) {
|
|
10
12
|
const r = params ? this._stmt.run(params) : this._stmt.run();
|
|
11
13
|
return {
|
|
@@ -22,6 +24,7 @@ class NodeStatement {
|
|
|
22
24
|
iterate(params) {
|
|
23
25
|
return (params ? this._stmt.iterate(params) : this._stmt.iterate());
|
|
24
26
|
}
|
|
27
|
+
/** Converts `node:sqlite`'s column metadata into {@link NativeColumnInfo}. */
|
|
25
28
|
columns() {
|
|
26
29
|
return this._stmt.columns().map((c) => ({
|
|
27
30
|
name: c.name,
|
|
@@ -29,6 +32,7 @@ class NodeStatement {
|
|
|
29
32
|
}));
|
|
30
33
|
}
|
|
31
34
|
}
|
|
35
|
+
/** {@link NativeDatabase} implementation wrapping `node:sqlite`'s `DatabaseSync`. */
|
|
32
36
|
class NodeDatabase {
|
|
33
37
|
_db;
|
|
34
38
|
constructor(_db) {
|
|
@@ -47,6 +51,7 @@ class NodeDatabase {
|
|
|
47
51
|
this._db.close();
|
|
48
52
|
}
|
|
49
53
|
}
|
|
54
|
+
/** {@link SqliteDriver} backed by Node's built-in `node:sqlite` module. */
|
|
50
55
|
export const nodeDriver = {
|
|
51
56
|
open(filename) {
|
|
52
57
|
return new NodeDatabase(new DatabaseSync(filename));
|
package/drivers/types.d.ts
CHANGED
|
@@ -1,11 +1,14 @@
|
|
|
1
|
+
/** A single result column's name and (if declared in the schema) SQL type, as reported by a {@link NativeStatement}. */
|
|
1
2
|
export interface NativeColumnInfo {
|
|
2
3
|
name: string;
|
|
3
4
|
declaredType: string | null;
|
|
4
5
|
}
|
|
6
|
+
/** Outcome of a data-modifying statement, normalized to plain `number`s across both `node:sqlite` (which returns `bigint`) and `bun:sqlite`. */
|
|
5
7
|
export interface NativeRunResult {
|
|
6
8
|
changes: number;
|
|
7
9
|
lastInsertRowid: number;
|
|
8
10
|
}
|
|
11
|
+
/** Common surface a prepared statement must expose, implemented separately for `node:sqlite` (`./node-driver.ts`) and `bun:sqlite` (`./bun-driver.ts`) so {@link SqliteConnection} can stay driver-agnostic. */
|
|
9
12
|
export interface NativeStatement {
|
|
10
13
|
run(params?: Record<string, any>): NativeRunResult;
|
|
11
14
|
get(params?: Record<string, any>): Record<string, any> | undefined;
|
|
@@ -17,12 +20,14 @@ export interface NativeStatement {
|
|
|
17
20
|
*/
|
|
18
21
|
columns(): NativeColumnInfo[];
|
|
19
22
|
}
|
|
23
|
+
/** Common surface an open database connection must expose, implemented separately for `node:sqlite` (`./node-driver.ts`) and `bun:sqlite` (`./bun-driver.ts`). */
|
|
20
24
|
export interface NativeDatabase {
|
|
21
25
|
readonly inTransaction: boolean;
|
|
22
26
|
exec(sql: string): void;
|
|
23
27
|
prepare(sql: string): NativeStatement;
|
|
24
28
|
close(): void;
|
|
25
29
|
}
|
|
30
|
+
/** Opens a {@link NativeDatabase} for a given runtime - see `./index.ts`, which picks `bunDriver` or `nodeDriver` based on which runtime is active. */
|
|
26
31
|
export interface SqliteDriver {
|
|
27
32
|
open(filename: string): NativeDatabase;
|
|
28
33
|
}
|
package/index.d.ts
CHANGED
|
@@ -1,4 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @sqb/sqlite - `@sqb/connect` database driver for SQLite, backed by
|
|
3
|
+
* Node's built-in `node:sqlite` or, when running under Bun, `bun:sqlite`.
|
|
4
|
+
*
|
|
5
|
+
* Importing this package (even without using any of its exports) registers
|
|
6
|
+
* {@link SqliteAdapter} with `@sqb/connect`'s `AdapterRegistry` as a side
|
|
7
|
+
* effect (and, via `@sqb/sqlite-dialect`, `@sqb/builder`'s
|
|
8
|
+
* `SerializerRegistry`), making `new SqbClient({ dialect: 'sqlite', ...
|
|
9
|
+
* })` work.
|
|
10
|
+
*/
|
|
1
11
|
import '@sqb/sqlite-dialect';
|
|
12
|
+
export * from './constants.js';
|
|
2
13
|
export * from './sqlite-adapter.js';
|
|
3
14
|
export * from './sqlite-connection.js';
|
|
4
15
|
export * from './sqlite-cursor.js';
|
package/index.js
CHANGED
|
@@ -1,6 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @sqb/sqlite - `@sqb/connect` database driver for SQLite, backed by
|
|
3
|
+
* Node's built-in `node:sqlite` or, when running under Bun, `bun:sqlite`.
|
|
4
|
+
*
|
|
5
|
+
* Importing this package (even without using any of its exports) registers
|
|
6
|
+
* {@link SqliteAdapter} with `@sqb/connect`'s `AdapterRegistry` as a side
|
|
7
|
+
* effect (and, via `@sqb/sqlite-dialect`, `@sqb/builder`'s
|
|
8
|
+
* `SerializerRegistry`), making `new SqbClient({ dialect: 'sqlite', ...
|
|
9
|
+
* })` work.
|
|
10
|
+
*/
|
|
1
11
|
import '@sqb/sqlite-dialect';
|
|
2
12
|
import { AdapterRegistry } from '@sqb/connect';
|
|
3
13
|
import { SqliteAdapter } from './sqlite-adapter.js';
|
|
14
|
+
export * from './constants.js';
|
|
4
15
|
export * from './sqlite-adapter.js';
|
|
5
16
|
export * from './sqlite-connection.js';
|
|
6
17
|
export * from './sqlite-cursor.js';
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sqb/sqlite",
|
|
3
3
|
"description": "SQB adapter for Node.js and Bun's native SQLite drivers",
|
|
4
|
-
"version": "6.0.
|
|
4
|
+
"version": "6.0.10",
|
|
5
5
|
"author": "Panates",
|
|
6
6
|
"license": "BSD-3-Clause",
|
|
7
7
|
"homepage": "https://www.sqbjs.org",
|
|
@@ -9,9 +9,9 @@
|
|
|
9
9
|
"tslib": "^2.8.1"
|
|
10
10
|
},
|
|
11
11
|
"peerDependencies": {
|
|
12
|
-
"@sqb/builder": "^6.0.
|
|
13
|
-
"@sqb/connect": "^6.0.
|
|
14
|
-
"@sqb/sqlite-dialect": "^6.0.
|
|
12
|
+
"@sqb/builder": "^6.0.10",
|
|
13
|
+
"@sqb/connect": "^6.0.10",
|
|
14
|
+
"@sqb/sqlite-dialect": "^6.0.10"
|
|
15
15
|
},
|
|
16
16
|
"type": "module",
|
|
17
17
|
"module": "./index.js",
|
|
@@ -28,7 +28,9 @@
|
|
|
28
28
|
},
|
|
29
29
|
"contributors": [
|
|
30
30
|
"Eray Hanoglu <e.hanoglu@panates.com>",
|
|
31
|
-
"Ilker Gurelli <i.gurelli@panates.com>"
|
|
31
|
+
"Ilker Gurelli <i.gurelli@panates.com>",
|
|
32
|
+
"Onur Tokel <o.tokel@panates.com>",
|
|
33
|
+
"Bircan Yuruk <b.yuruk@panates.com>"
|
|
32
34
|
],
|
|
33
35
|
"repository": {
|
|
34
36
|
"type": "git",
|
|
@@ -50,4 +52,4 @@
|
|
|
50
52
|
"publishConfig": {
|
|
51
53
|
"access": "public"
|
|
52
54
|
}
|
|
53
|
-
}
|
|
55
|
+
}
|
package/sqlite-adapter.d.ts
CHANGED
|
@@ -1,9 +1,29 @@
|
|
|
1
1
|
import '@sqb/sqlite-dialect';
|
|
2
2
|
import type { Adapter, ClientConfiguration } from '@sqb/connect';
|
|
3
|
+
/**
|
|
4
|
+
* `@sqb/connect` {@link Adapter} for SQLite, wrapping either Node's
|
|
5
|
+
* built-in `node:sqlite` or, when running under Bun, `bun:sqlite` (see
|
|
6
|
+
* `./drivers/index.ts`). Registered automatically as a side effect of
|
|
7
|
+
* importing this package - see `index.ts`.
|
|
8
|
+
*/
|
|
3
9
|
export declare class SqliteAdapter implements Adapter {
|
|
4
10
|
driver: string;
|
|
5
11
|
dialect: string;
|
|
6
12
|
features: Adapter.Features;
|
|
13
|
+
/**
|
|
14
|
+
* Opens (or reuses, via {@link dbCache}) the native SQLite database at
|
|
15
|
+
* `config.database` - a `:memory:`/`:memory:name` in-memory database, or
|
|
16
|
+
* a file path resolved relative to the current working directory.
|
|
17
|
+
*
|
|
18
|
+
* @throws {Error} if `config.database` isn't given
|
|
19
|
+
*/
|
|
7
20
|
connect(config: ClientConfiguration): Promise<Adapter.Connection>;
|
|
8
21
|
}
|
|
22
|
+
/**
|
|
23
|
+
* Closes and evicts an in-memory database from {@link dbCache} by name
|
|
24
|
+
* (defaulting to the unnamed `:memory:` database). In-memory databases are
|
|
25
|
+
* never closed automatically by a connection's `close()` (unlike file
|
|
26
|
+
* databases, which are refcounted), so this is the only way to release one
|
|
27
|
+
* once no longer needed - primarily useful for test teardown.
|
|
28
|
+
*/
|
|
9
29
|
export declare function closeMemoryDatabase(name?: string): Promise<void>;
|
package/sqlite-adapter.js
CHANGED
|
@@ -2,13 +2,38 @@ import '@sqb/sqlite-dialect';
|
|
|
2
2
|
import path from 'path';
|
|
3
3
|
import { openDatabase } from './drivers/index.js';
|
|
4
4
|
import { SqliteConnection } from './sqlite-connection.js';
|
|
5
|
+
/**
|
|
6
|
+
* Open native databases keyed by resolved file path, shared across
|
|
7
|
+
* concurrent SQB connections opened against the same file - SQLite has no
|
|
8
|
+
* connection pooling of its own, so a second `connect()` call for a file
|
|
9
|
+
* already open reuses the same native handle (refcounted) rather than
|
|
10
|
+
* opening it twice. An in-memory database (`:memory:` or `:memory:name`)
|
|
11
|
+
* is cached too (so multiple connections can see the same in-memory data)
|
|
12
|
+
* but is never refcount-closed by {@link SqliteAdapter.connect}'s closer -
|
|
13
|
+
* only {@link closeMemoryDatabase} closes one, since closing it on the
|
|
14
|
+
* last connection's `close()` would silently discard that in-memory data
|
|
15
|
+
* for any code still expecting to reopen it by name.
|
|
16
|
+
*/
|
|
5
17
|
const dbCache = new Map();
|
|
18
|
+
/**
|
|
19
|
+
* `@sqb/connect` {@link Adapter} for SQLite, wrapping either Node's
|
|
20
|
+
* built-in `node:sqlite` or, when running under Bun, `bun:sqlite` (see
|
|
21
|
+
* `./drivers/index.ts`). Registered automatically as a side effect of
|
|
22
|
+
* importing this package - see `index.ts`.
|
|
23
|
+
*/
|
|
6
24
|
export class SqliteAdapter {
|
|
7
25
|
driver = 'sqlite';
|
|
8
26
|
dialect = 'sqlite';
|
|
9
27
|
features = {
|
|
10
28
|
cursor: true,
|
|
11
29
|
};
|
|
30
|
+
/**
|
|
31
|
+
* Opens (or reuses, via {@link dbCache}) the native SQLite database at
|
|
32
|
+
* `config.database` - a `:memory:`/`:memory:name` in-memory database, or
|
|
33
|
+
* a file path resolved relative to the current working directory.
|
|
34
|
+
*
|
|
35
|
+
* @throws {Error} if `config.database` isn't given
|
|
36
|
+
*/
|
|
12
37
|
async connect(config) {
|
|
13
38
|
if (!config.database)
|
|
14
39
|
throw new Error('You must provide a sqlite database file');
|
|
@@ -35,6 +60,13 @@ export class SqliteAdapter {
|
|
|
35
60
|
});
|
|
36
61
|
}
|
|
37
62
|
}
|
|
63
|
+
/**
|
|
64
|
+
* Closes and evicts an in-memory database from {@link dbCache} by name
|
|
65
|
+
* (defaulting to the unnamed `:memory:` database). In-memory databases are
|
|
66
|
+
* never closed automatically by a connection's `close()` (unlike file
|
|
67
|
+
* databases, which are refcounted), so this is the only way to release one
|
|
68
|
+
* once no longer needed - primarily useful for test teardown.
|
|
69
|
+
*/
|
|
38
70
|
export async function closeMemoryDatabase(name) {
|
|
39
71
|
const memoryDbName = name || ':memory:';
|
|
40
72
|
const memDb = dbCache.get(memoryDbName);
|
package/sqlite-connection.d.ts
CHANGED
|
@@ -1,18 +1,46 @@
|
|
|
1
1
|
import type { Adapter, QueryRequest } from '@sqb/connect';
|
|
2
2
|
import type { NativeDatabase } from './drivers/types.js';
|
|
3
|
+
/**
|
|
4
|
+
* `@sqb/connect` {@link Adapter.Connection} wrapping a {@link
|
|
5
|
+
* NativeDatabase} (Node's `node:sqlite` or Bun's `bun:sqlite`, behind a
|
|
6
|
+
* common driver interface - see `./drivers/types.ts`): translates
|
|
7
|
+
* `QueryRequest`s into driver calls (including cursor-mode iteration and
|
|
8
|
+
* `RETURNING`-on-`INSERT`/`UPDATE` emulation, since this layer's SQL
|
|
9
|
+
* generation doesn't rely on SQLite's own `RETURNING` support) and
|
|
10
|
+
* normalizes results/column metadata back into SQB's portable shape.
|
|
11
|
+
*/
|
|
3
12
|
export declare class SqliteConnection implements Adapter.Connection {
|
|
4
13
|
private _onClose;
|
|
5
14
|
private intlcon?;
|
|
6
15
|
constructor(db: NativeDatabase, _onClose: () => void | Promise<void>);
|
|
16
|
+
/** SQLite has no server-side session concept - always `0`. */
|
|
7
17
|
get sessionId(): any;
|
|
18
|
+
/** Releases this connection's reference to the underlying native database via the closer passed to the constructor (see {@link SqliteAdapter.connect}), closing the file once no connection references it. */
|
|
8
19
|
close(): Promise<void>;
|
|
20
|
+
/** Rolls back any open transaction, readying the connection to be pooled/reused. */
|
|
9
21
|
reset(): Promise<void>;
|
|
22
|
+
/** Begins a transaction (`BEGIN`), swallowing the driver's error for a transaction already being open rather than throwing. */
|
|
10
23
|
startTransaction(): Promise<void>;
|
|
24
|
+
/** Commits the current transaction, swallowing the driver's error for no transaction being open rather than throwing. */
|
|
11
25
|
commit(): Promise<void>;
|
|
26
|
+
/** Rolls back the current transaction, swallowing the driver's error for no transaction being open rather than throwing. */
|
|
12
27
|
rollback(): Promise<void>;
|
|
13
28
|
getInTransaction(): boolean;
|
|
29
|
+
/** Validates the connection with a trivial `SELECT 1`. */
|
|
14
30
|
test(): Promise<void>;
|
|
31
|
+
/**
|
|
32
|
+
* Executes one query. For an `INSERT`/`UPDATE` with `returningFields`,
|
|
33
|
+
* runs the statement then a synthesized follow-up `SELECT` to emulate
|
|
34
|
+
* `RETURNING` (an `INSERT` is matched back by `ROWID`, an `UPDATE` by
|
|
35
|
+
* reusing the original `WHERE` clause) - `DELETE` isn't emulated here
|
|
36
|
+
* since there's nothing left to re-select afterward. Otherwise prepares
|
|
37
|
+
* and iterates the statement, wrapping it in a {@link SqliteCursor}
|
|
38
|
+
* (returned directly in cursor mode, or eagerly drained up to
|
|
39
|
+
* `fetchRows` rows otherwise).
|
|
40
|
+
*/
|
|
15
41
|
execute(query: QueryRequest): Promise<Adapter.Response>;
|
|
42
|
+
/** Copies a synthesized `SELECT` statement's rows/fields onto `out` - used to emulate `RETURNING` on `INSERT`/`UPDATE`. */
|
|
16
43
|
private _fillSelectResult;
|
|
44
|
+
/** Converts the driver's column metadata into SQB's portable {@link Adapter.Field} shape via {@link mapDeclaredType}. */
|
|
17
45
|
private _convertFields;
|
|
18
46
|
}
|
package/sqlite-connection.js
CHANGED
|
@@ -1,4 +1,13 @@
|
|
|
1
1
|
import { SqliteCursor } from './sqlite-cursor.js';
|
|
2
|
+
/**
|
|
3
|
+
* `@sqb/connect` {@link Adapter.Connection} wrapping a {@link
|
|
4
|
+
* NativeDatabase} (Node's `node:sqlite` or Bun's `bun:sqlite`, behind a
|
|
5
|
+
* common driver interface - see `./drivers/types.ts`): translates
|
|
6
|
+
* `QueryRequest`s into driver calls (including cursor-mode iteration and
|
|
7
|
+
* `RETURNING`-on-`INSERT`/`UPDATE` emulation, since this layer's SQL
|
|
8
|
+
* generation doesn't rely on SQLite's own `RETURNING` support) and
|
|
9
|
+
* normalizes results/column metadata back into SQB's portable shape.
|
|
10
|
+
*/
|
|
2
11
|
export class SqliteConnection {
|
|
3
12
|
_onClose;
|
|
4
13
|
intlcon;
|
|
@@ -6,18 +15,22 @@ export class SqliteConnection {
|
|
|
6
15
|
this._onClose = _onClose;
|
|
7
16
|
this.intlcon = db;
|
|
8
17
|
}
|
|
18
|
+
/** SQLite has no server-side session concept - always `0`. */
|
|
9
19
|
get sessionId() {
|
|
10
20
|
return 0;
|
|
11
21
|
}
|
|
22
|
+
/** Releases this connection's reference to the underlying native database via the closer passed to the constructor (see {@link SqliteAdapter.connect}), closing the file once no connection references it. */
|
|
12
23
|
async close() {
|
|
13
24
|
if (this.intlcon) {
|
|
14
25
|
this.intlcon = undefined;
|
|
15
26
|
await this._onClose();
|
|
16
27
|
}
|
|
17
28
|
}
|
|
29
|
+
/** Rolls back any open transaction, readying the connection to be pooled/reused. */
|
|
18
30
|
async reset() {
|
|
19
31
|
return this.rollback();
|
|
20
32
|
}
|
|
33
|
+
/** Begins a transaction (`BEGIN`), swallowing the driver's error for a transaction already being open rather than throwing. */
|
|
21
34
|
async startTransaction() {
|
|
22
35
|
assertDefined(this.intlcon);
|
|
23
36
|
try {
|
|
@@ -29,6 +42,7 @@ export class SqliteConnection {
|
|
|
29
42
|
throw e;
|
|
30
43
|
}
|
|
31
44
|
}
|
|
45
|
+
/** Commits the current transaction, swallowing the driver's error for no transaction being open rather than throwing. */
|
|
32
46
|
async commit() {
|
|
33
47
|
assertDefined(this.intlcon);
|
|
34
48
|
try {
|
|
@@ -40,6 +54,7 @@ export class SqliteConnection {
|
|
|
40
54
|
throw e;
|
|
41
55
|
}
|
|
42
56
|
}
|
|
57
|
+
/** Rolls back the current transaction, swallowing the driver's error for no transaction being open rather than throwing. */
|
|
43
58
|
async rollback() {
|
|
44
59
|
assertDefined(this.intlcon);
|
|
45
60
|
try {
|
|
@@ -54,10 +69,21 @@ export class SqliteConnection {
|
|
|
54
69
|
getInTransaction() {
|
|
55
70
|
return !!this.intlcon?.inTransaction;
|
|
56
71
|
}
|
|
72
|
+
/** Validates the connection with a trivial `SELECT 1`. */
|
|
57
73
|
async test() {
|
|
58
74
|
assertDefined(this.intlcon);
|
|
59
75
|
this.intlcon.exec('select 1');
|
|
60
76
|
}
|
|
77
|
+
/**
|
|
78
|
+
* Executes one query. For an `INSERT`/`UPDATE` with `returningFields`,
|
|
79
|
+
* runs the statement then a synthesized follow-up `SELECT` to emulate
|
|
80
|
+
* `RETURNING` (an `INSERT` is matched back by `ROWID`, an `UPDATE` by
|
|
81
|
+
* reusing the original `WHERE` clause) - `DELETE` isn't emulated here
|
|
82
|
+
* since there's nothing left to re-select afterward. Otherwise prepares
|
|
83
|
+
* and iterates the statement, wrapping it in a {@link SqliteCursor}
|
|
84
|
+
* (returned directly in cursor mode, or eagerly drained up to
|
|
85
|
+
* `fetchRows` rows otherwise).
|
|
86
|
+
*/
|
|
61
87
|
async execute(query) {
|
|
62
88
|
assertDefined(this.intlcon);
|
|
63
89
|
const intlcon = this.intlcon;
|
|
@@ -109,6 +135,7 @@ export class SqliteConnection {
|
|
|
109
135
|
}
|
|
110
136
|
return out;
|
|
111
137
|
}
|
|
138
|
+
/** Copies a synthesized `SELECT` statement's rows/fields onto `out` - used to emulate `RETURNING` on `INSERT`/`UPDATE`. */
|
|
112
139
|
_fillSelectResult(out, stmt, rows, query) {
|
|
113
140
|
const columns = stmt.columns();
|
|
114
141
|
out.fields = this._convertFields(columns);
|
|
@@ -118,6 +145,7 @@ export class SqliteConnection {
|
|
|
118
145
|
? rows.map(r => columns.map(c => r[c.name]))
|
|
119
146
|
: rows;
|
|
120
147
|
}
|
|
148
|
+
/** Converts the driver's column metadata into SQB's portable {@link Adapter.Field} shape via {@link mapDeclaredType}. */
|
|
121
149
|
_convertFields(columns) {
|
|
122
150
|
const result = [];
|
|
123
151
|
for (const c of columns) {
|
|
@@ -132,10 +160,12 @@ export class SqliteConnection {
|
|
|
132
160
|
return result;
|
|
133
161
|
}
|
|
134
162
|
}
|
|
163
|
+
/** Throws if the connection has already been closed. */
|
|
135
164
|
function assertDefined(d) {
|
|
136
165
|
if (d == null)
|
|
137
166
|
throw new Error('DB session is closed');
|
|
138
167
|
}
|
|
168
|
+
/** Rewrites SQB's plain-named params (`{ name: value }`) into the `:name`-prefixed keys both `node:sqlite` and `bun:sqlite` require for named binding. */
|
|
139
169
|
function mapParams(params) {
|
|
140
170
|
const out = {};
|
|
141
171
|
for (const k of Object.keys(params))
|
package/sqlite-cursor.d.ts
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
import type { Adapter, RowType } from '@sqb/connect';
|
|
2
2
|
import type { NativeColumnInfo } from './drivers/types.js';
|
|
3
|
+
/**
|
|
4
|
+
* `@sqb/connect` {@link Adapter.Cursor} wrapping a `NativeStatement`'s
|
|
5
|
+
* synchronous `iterate()` result: `fetch(n)` pulls up to `n` rows at a
|
|
6
|
+
* time, converting each row to an array (column-ordered via the
|
|
7
|
+
* statement's column metadata) when `rowType` is `'array'`.
|
|
8
|
+
*/
|
|
3
9
|
export declare class SqliteCursor implements Adapter.Cursor {
|
|
4
10
|
private _iterator?;
|
|
5
11
|
private readonly _rowType;
|
|
@@ -10,6 +16,8 @@ export declare class SqliteCursor implements Adapter.Cursor {
|
|
|
10
16
|
});
|
|
11
17
|
get isClosed(): boolean;
|
|
12
18
|
get rowType(): RowType;
|
|
19
|
+
/** Stops consuming the iterator by dropping it - SQLite's synchronous iterator has no `close()`/`return()` of its own that needs calling. */
|
|
13
20
|
close(): Promise<void>;
|
|
21
|
+
/** Pulls up to `nRows` more rows from the iterator, or `undefined` once it's exhausted. */
|
|
14
22
|
fetch(nRows: number): Promise<any[] | undefined>;
|
|
15
23
|
}
|
package/sqlite-cursor.js
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@sqb/connect` {@link Adapter.Cursor} wrapping a `NativeStatement`'s
|
|
3
|
+
* synchronous `iterate()` result: `fetch(n)` pulls up to `n` rows at a
|
|
4
|
+
* time, converting each row to an array (column-ordered via the
|
|
5
|
+
* statement's column metadata) when `rowType` is `'array'`.
|
|
6
|
+
*/
|
|
1
7
|
export class SqliteCursor {
|
|
2
8
|
_iterator;
|
|
3
9
|
_rowType;
|
|
@@ -13,9 +19,11 @@ export class SqliteCursor {
|
|
|
13
19
|
get rowType() {
|
|
14
20
|
return this._rowType;
|
|
15
21
|
}
|
|
22
|
+
/** Stops consuming the iterator by dropping it - SQLite's synchronous iterator has no `close()`/`return()` of its own that needs calling. */
|
|
16
23
|
async close() {
|
|
17
24
|
this._iterator = undefined;
|
|
18
25
|
}
|
|
26
|
+
/** Pulls up to `nRows` more rows from the iterator, or `undefined` once it's exhausted. */
|
|
19
27
|
async fetch(nRows) {
|
|
20
28
|
if (!this._iterator)
|
|
21
29
|
return undefined;
|