-
-
Notifications
You must be signed in to change notification settings - Fork 396
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
Using sqlite3_update_hook #62
Comments
You can do the same thing (and with more control) by combining triggers and custom functions: db.prepare('CREATE TABLE data (a, b, c)').run();
db.prepare('INSERT INTO data VALUES (12, 34, 56)').run();
// This is your actual update hook
db.register({ varargs: true }, function logger(...values) {
console.log(`row updated to (${values.join(', ')})`);
});
db.prepare('CREATE TRIGGER updateHook AFTER UPDATE ON data BEGIN SELECT logger(NEW.a, NEW.b, NEW.c); END').run();
db.prepare('UPDATE data SET a = 78').run();
// => row updated to (78, 34, 56) |
Didn't think about it. There is no need for this, then. |
😃 |
Nice! Will this trigger also fire if another connection updates the table? I think this use case warrants mentioning in the readme, btw… |
@wmertens this will only work if the |
Aww. I'll just continue polling the DB then 😢. I could probably improve that by using filesystem monitoring and watching the DB file for changes… |
Yeah unfortunately that's the only way to read changes made by other connections. Even |
@JoshuaWise No |
The mean |
Using a temp trigger allows you to create a trigger that invokes functions only installed on the current connection. You still, however, have the problem of not seeing changes made by other connections with this method. |
It can be re-implemented in user space if required. The problem with the old trigger-based approach to reactivity is that you'd be called prior to transaction commit, upating your app with potentially rolled-back data. WiseLibs/better-sqlite3#62 (comment)
Is there any interest in re-opening this issue? For example, the implementation of |
The trigger approach mentioned in #62 (comment) is substantially more flexible and doesn’t have the surprising behavior of |
Triggers are flexible in regards to update individual table update, but how will it handle constantly changing or user created schema, without the need to create trigger on every table? Also curious to learn what is the surprising behavior of |
It would be nice to be able to subscribe to events using the
sqlite3_update_hook
function.How about adding a
setUpdateHook()
method to the Database instance ?I know that this function only fires for updates within the same database connection, but it would still be of use to me (probably).
The text was updated successfully, but these errors were encountered: