Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transactions for SQLite #57431

Open
sant123 opened this issue Mar 13, 2025 · 3 comments
Open

Transactions for SQLite #57431

sant123 opened this issue Mar 13, 2025 · 3 comments
Labels
feature request Issues that request new features to be added to Node.js. sqlite Issues and PRs related to the SQLite subsystem.

Comments

@sant123
Copy link

sant123 commented Mar 13, 2025

Hello, I haven’t found a discussion neither an issue that talks about the support for transactions. Is this something Node.js will/would support in the future?

This is how better-sqlite3 uses it: https://github.com/WiseLibs/better-sqlite3/blob/master/docs/api.md#transactionfunction---function

Thanks 🙏🏼

Originally posted by @sant123 in #57396

@sant123 sant123 changed the title Transactions for SQlite Transactions for SQLite Mar 13, 2025
@targos targos added sqlite Issues and PRs related to the SQLite subsystem. feature request Issues that request new features to be added to Node.js. labels Mar 13, 2025
@targos
Copy link
Member

targos commented Mar 13, 2025

We can certainly discuss the addition of features like that one to make the API more ergonomic, but you can already do transactions using SQL statements: https://sqlite.org/lang_transaction.html

There are some examples in https://github.com/nodejs/node/blob/a0139e06a0754058ffd891f779be55584665f8a8/test/parallel/test-sqlite-transactions.js

@nodejs/sqlite

@sant123
Copy link
Author

sant123 commented Mar 13, 2025

Thanks @targos, I’m going to give it a try 🙏🏼

@sant123
Copy link
Author

sant123 commented Mar 13, 2025

This is my code and perfectly worked:

import { DatabaseSync } from "node:sqlite";
const db = new DatabaseSync(":memory:");

db.exec(`CREATE TABLE people (
  id INTEGER PRIMARY KEY,
  name TEXT NOT NULL,
  birthdate TEXT NOT NULL
) STRICT`);

const insertPeople = db.prepare(`
  INSERT INTO people
    (name, birthdate)
  VALUES
    (:name, :birthdate)
  `);

insertPeople.run({ name: "Flash", birthdate: "1956-07-16" });

db.exec("BEGIN TRANSACTION");
insertPeople.run({ name: "Bruno", birthdate: "1962-12-11" });
db.exec("ROLLBACK");

insertPeople.run({ name: "Bruce", birthdate: "1963-06-17" });

const people = db.prepare("SELECT * FROM people").all();

console.log(people);
[
  [Object: null prototype] {
    id: 1,
    name: 'Flash',
    birthdate: '1956-07-16'
  },
  [Object: null prototype] {
    id: 2,
    name: 'Bruce',
    birthdate: '1963-06-17'
  }
]

Thanks a lot @targos for pointing this out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request Issues that request new features to be added to Node.js. sqlite Issues and PRs related to the SQLite subsystem.
Projects
Status: Awaiting Triage
Development

No branches or pull requests

2 participants