tndrle / node-sqlite3-wasm

WebAssembly port of SQLite3 for Node.js with file system access
MIT License
55 stars 8 forks source link

[feature request] add support for `returing` #47

Closed subframe7536 closed 7 months ago

subframe7536 commented 7 months ago

Thanks for your great work!

I'm writing a dialect for Kysely, that needs get lastInsertRowid, changes and rows when the sql have returing, but in my current(v0.8.0) try:

import sqlite from "node-sqlite3-wasm";

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

db.run('create table test1(a INTEGER, b TEXT)')

console.log(db.run('insert into test1(a, b) values (1, \'test\') returning *'))
// { changes: 0, lastInsertRowid: 1 }

console.log(db.exec('insert into test1(a, b) values (1, \'test\') returning *'))
// undefined

console.log(db.prepare('insert into test1(a, b) values (1, \'test\') returning *').all())
// [{ a: 1, b: 'test' }], but no changes and lastInsertRowid

maybe expose a new API or just expose sqlite.changes()?

tndrle commented 7 months ago

Hi, thanks for your request.

The function .all() returns an array and the length of the array equals changes. So if you can live without lastInsertRowid, this would be a solution. If you need lastInsertRowid as well, I do not have quick a solution.

For the moment and unless there are more requests in this direction, I will not implement anything to solve this.

subframe7536 commented 7 months ago

Thanks for your reply!

Finally i implement it by checking if sql string has returning. If true, call db.all(sql, params) to get rows and db.get('select last_insert_rowid() as id').id to get lastInsertRowid

subframe7536 commented 7 months ago

find another way right now: call db.all(sql, params) to get rows and db.run('select 1') to get changes and lastInsertRowid

tndrle commented 7 months ago

Happy you found a solution. db.get('select last_insert_rowid() as id').id is nice 🙂