m4heshd / better-sqlite3-multiple-ciphers

better-sqlite3 with multiple-cipher encryption support 🔒
MIT License
140 stars 27 forks source link

db.register > TypeError: db.register is not a function #16

Closed leopucci closed 2 years ago

leopucci commented 2 years ago

I was trying to set a hook like sqlite3_update_hook and found this way of doing using db.register and triggers

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();

But i was unable to use. TypeError: db.register is not a function Any hints on how update hooks can be created?

https://github.com/JoshuaWise/better-sqlite3/issues/62#issuecomment-325797335

m4heshd commented 2 years ago

Hi @leopucci,

db.register() is not a valid function anymore. You're looking for db.function(). Always doublecheck with the API documentation.

leopucci commented 2 years ago

Thank you man, you are the best. I saw the function in the documentation but wasn´t sure if it was the same.

db.function('logger', { varargs: true }, function logger(...values) {
    console.log(`TESTE row updated to (${values.join(', ')})`);
  });

  db.prepare('DROP TRIGGER IF EXISTS updateHook;').run();
  db.prepare(
    'CREATE TRIGGER updateHook AFTER UPDATE ON files BEGIN SELECT logger(NEW.a,NEW.b); END'
  ).run();

It works