用户数据分表存储

This commit is contained in:
life
2015-11-19 17:56:53 +08:00
parent d26561a089
commit e50b518715
7 changed files with 109 additions and 35 deletions

84
node_modules/db.js generated vendored
View File

@@ -14,6 +14,61 @@ if(dbPath.length < 6) {
var dbPath = '/Users/life/Library/Application Support/Leanote' + '/nedb2';
}
// console.log(dbPath);
// g, 表全局环境
var db = {
// 为全部用户共有的表初始化
initGlobal: function () {
var dbNames = ['users', 'g'];
this._init(dbNames);
},
// 为特定用户初始化自己的表
initDBForUser: function (userId) {
var dbNames = ['notebooks', 'notes', 'tags', 'images', 'attachs', 'noteHistories'];
this._init(dbNames, userId);
},
// 过时
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, userId) {
var me = this;
for(var i in dbNames) {
var name = dbNames[i];
if (!userId) {
userId = '';
}
var baseDBPath = dbPath;
if (userId) {
baseDBPath += '/' + userId;
}
var dbFilepath = path.join(baseDBPath, name + '.db');
// console.log(dbFilepath);
(function (name) {
// 这部分非常慢!, 会卡界面
me[name] = new Datastore({ filename: dbFilepath, autoload: name != 'noteHistories' , onload: function () {
console.log(userId + '/' + name + ' is loaded');
}});
})(name);
}
console.log('db inited');
}
};
// 加载DB, 为noteHistories
Datastore.prototype.loadDB = function(callback) {
var me = this;
@@ -27,34 +82,5 @@ Datastore.prototype.loadDB = function(callback) {
}
};
// 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;