@homeofthings/sqlite3 0.0.1-alpha0 → 6.1.1

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.
@@ -0,0 +1,205 @@
1
+ // Type definitions for sqlite3
2
+ // Project: http://github.com/gms1/node-sqlite3
3
+
4
+ /// <reference types="node" />
5
+
6
+ import events = require("events");
7
+
8
+ export const OPEN_READONLY: number;
9
+ export const OPEN_READWRITE: number;
10
+ export const OPEN_CREATE: number;
11
+ export const OPEN_FULLMUTEX: number;
12
+ export const OPEN_SHAREDCACHE: number;
13
+ export const OPEN_PRIVATECACHE: number;
14
+ export const OPEN_URI: number;
15
+
16
+ export const VERSION: string;
17
+ export const SOURCE_ID: string;
18
+ export const VERSION_NUMBER: number;
19
+
20
+ export const OK: number;
21
+ export const ERROR: number;
22
+ export const INTERNAL: number;
23
+ export const PERM: number;
24
+ export const ABORT: number;
25
+ export const BUSY: number;
26
+ export const LOCKED: number;
27
+ export const NOMEM: number;
28
+ export const READONLY: number;
29
+ export const INTERRUPT: number
30
+ export const IOERR: number;
31
+ export const CORRUPT: number
32
+ export const NOTFOUND: number;
33
+ export const FULL: number;
34
+ export const CANTOPEN: number;
35
+ export const PROTOCOL: number;
36
+ export const EMPTY: number;
37
+ export const SCHEMA: number;
38
+ export const TOOBIG: number
39
+ export const CONSTRAINT: number
40
+ export const MISMATCH: number;
41
+ export const MISUSE: number;
42
+ export const NOLFS: number;
43
+ export const AUTH: number
44
+ export const FORMAT: number;
45
+ export const RANGE: number
46
+ export const NOTADB: number;
47
+
48
+ export const LIMIT_LENGTH: number;
49
+ export const LIMIT_SQL_LENGTH: number;
50
+ export const LIMIT_COLUMN: number;
51
+ export const LIMIT_EXPR_DEPTH: number;
52
+ export const LIMIT_COMPOUND_SELECT: number;
53
+ export const LIMIT_VDBE_OP: number;
54
+ export const LIMIT_FUNCTION_ARG: number;
55
+ export const LIMIT_ATTACHED: number;
56
+ export const LIMIT_LIKE_PATTERN_LENGTH: number;
57
+ export const LIMIT_VARIABLE_NUMBER: number;
58
+ export const LIMIT_TRIGGER_DEPTH: number;
59
+ export const LIMIT_WORKER_THREADS: number;
60
+
61
+ export const cached: {
62
+ Database(filename: string, callback?: (this: Database, err: Error | null) => void): Database;
63
+ Database(filename: string, mode?: number, callback?: (this: Database, err: Error | null) => void): Database;
64
+ };
65
+
66
+ export interface RunResult extends Statement {
67
+ lastID: number;
68
+ changes: number;
69
+ }
70
+
71
+ export class Statement extends events.EventEmitter {
72
+ bind(callback?: (err: Error | null) => void): this;
73
+ bind(...params: any[]): this;
74
+
75
+ reset(callback?: (err: null) => void): this;
76
+
77
+ finalize(callback?: (err: Error) => void): Database;
78
+
79
+ run(callback?: (err: Error | null) => void): this;
80
+ run(params: any, callback?: (this: RunResult, err: Error | null) => void): this;
81
+ run(...params: any[]): this;
82
+
83
+ get<T>(callback?: (err: Error | null, row?: T) => void): this;
84
+ get<T>(params: any, callback?: (this: RunResult, err: Error | null, row?: T) => void): this;
85
+ get(...params: any[]): this;
86
+
87
+ all<T>(callback?: (err: Error | null, rows: T[]) => void): this;
88
+ all<T>(params: any, callback?: (this: RunResult, err: Error | null, rows: T[]) => void): this;
89
+ all(...params: any[]): this;
90
+
91
+ each<T>(callback?: (err: Error | null, row: T) => void, complete?: (err: Error | null, count: number) => void): this;
92
+ each<T>(params: any, callback?: (this: RunResult, err: Error | null, row: T) => void, complete?: (err: Error | null, count: number) => void): this;
93
+ each(...params: any[]): this;
94
+ }
95
+
96
+ export class Database extends events.EventEmitter {
97
+ constructor(filename: string, callback?: (err: Error | null) => void);
98
+ constructor(filename: string, mode?: number, callback?: (err: Error | null) => void);
99
+
100
+ close(callback?: (err: Error | null) => void): void;
101
+
102
+ run(sql: string, callback?: (this: RunResult, err: Error | null) => void): this;
103
+ run(sql: string, params: any, callback?: (this: RunResult, err: Error | null) => void): this;
104
+ run(sql: string, ...params: any[]): this;
105
+
106
+ get<T>(sql: string, callback?: (this: Statement, err: Error | null, row: T) => void): this;
107
+ get<T>(sql: string, params: any, callback?: (this: Statement, err: Error | null, row: T) => void): this;
108
+ get(sql: string, ...params: any[]): this;
109
+
110
+ all<T>(sql: string, callback?: (this: Statement, err: Error | null, rows: T[]) => void): this;
111
+ all<T>(sql: string, params: any, callback?: (this: Statement, err: Error | null, rows: T[]) => void): this;
112
+ all(sql: string, ...params: any[]): this;
113
+
114
+ each<T>(sql: string, callback?: (this: Statement, err: Error | null, row: T) => void, complete?: (err: Error | null, count: number) => void): this;
115
+ each<T>(sql: string, params: any, callback?: (this: Statement, err: Error | null, row: T) => void, complete?: (err: Error | null, count: number) => void): this;
116
+ each(sql: string, ...params: any[]): this;
117
+
118
+ exec(sql: string, callback?: (this: Statement, err: Error | null) => void): this;
119
+
120
+ prepare(sql: string, callback?: (this: Statement, err: Error | null) => void): Statement;
121
+ prepare(sql: string, params: any, callback?: (this: Statement, err: Error | null) => void): Statement;
122
+ prepare(sql: string, ...params: any[]): Statement;
123
+
124
+ serialize(callback?: () => void): void;
125
+ parallelize(callback?: () => void): void;
126
+
127
+ on(event: "trace", listener: (sql: string) => void): this;
128
+ on(event: "profile", listener: (sql: string, time: number) => void): this;
129
+ on(event: "change", listener: (type: string, database: string, table: string, rowid: number) => void): this;
130
+ on(event: "error", listener: (err: Error) => void): this;
131
+ on(event: "open" | "close", listener: () => void): this;
132
+ on(event: string, listener: (...args: any[]) => void): this;
133
+
134
+ configure(option: "busyTimeout", value: number): void;
135
+ configure(option: "limit", id: number, value: number): void;
136
+
137
+ loadExtension(filename: string, callback?: (err: Error | null) => void): this;
138
+
139
+ wait(callback?: (param: null) => void): this;
140
+
141
+ interrupt(): void;
142
+ }
143
+
144
+ export function verbose(): sqlite3;
145
+
146
+ export interface sqlite3 {
147
+ OPEN_READONLY: number;
148
+ OPEN_READWRITE: number;
149
+ OPEN_CREATE: number;
150
+ OPEN_FULLMUTEX: number;
151
+ OPEN_SHAREDCACHE: number;
152
+ OPEN_PRIVATECACHE: number;
153
+ OPEN_URI: number;
154
+
155
+ VERSION: string;
156
+ SOURCE_ID: string;
157
+ VERSION_NUMBER: number;
158
+
159
+ OK: number;
160
+ ERROR: number;
161
+ INTERNAL: number;
162
+ PERM: number;
163
+ ABORT: number;
164
+ BUSY: number;
165
+ LOCKED: number;
166
+ NOMEM: number;
167
+ READONLY: number;
168
+ INTERRUPT: number
169
+ IOERR: number;
170
+ CORRUPT: number
171
+ NOTFOUND: number;
172
+ FULL: number;
173
+ CANTOPEN: number;
174
+ PROTOCOL: number;
175
+ EMPTY: number;
176
+ SCHEMA: number;
177
+ TOOBIG: number
178
+ CONSTRAINT: number
179
+ MISMATCH: number;
180
+ MISUSE: number;
181
+ NOLFS: number;
182
+ AUTH: number
183
+ FORMAT: number;
184
+ RANGE: number
185
+ NOTADB: number;
186
+
187
+ LIMIT_LENGTH: number;
188
+ LIMIT_SQL_LENGTH: number;
189
+ LIMIT_COLUMN: number;
190
+ LIMIT_EXPR_DEPTH: number;
191
+ LIMIT_COMPOUND_SELECT: number;
192
+ LIMIT_VDBE_OP: number;
193
+ LIMIT_FUNCTION_ARG: number;
194
+ LIMIT_ATTACHED: number;
195
+ LIMIT_LIKE_PATTERN_LENGTH: number;
196
+ LIMIT_VARIABLE_NUMBER: number;
197
+ LIMIT_TRIGGER_DEPTH: number;
198
+ LIMIT_WORKER_THREADS: number;
199
+
200
+ cached: typeof cached;
201
+ RunResult: RunResult;
202
+ Statement: typeof Statement;
203
+ Database: typeof Database;
204
+ verbose(): this;
205
+ }
package/lib/sqlite3.js ADDED
@@ -0,0 +1,207 @@
1
+ const path = require('path');
2
+ const sqlite3 = require('./sqlite3-binding.js');
3
+ const EventEmitter = require('events').EventEmitter;
4
+ module.exports = exports = sqlite3;
5
+
6
+ function normalizeMethod (fn) {
7
+ return function (sql) {
8
+ let errBack;
9
+ const args = Array.prototype.slice.call(arguments, 1);
10
+
11
+ if (typeof args[args.length - 1] === 'function') {
12
+ const callback = args[args.length - 1];
13
+ errBack = function(err) {
14
+ if (err) {
15
+ callback(err);
16
+ }
17
+ };
18
+ }
19
+ const statement = new Statement(this, sql, errBack);
20
+ return fn.call(this, statement, args);
21
+ };
22
+ }
23
+
24
+ function inherits(target, source) {
25
+ for (const k in source.prototype)
26
+ target.prototype[k] = source.prototype[k];
27
+ }
28
+
29
+ sqlite3.cached = {
30
+ Database: function(file, a, b) {
31
+ if (file === '' || file === ':memory:') {
32
+ // Don't cache special databases.
33
+ return new Database(file, a, b);
34
+ }
35
+
36
+ let db;
37
+ file = path.resolve(file);
38
+
39
+ if (!sqlite3.cached.objects[file]) {
40
+ db = sqlite3.cached.objects[file] = new Database(file, a, b);
41
+ }
42
+ else {
43
+ // Make sure the callback is called.
44
+ db = sqlite3.cached.objects[file];
45
+ const callback = (typeof a === 'number') ? b : a;
46
+ if (typeof callback === 'function') {
47
+ function cb() { callback.call(db, null); }
48
+ if (db.open) process.nextTick(cb);
49
+ else db.once('open', cb);
50
+ }
51
+ }
52
+
53
+ return db;
54
+ },
55
+ objects: {}
56
+ };
57
+
58
+
59
+ const Database = sqlite3.Database;
60
+ const Statement = sqlite3.Statement;
61
+ const Backup = sqlite3.Backup;
62
+
63
+ inherits(Database, EventEmitter);
64
+ inherits(Statement, EventEmitter);
65
+ inherits(Backup, EventEmitter);
66
+
67
+ // Database#prepare(sql, [bind1, bind2, ...], [callback])
68
+ Database.prototype.prepare = normalizeMethod(function(statement, params) {
69
+ return params.length
70
+ ? statement.bind.apply(statement, params)
71
+ : statement;
72
+ });
73
+
74
+ // Database#run(sql, [bind1, bind2, ...], [callback])
75
+ Database.prototype.run = normalizeMethod(function(statement, params) {
76
+ statement.run.apply(statement, params).finalize();
77
+ return this;
78
+ });
79
+
80
+ // Database#get(sql, [bind1, bind2, ...], [callback])
81
+ Database.prototype.get = normalizeMethod(function(statement, params) {
82
+ statement.get.apply(statement, params).finalize();
83
+ return this;
84
+ });
85
+
86
+ // Database#all(sql, [bind1, bind2, ...], [callback])
87
+ Database.prototype.all = normalizeMethod(function(statement, params) {
88
+ statement.all.apply(statement, params).finalize();
89
+ return this;
90
+ });
91
+
92
+ // Database#each(sql, [bind1, bind2, ...], [callback], [complete])
93
+ Database.prototype.each = normalizeMethod(function(statement, params) {
94
+ statement.each.apply(statement, params).finalize();
95
+ return this;
96
+ });
97
+
98
+ Database.prototype.map = normalizeMethod(function(statement, params) {
99
+ statement.map.apply(statement, params).finalize();
100
+ return this;
101
+ });
102
+
103
+ // Database#backup(filename, [callback])
104
+ // Database#backup(filename, destName, sourceName, filenameIsDest, [callback])
105
+ Database.prototype.backup = function() {
106
+ let backup;
107
+ if (arguments.length <= 2) {
108
+ // By default, we write the main database out to the main database of the named file.
109
+ // This is the most likely use of the backup api.
110
+ backup = new Backup(this, arguments[0], 'main', 'main', true, arguments[1]);
111
+ } else {
112
+ // Otherwise, give the user full control over the sqlite3_backup_init arguments.
113
+ backup = new Backup(this, arguments[0], arguments[1], arguments[2], arguments[3], arguments[4]);
114
+ }
115
+ // Per the sqlite docs, exclude the following errors as non-fatal by default.
116
+ backup.retryErrors = [sqlite3.BUSY, sqlite3.LOCKED];
117
+ return backup;
118
+ };
119
+
120
+ Statement.prototype.map = function() {
121
+ const params = Array.prototype.slice.call(arguments);
122
+ const callback = params.pop();
123
+ params.push(function(err, rows) {
124
+ if (err) return callback(err);
125
+ const result = {};
126
+ if (rows.length) {
127
+ const keys = Object.keys(rows[0]);
128
+ const key = keys[0];
129
+ if (keys.length > 2) {
130
+ // Value is an object
131
+ for (let i = 0; i < rows.length; i++) {
132
+ result[rows[i][key]] = rows[i];
133
+ }
134
+ } else {
135
+ const value = keys[1];
136
+ // Value is a plain value
137
+ for (let i = 0; i < rows.length; i++) {
138
+ result[rows[i][key]] = rows[i][value];
139
+ }
140
+ }
141
+ }
142
+ callback(err, result);
143
+ });
144
+ return this.all.apply(this, params);
145
+ };
146
+
147
+ let isVerbose = false;
148
+
149
+ const supportedEvents = [ 'trace', 'profile', 'change' ];
150
+
151
+ Database.prototype.addListener = Database.prototype.on = function(type) {
152
+ const val = EventEmitter.prototype.addListener.apply(this, arguments);
153
+ if (supportedEvents.indexOf(type) >= 0) {
154
+ this.configure(type, true);
155
+ }
156
+ return val;
157
+ };
158
+
159
+ Database.prototype.removeListener = function(type) {
160
+ const val = EventEmitter.prototype.removeListener.apply(this, arguments);
161
+ if (supportedEvents.indexOf(type) >= 0 && !this._events[type]) {
162
+ this.configure(type, false);
163
+ }
164
+ return val;
165
+ };
166
+
167
+ Database.prototype.removeAllListeners = function(type) {
168
+ const val = EventEmitter.prototype.removeAllListeners.apply(this, arguments);
169
+ if (supportedEvents.indexOf(type) >= 0) {
170
+ this.configure(type, false);
171
+ }
172
+ return val;
173
+ };
174
+
175
+ // Save the stack trace over EIO callbacks.
176
+ sqlite3.verbose = function() {
177
+ if (!isVerbose) {
178
+ const trace = require('./trace');
179
+ [
180
+ 'prepare',
181
+ 'get',
182
+ 'run',
183
+ 'all',
184
+ 'each',
185
+ 'map',
186
+ 'close',
187
+ 'exec'
188
+ ].forEach(function (name) {
189
+ trace.extendTrace(Database.prototype, name);
190
+ });
191
+ [
192
+ 'bind',
193
+ 'get',
194
+ 'run',
195
+ 'all',
196
+ 'each',
197
+ 'map',
198
+ 'reset',
199
+ 'finalize',
200
+ ].forEach(function (name) {
201
+ trace.extendTrace(Statement.prototype, name);
202
+ });
203
+ isVerbose = true;
204
+ }
205
+
206
+ return sqlite3;
207
+ };
package/lib/trace.js ADDED
@@ -0,0 +1,38 @@
1
+ // Inspired by https://github.com/tlrobinson/long-stack-traces
2
+ const util = require('util');
3
+
4
+ function extendTrace(object, property, pos) {
5
+ const old = object[property];
6
+ object[property] = function() {
7
+ const error = new Error();
8
+ const name = object.constructor.name + '#' + property + '(' +
9
+ Array.prototype.slice.call(arguments).map(function(el) {
10
+ return util.inspect(el, false, 0);
11
+ }).join(', ') + ')';
12
+
13
+ if (typeof pos === 'undefined') pos = -1;
14
+ if (pos < 0) pos += arguments.length;
15
+ const cb = arguments[pos];
16
+ if (typeof arguments[pos] === 'function') {
17
+ arguments[pos] = function replacement() {
18
+ const err = arguments[0];
19
+ if (err && err.stack && !err.__augmented) {
20
+ err.stack = filter(err).join('\n');
21
+ err.stack += '\n--> in ' + name;
22
+ err.stack += '\n' + filter(error).slice(1).join('\n');
23
+ err.__augmented = true;
24
+ }
25
+ return cb.apply(this, arguments);
26
+ };
27
+ }
28
+ return old.apply(this, arguments);
29
+ };
30
+ }
31
+ exports.extendTrace = extendTrace;
32
+
33
+
34
+ function filter(error) {
35
+ return error.stack.split('\n').filter(function(line) {
36
+ return line.indexOf(__filename) < 0;
37
+ });
38
+ }
package/package.json CHANGED
@@ -1,14 +1,93 @@
1
1
  {
2
2
  "name": "@homeofthings/sqlite3",
3
- "version": "0.0.1-alpha0",
4
3
  "description": "Asynchronous, non-blocking SQLite3 bindings",
5
- "main": "index.js",
4
+ "version": "6.1.1",
5
+ "homepage": "https://github.com/gms1/node-sqlite3",
6
+ "author": {
7
+ "name": "Mapbox",
8
+ "url": "https://mapbox.com/"
9
+ },
10
+ "binary": {
11
+ "napi_versions": [
12
+ 3,
13
+ 6
14
+ ]
15
+ },
16
+ "contributors": [
17
+ "Daniel Lockyer <hi@daniellockyer.com>",
18
+ "Konstantin Käfer <mail@kkaefer.com>",
19
+ "Dane Springmeyer <dane@mapbox.com>",
20
+ "Will White <will@mapbox.com>",
21
+ "Orlando Vazquez <ovazquez@gmail.com>",
22
+ "Artem Kustikov <kustikoff@gmail.com>",
23
+ "Eric Fredricksen <efredricksen@gmail.com>",
24
+ "John Wright <mrjjwright@gmail.com>",
25
+ "Ryan Dahl <ry@tinyclouds.org>",
26
+ "Tom MacWright <tom@mapbox.com>",
27
+ "Carter Thaxton <carter.thaxton@gmail.com>",
28
+ "Audrius Kažukauskas <audrius@neutrino.lt>",
29
+ "Johannes Schauer <josch@pyneo.org>",
30
+ "Nathan Rajlich <nathan@tootallnate.net>",
31
+ "AJ ONeal <coolaj86@gmail.com>",
32
+ "Mithgol",
33
+ "Ben Noordhuis <ben@strongloop.com>"
34
+ ],
35
+ "files": [
36
+ "binding.gyp",
37
+ "deps/",
38
+ "lib/*.js",
39
+ "lib/*.d.ts",
40
+ "src/"
41
+ ],
6
42
  "repository": {
7
43
  "type": "git",
8
44
  "url": "https://github.com/gms1/node-sqlite3.git"
9
45
  },
46
+ "dependencies": {
47
+ "bindings": "^1.5.0",
48
+ "node-addon-api": "^8.0.0",
49
+ "prebuild-install": "^7.1.3",
50
+ "tar": "^7.5.10"
51
+ },
52
+ "devDependencies": {
53
+ "eslint": "8.56.0",
54
+ "mocha": "10.2.0",
55
+ "prebuild": "13.0.1",
56
+ "tinybench": "^2.9.0"
57
+ },
58
+ "peerDependencies": {
59
+ "node-gyp": "12.x"
60
+ },
61
+ "peerDependenciesMeta": {
62
+ "node-gyp": {
63
+ "optional": true
64
+ }
65
+ },
66
+ "optionalDependencies": {
67
+ "node-gyp": "12.x"
68
+ },
69
+ "engines": {
70
+ "node": ">=20.17.0"
71
+ },
72
+ "scripts": {
73
+ "install": "prebuild-install -r napi || node-gyp rebuild",
74
+ "prebuild": "prebuild --runtime napi --all --verbose",
75
+ "rebuild": "node-gyp rebuild",
76
+ "upload": "prebuild --verbose --prerelease",
77
+ "frozen-install": "yarn install --frozen-lockfile",
78
+ "lint": "eslint .eslintrc.js lib/ test/ tools/",
79
+ "test": "node test/support/createdb.js && mocha -R spec --timeout 480000"
80
+ },
10
81
  "license": "BSD-3-Clause",
11
82
  "publishConfig": {
12
83
  "access": "public"
13
- }
84
+ },
85
+ "keywords": [
86
+ "sql",
87
+ "sqlite",
88
+ "sqlite3",
89
+ "database"
90
+ ],
91
+ "main": "./lib/sqlite3",
92
+ "types": "./lib/sqlite3.d.ts"
14
93
  }
