rpdkey
Version:
RpdKey client
720 lines (708 loc) • 27.3 kB
JavaScript
let http=require('http')
class rpdkey {
static host="127.0.0.1";
static port=6888;
static protocol=http;
static token=''
static initlist=[]
static user="";
static password="";
static todisconnect=0;
static maxBatchCount=1800;
constructor(){
}
static postdata(purl,post_data,cb){
//console.log("s:",purl,JSON.stringify(post_data))
var content = JSON.stringify(post_data);
let headers={
'Content-Type': 'application/json; charset=UTF-8'
};
if(this.token!="") headers["X-Token"]=this.token;
var options = {
hostname: this.host,
port: this.port,
path: purl,
method: 'POST',
headers: headers
};
function getdata(level){
level++;
//console.log("post:",content)
var req = rpdkey.protocol.request(options, function (res) {
res.setEncoding('utf8');
let data=""
res.on('data', function (chunk) {
data+=chunk.toString()
});
res.on('end',function(){
//console.log("g:",data)
if(typeof cb!='undefined'){
let jdata=JSON.parse(data);
cb(jdata.result==0?null:jdata,jdata);
}
})
});
req.on('error', function (e) {
if(level<3){
getdata(level)
}else{
cb({result:21101},null)
}
});
req.write(content);
req.end();
}
getdata(0)
}
static postlogindata(cb){
this.postdata("/admin/user/login",{user:rpdkey.user,password:rpdkey.password},function(err,res){
if(!err){
rpdkey.token=res.token;
if(rpdkey.initlist.length){
for(let k=0;k<rpdkey.initlist.length;k++)
rpdkey.pushcmd(rpdkey.initlist[k].url,rpdkey.initlist[k].data,rpdkey.initlist[k].cb)
rpdkey.initlist=[]
}else{
if(rpdkey.todisconnect){
rpdkey.disconnect();
}
}
}else{
rpdkey.todisconnect=0;
rpdkey.initlist=[]
//console.log("login fail!!!")
}
if(typeof cb!='undefined')
cb(err,res)
})
}
static connect(cfg,cb){
if(typeof cfg.host!='undefined')
this.host=cfg.host;
if(typeof cfg.port!='undefined')
this.port=cfg.port;
if(typeof cfg.protocol!='undefined' && cfg.protocol=='https')
this.protocol=require('http');
this.initlist=[]
this.todisconnect=0;
this.user=cfg.user;
this.password=cfg.password;
this.postlogindata(cb);
}
static disconnect( ){
if(rpdkey.token!='')
this.postdata("/admin/user/logout",{},function(err,res){
if(!err){
console.log("disconnect ok")
rpdkey.token="";
}
});
else{
rpdkey.todisconnect=1;
}
}
static cmdlist=[]
static reqing=false;
static reqlength=0;
static checkcmd(){
let _this=rpdkey;
if(_this.reqing ) return;
if(_this.cmdlist.length==0) {
if(_this.todisconnect){
_this.disconnect();
}
return;
}
let batch=[]
let cobj=null;
let lasturl=null;
let lastsql=null;
for(let k=0;k<_this.cmdlist.length;k++){
if(_this.cmdlist[k].req==0 //&&(cobj==null||cobj!=null && cobj==_this.cmdlist[k].o)
){
_this.cmdlist[k].req=1;
let paraobj={d:_this.cmdlist[k].d}
let url=_this.cmdlist[k].u.substring(4);
if(lasturl!=null && url==lasturl){
}else
paraobj.u=url;
lasturl=url;
if(url=="/execsql"){
if(lastsql!=null && paraobj.d.sql==lastsql){
delete paraobj.d.sql;
}else{
lastsql=paraobj.d.sql;
}
}
batch.push(paraobj);
if(batch.length>rpdkey.maxBatchCount) break;
}
}
if(batch.length==0) return;
_this.reqing=true;
_this.reqlength=batch.length;
rpdkey.postdata("/sys/record/batch",{batch:batch},function(err,res){
_this.reqing=false;
let listoff=0;
if(res.result==21100|| res.result==21101){
for(let k=0;k<_this.cmdlist.length;k++){
if(_this.cmdlist[k].req==1 && listoff<_this.reqlength){
_this.cmdlist[k].req=2;
if(typeof _this.cmdlist[k].cb!='undefined')
_this.cmdlist[k].cb(res,null)
}
}
}
else{
for(let k=0;k<_this.cmdlist.length;k++){
if(_this.cmdlist[k].req==1 && listoff<res.list.length){
_this.cmdlist[k].req=2;
let obj=res.list[listoff++];
if(typeof _this.cmdlist[k].cb!='undefined')
_this.cmdlist[k].cb(obj.result==0?null:obj,obj)
}
}
}
for(let k=0;k<_this.cmdlist.length;k++){
if(_this.cmdlist[k].req!=2){
if(k)
_this.cmdlist.splice(0,k)
break;
}
}
_this.checkcmd();
})
}
static execsql(sql,parameter,cb){
let _this=rpdkey;
if(typeof parameter=='function'){
cb=parameter;
_this.pushcmd("/sys/execsql",{sql:sql},cb)
}else{
_this.pushcmd("/sys/execsql",{sql:sql,p:parameter},cb)
}
}
static execSql(sql,parameter,cb){
let _this=rpdkey;
function sqlcb(err,res){
if(typeof res.list!='undefined'){
res.cursor=rpdkey.cursor(res.list,-1)
}
cb(err,res)
}
if(typeof sql=='object'){
if(typeof parameter=='function'){
cb=parameter;
_this.pushcmd("/sys/execsql",{sql:sql.sql,name:sql.name},sqlcb)
}else{
_this.pushcmd("/sys/execsql",{sql:sql.sql,name:sql.name,p:parameter},sqlcb)
}
return;
}
if(typeof parameter=='function'){
cb=parameter;
_this.pushcmd("/sys/execsql",{sql:sql},sqlcb)
}else{
_this.pushcmd("/sys/execsql",{sql:sql,p:parameter},sqlcb)
}
}
static pushcmd(url,data,cb){
let _this=rpdkey;
if(_this.token==""){
_this.initlist.push({url:url,data:data,cb:cb})
return;
}
if(_this.cmdlist.length==0 && !_this.reqing){
_this.reqing=true;
_this.postdata(url,data,function(err,res){
_this.reqing=false;
if(res.result==21100){
_this.token="";
_this.initlist.push({url:url,data:data,cb:cb})
postlogindata(cb);
}else{
if(typeof cb!='undefined'){
cb(res.result==0?null:res,res);
}
}
_this.checkcmd();
})
return false;
}
rpdkey.cmdlist.push({u:url,d:data,cb:cb,req:0})
rpdkey.checkcmd();
return true;
}
static connection(cfg){
class connection{
constructor() {
this.cfg=cfg
this.token=""
this.protocol=http
this.cmdlist=[]
this.reqing=false;
this.reqlength=0;
this.bufftable=null;
this.seq=0;
}
pushcmd(url,data,cb){
let _this=this;
if(_this.token==""){
_this.cmdlist.push({u:url,d:data,cb:cb,req:0})
return;
}
if(_this.cmdlist.length==0 && !_this.reqing){
_this.reqing=true;
_this.postdata(url,data,function(err,res){
_this.reqing=false;
if(res.result==21100){
_this.token="";
_this.cmdlist.push({u:url,d:data,cb:cb,req:0})
_this.postlogindata(cb);
}else{
if(typeof cb!='undefined'){
cb(res.result==0?null:res,res);
}
}
_this.checkcmd();
})
return false;
}
_this.cmdlist.push({u:url,d:data,cb:cb,req:0})
_this.checkcmd();
return true;
}
checkcmd(){
let _this=this;
if(_this.reqing ) return;
if(_this.cmdlist.length==0) {
if(_this.todisconnect){
_this.disconnect();
}
return;
}
let batch=[]
let cobj=null;
let lasturl=null;
let lastsql=null;
for(let k=0;k<_this.cmdlist.length;k++){
if(_this.cmdlist[k].req==0 ){
_this.cmdlist[k].req=1;
let paraobj={d:_this.cmdlist[k].d}
let url=_this.cmdlist[k].u.substring(4);
if(lasturl!=null && url==lasturl){
}else
paraobj.u=url;
lasturl=url;
if(url=="/execsql"){
if(lastsql!=null && paraobj.d.sql==lastsql){
delete paraobj.d.sql;
}else{
lastsql=paraobj.d.sql;
}
}
batch.push(paraobj);
if(batch.length>rpdkey.maxBatchCount) break;
}
}
if(batch.length==0) return;
_this.reqing=true;
_this.reqlength=batch.length;
this.postdata("/sys/record/batch",{batch:batch},function(err,res){
_this.reqing=false;
let listoff=0;
if(res.result==21100|| res.result==21101){
for(let k=0;k<_this.cmdlist.length;k++){
if(_this.cmdlist[k].req==1 && listoff<_this.reqlength){
_this.cmdlist[k].req=2;
if(typeof _this.cmdlist[k].cb!='undefined')
_this.cmdlist[k].cb(res,null)
}
}
}
else{
for(let k=0;k<_this.cmdlist.length;k++){
if(_this.cmdlist[k].req==1 && listoff<res.list.length){
_this.cmdlist[k].req=2;
let obj=res.list[listoff++];
if(typeof _this.cmdlist[k].cb!='undefined')
_this.cmdlist[k].cb(obj.result==0?null:obj,obj)
}
}
}
for(let k=0;k<_this.cmdlist.length;k++){
if(_this.cmdlist[k].req!=2){
if(k)
_this.cmdlist.splice(0,k)
break;
}
}
_this.checkcmd();
})
}
postdata(purl,post_data,cb){
let _this=this;
var content = JSON.stringify(post_data);
let cseq=this.seq.toString();
this.seq++;
//console.log("add seq",this.cfg)
let headers={
'Seq': cseq,
'Content-Type': 'application/json; charset=UTF-8'
};
if(this.token!="") headers["X-Token"]=this.token;
var options = {
hostname: this.cfg.host,
port: this.cfg.port,
path: purl,
method: 'POST',
headers: headers
};
function getdata(level){
level++;
var req = _this.protocol.request(options, function (res) {
res.setEncoding('utf8');
let data=""
res.on('data', function (chunk) {
data+=chunk.toString()
});
res.on('end',function(){
//console.log("g:",data)
if(typeof cb!='undefined'){
let jdata=JSON.parse(data);
cb(jdata.result==0?null:jdata,jdata);
}
})
});
req.on('error', function (e) {
if(level<3){
getdata(level)
}else{
cb({result:21101},null)
}
});
req.write(content);
req.end();
}
getdata(0)
}
postlogindata(cb){
let _this=this;
this.postdata("/admin/user/login",{user:this.cfg.user,password:this.cfg.password},function(err,res){
if(!err){
_this.token=res.token;
if(_this.cmdlist.length){
}else{
if(_this.todisconnect){
_this.disconnect();
}
}
}else{
_this.todisconnect=0;
}
if(typeof cb!='undefined')
cb(err,res)
_this.checkcmd();
})
}
connect(cb){
if(typeof this.cfg.protocol!='undefined' && this.cfg.protocol=='https')
this.protocol=require('http');
this.todisconnect=0;
this.postlogindata(cb);
}
disconnect(cb){
if(this.token!='')
this.postdata("/admin/user/logout",{},function(err,res){
if(!err){
console.log("disconnect ok")
this.token="";
}
cb(err,res)
});
else{
this.todisconnect=1;
}
}
execsql(sql,parameter,cb){
let _this=this;
if(typeof parameter=='function'){
cb=parameter;
_this.pushcmd("/sys/execsql",{sql:sql},cb)
}else{
_this.pushcmd("/sys/execsql",{sql:sql,p:parameter},cb)
}
}
execSql(sql,parameter,cb){
let _this=this;
function sqlcb(err,res){
if(typeof cb!='function')
return;
if(typeof res.list!='undefined'){
res.rows=res.list;//rpdkey.cursor(res.list,-1)
}else res.rows=null;
cb(err,res)
}
if(typeof sql=='object'){
let sv={sql:sql.sql};
if(typeof sql.name!='undefined') sv.name=sql.name;
if(typeof parameter=='function'){
cb=parameter;
_this.pushcmd("/sys/execsql",sv,sqlcb)
}else{
sv.p=parameter;
_this.pushcmd("/sys/execsql",sv,sqlcb)
}
return;
}
if(typeof parameter=='function'){
cb=parameter;
_this.pushcmd("/sys/execsql",{sql:sql},sqlcb)
}else{
_this.pushcmd("/sys/execsql",{sql:sql,p:parameter},sqlcb)
}
}
table(tablename){
let _this=this;
class table {
constructor() {
this.conn=_this
//this.cmdlist=[]
this.name=tablename
//this.reqing=false;
}
pushcmd(url,data,cb){
return this.conn.pushcmd(url,data,cb)
}
insert(rowobject,cb){
this.pushcmd("/sys/record/insert",{table:this.name,value:rowobject},cb);
}
update(rowobject,cb){
this.pushcmd("/sys/record/update"
,{table:this.name,value:rowobject},cb);
}
truncate(cb){
this.pushcmd("/sys/record/truncate"
,{table:this.name},cb);
}
delete(key,cb){
this.pushcmd("/sys/record/delete",{table:this.name,key:key},cb);
}
get(key,cb){
this.pushcmd("/sys/record/get",{table:this.name,key:key},cb);
}
find(query,cb){
if(typeof query.key!='undefined'){
this.pushcmd("/sys/record/get",{table:this.name,key:query.key},function(err,data){
cb(err,typeof data.data!='undefined'?rpdkey.cursor([data.data],1):null)
});
}
else
this.pushcmd("/sys/record/list",{table:this.name,query:query},function(err,data){
cb(err,typeof data.list!='undefined'?rpdkey.cursor(data.list
,typeof res.total!='undefined'?res.total:-1):null)
});
}
}
if(this.bufftable!=null && this.bufftable.name==tablename) return this.bufftable;
return this.bufftable=new table();
}
}
return new connection();
}
static bufftable=null;
static table(tablename){
class table {
constructor() {
//this.cmdlist=[]
this.name=tablename
//this.reqing=false;
}
pushcmd(url,data,cb){
return rpdkey.pushcmd(url,data,cb,this)
}
insert(rowobject,cb){
this.pushcmd("/sys/record/insert",{table:this.name,value:rowobject},cb);
}
update(rowobject,cb){
this.pushcmd("/sys/record/update"
,{table:this.name,value:rowobject},cb);
}
truncate(cb){
this.pushcmd("/sys/record/truncate"
,{table:this.name},cb);
}
delete(key,cb){
this.pushcmd("/sys/record/delete",{table:this.name,key:key},cb);
}
get(key,cb){
this.pushcmd("/sys/record/get",{table:this.name,key:key},cb);
}
find(query,cb){
this.pushcmd("/sys/record/list",{table:this.name,query:query},function(err,data){
if(typeof cb!='undefined')
cb(err,typeof data.list!='undefined'?rpdkey.cursor(data.list,data.total):null)
});
}
}
if(rpdkey.bufftable!=null && rpdkey.bufftable.name==tablename) return rpdkey.bufftable;
return rpdkey.bufftable=new table();
}
static cursor(list,cnt){
class cursor {
constructor(list,count) {
this.resultlist=list;
this.resultptr=0;
if(typeof count=='undefined')
this.count=list.length;
else
this.count=count;
}
toArray(){
return this.resultlist
}
fetchone(){
if(this.resultlist==null || this.resultptr>=this.resultlist.length)
return null
return this.resultlist[this.resultptr++]
}
fetchmany(rows){
if(this.resultlist==null) return null;
let list=[]
for(let k=this.resultptr;k<this.resultptr+rows;k++){
let v=this.fetchone()
if(v==null) break;
list.push(v)
}
this.resultptr+=rows;
return list;
}
fetchall(){
return this.resultlist
}
total(){
return this.count
}
}
return new cursor(list,cnt);
}
static postdata2(purl,post_data,obj,cb){
var content = JSON.stringify(post_data);
let headers={
'Content-Type': 'application/json; charset=UTF-8'
};
if(this.token!="") headers["X-Token"]=this.token;
var options = {
hostname: rpdkey.host,
port: rpdkey.port,
path: purl,
method: 'POST',
headers: headers
};
console.log("s:",options)
var req = http.request(options, function (res) {
res.setEncoding('utf8');
let data=""
res.on('data', function (chunk) {
data+=chunk.toString()
});
res.on('end',function(){
console.log("g:",data)
if(typeof cb!='undefined')
cb(obj,JSON.parse(data));
})
});
req.on('error', function (e) {
console.log('problem with request: ' + e.message);
});
req.write(content);
req.end();
}
static batch(){
class batch{
constructor(){
this.cmdlist=[]
}
pushcmd(url,data,cb){
this.cmdlist.push({u:"/record/"+url,d:data})
return this.cmdlist.length-1;
}
insert(table,rowobject){
return this.pushcmd("insert",{table:typeof table.name!='undefined'?table.name:table,value:rowobject},null);
}
update(table,rowobject){
return this.pushcmd("update",{table:typeof table.name!='undefined'?table.name:table,value:rowobject},null);
}
truncate(table){
return this.pushcmd("truncate",{table:typeof table.name!='undefined'?table.name:table},null);
}
delete(table,rowobject){
return this.pushcmd("delete",{table:typeof table.name!='undefined'?table.name:table,key:rowobject},null);
}
query(table,_query){
return this.pushcmd("list",{table:typeof table.name!='undefined'?table.name:table,query:_query},null);
}
exec(cb){
class Result{
constructor(){
this.total=0;
this.total_success=0;
this.resultlist=[]
}
get(i){
if(i<this.total){
for(let k=0;k<this.resultlist.length;k++){
if(i<this.resultlist[k].len)
return this.resultlist[k].list[i]
i-=this.resultlist[k].len
}
}
return null;
}
getresult(i){
if(i>=0 && i<this.total){
for(let k=0;k<this.resultlist.length;k++){
if(i<this.resultlist[k].len)
return this.resultlist[k].list[i]
i-=this.resultlist[k].len
}
}
return null;
}
}
let obj=new Result()
let tlen=this.cmdlist.length;
function getdata(batchobj){
let slen=batchobj.cmdlist.length;
let sendobj=batchobj.cmdlist
if(slen>rpdkey.maxBatchCount){
slen=rpdkey.maxBatchCount;
sendobj=batchobj.cmdlist.slice(0,slen)
batchobj.cmdlist=batchobj.cmdlist.slice(slen)
}
rpdkey.postdata2("/sys/record/batch",{batch:sendobj},this,function(_this,result){
if(result==null || typeof result.list=='undefined'){
obj.resultlist.push({list:null,status:result==null?-1:result.result,len:slen})
obj.total+=slen;
}else{
obj.resultlist.push({list:result.list,status:result.result,len:result.list.length})
for(let k=0;k<result.list.length;k++){
if(result.list[k] && result.list[k].result==0){
obj.total_success++;
}
}
obj.total+=result.list.length;
}
if(obj.total>=tlen)
cb(obj)
else
getdata(batchobj)
});
}
getdata(this)
}
}
return new batch();
}
}
module.exports=rpdkey;