var Datastore = require('nedb'); var path = require('path'); var Evt = require('evt'); // 数据库初始化 // var dbPath = require('nw.gui').App.dataPath + '/nedb'; // var dbPath = Evt.getBasePath() + '/Users/life/Library/Application Support/Leanote' + '/nedb'; // nedb2 为了port var dbPath = Evt.getBasePath() + '/nedb55'; // console.error(dbPath); // test if(dbPath.length < 6) { var dbPath = '/Users/life/Library/Application Support/Leanote' + '/nedb2'; } // 加载DB, 为noteHistories Datastore.prototype.loadDB = function(callback) { var me = this; if (this.__loaded) { callback(); } else { this.loadDatabase(function (err) { me.__loaded = true; callback(err); }); } }; // console.log(dbPath); // g, 表全局环境 var db = { init: function () { var dbNames = ['users', 'notebooks', 'notes', 'tags', 'images', 'attachs', 'noteHistories', 'g']; this._init(dbNames); }, initForLogin: function () { // var dbNames = ['users']; var dbNames = ['users', 'notebooks', 'notes', 'tags', 'noteHistories']; this._init(dbNames); }, _init: function (dbNames) { var me = this; for(var i in dbNames) { var name = dbNames[i]; var p = path.join(dbPath, name + '.db'); (function (name) { // 这部分非常慢!, 会卡界面 me[name] = new Datastore({ filename: p, autoload: name != 'noteHistories' , onload: function () { console.log(name + ' is loaded'); }}); })(name); } console.log('db inited'); } }; module.exports = db;