db.users.update({_id: everUserId}, {$set: data}, function(err, cnt) { if(err || cnt == 0) { console.log('用户信息更新失败', err); } else { Evt.setHost(me.host); console.log('用户信息更新成功'); db.notes.update({UserId: everUserId}, {$set: {UserId: me.userId, IsDirty: true, ServerNoteId: '', LocalIsNew: true}}, {multi: true}, function () { console.log('notes 数据更新成功'); }); db.notebooks.update({UserId: everUserId}, {$set: {UserId: me.userId, IsDirty: true, ServerNotebookId: '', LocalIsNew: true}}, {multi: true}, function () { console.log('notebooks 数据更新成功'); }); db.tags.update({UserId: everUserId}, {$set: {UserId: me.userId, IsDirty: true}}, {multi: true}, function () { console.log('tags 数据更新成功'); }); db.attachs.update({UserId: everUserId}, {$set: {UserId: me.userId, ServerFileId: '', IsDirty: true}}, {multi: true}, function () { console.log('attachs 数据更新成功'); }); dbClient.images.update({UserId: everUserId}, {$set: {UserId: me.userId, ServerFileId: '', IsDirty: true}}, {multi: true}, function () { console.log('images 数据更新成功'); }); } });
updateNotebookTitle: function(notebookId, title, callback) { db.notebooks.update({NotebookId: notebookId}, {$set: {Title: title, IsDirty: true, UpdatedTime: new Date()} }, function(err, n) { callback(true); }); },
}, function(err, count) { if(err) { log(err); return; } Web.updateNotebookNumberNotes(notebookId, count); db.notebooks.update({NotebookId: notebookId}, {$set: {NumberNotes: count}}, {}) });
Note.hasNotes(notebookId, function(has) { if(has) { callback(false, 'This notebook has notes, please delete notes firstly.'); } else { db.notebooks.update({NotebookId: notebookId}, {$set: {LocalIsDelete: true, IsDirty: true, UpdatedTime: new Date()}}, function(err, n) { callback(true); }); } });
deleteNotebookForce: function(notebookId, callback) { var me = this; db.notebooks.remove({ServerNotebookId: notebookId}, function(err, n) { if(err) { callback && callback(false); } else { callback && callback(true); } }); },
getServerNotebookIdByNotebookId: function(notebookId, callback) { if(!notebookId) { return callback(false); } db.notebooks.findOne({NotebookId: notebookId}, function(err, notebook) { if(err || !notebook) { return callback(false); } callback(notebook.ServerNotebookId); }); },
getNotebookIdByServerNotebookId: function(serverNotebookId, callback) { if(!serverNotebookId) { return callback(false); } db.notebooks.findOne({ServerNotebookId: serverNotebookId}, function(err, notebook) { if(err || !notebook) { return callback(false); } callback(notebook.NotebookId); }); },
getNotebookByServerNotebookId: function(notebookId, callback) { var me = this; db.notebooks.findOne({ServerNotebookId: notebookId}, function(err, doc) { if(err || !doc) { log('不存在'); callback && callback(false); } else { callback && callback(doc); } }); },
getNotebooks: function(callback) { var me = this; var userId = User.getCurActiveUserId(); db.notebooks.find({UserId: userId, $or: [{LocalIsDelete : { $exists : false }}, {LocalIsDelete: false}] }, function(err, notebooks) { if(err) { log(err); return callback && callback(false); } callback && callback(me._mapNotebooks(notebooks)); }); },
me.getNotebookIdByServerNotebookId(notebook.ParentNotebookId, function(parentNotebookId) { notebook.ParentNotebookId = parentNotebookId; notebook.ServerNotebookId = notebook.NotebookId; notebook.NotebookId = notebookLocal.NotebookId; db.notebooks.update({ServerNotebookId: serverNotebookId}, {$set: notebook}, {}, function (err, updates) { // Callback is optional if(err) { console.log(err); callback && callback(false); } else { callback && callback(notebook); } }); });
me.getNotebookIdByServerNotebookId(notebook.ParentNotebookId, function(parentNotebookId) { notebook.ParentNotebookId = parentNotebookId; // console.log(notebook2); // notebook2.Title += " H-"; // multi, 因为历史原因, 导致大量重复notebookId的元素 db.notebooks.update({NotebookId: notebookId}, {$set: notebook}, {multi: true}, function (err, n) { // console.log('updateNotebookForceForSendChange end' + notebookId + ' ' + n); if(err) { console.log(err); callback && callback(false); } else { callback && callback(notebook); } }); });
getDirtyNotebooks: function(callback) { var me = this; db.notebooks.find({UserId: User.getCurActiveUserId(), IsDirty: true}, function(err, notebooks) { if(err) { log(err); return callback && callback(false); } else { var mapNotebooks = me._mapNotebooks(notebooks); // 深度优先一个序列 me._deepTraversal = []; me._visited = {}; me.deep({Subs: mapNotebooks}); // 返回之 callback(me._deepTraversal); } }); },
addNotebookTest: function() { var doc = { // _id: "xxxxx", hello: 'world' , n: 5 , today: new Date() , nedbIsAwesome: true , notthere: null , notToBeSaved: undefined // Will not be saved , fruits: [ 'apple', 'orange', 'pear' ] , infos: { name: 'nedb' } }; console.log("save before") db.notebooks.insert(doc, function (err, newDoc) { // Callback is optional // newDoc is the newly inserted document, including its _id // newDoc has no key called notToBeSaved since its value was undefined // console.log(err); // console.log(newDoc); }); },
me.getNotebook(curNotebookId, function(notebook) { if(!notebook) { console.log('not fount'); return; } // 先更新之 // db.notebooks.update({NotebookId: notebookId}, {$set: {ParentNotebookId: parentNotebookId, IsDirty: true, UpdatedTime: new Date()}}, function(err, n) { // }); siblingNotebookIds = siblingNotebookIds || []; // 再更新所有子孩子的seq for(var i = 0; i < siblingNotebookIds.length; ++i) { var siblingNotebookId = siblingNotebookIds[i]; console.log('siblingNotebookId: ' + siblingNotebookId); db.notebooks.update({NotebookId: siblingNotebookId}, {$set: {ParentNotebookId: parentNotebookId, Seq: i, IsDirty: true, UpdatedTime: new Date()} } ); } });
addNotebook: function(notebookId, title, parentNotebookId, callback) { var notebook = { Title: title, Seq: -1, UserId: User.getCurActiveUserId(), ParentNotebookId: parentNotebookId, LocalIsNew: true, IsDirty: true, // 必须, 同步后才为非dirty状态 // TODO UrlTitle } if(notebookId) { notebook['NotebookId'] = notebookId; } db.notebooks.insert(notebook, function (err, newDoc) { // Callback is optional if(err) { console.log(err); callback && callback(false); } else { callback && callback(newDoc); } }); },
me.getNotebookIdByServerNotebookId(notebook.ParentNotebookId, function(parentNotebookId) { // 如果是第一次添加可能会有问题, 数据库还没有数据, 那么还找不到 if(parentNotebookId) { notebook.ParentNotebookId = parentNotebookId; } else { // 否则, 就用服务器上的 } notebook.CreatedTime = Common.goNowToDate(notebook.CreatedTime); notebook.UpdatedTime = Common.goNowToDate(notebook.UpdatedTime); notebook.IsDirty = false; notebook.LocalIsNew = false; notebook.LocalIsDelete = false; db.notebooks.insert(notebook, function (err, newDoc) { // Callback is optional if(err) { console.log(err); callback && callback(false); } else { callback && callback(newDoc); } }); });
setIsNew: function(notebookId) { db.notebooks.update({NotebookId: notebookId}, {$set:{LocalIsNew: true, IsDirty: true}}) }
setNotDirtyNotDelete: function(notebookId) { db.notebooks.update({NotebookId: notebookId}, {$set:{IsDirty: false, LocalIsDelete: false}}) },
deleteLocalNotebook: function(notebookId, callback) { db.notebooks.remove({NotebookId: notebookId}, function(err, n) { callback && callback(); }); },