allow configurable storage engine

pull/19/head
David Alberto Adler 3 years ago
parent 49574da5e2
commit 4d11141f7b
  1. 4
      lib/datastore.js
  2. 15
      lib/persistence.js

@ -21,6 +21,7 @@ class Datastore extends EventEmitter {
* @param {Function} [options.afterSerialization] Optional, serialization hooks
* @param {Number} [options.corruptAlertThreshold] Optional, threshold after which an alert is thrown if too much data is corrupt
* @param {Function} [options.compareStrings] Optional, string comparison function that overrides default for sorting
* @param {Object} [options.storage] Optional, custom storage engine
*
* Event Emitter - Events
* * compaction.done - Fired whenever a compaction operation was finished
@ -58,7 +59,8 @@ class Datastore extends EventEmitter {
nodeWebkitAppName: options.nodeWebkitAppName,
afterSerialization: options.afterSerialization,
beforeDeserialization: options.beforeDeserialization,
corruptAlertThreshold: options.corruptAlertThreshold
corruptAlertThreshold: options.corruptAlertThreshold,
storage: options.storage
})
// This new executor is ready if we don't use persistence

@ -21,6 +21,7 @@ class Persistence {
*/
constructor (options) {
this.db = options.db
this.storage = options.storage || storage
this.inMemoryOnly = this.db.inMemoryOnly
this.filename = this.db.filename
this.corruptAlertThreshold = options.corruptAlertThreshold !== undefined ? options.corruptAlertThreshold : 0.1
@ -92,7 +93,7 @@ class Persistence {
}
})
storage.crashSafeWriteFileLines(this.filename, lines, err => {
this.storage.crashSafeWriteFileLines(this.filename, lines, err => {
if (err) return callback(err)
this.db.emit('compaction.done')
return callback(null)
@ -146,7 +147,7 @@ class Persistence {
if (toPersist.length === 0) return callback(null)
storage.appendFile(this.filename, toPersist, 'utf8', err => callback(err))
this.storage.appendFile(this.filename, toPersist, 'utf8', err => callback(err))
}
/**
@ -254,7 +255,7 @@ class Persistence {
Persistence.ensureDirectoryExists(path.dirname(this.filename), err => {
// TODO: handle error
// eslint-disable-next-line node/handle-callback-err
storage.ensureDatafileIntegrity(this.filename, err => {
this.storage.ensureDatafileIntegrity(this.filename, err => {
// TODO: handle error
const treatedDataCallback = (err, treatedData) => {
if (err) return cb(err)
@ -275,15 +276,15 @@ class Persistence {
this.db.persistence.persistCachedDatabase(cb)
}
if (storage.readFileStream) {
if (this.storage.readFileStream) {
// Server side
const fileStream = storage.readFileStream(this.filename, { encoding: 'utf8' })
const fileStream = this.storage.readFileStream(this.filename, { encoding: 'utf8' })
this.treatRawStream(fileStream, treatedDataCallback)
return
}
// Browser
storage.readFile(this.filename, 'utf8', (err, rawData) => {
this.storage.readFile(this.filename, 'utf8', (err, rawData) => {
if (err) return cb(err)
try {
@ -309,7 +310,7 @@ class Persistence {
* cb is optional, signature: err
*/
static ensureDirectoryExists (dir, callback = () => {}) {
storage.mkdir(dir, { recursive: true }, err => { callback(err) })
this.storage.mkdir(dir, { recursive: true }, err => { callback(err) })
}
/**

Loading…
Cancel
Save