sqlite: bind parameters the same way `better-sqlite3` does.

オープン
#67,353 コメント 2 件 リアクション 2 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
45/100
issue の種類
機能追加
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
javascript, sqlite
領域
backend, databases

調査の方向性

Start by reading the parameter binding logic in src/node_sqlite.cc, especially the call path that currently treats only the first argument as a named parameter object. Compare its behavior with the documented call shapes and add coverage for all(), get(), iterate() and run(). Done means arrays and a single plain object bind as described in the issue, with tests confirming each listed call shape.

索引モデルが issue の本文から書いたものです。

説明

feature request sqlite
What is the problem this feature will solve?

Hey 👋

Currently, node:sqlite is close to being a drop-in replacement for better-sqlite3 in query builders, ORMs and drivers, which is great, except parameters are bound differently. Integrations written against better-sqlite3 pass parameters as an array, e.g. Kysely, Knex and Prisma 7:

https://github.com/kysely-org/kysely/blob/67263069a242600e914e8a9e03b118e8cbf4267c/src/dialect/sqlite/sqlite-driver.ts#L117
https://github.com/knex/knex/blob/e25d54bcb707714a17f5a5744eba5c4246bb4d1d/lib/dialects/better-sqlite3/index.js#L52
https://github.com/prisma/prisma/blob/0b596ffecfcc55e37d6a7095c03193e2d28b8090/packages/adapter-better-sqlite3/src/better-sqlite3.ts#L90

node:sqlite treats an array as a named parameters object, with keys '0', '1', etc., so none of them can accept a node:sqlite Database without changes, and wrappers have to adapt every call, e.g. MikroORM 7:

https://github.com/mikro-orm/mikro-orm/blob/14737a2349c834dbb39e1dc82bd5adf32fb2531c/packages/sql/src/dialects/sqlite/NodeSqliteDialect.ts#L32-L33

better-sqlite3 flattens arrays into anonymous parameters, in any position, and accepts a single named parameters object in any position:

https://github.com/WiseLibs/better-sqlite3/blob/dbc2ea1165fef1f599b9be12faea33fa5e9d7ffb/src/util/binder.cpp#L165-L186

node:sqlite only treats the first argument as a potential named parameters object:

https://github.com/nodejs/node/blob/18b32a3b1552e199a01f1a8d9fd64aa756d3b75b/src/node_sqlite.cc#L3859-L3860

import { Database } from 'node:sqlite'

const db = new Database(':memory:')

db.prepare('select ? as a, ? as b').all([1, 2])
// Error: Unknown named parameter '0'

db.prepare('select ? as a, ? as b, ? as c').all(1, [2, 3])
// Error: Provided value cannot be bound to SQLite parameter 2.

db.prepare('select :x as x, ? as a').all(2, { x: 1 })
// Error: Provided value cannot be bound to SQLite parameter 3.

const stmt = db.prepare('select ? as a, ? as b')
stmt.setAllowUnknownNamedParameters(true)
stmt.all([1, 2])
// [ { a: null, b: null } ] 😬
better-sqlite3 node:sqlite
all(1, 2) ✅ ✅
all([1, 2]) ✅ ❌ Unknown named parameter '0'
all([1, 2], 3) ✅ ❌ Unknown named parameter '0'
all(1, [2, 3]) ✅ ❌ Provided value cannot be bound to SQLite parameter 2.
all([1], [2, 3]) ✅ ❌ Unknown named parameter '0'
all({ x: 1 }, 2) ✅ ✅
all(2, { x: 1 }) ✅ ❌ Provided value cannot be bound to SQLite parameter 3.
all([2, 3], { x: 1 }) ✅ ❌ Unknown named parameter '0'
all({ x: 1 }, [2, 3]) ✅ ❌ Provided value cannot be bound to SQLite parameter 2.

This was raised from a different angle (stack size) in #57932, which was closed as stale.

What is the feature you are proposing to solve the problem?

all(), get(), iterate() and run() should bind parameters the same way better-sqlite3 does: arrays are flattened into anonymous parameters, in any position, and a single plain object, in any position, is named parameters.

Every call shape this changes currently throws, or silently binds NULL with setAllowUnknownNamedParameters(true), so no working code relies on the current behavior.

Together with #67352 (reader), this lets Kysely's better-sqlite3 dialect run on a node:sqlite Database as is: migrations, insert ... returning, raw queries, streaming, transactions and introspection all pass with just those two differences shimmed.

What alternatives have you considered?

Only accepting an array as the first argument. It covers the most common call shape, but integrations that use the other shapes would still need changes.

Spreading in userland, statement.all(...parameters). It works up to SQLite's default limit of 32766 parameters, but it's exactly the downstream change we want to avoid: every better-sqlite3-based integration has to special-case node:sqlite, and switching their call sites to spread breaks wrappers that implement the array signature.

主要言語
JavaScript
スター
124k
フォーク
38k
平均マージ
4日 2時間
マージ済み PR(30日)
266

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

nodejs/node のほかの issue

nodejs/node の issue をすべて見る

似ている issue

JavaScript の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。