mirror of
https://github.com/leanote/desktop-app.git
synced 2025-10-17 16:45:21 +00:00
leanote protocol移到main进程, 所有图片的操作通过db_client进行操作
This commit is contained in:
90
node_modules/nedb_proxy.js
generated
vendored
Normal file
90
node_modules/nedb_proxy.js
generated
vendored
Normal file
@@ -0,0 +1,90 @@
|
||||
var ipc = require('ipc');
|
||||
|
||||
function Find(dbProxy, dbname, query) {
|
||||
this.query = query;
|
||||
this.dbProxy = dbProxy;
|
||||
this.dbname = dbname;
|
||||
}
|
||||
Find.prototype.sort = function (sorter) {
|
||||
this.sorter = sorter;
|
||||
return this;
|
||||
}
|
||||
|
||||
Find.prototype.exec = function (callback) {
|
||||
var params = {
|
||||
query: this.query,
|
||||
sorter: this.sorter
|
||||
};
|
||||
this.dbProxy.send(params, callback, 'find');
|
||||
};
|
||||
|
||||
//==========
|
||||
|
||||
function DBProxy(dbname) {
|
||||
this.dbname = dbname;
|
||||
};
|
||||
|
||||
var token = 1;
|
||||
var token2Callback = {};
|
||||
|
||||
DBProxy.prototype.send = function(params, callback, method) {
|
||||
token++;
|
||||
var m = {
|
||||
token: token,
|
||||
method: method,
|
||||
dbname: this.dbname,
|
||||
params: params
|
||||
}
|
||||
token2Callback[token] = callback;
|
||||
ipc.send('db-exec', m);
|
||||
};
|
||||
|
||||
// NB.find({UserId: userId, $or: [{LocalIsDelete : { $exists : false }}, {LocalIsDelete: false}] }, function(err, notebooks) {
|
||||
// Notes.find(query).sort({'UpdatedTime': -1}).exec(function(err, notes) {
|
||||
DBProxy.prototype.find = function (params, callback) {
|
||||
if (callback) {
|
||||
this.send({query: params}, callback, 'find');
|
||||
}
|
||||
else {
|
||||
return new Find(this, this.dbname, params);
|
||||
}
|
||||
};
|
||||
DBProxy.prototype.findOne = function (params, callback) {
|
||||
this.send(params, callback, 'findOne');
|
||||
};
|
||||
DBProxy.prototype.count = function (params, callback) {
|
||||
this.send(params, callback, 'count');
|
||||
};
|
||||
DBProxy.prototype.insert = function (params, callback) {
|
||||
this.send(params, callback, 'insert');
|
||||
};
|
||||
DBProxy.prototype.update = function (query, sets, options, callback) {
|
||||
if (typeof options == 'function') {
|
||||
callback = options;
|
||||
options = {};
|
||||
}
|
||||
if (!options) {
|
||||
options = {};
|
||||
}
|
||||
var params = {
|
||||
query: query,
|
||||
sets: sets,
|
||||
options: options
|
||||
};
|
||||
this.send(params, callback, 'update');
|
||||
};
|
||||
DBProxy.prototype.remove = function (params, callback) {
|
||||
this.send(params, callback, 'remove');
|
||||
};
|
||||
|
||||
// m = {token: , err : , ret: }
|
||||
ipc.on('db-exec-ret', function(m) {
|
||||
var token = m.token;
|
||||
var callback = token2Callback[token];
|
||||
// console.log('clent 接收到消息');
|
||||
// console.log(m);
|
||||
// console.log('--------------');
|
||||
callback && callback(m.err, m.ret);
|
||||
});
|
||||
|
||||
module.exports = DBProxy;
|
Reference in New Issue
Block a user