Title: sqlite: add `reader` to `Statement`, aligned with `better-sqlite3` · Issue #67352 · nodejs/node · GitHub
Open Graph Title: sqlite: add `reader` to `Statement`, aligned with `better-sqlite3` · Issue #67352 · nodejs/node
X Title: sqlite: add `reader` to `Statement`, aligned with `better-sqlite3` · Issue #67352 · nodejs/node
Description: What is the problem this feature will solve? Hey 👋 Currently, the only way to tell whether a prepared Statement returns rows is statement.columns().length > 0, which is great when you also need the column metadata, except when all you ne...
Open Graph Description: What is the problem this feature will solve? Hey 👋 Currently, the only way to tell whether a prepared Statement returns rows is statement.columns().length > 0, which is great when you also need the...
X Description: What is the problem this feature will solve? Hey 👋 Currently, the only way to tell whether a prepared Statement returns rows is statement.columns().length > 0, which is great when you also need ...
Opengraph URL: https://github.com/nodejs/node/issues/67352
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"sqlite: add `reader` to `Statement`, aligned with `better-sqlite3`","articleBody":"### What is the problem this feature will solve?\n\nHey 👋\n\nCurrently, the only way to tell whether a prepared `Statement` returns rows is `statement.columns().length > 0`, which is great when you also need the column metadata, except when all you need is a yes/no. `columns()` builds a metadata object per column, so it gets slower the wider the result set is.\n\nQuery builders and ORMs need this answer on every execution, to pick `all()` vs `run()`. Kysely does it for raw SQL, where `insert ... returning` and friends can't be classified from the query alone (https://github.com/kysely-org/kysely/issues/1292). Knex, TypeORM and Prisma 7 do it for every query:\n\nhttps://github.com/knex/knex/blob/e25d54bcb707714a17f5a5744eba5c4246bb4d1d/lib/dialects/better-sqlite3/index.js#L51\nhttps://github.com/typeorm/typeorm/blob/5a572a40bae2f85a1d5f7b87a0aa88bbbaec5ab3/packages/typeorm/src/driver/better-sqlite3/BetterSqlite3QueryRunner.ts#L122\nhttps://github.com/prisma/prisma/blob/0b596ffecfcc55e37d6a7095c03193e2d28b8090/packages/adapter-better-sqlite3/src/better-sqlite3.ts#L110\n\nThese integrations were written against `better-sqlite3`, which exposes it as a `reader` boolean, computed once at prepare time from `sqlite3_column_count`:\n\nhttps://github.com/WiseLibs/better-sqlite3/blob/dbc2ea1165fef1f599b9be12faea33fa5e9d7ffb/src/objects/statement.cpp#L142-L155\n\nOn `node:sqlite`, they're left with paying for `columns()` or guessing. Prisma 8 calls `columns()` on every write, and MikroORM 7 fakes `reader` with regexes over the SQL text:\n\nhttps://github.com/prisma/prisma/blob/557c0d4c1b1061b8fff954dc96598ff4db95d262/packages/3-targets/sqlite/3-driver/src/sqlite-driver.ts#L85\nhttps://github.com/mikro-orm/mikro-orm/blob/14737a2349c834dbb39e1dc82bd5adf32fb2531c/packages/sql/src/dialects/sqlite/NodeSqliteDialect.ts#L31\n\n`node:sqlite` already calls `sqlite3_column_count` internally, but only exposes it through `columns()`:\n\nhttps://github.com/nodejs/node/blob/18b32a3b1552e199a01f1a8d9fd64aa756d3b75b/src/node_sqlite.cc#L4397\n\n`columns().length > 0` on v26.13.0 (darwin-arm64):\n\n```text\ninsert (0 cols) 20 ns/op\nselect 1 col 315 ns/op\nselect 8 cols 2,202 ns/op\nselect 32 cols 8,653 ns/op\n```\n\nFor comparison, `prepare()` of the 8 column select is ~5,200 ns/op, and `better-sqlite3`'s `stmt.reader` is ~8 ns/op.\n\n```js\nimport { Database } from 'node:sqlite'\n\nconst db = new Database(':memory:')\ndb.exec('create table t (id integer primary key, name text, a, b, c, d, e, f)')\n\nconst N = 1_000_000\nconst bench = (label, fn) => {\n for (let i = 0; i \u003c 1e5; i++) fn()\n const start = process.hrtime.bigint()\n for (let i = 0; i \u003c N; i++) fn()\n console.log(label, (Number(process.hrtime.bigint() - start) / N).toFixed(1), 'ns/op')\n}\n\nfor (const [label, sql] of [\n ['insert (0 cols)', 'insert into t (name) values (?)'],\n ['select 1 col', 'select id from t'],\n ['select 8 cols', 'select * from t'],\n ['select 32 cols', 'select *, *, *, * from t'],\n]) {\n const stmt = db.prepare(sql)\n bench(label, () => stmt.columns().length > 0)\n}\n```\n\n### What is the feature you are proposing to solve the problem?\n\nA side-effect-free `reader` getter on `Statement`, next to `sourceSQL` and `expandedSQL`, with the same semantics as `better-sqlite3` (`true` when the statement returns data):\n\nhttps://github.com/nodejs/node/blob/18b32a3b1552e199a01f1a8d9fd64aa756d3b75b/src/node_sqlite.cc#L4945-L4952\n\n`columns()` itself was added to mirror `better-sqlite3` (#57490). This is purely additive, like `stat()` and `resetStats()` (#64541), and removes one of the differences that keep `better-sqlite3`-based integrations from accepting a `node:sqlite` `Database` as is.\n\n### What alternatives have you considered?\n\nA `columnCount` getter that returns `sqlite3_column_count` as is. It's more general, and `reader` would just be `columnCount > 0`, but integrations written against `better-sqlite3` would have to special-case it.\n\n`sqlite3_stmt_readonly` is not a substitute, `insert ... returning` is not read-only but does return rows.\n\nClassifying the SQL text or the query builder's AST in userland works for builder queries, but not for raw SQL, which is why MikroORM's regexes above can misclassify statements.\n","author":{"url":"https://github.com/igalklebanov","@type":"Person","name":"igalklebanov"},"datePublished":"2026-11-08T22:14:25Z","url":"https://github.com/nodejs/node/issues/67352"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:da63584d-2827-3126-eacb-87e52429518e |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E23C:311640:2ED97D2:3B9977A:6B01C481 |
| html-safe-nonce | a634c2e317d484c8166e2ee2be0fad930366273de00198b1bb6fdc4f3b9c0e6e |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFMjNDOjMxMTY0MDoyRUQ5N0QyOjNCOTk3N0E6NkIwMUM0ODEiLCJ2aXNpdG9yX2lkIjoiMzI4NjY5NDQ5MjQ4OTQ5Mzc3IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 31bc729e7e5117cd581a52ec581af330f883f7db0d1f2cd5592deeac881c90c3 |
| hovercard-subject-tag | issue:6276245161 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/nodejs/node/67352/issue_layout |
| twitter:image | https://opengraph.githubassets.com/0bb430cce524e21cc65075b9f9aa0fcbb52115d85eca6578b62b7f3943b8c8bc/nodejs/node/issues/67352 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/0bb430cce524e21cc65075b9f9aa0fcbb52115d85eca6578b62b7f3943b8c8bc/nodejs/node/issues/67352 |
| og:image:alt | What is the problem this feature will solve? Hey 👋 Currently, the only way to tell whether a prepared Statement returns rows is statement.columns().length > 0, which is great when you also need the... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | igalklebanov |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| turbo-cache-control | no-preview |
| go-import | github.com/nodejs/node git https://github.com/nodejs/node.git |
| octolytics-dimension-user_id | 9950313 |
| octolytics-dimension-user_login | nodejs |
| octolytics-dimension-repository_id | 27193779 |
| octolytics-dimension-repository_nwo | nodejs/node |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 27193779 |
| octolytics-dimension-repository_network_root_nwo | nodejs/node |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width