From e0a327a5a36f26542467571dcaa3b7cec3fd55a8 Mon Sep 17 00:00:00 2001 From: Christopher Pruijsen Date: Sun, 13 Sep 2026 08:52:06 +0000 Subject: [PATCH] fix: make Database.each return the database object for chaining The documented return value of Database.each() is the Database instance, but the implementation returned the done callback's result or undefined. Call done() and return this instead, matching the documentation and the convention used by run(), create_function(), create_aggregate(), and updateHook(). Fixes #395 --- src/api.js | 4 ++-- test/test_database.js | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/api.js b/src/api.js index 126baf1c..d34320a7 100644 --- a/src/api.js +++ b/src/api.js @@ -1035,9 +1035,9 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() { stmt.free(); } if (typeof done === "function") { - return done(); + done(); } - return undefined; + return this; }; /** Prepare an SQL statement diff --git a/test/test_database.js b/test/test_database.js index 1759749b..8ab19eec 100644 --- a/test/test_database.js +++ b/test/test_database.js @@ -43,6 +43,8 @@ exports.test = function(SQL, assert, done) { // Testing db.each db.run("CREATE TABLE test (a,b); INSERT INTO test VALUES (1,'a'),(2,'b')"); var count = 0, finished = false; + var chained = db.each("SELECT * FROM test", function () {}, function () {}); + assert.strictEqual(chained, db, "db.each returns the database object"); db.each("SELECT * FROM test ORDER BY a", function callback (row){ count++; if (count === 1) assert.deepEqual(row, {a:1,b:'a'}, 'db.each returns the correct 1st row');