@sqb/sqlite 6.0.7 → 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/LICENSE CHANGED
@@ -1,191 +1,30 @@
1
-
2
- Apache License
3
- Version 2.0, January 2004
4
- https://www.apache.org/licenses/
5
-
6
- TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
-
8
- 1. Definitions.
9
-
10
- "License" shall mean the terms and conditions for use, reproduction,
11
- and distribution as defined by Sections 1 through 9 of this document.
12
-
13
- "Licensor" shall mean the copyright owner or entity authorized by
14
- the copyright owner that is granting the License.
15
-
16
- "Legal Entity" shall mean the union of the acting entity and all
17
- other entities that control, are controlled by, or are under common
18
- control with that entity. For the purposes of this definition,
19
- "control" means (i) the power, direct or indirect, to cause the
20
- direction or management of such entity, whether by contract or
21
- otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
- outstanding shares, or (iii) beneficial ownership of such entity.
23
-
24
- "You" (or "Your") shall mean an individual or Legal Entity
25
- exercising permissions granted by this License.
26
-
27
- "Source" form shall mean the preferred form for making modifications,
28
- including but not limited to software source code, documentation
29
- source, and configuration files.
30
-
31
- "Object" form shall mean any form resulting from mechanical
32
- transformation or translation of a Source form, including but
33
- not limited to compiled object code, generated documentation,
34
- and conversions to other media types.
35
-
36
- "Work" shall mean the work of authorship, whether in Source or
37
- Object form, made available under the License, as indicated by a
38
- copyright notice that is included in or attached to the work
39
- (an example is provided in the Appendix below).
40
-
41
- "Derivative Works" shall mean any work, whether in Source or Object
42
- form, that is based on (or derived from) the Work and for which the
43
- editorial revisions, annotations, elaborations, or other modifications
44
- represent, as a whole, an original work of authorship. For the purposes
45
- of this License, Derivative Works shall not include works that remain
46
- separable from, or merely link (or bind by name) to the interfaces of,
47
- the Work and Derivative Works thereof.
48
-
49
- "Contribution" shall mean any work of authorship, including
50
- the original version of the Work and any modifications or additions
51
- to that Work or Derivative Works thereof, that is intentionally
52
- submitted to Licensor for inclusion in the Work by the copyright owner
53
- or by an individual or Legal Entity authorized to submit on behalf of
54
- the copyright owner. For the purposes of this definition, "submitted"
55
- means any form of electronic, verbal, or written communication sent
56
- to the Licensor or its representatives, including but not limited to
57
- communication on electronic mailing lists, source code control systems,
58
- and issue tracking systems that are managed by, or on behalf of, the
59
- Licensor for the purpose of discussing and improving the Work, but
60
- excluding communication that is conspicuously marked or otherwise
61
- designated in writing by the copyright owner as "Not a Contribution."
62
-
63
- "Contributor" shall mean Licensor and any individual or Legal Entity
64
- on behalf of whom a Contribution has been received by Licensor and
65
- subsequently incorporated within the Work.
66
-
67
- 2. Grant of Copyright License. Subject to the terms and conditions of
68
- this License, each Contributor hereby grants to You a perpetual,
69
- worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
- copyright license to reproduce, prepare Derivative Works of,
71
- publicly display, publicly perform, sublicense, and distribute the
72
- Work and such Derivative Works in Source or Object form.
73
-
74
- 3. Grant of Patent License. Subject to the terms and conditions of
75
- this License, each Contributor hereby grants to You a perpetual,
76
- worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
- (except as stated in this section) patent license to make, have made,
78
- use, offer to sell, sell, import, and otherwise transfer the Work,
79
- where such license applies only to those patent claims licensable
80
- by such Contributor that are necessarily infringed by their
81
- Contribution(s) alone or by combination of their Contribution(s)
82
- with the Work to which such Contribution(s) was submitted. If You
83
- institute patent litigation against any entity (including a
84
- cross-claim or counterclaim in a lawsuit) alleging that the Work
85
- or a Contribution incorporated within the Work constitutes direct
86
- or contributory patent infringement, then any patent licenses
87
- granted to You under this License for that Work shall terminate
88
- as of the date such litigation is filed.
89
-
90
- 4. Redistribution. You may reproduce and distribute copies of the
91
- Work or Derivative Works thereof in any medium, with or without
92
- modifications, and in Source or Object form, provided that You
93
- meet the following conditions:
94
-
95
- (a) You must give any other recipients of the Work or
96
- Derivative Works a copy of this License; and
97
-
98
- (b) You must cause any modified files to carry prominent notices
99
- stating that You changed the files; and
100
-
101
- (c) You must retain, in the Source form of any Derivative Works
102
- that You distribute, all copyright, patent, trademark, and
103
- attribution notices from the Source form of the Work,
104
- excluding those notices that do not pertain to any part of
105
- the Derivative Works; and
106
-
107
- (d) If the Work includes a "NOTICE" text file as part of its
108
- distribution, then any Derivative Works that You distribute must
109
- include a readable copy of the attribution notices contained
110
- within such NOTICE file, excluding those notices that do not
111
- pertain to any part of the Derivative Works, in at least one
112
- of the following places: within a NOTICE text file distributed
113
- as part of the Derivative Works; within the Source form or
114
- documentation, if provided along with the Derivative Works; or,
115
- within a display generated by the Derivative Works, if and
116
- wherever such third-party notices normally appear. The contents
117
- of the NOTICE file are for informational purposes only and
118
- do not modify the License. You may add Your own attribution
119
- notices within Derivative Works that You distribute, alongside
120
- or as an addendum to the NOTICE text from the Work, provided
121
- that such additional attribution notices cannot be construed
122
- as modifying the License.
123
-
124
- You may add Your own copyright statement to Your modifications and
125
- may provide additional or different license terms and conditions
126
- for use, reproduction, or distribution of Your modifications, or
127
- for any such Derivative Works as a whole, provided Your use,
128
- reproduction, and distribution of the Work otherwise complies with
129
- the conditions stated in this License.
130
-
131
- 5. Submission of Contributions. Unless You explicitly state otherwise,
132
- any Contribution intentionally submitted for inclusion in the Work
133
- by You to the Licensor shall be under the terms and conditions of
134
- this License, without any additional terms or conditions.
135
- Notwithstanding the above, nothing herein shall supersede or modify
136
- the terms of any separate license agreement you may have executed
137
- with Licensor regarding such Contributions.
138
-
139
- 6. Trademarks. This License does not grant permission to use the trade
140
- names, trademarks, service marks, or product names of the Licensor,
141
- except as required for reasonable and customary use in describing the
142
- origin of the Work and reproducing the content of the NOTICE file.
143
-
144
- 7. Disclaimer of Warranty. Unless required by applicable law or
145
- agreed to in writing, Licensor provides the Work (and each
146
- Contributor provides its Contributions) on an "AS IS" BASIS,
147
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
- implied, including, without limitation, any warranties or conditions
149
- of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
- PARTICULAR PURPOSE. You are solely responsible for determining the
151
- appropriateness of using or redistributing the Work and assume any
152
- risks associated with Your exercise of permissions under this License.
153
-
154
- 8. Limitation of Liability. In no event and under no legal theory,
155
- whether in tort (including negligence), contract, or otherwise,
156
- unless required by applicable law (such as deliberate and grossly
157
- negligent acts) or agreed to in writing, shall any Contributor be
158
- liable to You for damages, including any direct, indirect, special,
159
- incidental, or consequential damages of any character arising as a
160
- result of this License or out of the use or inability to use the
161
- Work (including but not limited to damages for loss of goodwill,
162
- work stoppage, computer failure or malfunction, or any and all
163
- other commercial damages or losses), even if such Contributor
164
- has been advised of the possibility of such damages.
165
-
166
- 9. Accepting Warranty or Additional Liability. While redistributing
167
- the Work or Derivative Works thereof, You may choose to offer,
168
- and charge a fee for, acceptance of support, warranty, indemnity,
169
- or other liability obligations and/or rights consistent with this
170
- License. However, in accepting such obligations, You may act only
171
- on Your own behalf and on Your sole responsibility, not on behalf
172
- of any other Contributor, and only if You agree to indemnify,
173
- defend, and hold each Contributor harmless for any liability
174
- incurred by, or claims asserted against, such Contributor by reason
175
- of your accepting any such warranty or additional liability.
176
-
177
- END OF TERMS AND CONDITIONS
178
-
179
- Copyright 2013-2018 Docker, Inc.
180
-
181
- Licensed under the Apache License, Version 2.0 (the "License");
182
- you may not use this file except in compliance with the License.
183
- You may obtain a copy of the License at
184
-
185
- https://www.apache.org/licenses/LICENSE-2.0
186
-
187
- Unless required by applicable law or agreed to in writing, software
188
- distributed under the License is distributed on an "AS IS" BASIS,
189
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
190
- See the License for the specific language governing permissions and
191
- limitations under the License.
1
+ BSD 3-Clause License
2
+
3
+ Copyright (c) 2020 Panates
4
+ All rights reserved.
5
+
6
+ Redistribution and use in source and binary forms, with or without
7
+ modification, are permitted provided that the following conditions are met:
8
+
9
+ 1. Redistributions of source code must retain the above copyright notice,
10
+ this list of conditions and the following disclaimer.
11
+
12
+ 2. Redistributions in binary form must reproduce the above copyright
13
+ notice, this list of conditions and the following disclaimer in the
14
+ documentation and/or other materials provided with the distribution.
15
+
16
+ 3. Neither the name of the copyright holder nor the names of its
17
+ contributors may be used to endorse or promote products derived from
18
+ this software without specific prior written permission.
19
+
20
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23
+ ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
24
+ LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25
+ CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26
+ SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27
+ INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28
+ CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29
+ ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30
+ POSSIBILITY OF SUCH DAMAGE.
package/README.md CHANGED
@@ -1,5 +1,6 @@
1
- <p style="text-align:center">
2
- <img src="https://user-images.githubusercontent.com/3836517/32965280-1a2b63ce-cbe7-11e7-8ee1-ba47313503c5.png" width="500px" alt="SQB Logo"/>
1
+
2
+ <p style="text-align: center;">
3
+ <img src="https://sqb.panates.com/img/github-hero.webp" width="1280" alt="SQB" />
3
4
  </p>
