Skip to content

Commit 71faf68

Browse files
committed
src, doc, test: allow passing name of database to createSession
1 parent 1948ee0 commit 71faf68

File tree

3 files changed

+42
-6
lines changed

3 files changed

+42
-6
lines changed

doc/api/sqlite.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ around [`sqlite3_prepare_v2()`][].
158158
* `options` {Object} An optional object used to configure the session.
159159
* `table` {string} When provided, only changes to this table are tracked by the created session.
160160
By default, changes to all tables are tracked.
161+
* `db` {string} Name of the database. Default: `'main'`.
161162
* Returns: {Session} A session handle.
162163

163164
Creates and attackes a session to the database. This method is a wrapper around [`sqlite3session_create()`][] and [`sqlite3session_attach()`][].

src/node_sqlite.cc

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ void DatabaseSync::Exec(const FunctionCallbackInfo<Value>& args) {
224224

225225
void DatabaseSync::CreateSession(const FunctionCallbackInfo<Value>& args) {
226226
std::string table;
227+
std::string dbName = "main";
227228

228229
Environment* env = Environment::GetCurrent(args);
229230
if (args.Length() > 0) {
@@ -253,16 +254,33 @@ void DatabaseSync::CreateSession(const FunctionCallbackInfo<Value>& args) {
253254
return;
254255
}
255256
}
257+
258+
Local<String> db_key = String::NewFromUtf8(
259+
env->isolate(),
260+
"db",
261+
v8::NewStringType::kNormal).ToLocalChecked();
262+
if (options->HasOwnProperty(env->context(), db_key).FromJust()) {
263+
Local<Value> db_value = options->Get(
264+
env->context(),
265+
db_key).ToLocalChecked();
266+
if (db_value->IsString()) {
267+
String::Utf8Value str(env->isolate(), db_value);
268+
dbName = std::string(*str);
269+
} else {
270+
node::THROW_ERR_INVALID_ARG_TYPE(
271+
env->isolate(), "The \"options.db\" argument must be a string.");
272+
return;
273+
}
274+
}
256275
}
257276

258277
DatabaseSync* db;
259278
ASSIGN_OR_RETURN_UNWRAP(&db, args.This());
260279
THROW_AND_RETURN_ON_BAD_STATE(
261280
env, db->connection_ == nullptr, "database is not open");
262281

263-
const char* zDb = "main"; // TODO(louwers): take parameter
264282
sqlite3_session* pSession;
265-
int r = sqlite3session_create(db->connection_, zDb, &pSession);
283+
int r = sqlite3session_create(db->connection_, dbName.c_str(), &pSession);
266284
CHECK_ERROR_OR_THROW(env->isolate(), db->connection_, r, SQLITE_OK, void());
267285
db->sessions_.insert(pSession);
268286

test/parallel/test-sqlite.js

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -901,8 +901,8 @@ suite('session extension', () => {
901901
return {
902902
database2,
903903
changeset: session.changeset()
904-
}
905-
}
904+
};
905+
};
906906

907907
test('conflict while applying changeset (default abort)', (t) => {
908908
const { database2, changeset } = prepareConflict();
@@ -934,7 +934,7 @@ suite('session extension', () => {
934934
t.assert.strictEqual(result, true);
935935
t.assert.deepStrictEqual(
936936
database2.prepare('SELECT value from data ORDER BY key').all(),
937-
[{ value: 'hello'}, { value: 'foo' }]); // replaced
937+
[{ value: 'hello' }, { value: 'foo' }]); // replaced
938938
});
939939

940940
test('conflict while applying changeset (omit)', (t) => {
@@ -946,7 +946,7 @@ suite('session extension', () => {
946946
t.assert.strictEqual(result, true);
947947
t.assert.deepStrictEqual(
948948
database2.prepare('SELECT value from data ORDER BY key ASC').all(),
949-
[{ value: 'world'}, { value: 'foo' }]); // conflicting change omitted
949+
[{ value: 'world' }, { value: 'foo' }]); // Conflicting change omitted
950950
});
951951

952952
test('constants are defined', (t) => {
@@ -979,4 +979,21 @@ suite('session extension', () => {
979979
// Expect 5 rows since these changes where not filtered out
980980
t.assert.strictEqual(data2Rows.length, 5);
981981
});
982+
983+
test('specify other database', (t) => {
984+
const database = new DatabaseSync(':memory:');
985+
const session = database.createSession();
986+
const sessionMain = database.createSession({
987+
db: 'main'
988+
});
989+
const sessionTest = database.createSession({
990+
db: 'test'
991+
});
992+
database.exec('CREATE TABLE data (key INTEGER PRIMARY KEY)');
993+
database.exec('INSERT INTO data (key) VALUES (1)');
994+
t.assert.notStrictEqual(session.changeset().length, 0);
995+
t.assert.notStrictEqual(sessionMain.changeset().length, 0);
996+
// Since this session is attached to a different database, its changeset should be empty
997+
t.assert.strictEqual(sessionTest.changeset().length, 0);
998+
});
982999
});

0 commit comments

Comments
 (0)