Skip to content

fix: make Database.each return the database object for chaining - #633

Open
cpruijsen wants to merge 1 commit into
sql-js:masterfrom
cpruijsen:fix/issue-395
Open

cpruijsen wants to merge 1 commit into
sql-js:masterfrom
cpruijsen:fix/issue-395

Conversation

@cpruijsen

@cpruijsen cpruijsen commented Sep 14, 2026

Copy link
Copy Markdown

Fixes #395.

Database.each in src/api.js returned whatever the done callback returned, or undefined when
no done was passed, while the documentation says it returns the database object for chaining.

This changes the code rather than the documentation, which is the choice the issue leaves open, for
two reasons. The rest of the class already reads that way: run, create_function,
create_aggregate and updateHook all return this and document it as chaining. And no caller
can use the old value for anything, since done's return value is not part of any documented
contract, so the current behaviour is closer to an omission than a feature.

if (typeof done === "function") {
    done();
}
return this;

done is still called, at the same point, with the same arguments. Only its return value stops
being forwarded.

This is a behaviour change on a public method, so it is worth saying plainly what breaks:
code doing var x = db.each(sql, cb, function () { return something; }) gets the database instead
of something. The alternative resolution, correcting the documentation to say undefined, is
a one-line docs change and leaves that code working. This PR takes the first reading because the
documented contract is also the more useful one and matches its siblings; say the word if you
prefer the second and I will send that instead.

The test in test/test_database.js asserts strictEqual(chained, db). I could not run emscripten
here, so I checked it both ways by applying the same two-line change to the shipped unminified
dist/sql-asm-debug.js and running npm run test-asm-debug: it fails on the current code with
db.each returns the database object and passes with the change, 24 passed and 0 failed. That is a
patched artefact rather than a compile of this diff, so CI is the real check.

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 sql-js#395
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Database.each() documented return value doesn't match code

1 participant