dbtabla 2.0.0 → 2.1.0
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/lib/Connect.js +39 -39
- package/lib/dbTabla.js +45 -45
- package/package.json +2 -2
package/lib/Connect.js
CHANGED
|
@@ -8,16 +8,16 @@ const procesingSql = require('./procesingSql')
|
|
|
8
8
|
* clase abstracta para crear la clase de conexion a la base de datos
|
|
9
9
|
*/
|
|
10
10
|
class Connect {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
11
|
+
_escapeChar;
|
|
12
|
+
_reserveIdentifiers;
|
|
13
|
+
_ar_aliased_tables;
|
|
14
|
+
_dbprefix;
|
|
15
|
+
_swap_pre;
|
|
16
|
+
_information_schema;
|
|
17
|
+
_models;
|
|
18
|
+
_caheTablas;
|
|
19
|
+
_createdTables;
|
|
20
|
+
_escapeString;
|
|
21
21
|
/**
|
|
22
22
|
* constructor de la clase
|
|
23
23
|
* @param {object} params - parametros de conexion
|
|
@@ -25,15 +25,15 @@ class Connect {
|
|
|
25
25
|
constructor(params,typeDB="")
|
|
26
26
|
{
|
|
27
27
|
this.config=params
|
|
28
|
-
this
|
|
29
|
-
this
|
|
30
|
-
this
|
|
31
|
-
this
|
|
32
|
-
this
|
|
33
|
-
this
|
|
34
|
-
this
|
|
28
|
+
this._escapeChar=""
|
|
29
|
+
this._reserveIdentifiers=["*"]
|
|
30
|
+
this._ar_aliased_tables=[]
|
|
31
|
+
this._dbprefix=""
|
|
32
|
+
this._swap_pre=""
|
|
33
|
+
this._information_schema = " "
|
|
34
|
+
this._models={}
|
|
35
35
|
this.typeDB=typeDB
|
|
36
|
-
this
|
|
36
|
+
this._caheTablas={}
|
|
37
37
|
}
|
|
38
38
|
/**
|
|
39
39
|
* retorna la configuracion para los helpers
|
|
@@ -42,13 +42,13 @@ class Connect {
|
|
|
42
42
|
helpersConf()
|
|
43
43
|
{
|
|
44
44
|
return {
|
|
45
|
-
escapeChar:this
|
|
46
|
-
reserveIdentifiers:this
|
|
47
|
-
ar_aliased_tables:this
|
|
48
|
-
dbprefix:this
|
|
49
|
-
swap_pre:this
|
|
45
|
+
escapeChar:this._escapeChar,
|
|
46
|
+
reserveIdentifiers:this._reserveIdentifiers,
|
|
47
|
+
ar_aliased_tables:this._ar_aliased_tables,
|
|
48
|
+
dbprefix:this._dbprefix,
|
|
49
|
+
swap_pre:this._swap_pre,
|
|
50
50
|
typeDB:this.typeDB,
|
|
51
|
-
escapeString:typeof this
|
|
51
|
+
escapeString:typeof this._escapeString==="function"?e=>this._escapeString(e):null
|
|
52
52
|
}
|
|
53
53
|
}
|
|
54
54
|
/**
|
|
@@ -59,7 +59,7 @@ class Connect {
|
|
|
59
59
|
model(tabla)
|
|
60
60
|
{
|
|
61
61
|
|
|
62
|
-
return this
|
|
62
|
+
return this._models[tabla] instanceof dbTablaModel?this._models[tabla]:false
|
|
63
63
|
}
|
|
64
64
|
/**
|
|
65
65
|
* agrega un modelo a la lista de modelos interna
|
|
@@ -69,11 +69,11 @@ class Connect {
|
|
|
69
69
|
{
|
|
70
70
|
if(model instanceof dbTablaModel)
|
|
71
71
|
{
|
|
72
|
-
this
|
|
72
|
+
this._models[model.tabla]=model
|
|
73
73
|
}else if(typeof model==="object" && model!==null){
|
|
74
|
-
this
|
|
74
|
+
this._models[model.tabla]= new dbTablaModel(model.tabla,model)
|
|
75
75
|
}else {
|
|
76
|
-
this
|
|
76
|
+
this._models[model.tabla]= new dbTablaModel(model,this.helpersConf().escapeChar)
|
|
77
77
|
}
|
|
78
78
|
|
|
79
79
|
}
|
|
@@ -123,12 +123,12 @@ class Connect {
|
|
|
123
123
|
*/
|
|
124
124
|
tabla(tabla,callback,verify=false)
|
|
125
125
|
{
|
|
126
|
-
if(typeof this
|
|
126
|
+
if(typeof this._caheTablas[tabla]!=="undefined")
|
|
127
127
|
{
|
|
128
|
-
typeof callback==="function"?callback(this
|
|
129
|
-
return this
|
|
128
|
+
typeof callback==="function"?callback(this._caheTablas[tabla]):null
|
|
129
|
+
return this._caheTablas[tabla]
|
|
130
130
|
}
|
|
131
|
-
return this
|
|
131
|
+
return this._caheTablas[tabla] = new dbTabla({
|
|
132
132
|
tabla:tabla,
|
|
133
133
|
connection:this,
|
|
134
134
|
callback:callback,
|
|
@@ -159,7 +159,7 @@ class Connect {
|
|
|
159
159
|
* @param {string} table - nombre de la tabla
|
|
160
160
|
* @return {Promise}
|
|
161
161
|
*/
|
|
162
|
-
|
|
162
|
+
_keysInTable(table)
|
|
163
163
|
{
|
|
164
164
|
return new Promise((res)=>
|
|
165
165
|
{
|
|
@@ -190,7 +190,7 @@ class Connect {
|
|
|
190
190
|
if(create)
|
|
191
191
|
{
|
|
192
192
|
|
|
193
|
-
this
|
|
193
|
+
this._createTable(this.model(tabla))
|
|
194
194
|
.then(res).catch(rej)
|
|
195
195
|
}else
|
|
196
196
|
{
|
|
@@ -206,12 +206,12 @@ class Connect {
|
|
|
206
206
|
* @param {dbTablaModel} model - objeto del modelo
|
|
207
207
|
* @return {Promise}
|
|
208
208
|
*/
|
|
209
|
-
|
|
209
|
+
_createTable(model)
|
|
210
210
|
{
|
|
211
211
|
|
|
212
212
|
return new Promise((res,rej)=>
|
|
213
213
|
{
|
|
214
|
-
this
|
|
214
|
+
this._createdTables={}
|
|
215
215
|
let rescursive=(foreingKey,i)=>
|
|
216
216
|
{
|
|
217
217
|
|
|
@@ -227,8 +227,8 @@ class Connect {
|
|
|
227
227
|
}else {
|
|
228
228
|
|
|
229
229
|
this.query(model.sql(this)).then(()=>{
|
|
230
|
-
this
|
|
231
|
-
this
|
|
230
|
+
this._createdTables[model.tabla]=true
|
|
231
|
+
this._initializeTable(model.tabla).then(()=>
|
|
232
232
|
{
|
|
233
233
|
|
|
234
234
|
res(model.getData())
|
|
@@ -239,7 +239,7 @@ class Connect {
|
|
|
239
239
|
rescursive(model.foreingKey(),0)
|
|
240
240
|
})
|
|
241
241
|
}
|
|
242
|
-
|
|
242
|
+
_initializeTable(tab)
|
|
243
243
|
{
|
|
244
244
|
return new Promise(async(res)=>
|
|
245
245
|
{
|
package/lib/dbTabla.js
CHANGED
|
@@ -6,13 +6,13 @@ const dbTablaError = require("./dbTablaError")
|
|
|
6
6
|
* representacion de una tabla en la base de datos
|
|
7
7
|
*/
|
|
8
8
|
class dbTabla {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
9
|
+
_connection;
|
|
10
|
+
_lastSql;
|
|
11
|
+
_keys;
|
|
12
|
+
_callbackKey;
|
|
13
|
+
_config;
|
|
14
14
|
|
|
15
|
-
get lastSql() { return this
|
|
15
|
+
get lastSql() { return this._lastSql; }
|
|
16
16
|
|
|
17
17
|
/**
|
|
18
18
|
* @param {object} tabla, connection, callback, config
|
|
@@ -20,15 +20,15 @@ class dbTabla {
|
|
|
20
20
|
*/
|
|
21
21
|
constructor({tabla,connection,callback,config},ret=false)
|
|
22
22
|
{
|
|
23
|
-
this
|
|
24
|
-
this
|
|
25
|
-
this
|
|
23
|
+
this._connection=connection
|
|
24
|
+
this._lastSql=""
|
|
25
|
+
this._keys=false
|
|
26
26
|
this.tabla=tabla || null
|
|
27
|
-
this
|
|
28
|
-
this
|
|
27
|
+
this._callbackKey=callback ||( e=>e)
|
|
28
|
+
this._config=config || {}
|
|
29
29
|
this.typeDB=connection.typeDB
|
|
30
30
|
if(ret)
|
|
31
|
-
this
|
|
31
|
+
this._verifyKeys()
|
|
32
32
|
}
|
|
33
33
|
/**
|
|
34
34
|
* agrega la propiedad $sql que continen
|
|
@@ -36,13 +36,13 @@ class dbTabla {
|
|
|
36
36
|
* @param {object}
|
|
37
37
|
* @return {object}
|
|
38
38
|
*/
|
|
39
|
-
|
|
39
|
+
_PropertyOk(ok)
|
|
40
40
|
{
|
|
41
41
|
if(typeof ok ==="object")
|
|
42
42
|
Object.defineProperty(ok, "$sql", {
|
|
43
43
|
configurable : true,
|
|
44
44
|
enumerable : false,
|
|
45
|
-
value : this
|
|
45
|
+
value : this._lastSql,
|
|
46
46
|
})
|
|
47
47
|
return ok
|
|
48
48
|
}
|
|
@@ -50,28 +50,28 @@ class dbTabla {
|
|
|
50
50
|
* verifica la existencia de la tabla y obtiene los metadatos
|
|
51
51
|
* @param {function} call
|
|
52
52
|
*/
|
|
53
|
-
|
|
53
|
+
_verifyKeys(call=e=>e)
|
|
54
54
|
{
|
|
55
|
-
if(!this
|
|
55
|
+
if(!this._keys)
|
|
56
56
|
{
|
|
57
|
-
this
|
|
57
|
+
this._connection.__keysInTable(this.tabla)
|
|
58
58
|
.then(keys=>
|
|
59
59
|
{
|
|
60
60
|
this.colum=keys.colum
|
|
61
|
-
this.sql=new procesingSql(keys, this
|
|
62
|
-
this
|
|
61
|
+
this.sql=new procesingSql(keys, this._config)
|
|
62
|
+
this._keys=true
|
|
63
63
|
if(keys.methods)
|
|
64
64
|
for(let i in keys.methods)
|
|
65
65
|
{
|
|
66
|
-
this[i]=(...params)=>keys.methods[i](this,this
|
|
66
|
+
this[i]=(...params)=>keys.methods[i](this,this._connection,...params)
|
|
67
67
|
}
|
|
68
68
|
|
|
69
|
-
this
|
|
69
|
+
this._callbackKey(this)
|
|
70
70
|
call()
|
|
71
71
|
}).catch(e=>{
|
|
72
72
|
|
|
73
73
|
|
|
74
|
-
this
|
|
74
|
+
this._callbackKey(null,e)
|
|
75
75
|
call(e)
|
|
76
76
|
|
|
77
77
|
|
|
@@ -86,7 +86,7 @@ class dbTabla {
|
|
|
86
86
|
* @param {array|object} resultado obtenido de la consulta ejecutada
|
|
87
87
|
* @return {dbResult}
|
|
88
88
|
*/
|
|
89
|
-
|
|
89
|
+
_formatResult(result)
|
|
90
90
|
{
|
|
91
91
|
return new dbResult(this,result)
|
|
92
92
|
}
|
|
@@ -106,7 +106,7 @@ class dbTabla {
|
|
|
106
106
|
return new Promise((resolve,reject)=>
|
|
107
107
|
{
|
|
108
108
|
|
|
109
|
-
this
|
|
109
|
+
this._verifyKeys(e=>
|
|
110
110
|
{
|
|
111
111
|
if(e)
|
|
112
112
|
{
|
|
@@ -114,14 +114,14 @@ class dbTabla {
|
|
|
114
114
|
}
|
|
115
115
|
if(params instanceof Array && params.length==1)
|
|
116
116
|
{
|
|
117
|
-
this
|
|
117
|
+
this._lastSql=this.sql.insert(params[0])
|
|
118
118
|
}else {
|
|
119
|
-
this
|
|
119
|
+
this._lastSql=this.sql.insert(params)
|
|
120
120
|
}
|
|
121
121
|
|
|
122
|
-
this
|
|
122
|
+
this._connection.query(this._lastSql).then(d=>{
|
|
123
123
|
|
|
124
|
-
resolve(this
|
|
124
|
+
resolve(this._PropertyOk(d))
|
|
125
125
|
}).catch(e=>reject(e))
|
|
126
126
|
})
|
|
127
127
|
})
|
|
@@ -158,15 +158,15 @@ class dbTabla {
|
|
|
158
158
|
{
|
|
159
159
|
return new Promise((resolve,reject)=>
|
|
160
160
|
{
|
|
161
|
-
this
|
|
161
|
+
this._verifyKeys(e=>
|
|
162
162
|
{
|
|
163
163
|
if(e)
|
|
164
164
|
{
|
|
165
165
|
return reject(e)
|
|
166
166
|
}
|
|
167
|
-
this
|
|
168
|
-
this
|
|
169
|
-
resolve(this
|
|
167
|
+
this._lastSql = this.sql.select(...params)
|
|
168
|
+
this._connection.query(this._lastSql).then(d=>{
|
|
169
|
+
resolve(this._formatResult(d))
|
|
170
170
|
}).catch(e=>reject(e))
|
|
171
171
|
})
|
|
172
172
|
|
|
@@ -278,11 +278,11 @@ class dbTabla {
|
|
|
278
278
|
{
|
|
279
279
|
return new Promise((resolve,reject)=>
|
|
280
280
|
{
|
|
281
|
-
this
|
|
281
|
+
this._verifyKeys(()=>
|
|
282
282
|
{
|
|
283
|
-
this
|
|
284
|
-
this
|
|
285
|
-
resolve(this
|
|
283
|
+
this._lastSql = this.sql.busqueda(cadena,campo_bus,campos,where,joins,group,having,order,limit)
|
|
284
|
+
this._connection.query(this._lastSql).then(d=>{
|
|
285
|
+
resolve(this._formatResult(d))
|
|
286
286
|
}).catch(e=>reject(e))
|
|
287
287
|
})
|
|
288
288
|
|
|
@@ -331,18 +331,18 @@ class dbTabla {
|
|
|
331
331
|
}
|
|
332
332
|
return new Promise((resolve,reject)=>
|
|
333
333
|
{
|
|
334
|
-
this
|
|
334
|
+
this._verifyKeys(e=>
|
|
335
335
|
{
|
|
336
336
|
if(e)
|
|
337
337
|
{
|
|
338
338
|
return reject(e)
|
|
339
339
|
}
|
|
340
|
-
this
|
|
341
|
-
if( this
|
|
340
|
+
this._lastSql =this.sql.update(sets,where,reject)
|
|
341
|
+
if( this._lastSql)
|
|
342
342
|
{
|
|
343
|
-
this
|
|
343
|
+
this._connection.query(this._lastSql).then(d=>{
|
|
344
344
|
|
|
345
|
-
resolve(this
|
|
345
|
+
resolve(this._PropertyOk(d))
|
|
346
346
|
}).catch(e=>reject(e))
|
|
347
347
|
}
|
|
348
348
|
})
|
|
@@ -384,16 +384,16 @@ class dbTabla {
|
|
|
384
384
|
}
|
|
385
385
|
return new Promise((resolve,reject)=>
|
|
386
386
|
{
|
|
387
|
-
this
|
|
387
|
+
this._verifyKeys(e=>
|
|
388
388
|
{
|
|
389
389
|
if(e)
|
|
390
390
|
{
|
|
391
391
|
return reject(e)
|
|
392
392
|
}
|
|
393
|
-
this
|
|
394
|
-
this
|
|
393
|
+
this._lastSql =this.sql.delete(where)
|
|
394
|
+
this._connection.query(this._lastSql).then(d=>{
|
|
395
395
|
|
|
396
|
-
resolve(this
|
|
396
|
+
resolve(this._PropertyOk(d))
|
|
397
397
|
}).catch(e=>reject(e))
|
|
398
398
|
})
|
|
399
399
|
})
|
package/package.json
CHANGED