node icon indicating copy to clipboard operation
node copied to clipboard

sqlite: add support for SQLite Session Extension

Open louwers opened this issue 6 months ago • 26 comments

We were talking about adding support for the Session Extension of SQLite in #53752. This is not a run-time extension but rather a feature built in to the SQLite amalgamation. It can be enabled with a compile flag and is enabled by default on some platforms.

I have never contributed to Node.js before so I will probably need some help to bring this to a conclusion.


TODO:

  • [x] Make sure sessions are deleted before the database they are attached to is closed
  • [ ] Consensus on API
  • [x] Documentation
  • [x] Allow custom conflict handler
  • [x] ~Throw with specific (documented) exception in case of conflict when applying changeset~ since SQLite doesn't consider this to be an error I return false when applying the changeset is aborted due to a conflict
  • [x] Allow generating a patchset as well
  • [x] Allow specifying table name when creating session (to only track that table)
  • [x] Implement Session.close()

Example usage:

const database1 = new DatabaseSync(':memory:');
const database2 = new DatabaseSync(':memory:');

database1.exec('CREATE TABLE data(key INTEGER PRIMARY KEY, value TEXT)');
database2.exec('CREATE TABLE data(key INTEGER PRIMARY KEY, value TEXT)');

const session = database1.createSession();

const insert = database1.prepare('INSERT INTO data (key, value) VALUES (?, ?)');
insert.run(1, 'hello');
insert.run(2, 'world');

const changeset = session.changeset();
database2.applyChangeset(changeset);
// Now database2 contains the same data as database1 

louwers avatar Aug 02 '24 17:08 louwers