Skip to content

Commit

Permalink
fs and path are both required only in the persistence module
Browse files Browse the repository at this point in the history
  • Loading branch information
louischatriot committed Jul 12, 2013
1 parent dcb1e16 commit 46c4723
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 55 deletions.
38 changes: 1 addition & 37 deletions lib/customUtils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
var crypto = require('crypto')
, path = require('path')
;
var crypto = require('crypto');

/**
* Return a random alphanumerical string of length len
Expand All @@ -18,38 +16,4 @@ function uid (len) {
}


/**
* Return the path the datafile if the given filename is relative to the directory where Node Webkit stores
* data for this application. Probably the best place to store data
*/
function getNWAppFilename (appName, relativeFilename) {
var home;

switch (process.platform) {
case 'win32':
case 'win64':
home = process.env.LOCALAPPDATA || process.env.APPDATA;
if (!home) { throw "Couldn't find the base application data folder"; }
home = path.join(home, appName);
break;
case 'darwin':
home = process.env.HOME;
if (!home) { throw "Couldn't find the base application data directory"; }
home = path.join(home, 'Library', 'Application Support', appName);
break;
case 'linux':
home = process.env.HOME;
if (!home) { throw "Couldn't find the base application data directory"; }
home = path.join(home, '.config', appName);
break;
default:
throw "Can't use the Node Webkit relative path for platform " + process.platform;
break;
}

return path.join(home, 'nedb-data', relativeFilename);
}


module.exports.uid = uid;
module.exports.getNWAppFilename = getNWAppFilename;
10 changes: 2 additions & 8 deletions lib/datastore.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ var customUtils = require('./customUtils')
* Create a new collection
* @param {String} options.filename Optional, datastore will be in-memory only if not provided
* @param {Boolean} options.inMemoryOnly Optional, default to false
* @param {Boolean} options.autoload Optional, defaults to false
* @param {Boolean} options.pipeline DEPRECATED, doesn't have any effect anymore
* @param {Boolean} options.nodeWebkitAppName Optional, specify the name of your NW app if you want options.filename to be relative to the directory where
* Node Webkit stores application data such as cookies and local storage (the best place to store data in my opinion)
* @param {Boolean} options.autoload Optional, defaults to false
*/
function Datastore (options) {
var filename;
Expand All @@ -40,13 +39,8 @@ function Datastore (options) {
this.filename = filename;
}

// For NW apps, store data in the same directory where NW stores application data
if (this.filename && options.nodeWebkitAppName) {
this.filename = customUtils.getNWAppFilename(options.nodeWebkitAppName, this.filename);
}

// Persistence handling
this.persistence = new Persistence({ db: this });
this.persistence = new Persistence({ db: this, nodeWebkitAppName: options.nodeWebkitAppName });

// This new executor is ready if we don't use persistence
// If we do, it will only be ready once loadDatabase is called
Expand Down
59 changes: 50 additions & 9 deletions lib/persistence.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
var fs = require('fs')
, path = require('path')
, model = require('./model')
, customUtils = require('./customUtils')
, async = require('async')
, mkdirp = require('mkdirp')
;
Expand All @@ -14,9 +13,18 @@ var fs = require('fs')
/**
* Create a new Persistence object for database options.db
* @param {Datastore} options.db
* @param {Boolean} options.nodeWebkitAppName Optional, specify the name of your NW app if you want options.filename to be relative to the directory where
* Node Webkit stores application data such as cookies and local storage (the best place to store data in my opinion)
*/
function Persistence (options) {
this.db = options.db;
this.inMemoryOnly = this.db.inMemoryOnly;
this.filename = this.db.filename;

// For NW apps, store data in the same directory where NW stores application data
if (this.filename && options.nodeWebkitAppName) {
this.filename = Persistence.getNWAppFilename(options.nodeWebkitAppName, this.filename);
}
}


Expand All @@ -32,6 +40,39 @@ Persistence.ensureDirectoryExists = function (dir, cb) {
}


/**
* Return the path the datafile if the given filename is relative to the directory where Node Webkit stores
* data for this application. Probably the best place to store data
*/
Persistence.getNWAppFilename = function (appName, relativeFilename) {
var home;

switch (process.platform) {
case 'win32':
case 'win64':
home = process.env.LOCALAPPDATA || process.env.APPDATA;
if (!home) { throw "Couldn't find the base application data folder"; }
home = path.join(home, appName);
break;
case 'darwin':
home = process.env.HOME;
if (!home) { throw "Couldn't find the base application data directory"; }
home = path.join(home, 'Library', 'Application Support', appName);
break;
case 'linux':
home = process.env.HOME;
if (!home) { throw "Couldn't find the base application data directory"; }
home = path.join(home, '.config', appName);
break;
default:
throw "Can't use the Node Webkit relative path for platform " + process.platform;
break;
}

return path.join(home, 'nedb-data', relativeFilename);
}


/**
* Persist cached database
* This serves as a compaction function since the cache always contains only the number of documents in the collection
Expand All @@ -49,7 +90,7 @@ Persistence.prototype.persistCachedDatabase = function (cb) {

if (toPersist.length === 0) { return callback(null); }

fs.writeFile(this.db.filename, toPersist, function (err) { return callback(err); });
fs.writeFile(this.filename, toPersist, function (err) { return callback(err); });
};


Expand All @@ -66,7 +107,7 @@ Persistence.prototype.persistNewState = function (newDocs, cb) {
;

// In-memory only datastore
if (self.db.inMemoryOnly) { return callback(null); }
if (self.inMemoryOnly) { return callback(null); }

self.db.datafileSize += newDocs.length;

Expand All @@ -76,7 +117,7 @@ Persistence.prototype.persistNewState = function (newDocs, cb) {

if (toPersist.length === 0) { return callback(null); }

fs.appendFile(self.db.filename, toPersist, 'utf8', function (err) {
fs.appendFile(self.filename, toPersist, 'utf8', function (err) {
return callback(err);
});
};
Expand Down Expand Up @@ -134,15 +175,15 @@ Persistence.prototype._loadDatabase = function (cb) {
self.db.datafileSize = 0;

// In-memory only datastore
if (self.db.inMemoryOnly) { return callback(null); }
if (self.inMemoryOnly) { return callback(null); }

async.waterfall([
function (cb) {
Persistence.ensureDirectoryExists(path.dirname(self.db.filename), function (err) {
fs.exists(self.db.filename, function (exists) {
if (!exists) { return fs.writeFile(self.db.filename, '', 'utf8', function (err) { cb(err); }); }
Persistence.ensureDirectoryExists(path.dirname(self.filename), function (err) {
fs.exists(self.filename, function (exists) {
if (!exists) { return fs.writeFile(self.filename, '', 'utf8', function (err) { cb(err); }); }

fs.readFile(self.db.filename, 'utf8', function (err, rawData) {
fs.readFile(self.filename, 'utf8', function (err, rawData) {
if (err) { return cb(err); }
var treatedData = Persistence.treatRawData(rawData);

Expand Down
1 change: 0 additions & 1 deletion test/db.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ var should = require('chai').should()
, _ = require('underscore')
, async = require('async')
, Datastore = require('../lib/datastore')
, customUtils = require('../lib/customUtils')
, model = require('../lib/model')
, Persistence = require('../lib/persistence')
;
Expand Down

0 comments on commit 46c4723

Please sign in to comment.