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

SQLite Session Extension #3321

Open
rodydavis opened this issue Oct 31, 2024 · 2 comments
Open

SQLite Session Extension #3321

rodydavis opened this issue Oct 31, 2024 · 2 comments
Labels
question Further information is requested

Comments

@rodydavis
Copy link
Contributor

What would be the best way to explore using the official SQLite Session Extension with Drift?

https://sqlite.org/sessionintro.html

From my research the API is in C, and would need some compile flags passed to the custom sqlite build.

I did find an interesting example for iOS/MacOS:
https://github.com/gerdemb/SQLiteChangesetSync

@rodydavis rodydavis added the enhancement New feature or request label Oct 31, 2024
@simolus3
Copy link
Owner

At the moment (I hope this gets a lot easier with the native assets SDK feature), you can't pass custom compiler flags to the buildscripts in sqlite3_flutter_libs. So the first step is to fork that package and add the relevant definitions for the platforms you need unfortunately.

Once you have the functions available, you can write bindings to them using dart:ffi. You'll need the sqlite3* pointer to the database connection drift uses. You get access to the underlying database in the setup method on NativeDatabase constructors and you can use .handle to get the native pointer to use when calling other sqlite3 functions (it might be easier to start with a local database instead of using background isolates to have everything available in the same isolate).

With the database pointer, it should be possible to create changesets and apply them. How to best integrate that functionality into drift is another question. You could call the methods to create sessions synchronously, then use drift methods to run statements and once that's done, use another C call to obtain a changeset.

@simolus3 simolus3 added question Further information is requested and removed enhancement New feature or request labels Oct 31, 2024
@rodydavis
Copy link
Contributor Author

rodydavis commented Nov 25, 2024

Node.js is about to unflag the SQLite API and I saw it supports the session extension!

https://nodejs.org/api/sqlite.html

I will be looking at it on how it maps to the c code from JS.

Here is a nice example on the docs:

const sourceDb = new DatabaseSync(':memory:');
const targetDb = new DatabaseSync(':memory:');

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

const session = sourceDb.createSession();

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

const changeset = session.changeset();
targetDb.applyChangeset(changeset);
// Now that the changeset has been applied, targetDb contains the same data as sourceDb.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants