New option to store data relative to NW working dir, still in testing

pull/2/head
Louis Chatriot 12 years ago
parent 911feed839
commit 17b2fdf60c
  1. 33
      lib/customUtils.js
  2. 9
      lib/datastore.js

@ -33,6 +33,39 @@ 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.ensureDirectoryExists = ensureDirectoryExists;
module.exports.uid = uid;
module.exports.getNWAppFilename = getNWAppFilename;

@ -15,6 +15,8 @@ var fs = require('fs')
* @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.pipeline Optional, defaults to false, pipeline appends to the data file (enable to return before writes are persisted to disk for greater speed)
* @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 Datastore (options) {
var filename;
@ -31,6 +33,7 @@ function Datastore (options) {
this.pipeline = options.pipeline || false;
}
// Determine whether in memory or persistent
if (!filename || typeof filename !== 'string' || filename.length === 0) {
this.filename = null;
this.inMemoryOnly = true;
@ -38,6 +41,11 @@ 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);
}
this.executor = new Executor();
if (this.pipeline) { this.persistenceExecutor = new Executor(); }
@ -581,5 +589,4 @@ Datastore.prototype.remove = function () {
module.exports = Datastore;

Loading…
Cancel
Save