@delali/sirannon-sqlite 0.0.0-stage → 0.3.5-next.71
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 +201 -0
- package/NOTICE +61 -0
- package/README.md +35 -2
- package/index.d.ts +7 -0
- package/index.js +59 -0
- package/lib/binding.js +62 -0
- package/lib/darwin-arm64.js +3 -0
- package/lib/darwin-x64.js +3 -0
- package/lib/database.js +109 -0
- package/lib/index.js +3 -0
- package/lib/linux-arm64.js +3 -0
- package/lib/linux-x64.js +3 -0
- package/lib/linuxmusl-arm64.js +4 -0
- package/lib/linuxmusl-x64.js +4 -0
- package/lib/methods/aggregate.js +43 -0
- package/lib/methods/backup.js +67 -0
- package/lib/methods/explain.js +9 -0
- package/lib/methods/function.js +31 -0
- package/lib/methods/inspect.js +7 -0
- package/lib/methods/pragma.js +12 -0
- package/lib/methods/serialize.js +16 -0
- package/lib/methods/table.js +195 -0
- package/lib/methods/transaction.js +78 -0
- package/lib/methods/wrappers.js +54 -0
- package/lib/sqlite-error.js +25 -0
- package/lib/util.js +12 -0
- package/lib/win32-arm64.js +3 -0
- package/lib/win32-x64.js +3 -0
- package/package.json +41 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2026 Delali (https://sondelali.com)
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/NOTICE
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
sirannon-db
|
|
2
|
+
Copyright 2026 Delali (https://sondelali.com)
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
you may not use this file except in compliance with the License.
|
|
6
|
+
You may obtain a copy of the License at
|
|
7
|
+
|
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
|
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
See the License for the specific language governing permissions and
|
|
14
|
+
limitations under the License.
|
|
15
|
+
|
|
16
|
+
This product includes code from these projects:
|
|
17
|
+
|
|
18
|
+
- workerd (https://github.com/cloudflare/workerd), licensed under the Apache
|
|
19
|
+
License, Version 2.0. native/sqlite/patches/sqlite/0001-row-counts.patch
|
|
20
|
+
starts from workerd's patches/sqlite/0001-row-counts-plain.patch, without
|
|
21
|
+
its comments and its shell hunk, and its header lists the changes that
|
|
22
|
+
Sirannon made to it.
|
|
23
|
+
- libSQL (https://github.com/tursodatabase/libsql), Copyright 2022 the libSQL
|
|
24
|
+
authors, licensed under the MIT License. workerd's row-count patch contains
|
|
25
|
+
libSQL's row-counting code.
|
|
26
|
+
- node-gyp (https://github.com/nodejs/node-gyp), Copyright (c) 2012 Nathan
|
|
27
|
+
Rajlich, licensed under the MIT License. native/sqlite/win-delay-load-hook.cc
|
|
28
|
+
follows node-gyp's src/win_delay_load_hook.cc.
|
|
29
|
+
- better-sqlite3 (https://github.com/WiseLibs/better-sqlite3), Copyright (c)
|
|
30
|
+
2017 Joshua Wise, licensed under the MIT License.
|
|
31
|
+
The patches in native/sqlite/patches/better-sqlite3 change better-sqlite3's
|
|
32
|
+
source.
|
|
33
|
+
The lib directory of the @delali/sirannon-sqlite package contains
|
|
34
|
+
better-sqlite3's JavaScript unchanged, and each
|
|
35
|
+
@delali/sirannon-sqlite-<platform> package contains better-sqlite3's native
|
|
36
|
+
module compiled from that changed source.
|
|
37
|
+
- node-addon-api (https://github.com/nodejs/node-addon-api), Copyright (c)
|
|
38
|
+
2017 Node.js API collaborators, licensed under the MIT License. Each
|
|
39
|
+
@delali/sirannon-sqlite-<platform> package contains code compiled from
|
|
40
|
+
node-addon-api's headers.
|
|
41
|
+
|
|
42
|
+
The MIT License grants the following permission for libSQL, node-gyp,
|
|
43
|
+
better-sqlite3, and node-addon-api:
|
|
44
|
+
|
|
45
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
46
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
47
|
+
in the Software without restriction, including without limitation the rights
|
|
48
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
49
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
50
|
+
furnished to do so, subject to the following conditions:
|
|
51
|
+
|
|
52
|
+
The above copyright notice and this permission notice shall be included in all
|
|
53
|
+
copies or substantial portions of the Software.
|
|
54
|
+
|
|
55
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
56
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
57
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
58
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
59
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
60
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
61
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,36 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @delali/sirannon-sqlite
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](https://www.npmjs.com/package/@delali/sirannon-sqlite)
|
|
4
|
+
[](https://github.com/assetcorp/sirannon-db/blob/main/LICENSE)
|
|
5
|
+
|
|
6
|
+
This package contains better-sqlite3 13.0.2, built against Sirannon's own build of SQLite, which counts the rows that each statement reads and writes. When you pass `rowCounts: true`, Sirannon's better-sqlite3 driver loads this package and reports both counts for every statement through `onQueryComplete`, so that a host that puts many tenants on one machine can bill each of them.
|
|
7
|
+
|
|
8
|
+
## Install
|
|
9
|
+
|
|
10
|
+
Install it with the library:
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
pnpm add -E @delali/sirannon-db @delali/sirannon-sqlite
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
```ts
|
|
17
|
+
import { Sirannon } from '@delali/sirannon-db'
|
|
18
|
+
import { betterSqlite3 } from '@delali/sirannon-db/driver/better-sqlite3'
|
|
19
|
+
|
|
20
|
+
const sirannon = new Sirannon({
|
|
21
|
+
driver: betterSqlite3({ rowCounts: true }),
|
|
22
|
+
metrics: { onQueryComplete: m => console.log(m.databaseId, m.rowsRead, m.rowsWritten) },
|
|
23
|
+
})
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
This package lists six platform packages as optional dependencies, so npm installs only the one whose `os` and `cpu` match your host. The release workflow publishes all seven packages at the library's version, and this package requires exactly that version of each platform package.
|
|
27
|
+
|
|
28
|
+
## What it contains
|
|
29
|
+
|
|
30
|
+
The `lib` directory contains better-sqlite3's JavaScript, unchanged. Sirannon compiles the native module from better-sqlite3's C++ source and the SQLite amalgamation, after it applies each patch in the repository's `native/sqlite/patches` directory. The first SQLite patch is Cloudflare's row-counting patch, which Sirannon changed to widen both counters to 64 bits and to leave the work of its own change-capture triggers out of both counts, and the first better-sqlite3 patch adds the `takeRowCounts` method that reads them. The counting rules are in the [core engine guide](https://github.com/assetcorp/sirannon-db/blob/main/docs/core.md#rows-read-and-rows-written).
|
|
31
|
+
|
|
32
|
+
On a platform without a published build, the driver throws `ROW_COUNTS_UNSUPPORTED` when it opens a database. Compile a build with `native/sqlite/build.mjs` from the repository, and pass its path to the driver as `sqliteBindingPath`.
|
|
33
|
+
|
|
34
|
+
## Licence
|
|
35
|
+
|
|
36
|
+
The Apache-2.0 and MIT licences cover this package. Each project whose code is in this package has its name and licence in the package's `NOTICE` file, and the patches, the build script, and the tests are in [assetcorp/sirannon-db](https://github.com/assetcorp/sirannon-db).
|
package/index.d.ts
ADDED
package/index.js
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
const { existsSync } = require('node:fs')
|
|
2
|
+
const { dirname, join, resolve } = require('node:path')
|
|
3
|
+
const createDatabase = require('./lib/database')
|
|
4
|
+
const SqliteError = require('./lib/sqlite-error')
|
|
5
|
+
|
|
6
|
+
const ADDON_FILE_NAME = 'better_sqlite3.node'
|
|
7
|
+
const MUSL_DIRECTORY = 'musl'
|
|
8
|
+
const PUBLISHED_TARGETS = new Set([
|
|
9
|
+
'darwin-arm64',
|
|
10
|
+
'darwin-x64',
|
|
11
|
+
'linux-arm64',
|
|
12
|
+
'linux-x64',
|
|
13
|
+
'win32-arm64',
|
|
14
|
+
'win32-x64',
|
|
15
|
+
])
|
|
16
|
+
|
|
17
|
+
let muslLibc
|
|
18
|
+
|
|
19
|
+
function usesMuslLibc() {
|
|
20
|
+
if (process.platform !== 'linux') return false
|
|
21
|
+
if (muslLibc === undefined) {
|
|
22
|
+
try {
|
|
23
|
+
muslLibc = !process.report.getReport().header.glibcVersionRuntime
|
|
24
|
+
} catch {
|
|
25
|
+
muslLibc = false
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
return muslLibc
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function bindingPath() {
|
|
32
|
+
const target = `${process.platform}-${process.arch}`
|
|
33
|
+
if (!PUBLISHED_TARGETS.has(target)) return null
|
|
34
|
+
let packageDirectory
|
|
35
|
+
try {
|
|
36
|
+
packageDirectory = dirname(require.resolve(`@delali/sirannon-sqlite-${target}/package.json`))
|
|
37
|
+
} catch {
|
|
38
|
+
return null
|
|
39
|
+
}
|
|
40
|
+
const segments = usesMuslLibc() ? [MUSL_DIRECTORY, ADDON_FILE_NAME] : [ADDON_FILE_NAME]
|
|
41
|
+
const addon = join(packageDirectory, ...segments)
|
|
42
|
+
return existsSync(addon) ? addon : null
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function loadAddon(nativeBinding) {
|
|
46
|
+
if (typeof nativeBinding === 'object' && nativeBinding !== null) return nativeBinding
|
|
47
|
+
const path = typeof nativeBinding === 'string' ? resolve(nativeBinding) : bindingPath()
|
|
48
|
+
if (path === null) {
|
|
49
|
+
const target = `${process.platform}-${process.arch}`
|
|
50
|
+
throw new Error(
|
|
51
|
+
`@delali/sirannon-sqlite found no native build for ${target}. Install @delali/sirannon-sqlite-${target}, or pass the path of a build that you compiled as the nativeBinding option.`,
|
|
52
|
+
)
|
|
53
|
+
}
|
|
54
|
+
return require(path)
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
module.exports = createDatabase(loadAddon, true)
|
|
58
|
+
module.exports.SqliteError = SqliteError
|
|
59
|
+
module.exports.bindingPath = bindingPath
|
package/lib/binding.js
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const fs = require('fs');
|
|
3
|
+
const path = require('path');
|
|
4
|
+
|
|
5
|
+
const PREBUILD_PLATFORMS = ['linux', 'darwin', 'win32'];
|
|
6
|
+
const PREBUILD_ARCHS = ['x64', 'arm64'];
|
|
7
|
+
let DEFAULT_ADDON;
|
|
8
|
+
|
|
9
|
+
function getBinding(nativeBinding) {
|
|
10
|
+
// If a path was provided, load the binding from the filesystem.
|
|
11
|
+
if (typeof nativeBinding === 'string') {
|
|
12
|
+
// See <https://webpack.js.org/api/module-variables/#__non_webpack_require__-webpack-specific>
|
|
13
|
+
const requireFunc = typeof __non_webpack_require__ === 'function' ? __non_webpack_require__ : require;
|
|
14
|
+
return requireFunc(path.resolve(nativeBinding).replace(/(\.node)?$/, '.node'));
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
// If an object was provided, use it as the binding directly.
|
|
18
|
+
if (typeof nativeBinding === 'object' && nativeBinding !== null) {
|
|
19
|
+
return nativeBinding;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// If we're using the default binding and it already exists, just return it.
|
|
23
|
+
if (DEFAULT_ADDON) {
|
|
24
|
+
return DEFAULT_ADDON;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// Otherwise, try to find the binding as a prebuilt binary.
|
|
28
|
+
let filename = getPrebuildPath();
|
|
29
|
+
if (filename) {
|
|
30
|
+
return DEFAULT_ADDON = require(filename);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// If no prebuilt binary was found, try the default node-gyp locations.
|
|
34
|
+
filename = path.join(__dirname, '..', 'build', 'Debug', 'better_sqlite3.node');
|
|
35
|
+
if (!fs.existsSync(filename)) {
|
|
36
|
+
filename = path.join(__dirname, '..', 'build', 'Release', 'better_sqlite3.node');
|
|
37
|
+
}
|
|
38
|
+
return DEFAULT_ADDON = require(filename);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function getPrebuildPath() {
|
|
42
|
+
if (PREBUILD_PLATFORMS.includes(process.platform) && PREBUILD_ARCHS.includes(process.arch)) {
|
|
43
|
+
const target = `${isLinuxMusl() ? 'linuxmusl' : process.platform}-${process.arch}`;
|
|
44
|
+
const filename = path.join(__dirname, '..', 'prebuilds', `${target}.node`);
|
|
45
|
+
if (fs.existsSync(filename)) {
|
|
46
|
+
return filename;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return null;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function isLinuxMusl() {
|
|
53
|
+
return process.platform === 'linux' && !process.report.getReport().header.glibcVersionRuntime;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
exports.getBinding = getBinding;
|
|
57
|
+
exports.getPrebuildPath = getPrebuildPath;
|
|
58
|
+
|
|
59
|
+
// This script is executed directly by binding.gyp to detect prebuilt binaries.
|
|
60
|
+
if (require.main === module) {
|
|
61
|
+
process.stdout.write(getPrebuildPath() ? '1' : '0');
|
|
62
|
+
}
|
package/lib/database.js
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const fs = require('fs');
|
|
3
|
+
const path = require('path');
|
|
4
|
+
const util = require('./util');
|
|
5
|
+
const SqliteError = require('./sqlite-error');
|
|
6
|
+
|
|
7
|
+
module.exports = function createDatabase(getAddon, allowNativeBinding) {
|
|
8
|
+
function Database(filenameGiven, options) {
|
|
9
|
+
if (new.target == null) {
|
|
10
|
+
return new Database(filenameGiven, options);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
// Apply defaults
|
|
14
|
+
let buffer;
|
|
15
|
+
if (Buffer.isBuffer(filenameGiven)) {
|
|
16
|
+
buffer = filenameGiven;
|
|
17
|
+
filenameGiven = ':memory:';
|
|
18
|
+
}
|
|
19
|
+
if (filenameGiven == null) filenameGiven = '';
|
|
20
|
+
if (options == null) options = {};
|
|
21
|
+
|
|
22
|
+
// Validate arguments
|
|
23
|
+
if (typeof filenameGiven !== 'string') throw new TypeError('Expected first argument to be a string');
|
|
24
|
+
if (typeof options !== 'object') throw new TypeError('Expected second argument to be an options object');
|
|
25
|
+
if ('readOnly' in options) throw new TypeError('Misspelled option "readOnly" should be "readonly"');
|
|
26
|
+
if ('memory' in options) throw new TypeError('Option "memory" was removed in v7.0.0 (use ":memory:" filename instead)');
|
|
27
|
+
|
|
28
|
+
// Interpret options
|
|
29
|
+
const filename = filenameGiven.trim();
|
|
30
|
+
const anonymous = filename === '' || filename === ':memory:';
|
|
31
|
+
const readonly = util.getBooleanOption(options, 'readonly');
|
|
32
|
+
const fileMustExist = util.getBooleanOption(options, 'fileMustExist');
|
|
33
|
+
const timeout = 'timeout' in options ? options.timeout : 5000;
|
|
34
|
+
const verbose = 'verbose' in options ? options.verbose : null;
|
|
35
|
+
const nativeBinding = 'nativeBinding' in options ? options.nativeBinding : null;
|
|
36
|
+
|
|
37
|
+
// Validate interpreted options
|
|
38
|
+
if (readonly && anonymous && !buffer) throw new TypeError('In-memory/temporary databases cannot be readonly');
|
|
39
|
+
if (!Number.isInteger(timeout) || timeout < 0) throw new TypeError('Expected the "timeout" option to be a positive integer');
|
|
40
|
+
if (timeout > 0x7fffffff) throw new RangeError('Option "timeout" cannot be greater than 2147483647');
|
|
41
|
+
if (verbose != null && typeof verbose !== 'function') throw new TypeError('Expected the "verbose" option to be a function');
|
|
42
|
+
if (!allowNativeBinding && 'nativeBinding' in options) throw new TypeError('The "nativeBinding" option is only supported by the default better-sqlite3 entrypoint');
|
|
43
|
+
if (allowNativeBinding && nativeBinding != null && typeof nativeBinding !== 'string' && typeof nativeBinding !== 'object') throw new TypeError('Expected the "nativeBinding" option to be a string or addon object');
|
|
44
|
+
|
|
45
|
+
// Load the native addon
|
|
46
|
+
const addon = getAddon(nativeBinding);
|
|
47
|
+
if (!addon.isInitialized) {
|
|
48
|
+
addon.initialize(SqliteError, arrayFactory, arrayAppender, rowFactory, recordFactory);
|
|
49
|
+
addon.isInitialized = true;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// Make sure the specified directory exists
|
|
53
|
+
if (!anonymous && !filename.startsWith('file:') && !fs.existsSync(path.dirname(filename))) {
|
|
54
|
+
throw new TypeError('Cannot open database because the directory does not exist');
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
Object.defineProperties(this, {
|
|
58
|
+
[util.cppdb]: { value: new addon.Database(filename, filenameGiven, anonymous, readonly, fileMustExist, timeout, verbose || null, buffer || null) },
|
|
59
|
+
...wrappers.getters,
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const wrappers = require('./methods/wrappers');
|
|
64
|
+
Database.prototype.prepare = wrappers.prepare;
|
|
65
|
+
Database.prototype.transaction = require('./methods/transaction');
|
|
66
|
+
Database.prototype.pragma = require('./methods/pragma');
|
|
67
|
+
Database.prototype.explain = require('./methods/explain');
|
|
68
|
+
Database.prototype.backup = require('./methods/backup');
|
|
69
|
+
Database.prototype.serialize = require('./methods/serialize');
|
|
70
|
+
Database.prototype.function = require('./methods/function');
|
|
71
|
+
Database.prototype.aggregate = require('./methods/aggregate');
|
|
72
|
+
Database.prototype.table = require('./methods/table');
|
|
73
|
+
Database.prototype.loadExtension = wrappers.loadExtension;
|
|
74
|
+
Database.prototype.exec = wrappers.exec;
|
|
75
|
+
Database.prototype.close = wrappers.close;
|
|
76
|
+
Database.prototype.defaultSafeIntegers = wrappers.defaultSafeIntegers;
|
|
77
|
+
Database.prototype.unsafeMode = wrappers.unsafeMode;
|
|
78
|
+
Database.prototype[util.inspect] = require('./methods/inspect');
|
|
79
|
+
|
|
80
|
+
return Database;
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
function arrayFactory(...values) {
|
|
84
|
+
return values;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function arrayAppender(array, ...values) {
|
|
88
|
+
const offset = array.length;
|
|
89
|
+
for (let i = 0; i < values.length; ++i) {
|
|
90
|
+
array[offset + i] = values[i];
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function rowFactory(...keys) {
|
|
95
|
+
if (!keys.includes('__proto__')) {
|
|
96
|
+
const parameters = keys.map((_, index) => `v${index}`).join(',');
|
|
97
|
+
const properties = keys.map((key, index) => `${JSON.stringify(key)}:v${index}`).join(',');
|
|
98
|
+
return Function(`return (${parameters}) => ({${properties}})`)();
|
|
99
|
+
}
|
|
100
|
+
return (...values) => {
|
|
101
|
+
const row = {};
|
|
102
|
+
for (let i = 0; i < keys.length; ++i) row[keys[i]] = values[i];
|
|
103
|
+
return row;
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function recordFactory(value) {
|
|
108
|
+
return { value, done: false };
|
|
109
|
+
}
|
package/lib/index.js
ADDED
package/lib/linux-x64.js
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const { getBooleanOption, cppdb } = require('../util');
|
|
3
|
+
|
|
4
|
+
module.exports = function defineAggregate(name, options) {
|
|
5
|
+
// Validate arguments
|
|
6
|
+
if (typeof name !== 'string') throw new TypeError('Expected first argument to be a string');
|
|
7
|
+
if (typeof options !== 'object' || options === null) throw new TypeError('Expected second argument to be an options object');
|
|
8
|
+
if (!name) throw new TypeError('User-defined function name cannot be an empty string');
|
|
9
|
+
|
|
10
|
+
// Interpret options
|
|
11
|
+
const start = 'start' in options ? options.start : null;
|
|
12
|
+
const step = getFunctionOption(options, 'step', true);
|
|
13
|
+
const inverse = getFunctionOption(options, 'inverse', false);
|
|
14
|
+
const result = getFunctionOption(options, 'result', false);
|
|
15
|
+
const safeIntegers = 'safeIntegers' in options ? +getBooleanOption(options, 'safeIntegers') : 2;
|
|
16
|
+
const deterministic = getBooleanOption(options, 'deterministic');
|
|
17
|
+
const directOnly = getBooleanOption(options, 'directOnly');
|
|
18
|
+
const varargs = getBooleanOption(options, 'varargs');
|
|
19
|
+
let argCount = -1;
|
|
20
|
+
|
|
21
|
+
// Determine argument count
|
|
22
|
+
if (!varargs) {
|
|
23
|
+
argCount = Math.max(getLength(step), inverse ? getLength(inverse) : 0);
|
|
24
|
+
if (argCount > 0) argCount -= 1;
|
|
25
|
+
if (argCount > 100) throw new RangeError('User-defined functions cannot have more than 100 arguments');
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
this[cppdb].aggregate(start, step, inverse, result, name, argCount, safeIntegers, deterministic, directOnly);
|
|
29
|
+
return this;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
const getFunctionOption = (options, key, required) => {
|
|
33
|
+
const value = key in options ? options[key] : null;
|
|
34
|
+
if (typeof value === 'function') return value;
|
|
35
|
+
if (value != null) throw new TypeError(`Expected the "${key}" option to be a function`);
|
|
36
|
+
if (required) throw new TypeError(`Missing required option "${key}"`);
|
|
37
|
+
return null;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
const getLength = ({ length }) => {
|
|
41
|
+
if (Number.isInteger(length) && length >= 0) return length;
|
|
42
|
+
throw new TypeError('Expected function.length to be a positive integer');
|
|
43
|
+
};
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const fs = require('fs');
|
|
3
|
+
const path = require('path');
|
|
4
|
+
const { promisify } = require('util');
|
|
5
|
+
const { cppdb } = require('../util');
|
|
6
|
+
const fsAccess = promisify(fs.access);
|
|
7
|
+
|
|
8
|
+
module.exports = async function backup(filename, options) {
|
|
9
|
+
if (options == null) options = {};
|
|
10
|
+
|
|
11
|
+
// Validate arguments
|
|
12
|
+
if (typeof filename !== 'string') throw new TypeError('Expected first argument to be a string');
|
|
13
|
+
if (typeof options !== 'object') throw new TypeError('Expected second argument to be an options object');
|
|
14
|
+
|
|
15
|
+
// Interpret options
|
|
16
|
+
filename = filename.trim();
|
|
17
|
+
const attachedName = 'attached' in options ? options.attached : 'main';
|
|
18
|
+
const handler = 'progress' in options ? options.progress : null;
|
|
19
|
+
|
|
20
|
+
// Validate interpreted options
|
|
21
|
+
if (!filename) throw new TypeError('Backup filename cannot be an empty string');
|
|
22
|
+
if (filename === ':memory:') throw new TypeError('Invalid backup filename ":memory:"');
|
|
23
|
+
if (typeof attachedName !== 'string') throw new TypeError('Expected the "attached" option to be a string');
|
|
24
|
+
if (!attachedName) throw new TypeError('The "attached" option cannot be an empty string');
|
|
25
|
+
if (handler != null && typeof handler !== 'function') throw new TypeError('Expected the "progress" option to be a function');
|
|
26
|
+
|
|
27
|
+
// Make sure the specified directory exists
|
|
28
|
+
await fsAccess(path.dirname(filename)).catch(() => {
|
|
29
|
+
throw new TypeError('Cannot save backup because the directory does not exist');
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
const isNewFile = await fsAccess(filename).then(() => false, () => true);
|
|
33
|
+
return runBackup(this[cppdb].backup(this, attachedName, filename, isNewFile), handler || null);
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
const runBackup = (backup, handler) => {
|
|
37
|
+
let rate = 0;
|
|
38
|
+
let useDefault = true;
|
|
39
|
+
|
|
40
|
+
return new Promise((resolve, reject) => {
|
|
41
|
+
setImmediate(function step() {
|
|
42
|
+
try {
|
|
43
|
+
const progress = backup.transfer(rate);
|
|
44
|
+
if (!progress.remainingPages) {
|
|
45
|
+
backup.close();
|
|
46
|
+
resolve(progress);
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
if (useDefault) {
|
|
50
|
+
useDefault = false;
|
|
51
|
+
rate = 100;
|
|
52
|
+
}
|
|
53
|
+
if (handler) {
|
|
54
|
+
const ret = handler(progress);
|
|
55
|
+
if (ret !== undefined) {
|
|
56
|
+
if (typeof ret === 'number' && ret === ret) rate = Math.max(0, Math.min(0x7fffffff, Math.round(ret)));
|
|
57
|
+
else throw new TypeError('Expected progress callback to return a number or undefined');
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
setImmediate(step);
|
|
61
|
+
} catch (err) {
|
|
62
|
+
backup.close();
|
|
63
|
+
reject(err);
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const { cppdb } = require('../util');
|
|
3
|
+
|
|
4
|
+
module.exports = function explain(source) {
|
|
5
|
+
if (typeof source !== 'string') throw new TypeError('Expected first argument to be a string');
|
|
6
|
+
|
|
7
|
+
const stmt = this[cppdb].prepare(`EXPLAIN ${source}`, this, false, true);
|
|
8
|
+
return stmt.all();
|
|
9
|
+
};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const { getBooleanOption, cppdb } = require('../util');
|
|
3
|
+
|
|
4
|
+
module.exports = function defineFunction(name, options, fn) {
|
|
5
|
+
// Apply defaults
|
|
6
|
+
if (options == null) options = {};
|
|
7
|
+
if (typeof options === 'function') { fn = options; options = {}; }
|
|
8
|
+
|
|
9
|
+
// Validate arguments
|
|
10
|
+
if (typeof name !== 'string') throw new TypeError('Expected first argument to be a string');
|
|
11
|
+
if (typeof fn !== 'function') throw new TypeError('Expected last argument to be a function');
|
|
12
|
+
if (typeof options !== 'object') throw new TypeError('Expected second argument to be an options object');
|
|
13
|
+
if (!name) throw new TypeError('User-defined function name cannot be an empty string');
|
|
14
|
+
|
|
15
|
+
// Interpret options
|
|
16
|
+
const safeIntegers = 'safeIntegers' in options ? +getBooleanOption(options, 'safeIntegers') : 2;
|
|
17
|
+
const deterministic = getBooleanOption(options, 'deterministic');
|
|
18
|
+
const directOnly = getBooleanOption(options, 'directOnly');
|
|
19
|
+
const varargs = getBooleanOption(options, 'varargs');
|
|
20
|
+
let argCount = -1;
|
|
21
|
+
|
|
22
|
+
// Determine argument count
|
|
23
|
+
if (!varargs) {
|
|
24
|
+
argCount = fn.length;
|
|
25
|
+
if (!Number.isInteger(argCount) || argCount < 0) throw new TypeError('Expected function.length to be a positive integer');
|
|
26
|
+
if (argCount > 100) throw new RangeError('User-defined functions cannot have more than 100 arguments');
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
this[cppdb].function(fn, name, argCount, safeIntegers, deterministic, directOnly);
|
|
30
|
+
return this;
|
|
31
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const { getBooleanOption, cppdb } = require('../util');
|
|
3
|
+
|
|
4
|
+
module.exports = function pragma(source, options) {
|
|
5
|
+
if (options == null) options = {};
|
|
6
|
+
if (typeof source !== 'string') throw new TypeError('Expected first argument to be a string');
|
|
7
|
+
if (typeof options !== 'object') throw new TypeError('Expected second argument to be an options object');
|
|
8
|
+
const simple = getBooleanOption(options, 'simple');
|
|
9
|
+
|
|
10
|
+
const stmt = this[cppdb].prepare(`PRAGMA ${source}`, this, true, false);
|
|
11
|
+
return simple ? stmt.pluck().get() : stmt.all();
|
|
12
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const { cppdb } = require('../util');
|
|
3
|
+
|
|
4
|
+
module.exports = function serialize(options) {
|
|
5
|
+
if (options == null) options = {};
|
|
6
|
+
|
|
7
|
+
// Validate arguments
|
|
8
|
+
if (typeof options !== 'object') throw new TypeError('Expected first argument to be an options object');
|
|
9
|
+
|
|
10
|
+
// Interpret and validate options
|
|
11
|
+
const attachedName = 'attached' in options ? options.attached : 'main';
|
|
12
|
+
if (typeof attachedName !== 'string') throw new TypeError('Expected the "attached" option to be a string');
|
|
13
|
+
if (!attachedName) throw new TypeError('The "attached" option cannot be an empty string');
|
|
14
|
+
|
|
15
|
+
return this[cppdb].serialize(attachedName);
|
|
16
|
+
};
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const { cppdb } = require('../util');
|
|
3
|
+
|
|
4
|
+
module.exports = function defineTable(name, factory) {
|
|
5
|
+
// Validate arguments
|
|
6
|
+
if (typeof name !== 'string') throw new TypeError('Expected first argument to be a string');
|
|
7
|
+
if (!name) throw new TypeError('Virtual table module name cannot be an empty string');
|
|
8
|
+
|
|
9
|
+
// Determine whether the module is eponymous-only or not
|
|
10
|
+
let eponymous = false;
|
|
11
|
+
if (typeof factory === 'object' && factory !== null) {
|
|
12
|
+
eponymous = true;
|
|
13
|
+
factory = defer(parseTableDefinition(factory, 'used', name));
|
|
14
|
+
} else {
|
|
15
|
+
if (typeof factory !== 'function') throw new TypeError('Expected second argument to be a function or a table definition object');
|
|
16
|
+
factory = wrapFactory(factory);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
this[cppdb].table(factory, name, eponymous);
|
|
20
|
+
return this;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
function wrapFactory(factory) {
|
|
24
|
+
return function virtualTableFactory(moduleName, databaseName, tableName, ...args) {
|
|
25
|
+
const thisObject = {
|
|
26
|
+
module: moduleName,
|
|
27
|
+
database: databaseName,
|
|
28
|
+
table: tableName,
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
// Generate a new table definition by invoking the factory
|
|
32
|
+
const def = apply.call(factory, thisObject, args);
|
|
33
|
+
if (typeof def !== 'object' || def === null) {
|
|
34
|
+
throw new TypeError(`Virtual table module "${moduleName}" did not return a table definition object`);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return parseTableDefinition(def, 'returned', moduleName);
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function parseTableDefinition(def, verb, moduleName) {
|
|
42
|
+
// Validate required properties
|
|
43
|
+
if (!hasOwnProperty.call(def, 'rows')) {
|
|
44
|
+
throw new TypeError(`Virtual table module "${moduleName}" ${verb} a table definition without a "rows" property`);
|
|
45
|
+
}
|
|
46
|
+
if (!hasOwnProperty.call(def, 'columns')) {
|
|
47
|
+
throw new TypeError(`Virtual table module "${moduleName}" ${verb} a table definition without a "columns" property`);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// Validate "rows" property
|
|
51
|
+
const rows = def.rows;
|
|
52
|
+
if (typeof rows !== 'function' || Object.getPrototypeOf(rows) !== GeneratorFunctionPrototype) {
|
|
53
|
+
throw new TypeError(`Virtual table module "${moduleName}" ${verb} a table definition with an invalid "rows" property (should be a generator function)`);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// Validate "columns" property
|
|
57
|
+
let columns = def.columns;
|
|
58
|
+
if (!Array.isArray(columns) || !isStringArray(columns = [...columns])) {
|
|
59
|
+
throw new TypeError(`Virtual table module "${moduleName}" ${verb} a table definition with an invalid "columns" property (should be an array of strings)`);
|
|
60
|
+
}
|
|
61
|
+
if (columns.length !== new Set(columns).size) {
|
|
62
|
+
throw new TypeError(`Virtual table module "${moduleName}" ${verb} a table definition with duplicate column names`);
|
|
63
|
+
}
|
|
64
|
+
if (!columns.length) {
|
|
65
|
+
throw new RangeError(`Virtual table module "${moduleName}" ${verb} a table definition with zero columns`);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// Validate "parameters" property
|
|
69
|
+
let parameters;
|
|
70
|
+
if (hasOwnProperty.call(def, 'parameters')) {
|
|
71
|
+
parameters = def.parameters;
|
|
72
|
+
if (!Array.isArray(parameters) || !isStringArray(parameters = [...parameters])) {
|
|
73
|
+
throw new TypeError(`Virtual table module "${moduleName}" ${verb} a table definition with an invalid "parameters" property (should be an array of strings)`);
|
|
74
|
+
}
|
|
75
|
+
} else {
|
|
76
|
+
parameters = inferParameters(rows);
|
|
77
|
+
}
|
|
78
|
+
if (parameters.length !== new Set(parameters).size) {
|
|
79
|
+
throw new TypeError(`Virtual table module "${moduleName}" ${verb} a table definition with duplicate parameter names`);
|
|
80
|
+
}
|
|
81
|
+
if (parameters.length > 32) {
|
|
82
|
+
throw new RangeError(`Virtual table module "${moduleName}" ${verb} a table definition with more than the maximum number of 32 parameters`);
|
|
83
|
+
}
|
|
84
|
+
for (const parameter of parameters) {
|
|
85
|
+
if (columns.includes(parameter)) {
|
|
86
|
+
throw new TypeError(`Virtual table module "${moduleName}" ${verb} a table definition with column "${parameter}" which was ambiguously defined as both a column and parameter`);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// Validate "safeIntegers" option
|
|
91
|
+
let safeIntegers = 2;
|
|
92
|
+
if (hasOwnProperty.call(def, 'safeIntegers')) {
|
|
93
|
+
const bool = def.safeIntegers;
|
|
94
|
+
if (typeof bool !== 'boolean') {
|
|
95
|
+
throw new TypeError(`Virtual table module "${moduleName}" ${verb} a table definition with an invalid "safeIntegers" property (should be a boolean)`);
|
|
96
|
+
}
|
|
97
|
+
safeIntegers = +bool;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// Validate "directOnly" option
|
|
101
|
+
let directOnly = false;
|
|
102
|
+
if (hasOwnProperty.call(def, 'directOnly')) {
|
|
103
|
+
directOnly = def.directOnly;
|
|
104
|
+
if (typeof directOnly !== 'boolean') {
|
|
105
|
+
throw new TypeError(`Virtual table module "${moduleName}" ${verb} a table definition with an invalid "directOnly" property (should be a boolean)`);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// Generate SQL for the virtual table definition
|
|
110
|
+
const columnDefinitions = [
|
|
111
|
+
...parameters.map(identifier).map(str => `${str} HIDDEN`),
|
|
112
|
+
...columns.map(identifier),
|
|
113
|
+
];
|
|
114
|
+
return [
|
|
115
|
+
`CREATE TABLE x(${columnDefinitions.join(', ')});`,
|
|
116
|
+
wrapGenerator(rows, new Map(columns.map((x, i) => [x, parameters.length + i])), moduleName),
|
|
117
|
+
parameters,
|
|
118
|
+
safeIntegers,
|
|
119
|
+
directOnly,
|
|
120
|
+
];
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function wrapGenerator(generator, columnMap, moduleName) {
|
|
124
|
+
return function* virtualTable(...args) {
|
|
125
|
+
/*
|
|
126
|
+
We must defensively clone any buffers in the arguments, because
|
|
127
|
+
otherwise the generator could mutate one of them, which would cause
|
|
128
|
+
us to return incorrect values for hidden columns, potentially
|
|
129
|
+
corrupting the database.
|
|
130
|
+
*/
|
|
131
|
+
const output = args.map(x => Buffer.isBuffer(x) ? Buffer.from(x) : x);
|
|
132
|
+
for (let i = 0; i < columnMap.size; ++i) {
|
|
133
|
+
output.push(null); // Fill with nulls to prevent gaps in array (v8 optimization)
|
|
134
|
+
}
|
|
135
|
+
for (const row of generator(...args)) {
|
|
136
|
+
if (Array.isArray(row)) {
|
|
137
|
+
extractRowArray(row, output, columnMap.size, moduleName);
|
|
138
|
+
yield output;
|
|
139
|
+
} else if (typeof row === 'object' && row !== null) {
|
|
140
|
+
extractRowObject(row, output, columnMap, moduleName);
|
|
141
|
+
yield output;
|
|
142
|
+
} else {
|
|
143
|
+
throw new TypeError(`Virtual table module "${moduleName}" yielded something that isn't a valid row object`);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function extractRowArray(row, output, columnCount, moduleName) {
|
|
150
|
+
if (row.length !== columnCount) {
|
|
151
|
+
throw new TypeError(`Virtual table module "${moduleName}" yielded a row with an incorrect number of columns`);
|
|
152
|
+
}
|
|
153
|
+
const offset = output.length - columnCount;
|
|
154
|
+
for (let i = 0; i < columnCount; ++i) {
|
|
155
|
+
output[i + offset] = row[i];
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function extractRowObject(row, output, columnMap, moduleName) {
|
|
160
|
+
let count = 0;
|
|
161
|
+
for (const key of Object.keys(row)) {
|
|
162
|
+
const index = columnMap.get(key);
|
|
163
|
+
if (index === undefined) {
|
|
164
|
+
throw new TypeError(`Virtual table module "${moduleName}" yielded a row with an undeclared column "${key}"`);
|
|
165
|
+
}
|
|
166
|
+
output[index] = row[key];
|
|
167
|
+
count += 1;
|
|
168
|
+
}
|
|
169
|
+
if (count !== columnMap.size) {
|
|
170
|
+
throw new TypeError(`Virtual table module "${moduleName}" yielded a row with missing columns`);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
function inferParameters({ length }) {
|
|
175
|
+
if (!Number.isInteger(length) || length < 0) {
|
|
176
|
+
throw new TypeError('Expected function.length to be a positive integer');
|
|
177
|
+
}
|
|
178
|
+
const params = [];
|
|
179
|
+
for (let i = 0; i < length; ++i) {
|
|
180
|
+
params.push(`$${i + 1}`);
|
|
181
|
+
}
|
|
182
|
+
return params;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
const { hasOwnProperty } = Object.prototype;
|
|
186
|
+
const { apply } = Function.prototype;
|
|
187
|
+
const GeneratorFunctionPrototype = Object.getPrototypeOf(function*(){});
|
|
188
|
+
const identifier = str => `"${str.replace(/"/g, '""')}"`;
|
|
189
|
+
const defer = x => () => x;
|
|
190
|
+
const isStringArray = (arr) => {
|
|
191
|
+
for (let i = 0; i < arr.length; ++i) {
|
|
192
|
+
if (typeof arr[i] !== 'string') return false;
|
|
193
|
+
}
|
|
194
|
+
return true;
|
|
195
|
+
};
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const { cppdb } = require('../util');
|
|
3
|
+
const controllers = new WeakMap();
|
|
4
|
+
|
|
5
|
+
module.exports = function transaction(fn) {
|
|
6
|
+
if (typeof fn !== 'function') throw new TypeError('Expected first argument to be a function');
|
|
7
|
+
|
|
8
|
+
const db = this[cppdb];
|
|
9
|
+
const controller = getController(db, this);
|
|
10
|
+
const { apply } = Function.prototype;
|
|
11
|
+
|
|
12
|
+
// Each version of the transaction function has these same properties
|
|
13
|
+
const properties = {
|
|
14
|
+
default: { value: wrapTransaction(apply, fn, db, controller.default) },
|
|
15
|
+
deferred: { value: wrapTransaction(apply, fn, db, controller.deferred) },
|
|
16
|
+
immediate: { value: wrapTransaction(apply, fn, db, controller.immediate) },
|
|
17
|
+
exclusive: { value: wrapTransaction(apply, fn, db, controller.exclusive) },
|
|
18
|
+
database: { value: this, enumerable: true },
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
Object.defineProperties(properties.default.value, properties);
|
|
22
|
+
Object.defineProperties(properties.deferred.value, properties);
|
|
23
|
+
Object.defineProperties(properties.immediate.value, properties);
|
|
24
|
+
Object.defineProperties(properties.exclusive.value, properties);
|
|
25
|
+
|
|
26
|
+
// Return the default version of the transaction function
|
|
27
|
+
return properties.default.value;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
// Return the database's cached transaction controller, or create a new one
|
|
31
|
+
const getController = (db, self) => {
|
|
32
|
+
let controller = controllers.get(db);
|
|
33
|
+
if (!controller) {
|
|
34
|
+
const shared = {
|
|
35
|
+
commit: db.prepare('COMMIT', self, false, false),
|
|
36
|
+
rollback: db.prepare('ROLLBACK', self, false, false),
|
|
37
|
+
savepoint: db.prepare('SAVEPOINT `\t_bs3.\t`', self, false, false),
|
|
38
|
+
release: db.prepare('RELEASE `\t_bs3.\t`', self, false, false),
|
|
39
|
+
rollbackTo: db.prepare('ROLLBACK TO `\t_bs3.\t`', self, false, false),
|
|
40
|
+
};
|
|
41
|
+
controllers.set(db, controller = {
|
|
42
|
+
default: Object.assign({ begin: db.prepare('BEGIN', self, false, false) }, shared),
|
|
43
|
+
deferred: Object.assign({ begin: db.prepare('BEGIN DEFERRED', self, false, false) }, shared),
|
|
44
|
+
immediate: Object.assign({ begin: db.prepare('BEGIN IMMEDIATE', self, false, false) }, shared),
|
|
45
|
+
exclusive: Object.assign({ begin: db.prepare('BEGIN EXCLUSIVE', self, false, false) }, shared),
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
return controller;
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
// Return a new transaction function by wrapping the given function
|
|
52
|
+
const wrapTransaction = (apply, fn, db, { begin, commit, rollback, savepoint, release, rollbackTo }) => function sqliteTransaction() {
|
|
53
|
+
let before, after, undo;
|
|
54
|
+
if (db.inTransaction) {
|
|
55
|
+
before = savepoint;
|
|
56
|
+
after = release;
|
|
57
|
+
undo = rollbackTo;
|
|
58
|
+
} else {
|
|
59
|
+
before = begin;
|
|
60
|
+
after = commit;
|
|
61
|
+
undo = rollback;
|
|
62
|
+
}
|
|
63
|
+
before.run();
|
|
64
|
+
try {
|
|
65
|
+
const result = apply.call(fn, this, arguments);
|
|
66
|
+
if (result && typeof result.then === 'function') {
|
|
67
|
+
throw new TypeError('Transaction function cannot return a promise');
|
|
68
|
+
}
|
|
69
|
+
after.run();
|
|
70
|
+
return result;
|
|
71
|
+
} catch (ex) {
|
|
72
|
+
if (db.inTransaction) {
|
|
73
|
+
undo.run();
|
|
74
|
+
if (undo !== rollback) after.run();
|
|
75
|
+
}
|
|
76
|
+
throw ex;
|
|
77
|
+
}
|
|
78
|
+
};
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const { cppdb } = require('../util');
|
|
3
|
+
|
|
4
|
+
exports.prepare = function prepare(sql) {
|
|
5
|
+
return this[cppdb].prepare(sql, this, false, false);
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
exports.exec = function exec(sql) {
|
|
9
|
+
this[cppdb].exec(sql);
|
|
10
|
+
return this;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
exports.close = function close() {
|
|
14
|
+
this[cppdb].close();
|
|
15
|
+
return this;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
exports.loadExtension = function loadExtension(...args) {
|
|
19
|
+
this[cppdb].loadExtension(...args);
|
|
20
|
+
return this;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
exports.defaultSafeIntegers = function defaultSafeIntegers(...args) {
|
|
24
|
+
this[cppdb].defaultSafeIntegers(...args);
|
|
25
|
+
return this;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
exports.unsafeMode = function unsafeMode(...args) {
|
|
29
|
+
this[cppdb].unsafeMode(...args);
|
|
30
|
+
return this;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
exports.getters = {
|
|
34
|
+
name: {
|
|
35
|
+
get: function name() { return this[cppdb].name; },
|
|
36
|
+
enumerable: true,
|
|
37
|
+
},
|
|
38
|
+
open: {
|
|
39
|
+
get: function open() { return this[cppdb].open; },
|
|
40
|
+
enumerable: true,
|
|
41
|
+
},
|
|
42
|
+
inTransaction: {
|
|
43
|
+
get: function inTransaction() { return this[cppdb].inTransaction; },
|
|
44
|
+
enumerable: true,
|
|
45
|
+
},
|
|
46
|
+
readonly: {
|
|
47
|
+
get: function readonly() { return this[cppdb].readonly; },
|
|
48
|
+
enumerable: true,
|
|
49
|
+
},
|
|
50
|
+
memory: {
|
|
51
|
+
get: function memory() { return this[cppdb].memory; },
|
|
52
|
+
enumerable: true,
|
|
53
|
+
},
|
|
54
|
+
};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
class SqliteError extends Error {
|
|
4
|
+
constructor(message, code) {
|
|
5
|
+
if (typeof code !== 'string') {
|
|
6
|
+
throw new TypeError('Expected second argument to be a string');
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
super('' + message);
|
|
10
|
+
this.code = code;
|
|
11
|
+
|
|
12
|
+
if (typeof Error.captureStackTrace === 'function') {
|
|
13
|
+
Error.captureStackTrace(this, SqliteError);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
Object.defineProperty(SqliteError.prototype, 'name', {
|
|
19
|
+
value: 'SqliteError',
|
|
20
|
+
writable: true,
|
|
21
|
+
enumerable: false,
|
|
22
|
+
configurable: true,
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
module.exports = SqliteError;
|
package/lib/util.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
exports.getBooleanOption = (options, key) => {
|
|
4
|
+
let value = false;
|
|
5
|
+
if (key in options && typeof (value = options[key]) !== 'boolean') {
|
|
6
|
+
throw new TypeError(`Expected the "${key}" option to be a boolean`);
|
|
7
|
+
}
|
|
8
|
+
return value;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
exports.cppdb = Symbol();
|
|
12
|
+
exports.inspect = Symbol.for('nodejs.util.inspect.custom');
|
package/lib/win32-x64.js
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,43 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@delali/sirannon-sqlite",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.3.5-next.71",
|
|
4
|
+
"description": "better-sqlite3 built against Sirannon's build of SQLite, which counts the rows that each statement reads and writes so that Sirannon can report them for per-tenant metering.",
|
|
5
|
+
"license": "Apache-2.0 AND MIT",
|
|
6
|
+
"author": "Delali (https://sondelali.com)",
|
|
7
|
+
"homepage": "https://github.com/assetcorp/sirannon-db",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "https://github.com/assetcorp/sirannon-db.git",
|
|
11
|
+
"directory": "native/sqlite/npm/sirannon-sqlite"
|
|
12
|
+
},
|
|
13
|
+
"main": "index.js",
|
|
14
|
+
"types": "index.d.ts",
|
|
15
|
+
"exports": {
|
|
16
|
+
".": {
|
|
17
|
+
"types": "./index.d.ts",
|
|
18
|
+
"default": "./index.js"
|
|
19
|
+
},
|
|
20
|
+
"./package.json": "./package.json"
|
|
21
|
+
},
|
|
22
|
+
"files": [
|
|
23
|
+
"index.js",
|
|
24
|
+
"index.d.ts",
|
|
25
|
+
"lib",
|
|
26
|
+
"LICENSE",
|
|
27
|
+
"NOTICE"
|
|
28
|
+
],
|
|
29
|
+
"engines": {
|
|
30
|
+
"node": ">=22"
|
|
31
|
+
},
|
|
32
|
+
"dependencies": {
|
|
33
|
+
"@types/better-sqlite3": "7.6.13"
|
|
34
|
+
},
|
|
35
|
+
"optionalDependencies": {
|
|
36
|
+
"@delali/sirannon-sqlite-darwin-arm64": "0.3.5-next.71",
|
|
37
|
+
"@delali/sirannon-sqlite-darwin-x64": "0.3.5-next.71",
|
|
38
|
+
"@delali/sirannon-sqlite-linux-arm64": "0.3.5-next.71",
|
|
39
|
+
"@delali/sirannon-sqlite-linux-x64": "0.3.5-next.71",
|
|
40
|
+
"@delali/sirannon-sqlite-win32-arm64": "0.3.5-next.71",
|
|
41
|
+
"@delali/sirannon-sqlite-win32-x64": "0.3.5-next.71"
|
|
42
|
+
}
|
|
43
|
+
}
|