|
|
|
@ -73,26 +73,27 @@ class Persistence { |
|
|
|
|
* @param {Function} callback Optional callback, signature: err |
|
|
|
|
*/ |
|
|
|
|
persistCachedDatabase (callback = () => {}) { |
|
|
|
|
let toPersist = '' |
|
|
|
|
|
|
|
|
|
const lines = [] |
|
|
|
|
|
|
|
|
|
if (this.inMemoryOnly) return callback(null) |
|
|
|
|
|
|
|
|
|
this.db.getAllData().forEach(doc => { |
|
|
|
|
toPersist += this.afterSerialization(model.serialize(doc)) + '\n' |
|
|
|
|
lines.push(this.afterSerialization(model.serialize(doc))) |
|
|
|
|
}) |
|
|
|
|
Object.keys(this.db.indexes).forEach(fieldName => { |
|
|
|
|
if (fieldName !== '_id') { // The special _id index is managed by datastore.js, the others need to be persisted
|
|
|
|
|
toPersist += this.afterSerialization(model.serialize({ |
|
|
|
|
lines.push(this.afterSerialization(model.serialize({ |
|
|
|
|
$$indexCreated: { |
|
|
|
|
fieldName: fieldName, |
|
|
|
|
unique: this.db.indexes[fieldName].unique, |
|
|
|
|
sparse: this.db.indexes[fieldName].sparse |
|
|
|
|
} |
|
|
|
|
})) + '\n' |
|
|
|
|
}))) |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
storage.crashSafeWriteFile(this.filename, toPersist, err => { |
|
|
|
|
storage.crashSafeWriteFileLines(this.filename, lines, err => { |
|
|
|
|
if (err) return callback(err) |
|
|
|
|
this.db.emit('compaction.done') |
|
|
|
|
return callback(null) |
|
|
|
|