@barakxyz/better-sqlite3 12.6.2 → 12.8.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/lib/database.js +2 -0
- package/lib/methods/backupFrom.js +31 -0
- package/lib/methods/deserialize.js +34 -0
- package/package.json +1 -1
- package/src/objects/database.cpp +172 -0
- package/src/objects/database.hpp +2 -0
- package/src/objects/session.cpp +19 -0
- package/src/objects/session.hpp +1 -0
package/lib/database.js
CHANGED
|
@@ -76,7 +76,9 @@ Database.prototype.prepare = wrappers.prepare;
|
|
|
76
76
|
Database.prototype.transaction = require('./methods/transaction');
|
|
77
77
|
Database.prototype.pragma = require('./methods/pragma');
|
|
78
78
|
Database.prototype.backup = require('./methods/backup');
|
|
79
|
+
Database.prototype.backupFrom = require('./methods/backupFrom');
|
|
79
80
|
Database.prototype.serialize = require('./methods/serialize');
|
|
81
|
+
Database.prototype.deserialize = require('./methods/deserialize');
|
|
80
82
|
Database.prototype.function = require('./methods/function');
|
|
81
83
|
Database.prototype.aggregate = require('./methods/aggregate');
|
|
82
84
|
Database.prototype.table = require('./methods/table');
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const { cppdb } = require('../util');
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Synchronous backup from another database to this database.
|
|
6
|
+
* Matches wa-sqlite's sqlite3.backup(dest, destName, source, sourceName) pattern.
|
|
7
|
+
*
|
|
8
|
+
* @param {Database} sourceDb - The source Database instance to backup from
|
|
9
|
+
* @param {string} [destName='main'] - The attached database name for destination
|
|
10
|
+
* @param {string} [srcName='main'] - The attached database name for source
|
|
11
|
+
* @returns {this} The database instance for chaining
|
|
12
|
+
*/
|
|
13
|
+
module.exports = function backupFrom(sourceDb, destName, srcName) {
|
|
14
|
+
// Validate sourceDb is a Database instance (must have cppdb symbol)
|
|
15
|
+
if (sourceDb == null || typeof sourceDb !== 'object' || !sourceDb[cppdb]) {
|
|
16
|
+
throw new TypeError('Expected first argument to be a Database instance');
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// Validate optional string arguments
|
|
20
|
+
if (destName !== undefined && typeof destName !== 'string') {
|
|
21
|
+
throw new TypeError('Expected second argument to be a string');
|
|
22
|
+
}
|
|
23
|
+
if (srcName !== undefined && typeof srcName !== 'string') {
|
|
24
|
+
throw new TypeError('Expected third argument to be a string');
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// Pass the native cppdb object from source, not the JS wrapper
|
|
28
|
+
// The C++ side unwraps it to get the Database pointer
|
|
29
|
+
this[cppdb].backupFrom(sourceDb[cppdb], destName, srcName);
|
|
30
|
+
return this;
|
|
31
|
+
};
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const { cppdb } = require('../util');
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Deserialize a buffer into the database.
|
|
6
|
+
*
|
|
7
|
+
* Uses the safe pattern (matches wa-sqlite's import pattern):
|
|
8
|
+
* 1. Create temp in-memory database
|
|
9
|
+
* 2. Deserialize buffer into temp
|
|
10
|
+
* 3. Backup from temp to current database
|
|
11
|
+
* 4. Close temp database
|
|
12
|
+
*
|
|
13
|
+
* This preserves file-based persistence and works with both in-memory and file databases.
|
|
14
|
+
*
|
|
15
|
+
* @param {Buffer} buffer - The serialized database buffer
|
|
16
|
+
* @param {Object} [options] - Options object
|
|
17
|
+
* @param {string} [options.attached='main'] - The attached database name
|
|
18
|
+
* @returns {this} The database instance for chaining
|
|
19
|
+
*/
|
|
20
|
+
module.exports = function deserialize(buffer, options) {
|
|
21
|
+
if (options == null) options = {};
|
|
22
|
+
|
|
23
|
+
// Validate arguments
|
|
24
|
+
if (!Buffer.isBuffer(buffer)) throw new TypeError('Expected first argument to be a buffer');
|
|
25
|
+
if (typeof options !== 'object') throw new TypeError('Expected second argument to be an options object');
|
|
26
|
+
|
|
27
|
+
// Interpret and validate options
|
|
28
|
+
const attachedName = 'attached' in options ? options.attached : 'main';
|
|
29
|
+
if (typeof attachedName !== 'string') throw new TypeError('Expected the "attached" option to be a string');
|
|
30
|
+
if (!attachedName) throw new TypeError('The "attached" option cannot be an empty string');
|
|
31
|
+
|
|
32
|
+
this[cppdb].deserialize(buffer, attachedName);
|
|
33
|
+
return this; // Return the JS Database instance for chaining
|
|
34
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@barakxyz/better-sqlite3",
|
|
3
|
-
"version": "12.
|
|
3
|
+
"version": "12.8.0",
|
|
4
4
|
"description": "The fastest and simplest library for SQLite in Node.js. Fork with session extension enabled.",
|
|
5
5
|
"homepage": "https://github.com/BarakXYZ/better-sqlite3",
|
|
6
6
|
"author": "Joshua Wise <joshuathomaswise@gmail.com>",
|
package/src/objects/database.cpp
CHANGED
|
@@ -131,7 +131,9 @@ INIT(Database::Init) {
|
|
|
131
131
|
SetPrototypeMethod(isolate, data, t, "prepare", JS_prepare);
|
|
132
132
|
SetPrototypeMethod(isolate, data, t, "exec", JS_exec);
|
|
133
133
|
SetPrototypeMethod(isolate, data, t, "backup", JS_backup);
|
|
134
|
+
SetPrototypeMethod(isolate, data, t, "backupFrom", JS_backupFrom);
|
|
134
135
|
SetPrototypeMethod(isolate, data, t, "serialize", JS_serialize);
|
|
136
|
+
SetPrototypeMethod(isolate, data, t, "deserialize", JS_deserialize);
|
|
135
137
|
SetPrototypeMethod(isolate, data, t, "function", JS_function);
|
|
136
138
|
SetPrototypeMethod(isolate, data, t, "aggregate", JS_aggregate);
|
|
137
139
|
SetPrototypeMethod(isolate, data, t, "table", JS_table);
|
|
@@ -295,6 +297,176 @@ NODE_METHOD(Database::JS_serialize) {
|
|
|
295
297
|
);
|
|
296
298
|
}
|
|
297
299
|
|
|
300
|
+
NODE_METHOD(Database::JS_backupFrom) {
|
|
301
|
+
// Synchronous backup from another database to this database.
|
|
302
|
+
// Matches wa-sqlite's sqlite3.backup(dest, destName, source, sourceName) pattern.
|
|
303
|
+
// Uses SQLite backup API: sqlite3_backup_init, sqlite3_backup_step, sqlite3_backup_finish.
|
|
304
|
+
|
|
305
|
+
Database* destDb = Unwrap<Database>(info.This());
|
|
306
|
+
REQUIRE_DATABASE_OPEN(destDb);
|
|
307
|
+
REQUIRE_DATABASE_NOT_BUSY(destDb);
|
|
308
|
+
REQUIRE_DATABASE_NO_ITERATORS(destDb);
|
|
309
|
+
|
|
310
|
+
UseAddon;
|
|
311
|
+
UseIsolate;
|
|
312
|
+
|
|
313
|
+
// First argument: source Database object
|
|
314
|
+
// Validate using wa-sqlite's verifyDatabase pattern: check if pointer is in tracked set
|
|
315
|
+
if (info.Length() < 1 || !info[0]->IsObject()) {
|
|
316
|
+
ThrowTypeError("Expected first argument to be a Database instance");
|
|
317
|
+
return;
|
|
318
|
+
}
|
|
319
|
+
v8::Local<v8::Object> srcObj = info[0].As<v8::Object>();
|
|
320
|
+
if (srcObj->InternalFieldCount() < 1) {
|
|
321
|
+
ThrowTypeError("Expected first argument to be a Database instance");
|
|
322
|
+
return;
|
|
323
|
+
}
|
|
324
|
+
Database* srcDb = Unwrap<Database>(srcObj);
|
|
325
|
+
if (addon->dbs.find(srcDb) == addon->dbs.end()) {
|
|
326
|
+
ThrowTypeError("Expected first argument to be a Database instance");
|
|
327
|
+
return;
|
|
328
|
+
}
|
|
329
|
+
if (!srcDb->open) {
|
|
330
|
+
ThrowTypeError("The source database is closed");
|
|
331
|
+
return;
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
// Optional second argument: attached database name for destination (default "main")
|
|
335
|
+
v8::Local<v8::String> destName;
|
|
336
|
+
if (info.Length() > 1 && !info[1]->IsUndefined()) {
|
|
337
|
+
if (!info[1]->IsString()) {
|
|
338
|
+
ThrowTypeError("Expected second argument to be a string");
|
|
339
|
+
return;
|
|
340
|
+
}
|
|
341
|
+
destName = info[1].As<v8::String>();
|
|
342
|
+
} else {
|
|
343
|
+
destName = StringFromUtf8(isolate, "main", -1);
|
|
344
|
+
}
|
|
345
|
+
v8::String::Utf8Value dest_name(isolate, destName);
|
|
346
|
+
|
|
347
|
+
// Optional third argument: attached database name for source (default "main")
|
|
348
|
+
v8::Local<v8::String> srcName;
|
|
349
|
+
if (info.Length() > 2 && !info[2]->IsUndefined()) {
|
|
350
|
+
if (!info[2]->IsString()) {
|
|
351
|
+
ThrowTypeError("Expected third argument to be a string");
|
|
352
|
+
return;
|
|
353
|
+
}
|
|
354
|
+
srcName = info[2].As<v8::String>();
|
|
355
|
+
} else {
|
|
356
|
+
srcName = StringFromUtf8(isolate, "main", -1);
|
|
357
|
+
}
|
|
358
|
+
v8::String::Utf8Value src_name(isolate, srcName);
|
|
359
|
+
|
|
360
|
+
// Initialize backup: sqlite3_backup_init(dest, destName, source, sourceName)
|
|
361
|
+
sqlite3_backup* backup_handle = sqlite3_backup_init(destDb->db_handle, *dest_name, srcDb->db_handle, *src_name);
|
|
362
|
+
if (backup_handle == NULL) {
|
|
363
|
+
destDb->ThrowDatabaseError();
|
|
364
|
+
return;
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
// Copy all pages in one step (-1 = all pages)
|
|
368
|
+
int status = sqlite3_backup_step(backup_handle, -1);
|
|
369
|
+
sqlite3_backup_finish(backup_handle);
|
|
370
|
+
|
|
371
|
+
if (status != SQLITE_DONE) {
|
|
372
|
+
ThrowSqliteError(addon, sqlite3_errstr(status), status);
|
|
373
|
+
return;
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
info.GetReturnValue().Set(info.This());
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
NODE_METHOD(Database::JS_deserialize) {
|
|
380
|
+
// Deserialize a buffer into the current database using the safe pattern:
|
|
381
|
+
// 1. Create temp in-memory database
|
|
382
|
+
// 2. Deserialize buffer into temp
|
|
383
|
+
// 3. Backup from temp to current database
|
|
384
|
+
// 4. Close temp database
|
|
385
|
+
//
|
|
386
|
+
// This preserves file-based persistence and matches wa-sqlite's import pattern.
|
|
387
|
+
|
|
388
|
+
Database* db = Unwrap<Database>(info.This());
|
|
389
|
+
REQUIRE_DATABASE_OPEN(db);
|
|
390
|
+
REQUIRE_DATABASE_NOT_BUSY(db);
|
|
391
|
+
REQUIRE_DATABASE_NO_ITERATORS(db);
|
|
392
|
+
|
|
393
|
+
UseAddon;
|
|
394
|
+
UseIsolate;
|
|
395
|
+
|
|
396
|
+
// First argument: buffer containing serialized database
|
|
397
|
+
v8::Local<v8::Value> bufferArg = info[0];
|
|
398
|
+
if (!node::Buffer::HasInstance(bufferArg)) {
|
|
399
|
+
ThrowTypeError("Expected first argument to be a buffer");
|
|
400
|
+
return;
|
|
401
|
+
}
|
|
402
|
+
v8::Local<v8::Object> buffer = bufferArg.As<v8::Object>();
|
|
403
|
+
|
|
404
|
+
// Second argument: attached database name (default "main")
|
|
405
|
+
v8::Local<v8::String> attachedName;
|
|
406
|
+
if (info.Length() > 1 && !info[1]->IsUndefined()) {
|
|
407
|
+
if (!info[1]->IsString()) {
|
|
408
|
+
ThrowTypeError("Expected second argument to be a string");
|
|
409
|
+
return;
|
|
410
|
+
}
|
|
411
|
+
attachedName = info[1].As<v8::String>();
|
|
412
|
+
} else {
|
|
413
|
+
attachedName = StringFromUtf8(isolate, "main", -1);
|
|
414
|
+
}
|
|
415
|
+
v8::String::Utf8Value attached_name(isolate, attachedName);
|
|
416
|
+
|
|
417
|
+
// Step 1: Create a temporary in-memory database
|
|
418
|
+
sqlite3* temp_handle;
|
|
419
|
+
int status = sqlite3_open_v2(":memory:", &temp_handle, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
|
|
420
|
+
if (status != SQLITE_OK) {
|
|
421
|
+
ThrowSqliteError(addon, "Failed to create temporary database", status);
|
|
422
|
+
return;
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
// Step 2: Deserialize the buffer into the temporary database
|
|
426
|
+
size_t length = node::Buffer::Length(buffer);
|
|
427
|
+
unsigned char* data = (unsigned char*)sqlite3_malloc64(length);
|
|
428
|
+
unsigned int flags = SQLITE_DESERIALIZE_FREEONCLOSE | SQLITE_DESERIALIZE_RESIZEABLE;
|
|
429
|
+
|
|
430
|
+
if (length) {
|
|
431
|
+
if (!data) {
|
|
432
|
+
sqlite3_close(temp_handle);
|
|
433
|
+
ThrowError("Out of memory");
|
|
434
|
+
return;
|
|
435
|
+
}
|
|
436
|
+
memcpy(data, node::Buffer::Data(buffer), length);
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
status = sqlite3_deserialize(temp_handle, "main", data, length, length, flags);
|
|
440
|
+
if (status != SQLITE_OK) {
|
|
441
|
+
sqlite3_close(temp_handle);
|
|
442
|
+
ThrowSqliteError(addon, status == SQLITE_ERROR ? "unable to deserialize database" : sqlite3_errstr(status), status);
|
|
443
|
+
return;
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
// Step 3: Backup from temp to current database (matches wa-sqlite backup pattern)
|
|
447
|
+
// sqlite3_backup_init(dest, destName, source, sourceName)
|
|
448
|
+
sqlite3_backup* backup_handle = sqlite3_backup_init(db->db_handle, *attached_name, temp_handle, "main");
|
|
449
|
+
if (backup_handle == NULL) {
|
|
450
|
+
sqlite3_close(temp_handle);
|
|
451
|
+
db->ThrowDatabaseError();
|
|
452
|
+
return;
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
// Copy all pages in one step (-1 = all pages)
|
|
456
|
+
status = sqlite3_backup_step(backup_handle, -1);
|
|
457
|
+
sqlite3_backup_finish(backup_handle);
|
|
458
|
+
|
|
459
|
+
// Step 4: Close temporary database
|
|
460
|
+
sqlite3_close(temp_handle);
|
|
461
|
+
|
|
462
|
+
if (status != SQLITE_DONE) {
|
|
463
|
+
ThrowSqliteError(addon, "Failed to restore database from buffer", status);
|
|
464
|
+
return;
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
info.GetReturnValue().Set(info.This());
|
|
468
|
+
}
|
|
469
|
+
|
|
298
470
|
NODE_METHOD(Database::JS_function) {
|
|
299
471
|
Database* db = Unwrap<Database>(info.This());
|
|
300
472
|
REQUIRE_ARGUMENT_FUNCTION(first, v8::Local<v8::Function> fn);
|
package/src/objects/database.hpp
CHANGED
|
@@ -80,7 +80,9 @@ private:
|
|
|
80
80
|
static NODE_METHOD(JS_prepare);
|
|
81
81
|
static NODE_METHOD(JS_exec);
|
|
82
82
|
static NODE_METHOD(JS_backup);
|
|
83
|
+
static NODE_METHOD(JS_backupFrom);
|
|
83
84
|
static NODE_METHOD(JS_serialize);
|
|
85
|
+
static NODE_METHOD(JS_deserialize);
|
|
84
86
|
static NODE_METHOD(JS_function);
|
|
85
87
|
static NODE_METHOD(JS_aggregate);
|
|
86
88
|
static NODE_METHOD(JS_table);
|
package/src/objects/session.cpp
CHANGED
|
@@ -30,6 +30,7 @@ INIT(Session::Init) {
|
|
|
30
30
|
v8::Local<v8::FunctionTemplate> t = NewConstructorTemplate(isolate, data, JS_new, "Session");
|
|
31
31
|
SetPrototypeMethod(isolate, data, t, "attach", JS_attach);
|
|
32
32
|
SetPrototypeMethod(isolate, data, t, "changeset", JS_changeset);
|
|
33
|
+
SetPrototypeMethod(isolate, data, t, "enable", JS_enable);
|
|
33
34
|
SetPrototypeMethod(isolate, data, t, "close", JS_close);
|
|
34
35
|
return t->GetFunction(OnlyContext).ToLocalChecked();
|
|
35
36
|
}
|
|
@@ -92,6 +93,24 @@ NODE_METHOD(Session::JS_attach) {
|
|
|
92
93
|
}
|
|
93
94
|
}
|
|
94
95
|
|
|
96
|
+
NODE_METHOD(Session::JS_enable) {
|
|
97
|
+
Session* session = Unwrap<Session>(info.This());
|
|
98
|
+
if (!session->alive) return ThrowTypeError("The session has been closed");
|
|
99
|
+
REQUIRE_DATABASE_OPEN(session->db->GetState());
|
|
100
|
+
|
|
101
|
+
UseIsolate;
|
|
102
|
+
|
|
103
|
+
// Require a boolean argument (matches wa-sqlite API pattern)
|
|
104
|
+
if (info.Length() < 1 || !info[0]->IsBoolean()) {
|
|
105
|
+
return ThrowTypeError("Expected first argument to be a boolean");
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
int enable_flag = info[0]->BooleanValue(isolate) ? 1 : 0;
|
|
109
|
+
sqlite3session_enable(session->session_handle, enable_flag);
|
|
110
|
+
|
|
111
|
+
// Return void (matches wa-sqlite API)
|
|
112
|
+
}
|
|
113
|
+
|
|
95
114
|
// Custom destructor for sqlite3_free
|
|
96
115
|
static void FreeSqliteMemory(char* data, void* hint) {
|
|
97
116
|
sqlite3_free(data);
|