4
5
 
5
6
  <br>
@@ -59,5 +60,5 @@ SQB is available under [MIT](LICENSE) license.
59
60
  [downloads-url]: https://npmjs.org/package/@sqb/sqlite
60
61
  [ci-test-image]: https://github.com/panates/sqb/actions/workflows/test.yml/badge.svg
61
62
  [ci-test-url]: https://github.com/panates/sqb/actions/workflows/test.yml
62
- [coveralls-image]: https://coveralls.io/repos/github/sqbjs/sqb/badge.svg?branch=master
63
+ [coveralls-image]: https://coveralls.io/repos/github/panates/sqb/badge.svg?branch=dev
63
64
  [coveralls-url]: https://coveralls.io/github/sqbjs/sqb?branch=master
package/constants.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ /** This package's own version, kept in step with its `package.json` by `rman version`
2
+ * (see `.rmanrc` `version.stamp`) - never edit it by hand. */
3
+ export declare const version = "6.0.10";
package/constants.js ADDED
@@ -0,0 +1,3 @@
1
+ /** This package's own version, kept in step with its `package.json` by `rman version`
2
+ * (see `.rmanrc` `version.stamp`) - never edit it by hand. */
3
+ export const version = '6.0.10';
@@ -1,2 +1,3 @@
1
1
  import type { SqliteDriver } from './types.js';
