m4heshd / better-sqlite3-multiple-ciphers

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

Add support in knex library for `better-sqlite3-multiple-ciphers` #69

Closed titanism closed 10 months ago

titanism commented 10 months ago

Right now better-sqlite3 is supported by knex – is there a way to workaround this string comparison (or make a resolution someone to make it work)? A link to the commit we'd have to mirror with better-sqlite3-multiple-ciphers instead of better-sqlite3 as the string is below.

Ref:

titanism commented 10 months ago

I'm testing resolutions right now with pnpm, for example adding this to package.json:

  "resolutions": {
    "better-sqlite3": "better-sqlite3-multiple-ciphers"
  },
titanism commented 10 months ago

OK got it working, there's just one change needed to support better-sqlite3-multiple-ciphers, and it's a simple require statement at https://github.com/knex/knex/commit/2bd181171b53dfa967503e53c90fb8201b46980e#diff-cde9b682a8cffbcbfda8552e00e2195defec3ef422a1c2083ecc923a3a92d5dfR7 (which needs swapped with better-sqlite3-multiple-ciphers).

Usage is as follows:

// attempt to use knex
// <https://github.com/m4heshd/better-sqlite3-multiple-ciphers/issues/69>
const knexDatabase = knex({
  client: 'better-sqlite3',
  connection: {
    filename: dbName,
    options: {
      // note: we need this in order to use encryption
      nativeBinding: path.join(
        __dirname,
        'node_modules',
        'better-sqlite3-multiple-ciphers',
        'build',
        'Release',
        'better_sqlite3.node'
      )
    }
  },
  debug: true,
  acquireConnectionTimeout: 10000, // default
  log: logger, // custom logger
  pool: {
    // <https://knexjs.org/faq/recipes.html#db-access-using-sqlite-and-sqlcipher>
    afterCreate(db, fn) {
      console.log('db', db);
      db.pragma("cipher='aes256cbc'");
      db.pragma(`key='password'`);
      fn();
    }
  }
});

console.log('knexDatabase', knexDatabase);

// <https://github.com/knex/knex/issues/360#issuecomment-481711368>
const results = await knexDatabase.raw(
  "SELECT name AS table_name FROM sqlite_master WHERE type='table'"
);
console.log('results', results);
titanism commented 10 months ago

Ref: https://github.com/knex/knex/issues/5719

titanism commented 10 months ago

It seems like you can already use better-sqlite3-multiple-ciphers by simply using better-sqlite3 and using nativeBinding approach as documented at https://github.com/m4heshd/better-sqlite3-multiple-ciphers/issues/69#issuecomment-1774236862.

The only difference I could see so far between better-sqlite3-multiple-ciphers and better-sqlite3 in terms of API is key and rekey added: https://github.com/m4heshd/better-sqlite3-multiple-ciphers/blob/cc6d5838fbaa88ed8b817b1949743854c92c1eb0/lib/database.js#L85-L86

m4heshd commented 10 months ago

It would be THEIR responsibility to add this functionality if the users of their library require it. Therefore, I'm closing this issue as it is now being tracked in the appropriate place. I suggest you create a PR there so it makes the job easier for the devs and as a plus, you'll get much better attention to the functionality you specifically require.

titanism commented 10 months ago

Thanks for closing and keeping this tidy, I must have missed the "close and comment" button last comment above.