@rocicorp/zero-sqlite3 1.0.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 +21 -0
- package/README.md +92 -0
- package/binding.gyp +71 -0
- package/deps/common.gypi +68 -0
- package/deps/copy.js +32 -0
- package/deps/defines.gypi +42 -0
- package/deps/download.sh +122 -0
- package/deps/patches/1208.patch +15 -0
- package/deps/sqlite3/shell.c +33359 -0
- package/deps/sqlite3/sqlite3.c +263193 -0
- package/deps/sqlite3/sqlite3.h +13721 -0
- package/deps/sqlite3/sqlite3ext.h +719 -0
- package/deps/sqlite3.gyp +102 -0
- package/deps/test_extension.c +21 -0
- package/lib/database.js +89 -0
- package/lib/index.d.ts +151 -0
- package/lib/index.js +3 -0
- package/lib/methods/aggregate.js +43 -0
- package/lib/methods/backup.js +67 -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 +189 -0
- package/lib/methods/transaction.js +75 -0
- package/lib/methods/wrappers.js +49 -0
- package/lib/sqlite-error.js +20 -0
- package/lib/util.js +12 -0
- package/package.json +58 -0
- package/src/better_sqlite3.cpp +2140 -0
- package/src/better_sqlite3.hpp +1034 -0
|
@@ -0,0 +1,189 @@
|
|
|
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) || !(columns = [...columns]).every(x => typeof x === 'string')) {
|
|
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) || !(parameters = [...parameters]).every(x => typeof x === 'string')) {
|
|
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;
|
|
@@ -0,0 +1,75 @@
|
|
|
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),
|
|
36
|
+
rollback: db.prepare('ROLLBACK', self, false),
|
|
37
|
+
savepoint: db.prepare('SAVEPOINT `\t_bs3.\t`', self, false),
|
|
38
|
+
release: db.prepare('RELEASE `\t_bs3.\t`', self, false),
|
|
39
|
+
rollbackTo: db.prepare('ROLLBACK TO `\t_bs3.\t`', self, false),
|
|
40
|
+
};
|
|
41
|
+
controllers.set(db, controller = {
|
|
42
|
+
default: Object.assign({ begin: db.prepare('BEGIN', self, false) }, shared),
|
|
43
|
+
deferred: Object.assign({ begin: db.prepare('BEGIN DEFERRED', self, false) }, shared),
|
|
44
|
+
immediate: Object.assign({ begin: db.prepare('BEGIN IMMEDIATE', self, false) }, shared),
|
|
45
|
+
exclusive: Object.assign({ begin: db.prepare('BEGIN EXCLUSIVE', self, 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
|
+
after.run();
|
|
67
|
+
return result;
|
|
68
|
+
} catch (ex) {
|
|
69
|
+
if (db.inTransaction) {
|
|
70
|
+
undo.run();
|
|
71
|
+
if (undo !== rollback) after.run();
|
|
72
|
+
}
|
|
73
|
+
throw ex;
|
|
74
|
+
}
|
|
75
|
+
};
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const { cppdb } = require('../util');
|
|
3
|
+
|
|
4
|
+
exports.prepare = function prepare(sql) {
|
|
5
|
+
return this[cppdb].prepare(sql, this, 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.defaultSafeIntegers = function defaultSafeIntegers(...args) {
|
|
19
|
+
this[cppdb].defaultSafeIntegers(...args);
|
|
20
|
+
return this;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
exports.unsafeMode = function unsafeMode(...args) {
|
|
24
|
+
this[cppdb].unsafeMode(...args);
|
|
25
|
+
return this;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
exports.getters = {
|
|
29
|
+
name: {
|
|
30
|
+
get: function name() { return this[cppdb].name; },
|
|
31
|
+
enumerable: true,
|
|
32
|
+
},
|
|
33
|
+
open: {
|
|
34
|
+
get: function open() { return this[cppdb].open; },
|
|
35
|
+
enumerable: true,
|
|
36
|
+
},
|
|
37
|
+
inTransaction: {
|
|
38
|
+
get: function inTransaction() { return this[cppdb].inTransaction; },
|
|
39
|
+
enumerable: true,
|
|
40
|
+
},
|
|
41
|
+
readonly: {
|
|
42
|
+
get: function readonly() { return this[cppdb].readonly; },
|
|
43
|
+
enumerable: true,
|
|
44
|
+
},
|
|
45
|
+
memory: {
|
|
46
|
+
get: function memory() { return this[cppdb].memory; },
|
|
47
|
+
enumerable: true,
|
|
48
|
+
},
|
|
49
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const descriptor = { value: 'SqliteError', writable: true, enumerable: false, configurable: true };
|
|
3
|
+
|
|
4
|
+
function SqliteError(message, code) {
|
|
5
|
+
if (new.target !== SqliteError) {
|
|
6
|
+
return new SqliteError(message, code);
|
|
7
|
+
}
|
|
8
|
+
if (typeof code !== 'string') {
|
|
9
|
+
throw new TypeError('Expected second argument to be a string');
|
|
10
|
+
}
|
|
11
|
+
Error.call(this, message);
|
|
12
|
+
descriptor.value = '' + message;
|
|
13
|
+
Object.defineProperty(this, 'message', descriptor);
|
|
14
|
+
Error.captureStackTrace(this, SqliteError);
|
|
15
|
+
this.code = code;
|
|
16
|
+
}
|
|
17
|
+
Object.setPrototypeOf(SqliteError, Error);
|
|
18
|
+
Object.setPrototypeOf(SqliteError.prototype, Error.prototype);
|
|
19
|
+
Object.defineProperty(SqliteError.prototype, 'name', descriptor);
|
|
20
|
+
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/package.json
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@rocicorp/zero-sqlite3",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "better-sqlite3 on bedrock",
|
|
5
|
+
"homepage": "https://github.com/rocicorp/zero-sqlite3",
|
|
6
|
+
"author": "Rocicorp",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git://github.com/rocicorp/zero-sqlite3.git"
|
|
10
|
+
},
|
|
11
|
+
"main": "lib/index.js",
|
|
12
|
+
"bin": {
|
|
13
|
+
"zero-sqlite3": "./build/Release/shell"
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"binding.gyp",
|
|
17
|
+
"src/*.[ch]pp",
|
|
18
|
+
"lib/**",
|
|
19
|
+
"deps/**"
|
|
20
|
+
],
|
|
21
|
+
"types": "lib/index.d.ts",
|
|
22
|
+
"dependencies": {
|
|
23
|
+
"bindings": "^1.5.0",
|
|
24
|
+
"prebuild-install": "^7.1.1"
|
|
25
|
+
},
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"chai": "^4.3.8",
|
|
28
|
+
"cli-color": "^2.0.3",
|
|
29
|
+
"fs-extra": "^11.1.1",
|
|
30
|
+
"mocha": "^10.2.0",
|
|
31
|
+
"nodemark": "^0.3.0",
|
|
32
|
+
"prebuild": "^13.0.0",
|
|
33
|
+
"sqlite": "^5.0.1",
|
|
34
|
+
"sqlite3": "^5.1.6"
|
|
35
|
+
},
|
|
36
|
+
"scripts": {
|
|
37
|
+
"install": "prebuild-install || node-gyp rebuild --release",
|
|
38
|
+
"build-release": "node-gyp rebuild --release",
|
|
39
|
+
"build-debug": "node-gyp rebuild --debug",
|
|
40
|
+
"rebuild-release": "npm run lzz && npm run build-release",
|
|
41
|
+
"rebuild-debug": "npm run lzz && npm run build-debug",
|
|
42
|
+
"test": "mocha --exit --slow=75 --timeout=5000",
|
|
43
|
+
"benchmark": "node benchmark",
|
|
44
|
+
"download": "bash ./deps/download.sh",
|
|
45
|
+
"lzz": "lzz -hx hpp -sx cpp -k BETTER_SQLITE3 -d -hl -sl -e ./src/better_sqlite3.lzz"
|
|
46
|
+
},
|
|
47
|
+
"license": "MIT",
|
|
48
|
+
"keywords": [
|
|
49
|
+
"sql",
|
|
50
|
+
"sqlite",
|
|
51
|
+
"sqlite3",
|
|
52
|
+
"transactions",
|
|
53
|
+
"user-defined functions",
|
|
54
|
+
"aggregate functions",
|
|
55
|
+
"window functions",
|
|
56
|
+
"database"
|
|
57
|
+
]
|
|
58
|
+
}
|