添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
腹黑的跑步机  ·  FreshPorts -- ...·  1 月前    · 
俊秀的小狗  ·  “钢锯岭”麒麟 vs. ...·  1 月前    · 
英勇无比的柿子  ·  Microsoft Entra ...·  2 月前    · 
有爱心的香烟  ·  5 Ways to Fix the ...·  5 月前    · 

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 [ ] @next
[ ] 0.x.x (or put your version here)

better-sqlite3-multiple-ciphers version: 7.4.5

  • The following method is in effect (using better-sqlite3-multiple-ciphers )
  • // encrypt db
    const db = require('better-sqlite3-multiple-ciphers')('foobar.db', { verbose: console.log })
    db.pragma("cipher='sqlcipher'")
    db.pragma(`rekey='secret-key'`)
    db.prepare(`CREATE TABLE "post" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar NOT NULL, "text" varchar NOT NULL)`).run()
    const stmt = db.prepare('INSERT INTO post (title, text) VALUES (?, ?)')
    const info = stmt.run('Joey', 'my homie')
    db.close()
    // decrypt db
    const db = require('better-sqlite3-multiple-ciphers')('foobar.db', { verbose: console.log });
    db.pragma(`cipher='sqlcipher'`)
    db.pragma("key='secret-key'");
    const stmt = db.prepare("SELECT * FROM post")
    console.log(stmt.get()); // { id: 1, title: 'Joey', text: 'my homie' }
  • The following method is not valid (using typeorm )
  • import { createConnection } from 'typeorm'
    import { BetterSqlite3ConnectionOptions } from 'typeorm/driver/better-sqlite3/BetterSqlite3ConnectionOptions'
    import { Post } from './entity/post'
    const config: BetterSqlite3ConnectionOptions = {
      type: 'better-sqlite3',
      key: 'secret-key',
      database: 'foobar.db',
      driver: require('better-sqlite3-multiple-ciphers'),
      entities: ['entity/*.ts'],
      logging: true,
      verbose: console.log,
      prepareDatabase: db => {
        db.pragma(`cipher='sqlcipher'`)
    const start = async () => {
      const conn = await createConnection(config)
      const posts = await conn.manager.find(Post)
      console.log(posts)
    start()  // SqliteError: file is not a database

    I don’t know what’s wrong with my options for typeorm .

    Related to issue: m4heshd/better-sqlite3-multiple-ciphers#4

    The reproducible repo: https://github.com/yolopunk/typeorm-better-sqlite-sqlcipher

    pavelkalin, fmaclen, wgerven, smallStall, and maximelafarie reacted with thumbs up emoji maximelafarie reacted with heart emoji All reactions

    The issue is typeorm tries to run PRAGMA foreign_keys = ON before executing prepareDatabase() which leads to file not being recognized as a DB since it's already encrypted.

    In my opinion this execution order is not effective or helpful at all when coupled with custom implementations of the library which in this case is an encryption extension. Current method of initialization also goes against the practices of official SQLite Encryption Extension which has identical usage and behavior to better-sqlite3-multiple-ciphers .

    You must invoke this pragma before trying to do any other interaction with the database. - SEE documentation

    There are two possible solutions for this.

  • Bring the execution of prepareDatabase() to the top
  • Introduce a new functional config that runs right after initializing the DB
  • Please feel free to point out anything I have missed here because I've never used typeorm and just looked through the source solely for the purpose of diagnosing this issue.

    @m4heshd Awesome! That is worked by verification. I could commit a pull request for fixed the bug!

    Thank you very much!

    see documentation: https://www.sqlite.org/see/doc/release/www/readme.wiki
    * fix: sqlite driver
    * fix: better-sqlite3 driver
    Closed: typeorm#8475
    see documentation: https://www.sqlite.org/see/doc/release/www/readme.wiki * fix: sqlite driver * fix: better-sqlite3 driver Closed : #8475