|
|
@ -21,6 +21,8 @@ var storage = require('./storage') |
|
|
|
* Node Webkit stores application data such as cookies and local storage (the best place to store data in my opinion) |
|
|
|
* Node Webkit stores application data such as cookies and local storage (the best place to store data in my opinion) |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
function Persistence (options) { |
|
|
|
function Persistence (options) { |
|
|
|
|
|
|
|
var i, j, randomString; |
|
|
|
|
|
|
|
|
|
|
|
this.db = options.db; |
|
|
|
this.db = options.db; |
|
|
|
this.inMemoryOnly = this.db.inMemoryOnly; |
|
|
|
this.inMemoryOnly = this.db.inMemoryOnly; |
|
|
|
this.filename = this.db.filename; |
|
|
|
this.filename = this.db.filename; |
|
|
@ -36,14 +38,22 @@ function Persistence (options) { |
|
|
|
|
|
|
|
|
|
|
|
// After serialization and before deserialization hooks with some basic sanity checks
|
|
|
|
// After serialization and before deserialization hooks with some basic sanity checks
|
|
|
|
if (options.afterSerialization && !options.beforeDeserialization) { |
|
|
|
if (options.afterSerialization && !options.beforeDeserialization) { |
|
|
|
throw "Serialization hook defined but deserialization hook undefined, cautiously refusing to start NeDB"; |
|
|
|
throw "Serialization hook defined but deserialization hook undefined, cautiously refusing to start NeDB to prevent dataloss"; |
|
|
|
} |
|
|
|
} |
|
|
|
if (!options.afterSerialization && options.beforeDeserialization) { |
|
|
|
if (!options.afterSerialization && options.beforeDeserialization) { |
|
|
|
throw "Serialization hook undefined but deserialization hook defined, cautiously refusing to start NeDB"; |
|
|
|
throw "Serialization hook undefined but deserialization hook defined, cautiously refusing to start NeDB to prevent dataloss"; |
|
|
|
} |
|
|
|
} |
|
|
|
this.afterSerialization = options.afterSerialization || function (s) { return s; }; |
|
|
|
this.afterSerialization = options.afterSerialization || function (s) { return s; }; |
|
|
|
this.beforeDeserialization = options.beforeDeserialization || function (s) { return s; }; |
|
|
|
this.beforeDeserialization = options.beforeDeserialization || function (s) { return s; }; |
|
|
|
|
|
|
|
for (i = 1; i < 30; i += 1) { |
|
|
|
|
|
|
|
for (j = 0; j < 10; j += 1) { |
|
|
|
|
|
|
|
randomString = customUtils.uid(i); |
|
|
|
|
|
|
|
if (this.beforeDeserialization(this.afterSerialization(randomString)) !== randomString) { |
|
|
|
|
|
|
|
throw "beforeDeserialization is not the reverse of afterSerialization, cautiously refusing to start NeDB to prevent dataloss"; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// For NW apps, store data in the same directory where NW stores application data
|
|
|
|
// For NW apps, store data in the same directory where NW stores application data
|
|
|
|
if (this.filename && options.nodeWebkitAppName) { |
|
|
|
if (this.filename && options.nodeWebkitAppName) { |
|
|
|
console.log("=================================================================="); |
|
|
|
console.log("=================================================================="); |
|
|
|