diff --git a/src/node_sqlite.cc b/src/node_sqlite.cc index fb229e19fd6aef..4d24b998d0aec5 100644 --- a/src/node_sqlite.cc +++ b/src/node_sqlite.cc @@ -2257,10 +2257,11 @@ bool StatementSync::BindParams(const FunctionCallbackInfo& args) { bool StatementSync::BindValue(const Local& value, const int index) { // SQLite only supports a subset of JavaScript types. Some JS types such as - // functions don't make sense to support. Other JS types such as booleans and + // functions don't make sense to support. Other JS types such as // Dates could be supported by converting them to numbers. However, there // would not be a good way to read the values back from SQLite with the - // original type. + // original type. We make an exception for JS Boolean binding to 1 and 0 + // because SQLite maps true and false keywords to 1 and 0. Isolate* isolate = env()->isolate(); int r; if (value->IsNumber()) { @@ -2293,6 +2294,8 @@ bool StatementSync::BindValue(const Local& value, const int index) { buf.data(), static_cast(buf.length()), SQLITE_TRANSIENT); + } else if (value->IsBoolean()) { + r = sqlite3_bind_int(statement_, index, value->IsTrue() ? 1 : 0); } else if (value->IsBigInt()) { bool lossless; int64_t as_int = value.As()->Int64Value(&lossless); diff --git a/test/parallel/test-sqlite-data-types.js b/test/parallel/test-sqlite-data-types.js index 26af15a777d234..90bd84d4c6f9e5 100644 --- a/test/parallel/test-sqlite-data-types.js +++ b/test/parallel/test-sqlite-data-types.js @@ -46,6 +46,10 @@ suite('data binding and mapping', () => { stmt.run(4, 99n, 0xf, '', new Uint8Array()), { changes: 1, lastInsertRowid: 4 }, ); + t.assert.deepStrictEqual( + stmt.run(5, true, false, true, null), + { changes: 1, lastInsertRowid: 5 }, + ); const query = db.prepare('SELECT * FROM types WHERE key = ?'); t.assert.deepStrictEqual(query.get(1), { @@ -80,6 +84,14 @@ suite('data binding and mapping', () => { text: '', buf: new Uint8Array(), }); + t.assert.deepStrictEqual(query.get(5), { + __proto__: null, + key: 5, + int: 1, + double: 0, + text: '1', + buf: null, + }); }); test('large strings are bound correctly', (t) => {