用户数据分表存储

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;

5
node_modules/evt.js generated vendored
View File

@@ -11,7 +11,6 @@ if(!fs.existsSync(dataBasePath)) {
*/
// dataBasePath = '';
var protocol = require('remote').require('protocol');
var Evt = {
@@ -89,11 +88,9 @@ var Evt = {
// /app/node_modules
return dirname.replace('/node_modules', '').replace('\\node_modules', ''); // windows情况
},
// 项目绝对地址
// 数据存储绝对地址
getBasePath: function() {
var me = this;
// return dataBasePath; // process.cwd();
// return process.cwd();
return me.dataBasePath;
},
getAbsolutePath: function(relative) {

11
node_modules/user.js generated vendored
View File

@@ -109,12 +109,17 @@ User = {
user.UserId = Common.objectId();
user._id = user.UserId;
user.Pwd = Common.md5(pwd, user.UserId);
// 有自己独立的数据库
user.HasDB = true;
db.users.insert(user, function(err, doc) {
// 创建默认的笔记本
if (!err) {
// 设为当前user
me.saveCurUser(doc);
// 为该用户初始化数据库
db.initDBForUser(user.UserId);
me.userId = user.UserId;
var Notebook = require('notebook');
var notebookId = Common.objectId();
@@ -204,6 +209,12 @@ User = {
me.LastSyncTime = user.LastSyncTime;
me.host = user.Host;
me.local = user.IsLocal;
me.hasDB = user.HasDB; // 是否有自己的DB
// 为该用户初始化数据库
db.initDBForUser(me.hasDB ? me.userId : '');
Evt.setHost(me.host);
// 全局配置也在user中, 到web端