package/src/async.h ADDED
@@ -0,0 +1,76 @@
1
+ #ifndef NODE_SQLITE3_SRC_ASYNC_H
2
+ #define NODE_SQLITE3_SRC_ASYNC_H
3
+
4
+ #include <napi.h>
5
+ #include <uv.h>
6
+
7
+ #include "threading.h"
8
+
9
+ // Generic uv_async handler.
10
+ template <class Item, class Parent> class Async {
11
+ typedef void (*Callback)(Parent* parent, Item* item);
12
+
13
+ protected:
14
+ uv_async_t watcher;
15
+ NODE_SQLITE3_MUTEX_t
16
+ std::vector<Item*> data;
17
+ Callback callback;
18
+ public:
19
+ Parent* parent;
20
+
21
+ public:
22
+ Async(Parent* parent_, Callback cb_)
23
+ : callback(cb_), parent(parent_) {
24
+ watcher.data = this;
25
+ NODE_SQLITE3_MUTEX_INIT
26
+ uv_loop_t *loop;
27
+ napi_get_uv_event_loop(parent_->Env(), &loop);
28
+ uv_async_init(loop, &watcher, reinterpret_cast<uv_async_cb>(listener));
29
+ }
30
+
31
+ static void listener(uv_async_t* handle) {
32
+ auto* async = static_cast<Async*>(handle->data);
33
+ std::vector<Item*> rows;
34
+ NODE_SQLITE3_MUTEX_LOCK(&async->mutex)
35
+ rows.swap(async->data);
36
+ NODE_SQLITE3_MUTEX_UNLOCK(&async->mutex)
37
+ for(auto row : rows)
38
+ async->callback(async->parent, row);
39
+ }
40
+
41
+ static void close(uv_handle_t* handle) {
42
+ assert(handle != NULL);
43
+ assert(handle->data != NULL);
44
+ auto* async = static_cast<Async*>(handle->data);
45
+ delete async;
46
+ }
47
+
48
+ void finish() {
49
+ // Need to call the listener again to ensure all items have been
50
+ // processed. Is this a bug in uv_async? Feels like uv_close
51
+ // should handle that.
52
+ listener(&watcher);
53
+ uv_close((uv_handle_t*)&watcher, close);
54
+ }
55
+
56
+ void add(Item* item) {
57
+ NODE_SQLITE3_MUTEX_LOCK(&mutex);
58
+ data.emplace_back(item);
59
+ NODE_SQLITE3_MUTEX_UNLOCK(&mutex)
60
+ }
61
+
62
+ void send() {
63
+ uv_async_send(&watcher);
64
+ }
65
+
66
+ void send(Item* item) {
67
+ add(item);
68
+ send();
69
+ }
70
+
71
+ ~Async() {
72
+ NODE_SQLITE3_MUTEX_DESTROY
73
+ }
74
+ };
75
+
76
+ #endif