libsql-js
libsql-js copied to clipboard
stmt.run().lastInsertRowid is wrong with RANDOM ROWID
Hi, I am new to JS so it is possible I am missing something.
- I am using the
RANDOM ROWIDfeature with libsql. When this is enabled I cannot retrieve items using thelastInsertRowidproperty fromstmt.run()commands after insertion. The lastInsertRowid is slightly off from the actual rowid. However usingstmt.all()after aselect * ...statement I can get the correct rowids. - Looking at the tests they do not seem to cover the
BigInt/SafeIntegersuse case.
Here is example code to reproduce the issue:
import Database from 'libsql';
const db = new Database(':memory:');
db.defaultSafeIntegers(true);
db.exec(`
DROP TABLE IF EXISTS users;
CREATE TABLE users (rowid INTEGER PRIMARY KEY, name TEXT) RANDOM ROWID;`);
const stmt_insert = db.prepare("INSERT INTO users(name) VALUES (?)");
const info_insert = stmt_insert.run('alma');
console.log([typeof info_insert.lastInsertRowid, info_insert.lastInsertRowid]);
const stmt_list = db.prepare("SELECT * from users");
const info_list = stmt_list.all()
console.log([typeof info_list[0].rowid ,info_list]);
and sample output:
$ node ex.js
[ 'number', 4504423795271622000 ]
[ 'bigint', [ { rowid: 4504423795271622254n, name: 'alma' } ] ]