afterSerialization と beforeDeserialization オプションを追加する。
import Datastore from 'nedb'
import crypto from 'crypto'
import path from 'path'
let password = 'hogehoge'
const db = new Datastore({
// 引数で受け取った文字列を暗号化して返す(改行コード \n は使用禁止)
afterSerialization: doc => {
const cipher = crypto.createCipher('aes-256-ctr', password)
let encrypted = cipher.update(doc, 'utf8', 'hex')
encrypted += cipher.final('hex')
return encrypted
},
// 復号化した文字列を返す
beforeDeserialization: encrypted => {
const decipher = crypto.createDecipher('aes-256-ctr', password)
let doc = decipher.update(encrypted, 'hex', 'utf8')
doc += decipher.final('utf8')
return doc
},
filename: path.join(__dirname, 'documents.db')
})