@cutils/sqlite 0.0.0-stage → 0.2.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 +202 -0
- package/README.md +148 -2
- package/build/libsqlite3-shim.a +0 -0
- package/dist/esm/errors.d.ts +5 -0
- package/dist/esm/errors.js +106 -0
- package/dist/esm/index.d.ts +138 -0
- package/dist/esm/index.js +761 -0
- package/dist/esm/internal/buffer.d.ts +8 -0
- package/dist/esm/internal/buffer.js +27 -0
- package/dist/esm/internal/ffi.d.ts +34 -0
- package/dist/esm/internal/ffi.js +110 -0
- package/dist/esm/internal/savepoint.d.ts +1 -0
- package/dist/esm/internal/savepoint.js +7 -0
- package/dist/esm/internal/utf8.d.ts +3 -0
- package/dist/esm/internal/utf8.js +10 -0
- package/dist/esm/node-binding.mjs +120 -0
- package/dist/esm/types.d.ts +66 -0
- package/dist/esm/types.js +29 -0
- package/dist/esm/validate.d.ts +7 -0
- package/dist/esm/validate.js +44 -0
- package/package.json +71 -3
- package/src/errors.ts +108 -0
- package/src/index.ts +926 -0
- package/src/internal/buffer.ts +31 -0
- package/src/internal/ffi.ts +151 -0
- package/src/internal/savepoint.ts +9 -0
- package/src/internal/utf8.ts +13 -0
- package/src/types.ts +83 -0
- package/src/validate.ts +60 -0
- package/vendor/README.md +48 -0
- package/vendor/manifest.json +166 -0
- package/vendor/shim/sqlite_shim.c +328 -0
- package/vendor/sqlite-amalgamation/SQLITE_VERSION +1 -0
- package/vendor/sqlite-amalgamation/sqlite3.c +262899 -0
- package/vendor/sqlite-amalgamation/sqlite3.h +13775 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://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
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright 2026 Joaquin Terrasa & contributors
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,3 +1,149 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @cutils/sqlite
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Embedded SQLite for scriptc: the amalgamation vendored in-package, a ~26-entry
|
|
4
|
+
C shim over the scriptc span FFI ABI, and a subset-clean TypeScript wrapper
|
|
5
|
+
(`Database` / `Stmt<TRow>` / `Transaction` / `SqliteError`). The same source
|
|
6
|
+
runs under plain Node (via the shipped shared-library build + koffi) and
|
|
7
|
+
compiles through `scriptc build` into a single static ELF with the database
|
|
8
|
+
linked in — no external dependency at either end.
|
|
9
|
+
|
|
10
|
+
```ts
|
|
11
|
+
import { Database } from "@cutils/sqlite";
|
|
12
|
+
import * as v from "@chttp/val";
|
|
13
|
+
|
|
14
|
+
const Todo = v.strictObject({
|
|
15
|
+
id: v.number(),
|
|
16
|
+
title: v.string(),
|
|
17
|
+
done: v.number(),
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
const db = Database.open("app.db");
|
|
21
|
+
db.exec(
|
|
22
|
+
"CREATE TABLE IF NOT EXISTS todos (id INTEGER PRIMARY KEY, title TEXT NOT NULL, done INTEGER DEFAULT 0)",
|
|
23
|
+
);
|
|
24
|
+
|
|
25
|
+
// Validating statements: params and rows are checked by the Standard-Schema
|
|
26
|
+
// validator — any compliant library works (val, valibot, ...).
|
|
27
|
+
const ins = db.prepare(
|
|
28
|
+
"INSERT INTO todos (title, done) VALUES (:title, :done)",
|
|
29
|
+
Todo,
|
|
30
|
+
);
|
|
31
|
+
ins.run({ title: "ship it", done: 0 });
|
|
32
|
+
|
|
33
|
+
const one = db.prepare("SELECT id, title, done FROM todos WHERE id = ?", Todo);
|
|
34
|
+
const row = one.getOrNull([1]); // TRow | null
|
|
35
|
+
|
|
36
|
+
using tx = db.transaction(); // auto-commit on dispose, auto-rollback on throw
|
|
37
|
+
db.exec("INSERT INTO todos (title) VALUES ('in tx')");
|
|
38
|
+
|
|
39
|
+
// Opt out of validation anywhere with the *Raw family.
|
|
40
|
+
const raw = db.prepareRaw("SELECT COUNT(*) AS n FROM todos");
|
|
41
|
+
const { n } = raw.getOrNull([]);
|
|
42
|
+
|
|
43
|
+
db.close();
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
## Opening
|
|
47
|
+
|
|
48
|
+
```ts
|
|
49
|
+
Database.open(path, opts?) // file DB; creates by default
|
|
50
|
+
Database.openInMemory() // ":memory:" — fast + transient
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
`opts.readonly` opens read-only (implies `fileMustExist`); `opts.fileMustExist`
|
|
54
|
+
fails when the file is absent. Getters: `db.path`, `db.memory`, `db.readonly`,
|
|
55
|
+
`db.inTransaction`, `db.lastInsertRowid`, `db.changes`.
|
|
56
|
+
|
|
57
|
+
## Prepared statements
|
|
58
|
+
|
|
59
|
+
`db.prepare(sql, schema)` validates bind parameters on write and row records on
|
|
60
|
+
read through `schema["~standard"].validate` (Standard Schema v1).
|
|
61
|
+
`db.prepareRaw(sql)` skips validation entirely. On `Stmt<TRow>`:
|
|
62
|
+
|
|
63
|
+
- `run(params)` / `runRaw` → `{ lastInsertRowid, changes }` for write SQL.
|
|
64
|
+
- `get(params)` → `TRow | undefined` (see subset quirk below), `getOrNull` →
|
|
65
|
+
`TRow | null` — the subset-strict twin; prefer it in scriptc apps.
|
|
66
|
+
- `all(params)` / `allRaw` → `TRow[]`; `getRaw`/`allRaw`/`runRaw` skip
|
|
67
|
+
validation on demand.
|
|
68
|
+
- Params bind by position (`[v1, v2]`) or by name (`{ title: "x" }`, also
|
|
69
|
+
`":title"`, `"@title"`, `"$title"` — sigils interchangeable).
|
|
70
|
+
- `bind(params)` makes bindings permanent until the next bind/clear;
|
|
71
|
+
`columns()` reports `sqlite3_column_*` metadata; `toString()` returns the
|
|
72
|
+
expanded SQL; `finalize()` (or `using`/`Symbol.dispose`) releases it.
|
|
73
|
+
|
|
74
|
+
## Transactions
|
|
75
|
+
|
|
76
|
+
`db.transaction()` returns a `Transaction`; dispose commits unless the tx (or
|
|
77
|
+
any nested op) threw or was marked failed — then it rolls back. Nested
|
|
78
|
+
`db.transaction()` inside an open tx becomes an automatic savepoint;
|
|
79
|
+
`tx.savepoint(name)` gives an explicit one with `commit()`/`rollback()`.
|
|
80
|
+
|
|
81
|
+
## Errors
|
|
82
|
+
|
|
83
|
+
All SQLite failures throw `SqliteError` with `code` = the SQLite result name
|
|
84
|
+
(`"SQLITE_CONSTRAINT_NOTNULL"`, `"SQLITE_BUSY"`, ...) and `message` =
|
|
85
|
+
`sqlite3_errmsg` at throw time. Schema failures use code `"VALIDATION"`;
|
|
86
|
+
bind misuse `"BIND_PARAM"`; statement-busy `"STMT_BUSY"`.
|
|
87
|
+
|
|
88
|
+
## Binary size
|
|
89
|
+
|
|
90
|
+
Indicative `scriptc build` sizes for a scriptc app (your app's size varies —
|
|
91
|
+
no perf claims until the bench cell lands):
|
|
92
|
+
|
|
93
|
+
| Build | Approx. added size |
|
|
94
|
+
| ----------------------------------------------- | ------------------ |
|
|
95
|
+
| `hello world` (scriptc only) | ~600 KB |
|
|
96
|
+
| + `@cutils/sqlite` (`-O2`, amalgamation + shim) | ~1.6 MB |
|
|
97
|
+
|
|
98
|
+
## Better-sqlite3 users: what changed
|
|
99
|
+
|
|
100
|
+
- Same shape: `prepare`/`run`/`get`/`all`, `transaction()`, `pragma`,
|
|
101
|
+
positional + named params, `lastInsertRowid`/`changes`.
|
|
102
|
+
- `get()` returns `TRow | undefined` like better-sqlite3 — the one documented
|
|
103
|
+
divergence from the cutils `| null` rule. `getOrNull()` is the `| null` twin.
|
|
104
|
+
- Validation is first-class: `prepare(sql, schema)` runs any Standard-Schema
|
|
105
|
+
validator on params AND rows; `prepareRaw` is the fast path.
|
|
106
|
+
- Results are plain records; INTEGER columns return `number` when safe,
|
|
107
|
+
`bigint` otherwise; BLOB returns `Uint8Array`; booleans bind as 0/1.
|
|
108
|
+
- No threads, no async, no `serialize`/`parallelize`, no extensions
|
|
109
|
+
(`SQLITE_OMIT_LOAD_EXTENSION`), no user functions/aggregates — synchronous
|
|
110
|
+
embedded SQL only.
|
|
111
|
+
|
|
112
|
+
## Node usage
|
|
113
|
+
|
|
114
|
+
The npm package ships `dist/esm`. Under Node the emitted `shim_*` calls resolve
|
|
115
|
+
on `globalThis` — an official [koffi](https://koffi.dev) binding loads
|
|
116
|
+
`build/libcutils_sqlite.so` for you:
|
|
117
|
+
|
|
118
|
+
```sh
|
|
119
|
+
# one-time, inside the installed package (needs a C toolchain):
|
|
120
|
+
npm run build:shared --prefix node_modules/@cutils/sqlite
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
```ts
|
|
124
|
+
import "koffi"; // ensure the optional peer is installed
|
|
125
|
+
import "@cutils/sqlite/node-binding"; // installs the shim_* globals
|
|
126
|
+
import { Database } from "@cutils/sqlite";
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
`koffi` is an **optional peer dependency** — install it when you run under
|
|
130
|
+
Node; scriptc builds never see it. The `.so` is linked `-Wl,-Bsymbolic` so the
|
|
131
|
+
vendored amalgamation's `sqlite3_*` symbols can't interpose against a host's
|
|
132
|
+
sqlite — this is load-bearing, Node ≥ 22 exports its own sqlite3 symbols.
|
|
133
|
+
|
|
134
|
+
Node ≥ 22 is the runtime floor; the test suite also runs there (no
|
|
135
|
+
`using`-syntax — dispose paths are exercised via `[Symbol.dispose]()`).
|
|
136
|
+
|
|
137
|
+
## Subset quirks
|
|
138
|
+
|
|
139
|
+
The package is authored in the [scriptc compilable subset](../../AGENTS.md):
|
|
140
|
+
|
|
141
|
+
- `get()` returning `| undefined` is the **only** `undefined` union in cutils —
|
|
142
|
+
`getOrNull()` exists for subset-strict consumers. See
|
|
143
|
+
`docs/subset-quirks.md`.
|
|
144
|
+
- `schema` params are Standard-Schema **classes**, not interfaces —
|
|
145
|
+
interface-typed fields erase under the subset; the seam re-declares
|
|
146
|
+
`~standard` as a `StdProps` class.
|
|
147
|
+
|
|
148
|
+
See `docs/integration.md`, `docs/schema.md`, `docs/subset-quirks.md`, and
|
|
149
|
+
`examples/todo-app/` for a worked chttp + sqlite app.
|
|
Binary file
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
// SqliteError + the numeric-result-code → name mapping.
|
|
2
|
+
export class SqliteError extends Error {
|
|
3
|
+
code;
|
|
4
|
+
constructor(code, message) {
|
|
5
|
+
super(message);
|
|
6
|
+
this.name = "SqliteError";
|
|
7
|
+
this.code = code;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
// Primary result codes (rc & 0xFF) plus the constraint extended codes —
|
|
11
|
+
// the names better-sqlite3 users will recognize from `err.code`.
|
|
12
|
+
export function sqliteCodeName(rc) {
|
|
13
|
+
switch (rc) {
|
|
14
|
+
case 275:
|
|
15
|
+
return "SQLITE_CONSTRAINT_CHECK";
|
|
16
|
+
case 531:
|
|
17
|
+
return "SQLITE_CONSTRAINT_COMMITHOOK";
|
|
18
|
+
case 787:
|
|
19
|
+
return "SQLITE_CONSTRAINT_FOREIGNKEY";
|
|
20
|
+
case 803:
|
|
21
|
+
return "SQLITE_CONSTRAINT_FUNCTION";
|
|
22
|
+
case 1027:
|
|
23
|
+
return "SQLITE_CONSTRAINT_TRIGGER";
|
|
24
|
+
case 1043:
|
|
25
|
+
return "SQLITE_CONSTRAINT_VTAB";
|
|
26
|
+
case 1299:
|
|
27
|
+
return "SQLITE_CONSTRAINT_NOTNULL";
|
|
28
|
+
case 1555:
|
|
29
|
+
return "SQLITE_CONSTRAINT_PRIMARYKEY";
|
|
30
|
+
case 2067:
|
|
31
|
+
return "SQLITE_CONSTRAINT_UNIQUE";
|
|
32
|
+
case 2579:
|
|
33
|
+
return "SQLITE_CONSTRAINT_ROWID";
|
|
34
|
+
case 2835:
|
|
35
|
+
return "SQLITE_CONSTRAINT_PINNED";
|
|
36
|
+
case 3091:
|
|
37
|
+
return "SQLITE_CONSTRAINT_DATATYPE";
|
|
38
|
+
default:
|
|
39
|
+
break;
|
|
40
|
+
}
|
|
41
|
+
const primary = rc & 255;
|
|
42
|
+
switch (primary) {
|
|
43
|
+
case 1:
|
|
44
|
+
return "SQLITE_ERROR";
|
|
45
|
+
case 2:
|
|
46
|
+
return "SQLITE_INTERNAL";
|
|
47
|
+
case 3:
|
|
48
|
+
return "SQLITE_PERM";
|
|
49
|
+
case 4:
|
|
50
|
+
return "SQLITE_ABORT";
|
|
51
|
+
case 5:
|
|
52
|
+
return "SQLITE_BUSY";
|
|
53
|
+
case 6:
|
|
54
|
+
return "SQLITE_LOCKED";
|
|
55
|
+
case 7:
|
|
56
|
+
return "SQLITE_NOMEM";
|
|
57
|
+
case 8:
|
|
58
|
+
return "SQLITE_READONLY";
|
|
59
|
+
case 9:
|
|
60
|
+
return "SQLITE_INTERRUPT";
|
|
61
|
+
case 10:
|
|
62
|
+
return "SQLITE_IOERR";
|
|
63
|
+
case 11:
|
|
64
|
+
return "SQLITE_CORRUPT";
|
|
65
|
+
case 12:
|
|
66
|
+
return "SQLITE_NOTFOUND";
|
|
67
|
+
case 13:
|
|
68
|
+
return "SQLITE_FULL";
|
|
69
|
+
case 14:
|
|
70
|
+
return "SQLITE_CANTOPEN";
|
|
71
|
+
case 15:
|
|
72
|
+
return "SQLITE_PROTOCOL";
|
|
73
|
+
case 16:
|
|
74
|
+
return "SQLITE_EMPTY";
|
|
75
|
+
case 17:
|
|
76
|
+
return "SQLITE_SCHEMA";
|
|
77
|
+
case 18:
|
|
78
|
+
return "SQLITE_TOOBIG";
|
|
79
|
+
case 19:
|
|
80
|
+
return "SQLITE_CONSTRAINT";
|
|
81
|
+
case 20:
|
|
82
|
+
return "SQLITE_MISMATCH";
|
|
83
|
+
case 21:
|
|
84
|
+
return "SQLITE_MISUSE";
|
|
85
|
+
case 22:
|
|
86
|
+
return "SQLITE_NOLFS";
|
|
87
|
+
case 23:
|
|
88
|
+
return "SQLITE_AUTH";
|
|
89
|
+
case 24:
|
|
90
|
+
return "SQLITE_FORMAT";
|
|
91
|
+
case 25:
|
|
92
|
+
return "SQLITE_RANGE";
|
|
93
|
+
case 26:
|
|
94
|
+
return "SQLITE_NOTADB";
|
|
95
|
+
case 27:
|
|
96
|
+
return "SQLITE_NOTICE";
|
|
97
|
+
case 28:
|
|
98
|
+
return "SQLITE_WARNING";
|
|
99
|
+
case 100:
|
|
100
|
+
return "SQLITE_ROW";
|
|
101
|
+
case 101:
|
|
102
|
+
return "SQLITE_DONE";
|
|
103
|
+
default:
|
|
104
|
+
return "SQLITE_ERROR";
|
|
105
|
+
}
|
|
106
|
+
}
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import type { BindRecord, BindValue, ColumnInfo, InferOutput, OpenOpts, Row, RunResult, SqlValue } from "./types.ts";
|
|
2
|
+
import { StandardSchemaV1 } from "./types.ts";
|
|
3
|
+
import { SqliteError } from "./errors.ts";
|
|
4
|
+
import { Scratch } from "./internal/buffer.ts";
|
|
5
|
+
export type { BindRecord, BindValue, ColumnInfo, InferOutput, OpenOpts, Row, RunResult, SqlValue };
|
|
6
|
+
export { StandardSchemaV1, StdProps } from "./types.ts";
|
|
7
|
+
export { SqliteError };
|
|
8
|
+
declare class StmtCore implements Disposable {
|
|
9
|
+
_db: Database;
|
|
10
|
+
_stmt: bigint;
|
|
11
|
+
_source: string;
|
|
12
|
+
_schema: StandardSchemaV1 | null;
|
|
13
|
+
_reader: boolean;
|
|
14
|
+
_readonly: boolean;
|
|
15
|
+
_finalized: boolean;
|
|
16
|
+
constructor(db: Database, stmt: bigint, source: string, schema: StandardSchemaV1 | null);
|
|
17
|
+
get source(): string;
|
|
18
|
+
get reader(): boolean;
|
|
19
|
+
get readonly(): boolean;
|
|
20
|
+
get database(): Database;
|
|
21
|
+
get schema(): StandardSchemaV1 | null;
|
|
22
|
+
_checkLive(): void;
|
|
23
|
+
_bindValue(idx: number, v: BindValue): void;
|
|
24
|
+
_namedIndex(key: string): number;
|
|
25
|
+
_bindParams(params: unknown): void;
|
|
26
|
+
_columnName(idx: number): string;
|
|
27
|
+
_columnMeta(idx: number, which: number): string | null;
|
|
28
|
+
_columnValue(idx: number): SqlValue;
|
|
29
|
+
_readRow(): Row;
|
|
30
|
+
_validateOut(row: Row): unknown;
|
|
31
|
+
_stepOnce(): unknown;
|
|
32
|
+
_allRows(): unknown[];
|
|
33
|
+
_drain(): void;
|
|
34
|
+
_runResult(): RunResult;
|
|
35
|
+
run(params: BindValue[]): RunResult;
|
|
36
|
+
run(params: BindRecord): RunResult;
|
|
37
|
+
runRaw(params: BindValue[]): RunResult;
|
|
38
|
+
runRaw(params: BindRecord): RunResult;
|
|
39
|
+
get<TRow>(params: BindValue[]): TRow;
|
|
40
|
+
get<TRow>(params: BindRecord): TRow;
|
|
41
|
+
getOrNull<TRow>(params: BindValue[]): TRow | null;
|
|
42
|
+
getOrNull<TRow>(params: BindRecord): TRow | null;
|
|
43
|
+
getRaw(params: BindValue[]): unknown;
|
|
44
|
+
getRaw(params: BindRecord): unknown;
|
|
45
|
+
all<TRow>(params: BindValue[]): TRow[];
|
|
46
|
+
all<TRow>(params: BindRecord): TRow[];
|
|
47
|
+
allRaw(params: BindValue[]): unknown[];
|
|
48
|
+
allRaw(params: BindRecord): unknown[];
|
|
49
|
+
bind(params: BindValue[]): StmtCore;
|
|
50
|
+
bind(params: BindRecord): StmtCore;
|
|
51
|
+
columns(): ColumnInfo[];
|
|
52
|
+
toString(): string;
|
|
53
|
+
finalize(): void;
|
|
54
|
+
[Symbol.dispose](): void;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* `Stmt<TRow>` — a prepared statement. `TRow` is the schema's
|
|
58
|
+
* `InferOutput` when prepared via `prepare(sql, schema)`, else `unknown`.
|
|
59
|
+
* The runtime class is `StmtCore`; this interface supplies the `TRow`-typed
|
|
60
|
+
* method surface (generic classes do not survive scriptc 0.3.x).
|
|
61
|
+
*/
|
|
62
|
+
export interface Stmt<TRow = unknown> extends StmtCore {
|
|
63
|
+
readonly source: string;
|
|
64
|
+
readonly reader: boolean;
|
|
65
|
+
readonly readonly: boolean;
|
|
66
|
+
readonly database: Database;
|
|
67
|
+
readonly schema: StandardSchemaV1 | null;
|
|
68
|
+
run(params: BindValue[]): RunResult;
|
|
69
|
+
run(params: BindRecord): RunResult;
|
|
70
|
+
runRaw(params: BindValue[]): RunResult;
|
|
71
|
+
runRaw(params: BindRecord): RunResult;
|
|
72
|
+
get(params: BindValue[]): TRow | undefined;
|
|
73
|
+
get(params: BindRecord): TRow | undefined;
|
|
74
|
+
getOrNull(params: BindValue[]): TRow | null;
|
|
75
|
+
getOrNull(params: BindRecord): TRow | null;
|
|
76
|
+
getRaw(params: BindValue[]): unknown;
|
|
77
|
+
getRaw(params: BindRecord): unknown;
|
|
78
|
+
all(params: BindValue[]): TRow[];
|
|
79
|
+
all(params: BindRecord): TRow[];
|
|
80
|
+
allRaw(params: BindValue[]): unknown[];
|
|
81
|
+
allRaw(params: BindRecord): unknown[];
|
|
82
|
+
bind(params: BindValue[]): this;
|
|
83
|
+
bind(params: BindRecord): this;
|
|
84
|
+
}
|
|
85
|
+
export declare const Stmt: typeof StmtCore;
|
|
86
|
+
export declare class Database implements Disposable {
|
|
87
|
+
_db: bigint;
|
|
88
|
+
_path: string;
|
|
89
|
+
_memory: boolean;
|
|
90
|
+
_readonly: boolean;
|
|
91
|
+
_closed: boolean;
|
|
92
|
+
_scratch: Scratch;
|
|
93
|
+
_stmts: StmtCore[];
|
|
94
|
+
_txs: Transaction[];
|
|
95
|
+
constructor(handle: bigint, path: string, memory: boolean, readonly_: boolean);
|
|
96
|
+
static open(path: string, opts?: OpenOpts): Database;
|
|
97
|
+
static openInMemory(): Database;
|
|
98
|
+
get path(): string;
|
|
99
|
+
get memory(): boolean;
|
|
100
|
+
get readonly(): boolean;
|
|
101
|
+
get inTransaction(): boolean;
|
|
102
|
+
_checkOpen(): void;
|
|
103
|
+
_errmsg(): string;
|
|
104
|
+
_throwDb(rc: number, fallback: string): never;
|
|
105
|
+
_untrack(stmt: StmtCore): void;
|
|
106
|
+
_prepareCore(sql: string, schema: StandardSchemaV1 | null): StmtCore;
|
|
107
|
+
exec(sql: string): void;
|
|
108
|
+
execRaw(sql: string): void;
|
|
109
|
+
pragma(sql: string): {
|
|
110
|
+
rows: Row[];
|
|
111
|
+
};
|
|
112
|
+
transaction(): Transaction;
|
|
113
|
+
prepare<Schema extends StandardSchemaV1>(sql: string, schema: Schema): Stmt<InferOutput<Schema>>;
|
|
114
|
+
prepareRaw(sql: string): Stmt<unknown>;
|
|
115
|
+
lastInsertRowid(): number;
|
|
116
|
+
changes(): number;
|
|
117
|
+
close(): void;
|
|
118
|
+
[Symbol.dispose](): void;
|
|
119
|
+
}
|
|
120
|
+
export declare class Transaction implements Disposable {
|
|
121
|
+
_db: Database;
|
|
122
|
+
_parent: Transaction | null;
|
|
123
|
+
_savepoint: string | null;
|
|
124
|
+
_done: boolean;
|
|
125
|
+
_failed: boolean;
|
|
126
|
+
constructor(db: Database, parent: Transaction | null, savepoint: string | null);
|
|
127
|
+
get database(): Database;
|
|
128
|
+
_checkLive(): void;
|
|
129
|
+
_popSelf(): void;
|
|
130
|
+
exec(sql: string): void;
|
|
131
|
+
execRaw(sql: string): void;
|
|
132
|
+
prepare<Schema extends StandardSchemaV1>(sql: string, schema: Schema): Stmt<InferOutput<Schema>>;
|
|
133
|
+
prepareRaw(sql: string): Stmt<unknown>;
|
|
134
|
+
savepoint(name: string): Transaction;
|
|
135
|
+
commit(): void;
|
|
136
|
+
rollback(): void;
|
|
137
|
+
[Symbol.dispose](): void;
|
|
138
|
+
}
|