diff --git a/browser-version/browser-specific/lib/storage.js b/browser-version/browser-specific/lib/storage.js new file mode 100644 index 0000000..210ee67 --- /dev/null +++ b/browser-version/browser-specific/lib/storage.js @@ -0,0 +1,80 @@ +/** + * Way data is stored for this database + * For a Node.js/Node Webkit database it's the file system + * For a browser-side database it's localStorage when supported + * + * This version is the Node.js/Node Webkit version + */ + + + +function exists (filename, callback) { + // In this specific case this always answers that the file doesn't exist + if (typeof localStorage === 'undefined') { console.log("WARNING - This browser doesn't support localStorage, no data will be saved in NeDB!"); return callback(); } + + if (localStorage.getItem(filename) !== null) { + return callback(true); + } else { + return callback(false); + } +} + + +function rename (filename, newFilename, callback) { + if (typeof localStorage === 'undefined') { console.log("WARNING - This browser doesn't support localStorage, no data will be saved in NeDB!"); return callback(); } + + if (localStorage.getItem(filename) === null) { + localStorage.removeItem(newFilename); + } else { + localStorage.setItem(newFilename, localStorage.getItem(filename)); + localStorage.removeItem(filename); + } + + return callback(); +} + + +function writeFile (filename, contents, options, callback) { + if (typeof localStorage === 'undefined') { console.log("WARNING - This browser doesn't support localStorage, no data will be saved in NeDB!"); return callback(); } + + // Options do not matter in browser setup + if (typeof options === 'function') { callback = options; } + + localStorage.setItem(filename, contents); + return callback(); +} + + +function appendFile (filename, toAppend, options, callback) { + if (typeof localStorage === 'undefined') { console.log("WARNING - This browser doesn't support localStorage, no data will be saved in NeDB!"); return callback(); } + + // Options do not matter in browser setup + if (typeof options === 'function') { callback = options; } + + var contents = localStorage.getItem(filename) || ''; + contents += toAppend; + + localStorage.setItem(filename, contents); + return callback(); +} + + +function readFile (filename, options, callback) { + if (typeof localStorage === 'undefined') { console.log("WARNING - This browser doesn't support localStorage, no data will be saved in NeDB!"); return callback(); } + + // Options do not matter in browser setup + if (typeof options === 'function') { callback = options; } + + var contents = localStorage.getItem(filename) || ''; + return callback(null, contents); +} + + +// Interface +module.exports.exists = exists; +module.exports.rename = rename; +module.exports.writeFile = writeFile; +module.exports.appendFile = appendFile; +module.exports.readFile = readFile; + + diff --git a/lib/persistence.js b/lib/persistence.js index 5bf5e96..b080db7 100644 --- a/lib/persistence.js +++ b/lib/persistence.js @@ -6,6 +6,7 @@ */ var fs = require('fs') + , storage = require('./storage') , path = require('path') , model = require('./model') , async = require('async') @@ -123,19 +124,19 @@ Persistence.prototype.persistCachedDatabase = function (cb) { async.apply(customUtils.ensureFileDoesntExist, self.tempFilename) , async.apply(customUtils.ensureFileDoesntExist, self.oldFilename) , function (cb) { - fs.exists(self.filename, function (exists) { + storage.exists(self.filename, function (exists) { if (exists) { - fs.rename(self.filename, self.oldFilename, function (err) { return cb(err); }); + storage.rename(self.filename, self.oldFilename, function (err) { return cb(err); }); } else { return cb(); } }); } , function (cb) { - fs.writeFile(self.tempFilename, toPersist, function (err) { return cb(err); }); + storage.writeFile(self.tempFilename, toPersist, function (err) { return cb(err); }); } , function (cb) { - fs.rename(self.tempFilename, self.filename, function (err) { return cb(err); }); + storage.rename(self.tempFilename, self.filename, function (err) { return cb(err); }); } , async.apply(customUtils.ensureFileDoesntExist, self.oldFilename) ], function (err) { if (err) { return callback(err); } else { return callback(null); } }) @@ -197,7 +198,7 @@ Persistence.prototype.persistNewState = function (newDocs, cb) { if (toPersist.length === 0) { return callback(null); } - fs.appendFile(self.filename, toPersist, 'utf8', function (err) { + storage.appendFile(self.filename, toPersist, 'utf8', function (err) { return callback(err); }); }; @@ -250,18 +251,18 @@ Persistence.treatRawData = function (rawData) { Persistence.prototype.ensureDatafileIntegrity = function (callback) { var self = this ; - fs.exists(self.filename, function (filenameExists) { + storage.exists(self.filename, function (filenameExists) { // Write was successful if (filenameExists) { return callback(null); } - fs.exists(self.oldFilename, function (oldFilenameExists) { + storage.exists(self.oldFilename, function (oldFilenameExists) { // New database if (!oldFilenameExists) { - return fs.writeFile(self.filename, '', 'utf8', function (err) { callback(err); }); + return storage.writeFile(self.filename, '', 'utf8', function (err) { callback(err); }); } // Write failed, use old version - fs.rename(self.oldFilename, self.filename, function (err) { return callback(err); }); + storage.rename(self.oldFilename, self.filename, function (err) { return callback(err); }); }); }); }; @@ -291,7 +292,7 @@ Persistence.prototype.loadDatabase = function (cb) { function (cb) { Persistence.ensureDirectoryExists(path.dirname(self.filename), function (err) { self.ensureDatafileIntegrity(function (exists) { - fs.readFile(self.filename, 'utf8', function (err, rawData) { + storage.readFile(self.filename, 'utf8', function (err, rawData) { if (err) { return cb(err); } var treatedData = Persistence.treatRawData(rawData); diff --git a/lib/storage.js b/lib/storage.js new file mode 100644 index 0000000..7a2fa05 --- /dev/null +++ b/lib/storage.js @@ -0,0 +1,13 @@ +/** + * Way data is stored for this database + * For a Node.js/Node Webkit database it's the file system + * For a browser-side database it's localStorage when supported + * + * This version is the Node.js/Node Webkit version + */ + +var fs = require('fs'); + + +module.exports = fs; +