bonsaif 1.10.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +201 -0
- package/index.js +6 -0
- package/lib/bonsaif.js +27 -0
- package/lib/ec.js +202 -0
- package/lib/execute.js +389 -0
- package/lib/hookup/exec.js +93 -0
- package/lib/hookup/mariadb.js +106 -0
- package/lib/hookup/mongodb.js +681 -0
- package/lib/hookup/postgres.js +111 -0
- package/lib/hookup/redis.js +622 -0
- package/lib/network.js +57 -0
- package/lib/utl.js +1422 -0
- package/package.json +41 -0
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ::: B[]NSAIF() ::: => 2022
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
'use strict';
|
|
6
|
+
|
|
7
|
+
const utl = require('../utl');
|
|
8
|
+
let debug = false;
|
|
9
|
+
let tag = ` ::: B[]NSAIF() ::: => postgres.js `;
|
|
10
|
+
|
|
11
|
+
const qson = async (o, alias, query,dml,ip)=>{
|
|
12
|
+
let data;
|
|
13
|
+
let headers = '';
|
|
14
|
+
const start = Date.now();
|
|
15
|
+
let tipo = 'query';
|
|
16
|
+
return new Promise(function(resolve, reject) {
|
|
17
|
+
//2022/08/27 VRSZ Se cambia el la utileria para consumirmo por mysql ya que mariadb no regresa los campos de las columnas
|
|
18
|
+
const {port='', uri = '', user='', password='', debug=false } = o.endpoint ? o.endpoint:{};
|
|
19
|
+
const {db=''} = o.options ? o.options : {};
|
|
20
|
+
|
|
21
|
+
const { Client } = require('pg');
|
|
22
|
+
const client = new Client({ host:uri, user, port , password, database :db });
|
|
23
|
+
client.connect(function(e){
|
|
24
|
+
if(e){
|
|
25
|
+
utl.log(`${tag} [postgres] is OFFline`,e);
|
|
26
|
+
}else{
|
|
27
|
+
debug ? utl.log(`${tag} [postgres] connection is online `):'';
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
if (query.toLowerCase().includes("select ") && query.toLowerCase().includes(" from ")){
|
|
32
|
+
tipo = 'select';
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
//2023/11/07 Recuperar id
|
|
36
|
+
if (query.toLowerCase().includes("insert ") && query.toLowerCase().includes(" into ")){
|
|
37
|
+
query = query.replace(';','');
|
|
38
|
+
query = query + ' RETURNING id';
|
|
39
|
+
tipo = 'insert';
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
client.query(query, function(err, res) {
|
|
43
|
+
const end = Date.now();
|
|
44
|
+
const time = end - start;
|
|
45
|
+
client.end(function(){
|
|
46
|
+
debug ? utl.log(`${tag} [postgres] connection is closing connection `):'';
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
if (err) {
|
|
50
|
+
resolve({"result":{dml, headers, time:utl.milisegundosASegundos(time), code:err.code ? err.code:0, error:1, msg:err.routine ? err.routine:''}, results:err});
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
data = res.rows ? res.rows: [];
|
|
54
|
+
let rowCount = res.rowCount ? res.rowCount:0;
|
|
55
|
+
|
|
56
|
+
if (tipo!='select'){
|
|
57
|
+
data =[{"insert_id":data[0] ? data[0].id : 0, "changed_rows":rowCount}];
|
|
58
|
+
headers = 'insert_id|changed_rows';
|
|
59
|
+
}else{
|
|
60
|
+
if (data.length==0){
|
|
61
|
+
let {fields=[]} = res ? res : {};
|
|
62
|
+
for (let field of fields ){
|
|
63
|
+
if (headers.length>0){
|
|
64
|
+
headers = headers+'|';
|
|
65
|
+
}
|
|
66
|
+
headers = headers+field.name;
|
|
67
|
+
}
|
|
68
|
+
}else{
|
|
69
|
+
try{ headers = Object.keys(data[0]); headers = headers.join("|"); }catch(e){}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
resolve({"result":{dml, headers, time:utl.milisegundosASegundos(time), code:200, error:0}, data});
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const api=async (options,db,query,dml,ip,tag,callback)=>{
|
|
79
|
+
//Inicializar conexiones a bases
|
|
80
|
+
tag = tag == null ? '': tag;
|
|
81
|
+
|
|
82
|
+
let r ={result:{"alias":"", dml:"", headers:"" },error: 1, code:404, msg:'', };
|
|
83
|
+
try{
|
|
84
|
+
r = await qson(options,db,query,dml,ip);
|
|
85
|
+
}catch(e){
|
|
86
|
+
r.result.headers = 'err';
|
|
87
|
+
r.data=[{'err':e}];
|
|
88
|
+
}finally {
|
|
89
|
+
try{
|
|
90
|
+
let code = r.result.code;
|
|
91
|
+
let msg = r.result.msg;
|
|
92
|
+
if (code!=200){
|
|
93
|
+
let qErr = "insert into aw_log (Sentencia, ErrorCode,Error, Fecha, id_pw, IP, alias) values (\""+query.replace(/\"/g,"\"\"")+"\",\""+code+"\",\""+msg.replace(/\"/g,"\"\"")+"\", NOW(), \""+tag+"\", \""+ip+"\", \""+db+"\" );";
|
|
94
|
+
try{
|
|
95
|
+
let rerr = await qson(options,db,qErr,dml,ip);
|
|
96
|
+
}catch(e){
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}catch(e){
|
|
100
|
+
utl.log('api.finally err '+e);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
return new Promise((resolve, reject)=>{
|
|
104
|
+
resolve (r);
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
module.exports ={
|
|
109
|
+
api,
|
|
110
|
+
qson
|
|
111
|
+
}
|