2
+ /** {@link SqliteDriver} backed by Bun's built-in `bun:sqlite` module. */
2
3
  export declare const bunDriver: SqliteDriver;
@@ -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));
@@ -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');
@@ -1,2 +1,3 @@
1
1
  import type { SqliteDriver } from './types.js';
2
+ /** {@link SqliteDriver} backed by Node's built-in `node:sqlite` module. */
2
3
  export declare const nodeDriver: SqliteDriver;
@@ -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));
@@ -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,16 +1,17 @@
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.7",
4
+ "version": "6.0.10",
5
5
  "author": "Panates",
6
- "license": "Apache-2.0",
6
+ "license": "BSD-3-Clause",
7
+ "homepage": "https://www.sqbjs.org",
7
8
  "dependencies": {
8
9
  "tslib": "^2.8.1"
9
10
  },
10
11
  "peerDependencies": {
11
- "@sqb/builder": "^6.0.7",
12
- "@sqb/connect": "^6.0.7",
13
- "@sqb/sqlite-dialect": "^6.0.7"
12
+ "@sqb/builder": "^6.0.10",
13
+ "@sqb/connect": "^6.0.10",
14
+ "@sqb/sqlite-dialect": "^6.0.10"
14
15
  },
15
16
  "type": "module",
16
17
  "module": "./index.js",
@@ -27,7 +28,9 @@
27
28
  },
28
29
  "contributors": [
29
30
  "Eray Hanoglu <e.hanoglu@panates.com>",
30
- "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>"
31
34
  ],
32
35
  "repository": {
33
36
  "type": "git",
@@ -49,4 +52,4 @@
49
52
  "publishConfig": {
50
53
  "access": "public"
51
54
  }
52
- }
55
+ }
@@ -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);
@@ -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
  }
@@ -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))
@@ -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;