dbtabla 2.1.0 → 2.1.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/lib/dbTabla.js +396 -396
- package/package.json +1 -1
- package/test_results.txt +65 -0
package/lib/dbTabla.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
const dbResult= require("./dbResult")
|
|
2
|
-
const procesingSql = require("./procesingSql")
|
|
3
|
-
const dbTablaError = require("./dbTablaError")
|
|
4
|
-
/**
|
|
5
|
-
* dbTabla
|
|
6
|
-
* representacion de una tabla en la base de datos
|
|
7
|
-
*/
|
|
1
|
+
const dbResult= require("./dbResult")
|
|
2
|
+
const procesingSql = require("./procesingSql")
|
|
3
|
+
const dbTablaError = require("./dbTablaError")
|
|
4
|
+
/**
|
|
5
|
+
* dbTabla
|
|
6
|
+
* representacion de una tabla en la base de datos
|
|
7
|
+
*/
|
|
8
8
|
class dbTabla {
|
|
9
9
|
_connection;
|
|
10
10
|
_lastSql;
|
|
@@ -13,392 +13,392 @@ class dbTabla {
|
|
|
13
13
|
_config;
|
|
14
14
|
|
|
15
15
|
get lastSql() { return this._lastSql; }
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* @param {object} tabla, connection, callback, config
|
|
19
|
-
* @param {boolean} ret indica si verificara la existencia de la tabla o esperara la primera consulta
|
|
20
|
-
*/
|
|
21
|
-
constructor({tabla,connection,callback,config},ret=false)
|
|
22
|
-
{
|
|
23
|
-
this._connection=connection
|
|
24
|
-
this._lastSql=""
|
|
25
|
-
this._keys=false
|
|
26
|
-
this.tabla=tabla || null
|
|
27
|
-
this._callbackKey=callback ||( e=>e)
|
|
28
|
-
this._config=config || {}
|
|
29
|
-
this.typeDB=connection.typeDB
|
|
30
|
-
if(ret)
|
|
31
|
-
this._verifyKeys()
|
|
32
|
-
}
|
|
33
|
-
/**
|
|
34
|
-
* agrega la propiedad $sql que continen
|
|
35
|
-
* el sql de la ultima consulta al objeto pasado
|
|
36
|
-
* @param {object}
|
|
37
|
-
* @return {object}
|
|
38
|
-
*/
|
|
39
|
-
_PropertyOk(ok)
|
|
40
|
-
{
|
|
41
|
-
if(typeof ok ==="object")
|
|
42
|
-
Object.defineProperty(ok, "$sql", {
|
|
43
|
-
configurable : true,
|
|
44
|
-
enumerable : false,
|
|
45
|
-
value : this._lastSql,
|
|
46
|
-
})
|
|
47
|
-
return ok
|
|
48
|
-
}
|
|
49
|
-
/**
|
|
50
|
-
* verifica la existencia de la tabla y obtiene los metadatos
|
|
51
|
-
* @param {function} call
|
|
52
|
-
*/
|
|
53
|
-
_verifyKeys(call=e=>e)
|
|
54
|
-
{
|
|
55
|
-
if(!this._keys)
|
|
56
|
-
{
|
|
57
|
-
this._connection.
|
|
58
|
-
.then(keys=>
|
|
59
|
-
{
|
|
60
|
-
this.colum=keys.colum
|
|
61
|
-
this.sql=new procesingSql(keys, this._config)
|
|
62
|
-
this._keys=true
|
|
63
|
-
if(keys.methods)
|
|
64
|
-
for(let i in keys.methods)
|
|
65
|
-
{
|
|
66
|
-
this[i]=(...params)=>keys.methods[i](this,this._connection,...params)
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
this._callbackKey(this)
|
|
70
|
-
call()
|
|
71
|
-
}).catch(e=>{
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
this._callbackKey(null,e)
|
|
75
|
-
call(e)
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
})
|
|
79
|
-
|
|
80
|
-
}else {
|
|
81
|
-
call()
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
/**
|
|
85
|
-
* construlle el objeto de resultado para consultas select
|
|
86
|
-
* @param {array|object} resultado obtenido de la consulta ejecutada
|
|
87
|
-
* @return {dbResult}
|
|
88
|
-
*/
|
|
89
|
-
_formatResult(result)
|
|
90
|
-
{
|
|
91
|
-
return new dbResult(this,result)
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
/**
|
|
96
|
-
* envia una sentencia insert a la base de datos
|
|
97
|
-
* @param {array|object|string} ...campos - campos a insertar
|
|
98
|
-
* @retuen {Promise}
|
|
99
|
-
*/
|
|
100
|
-
insert(...params)
|
|
101
|
-
{
|
|
102
|
-
if(params.length===0)
|
|
103
|
-
{
|
|
104
|
-
throw new dbTablaError("debe pasar almenos un parametro")
|
|
105
|
-
}
|
|
106
|
-
return new Promise((resolve,reject)=>
|
|
107
|
-
{
|
|
108
|
-
|
|
109
|
-
this._verifyKeys(e=>
|
|
110
|
-
{
|
|
111
|
-
if(e)
|
|
112
|
-
{
|
|
113
|
-
return reject(e)
|
|
114
|
-
}
|
|
115
|
-
if(params instanceof Array && params.length==1)
|
|
116
|
-
{
|
|
117
|
-
this._lastSql=this.sql.insert(params[0])
|
|
118
|
-
}else {
|
|
119
|
-
this._lastSql=this.sql.insert(params)
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
this._connection.query(this._lastSql).then(d=>{
|
|
123
|
-
|
|
124
|
-
resolve(this._PropertyOk(d))
|
|
125
|
-
}).catch(e=>reject(e))
|
|
126
|
-
})
|
|
127
|
-
})
|
|
128
|
-
}
|
|
129
|
-
/**
|
|
130
|
-
* envia una sentencia select a la base de datos
|
|
131
|
-
* @param {array|object|string} campos - campos de la tabla que se seleccionaran si es un object
|
|
132
|
-
* se tomara con el parametro join si es un string entonces se utilisara como el parametro
|
|
133
|
-
* where y todos los parametros se correran hacia la izquierda
|
|
134
|
-
* @param {object|string} joins - clausulas jois utilizando un object con las tablas que se
|
|
135
|
-
* relacionaran ateponiendo los singnos <,>,= al nombre de la tabla foranea
|
|
136
|
-
* < sera para left join
|
|
137
|
-
* > sera para ringt join
|
|
138
|
-
* = sera innert join
|
|
139
|
-
* si no se antepone nada sera natural join
|
|
140
|
-
* sera tomado de las claves primarias que contengan las tablas para la clausula using
|
|
141
|
-
* para la clausula 'on' o 'using' se coloca el nombre de la tabla como atributo
|
|
142
|
-
* y el valor 'using' si es uno como valor string si son varios en un array
|
|
143
|
-
* y para la exprecion 'on' un string con la exprecion
|
|
144
|
-
* si joins es un string entonces se utilisara el parametro where y
|
|
145
|
-
* todos los parametros se correran hacia la izquierda
|
|
146
|
-
* @param {string|object} where - clausula where desde este parametro se puede indicar
|
|
147
|
-
* explicitamente las clausulas GROUP BY, OERDER BY, HAVING, LIMIT y los parametros siguentes se correran hacia la izquierda
|
|
148
|
-
* segun la posicion del parametro indicado
|
|
149
|
-
* ejemplo: <em>si en el parametro where se coloca grup by micampo entonces where sera tomado
|
|
150
|
-
* como group y group como having y asi sucesivamente </em>
|
|
151
|
-
* @param {string} group - clausula group
|
|
152
|
-
* @param {string} having - clausula having
|
|
153
|
-
* @param {string} order - clausula order
|
|
154
|
-
* @param {string} limit - clausula limit
|
|
155
|
-
* @return {Promise} la sentencia sql generada
|
|
156
|
-
*/
|
|
157
|
-
select(...params)
|
|
158
|
-
{
|
|
159
|
-
return new Promise((resolve,reject)=>
|
|
160
|
-
{
|
|
161
|
-
this._verifyKeys(e=>
|
|
162
|
-
{
|
|
163
|
-
if(e)
|
|
164
|
-
{
|
|
165
|
-
return reject(e)
|
|
166
|
-
}
|
|
167
|
-
this._lastSql = this.sql.select(...params)
|
|
168
|
-
this._connection.query(this._lastSql).then(d=>{
|
|
169
|
-
resolve(this._formatResult(d))
|
|
170
|
-
}).catch(e=>reject(e))
|
|
171
|
-
})
|
|
172
|
-
|
|
173
|
-
})
|
|
174
|
-
}
|
|
175
|
-
/**
|
|
176
|
-
* envia una sentencia select a la base de datos y obtiene la primera fila
|
|
177
|
-
* @param {array|object|string} campos - campos de la tabla que se seleccionaran si es un object
|
|
178
|
-
* se tomara con el parametro join si es un string entonces se utilisara como el parametro
|
|
179
|
-
* where y todos los parametros se correran hacia la izquierda
|
|
180
|
-
* @param {object|string} joins - clausulas jois utilizando un object con las tablas que se
|
|
181
|
-
* relacionaran ateponiendo los singnos <,>,= al nombre de la tabla foranea
|
|
182
|
-
* < sera para left join
|
|
183
|
-
* > sera para ringt join
|
|
184
|
-
* = sera innert join
|
|
185
|
-
* si no se antepone nada sera natural join
|
|
186
|
-
* sera tomado de las claves primarias que contengan las tablas para la clausula using
|
|
187
|
-
* para la clausula 'on' o 'using' se coloca el nombre de la tabla como atributo
|
|
188
|
-
* y el valor 'using' si es uno como valor string si son varios en un array
|
|
189
|
-
* y para la exprecion 'on' un string con la exprecion
|
|
190
|
-
* si joins es un string entonces se utilisara el parametro where y
|
|
191
|
-
* todos los parametros se correran hacia la izquierda
|
|
192
|
-
* @param {string|object} where - clausula where desde este parametro se puede indicar
|
|
193
|
-
* explicitamente las clausulas GROUP BY, OERDER BY, HAVING, LIMIT y los parametros siguentes se correran hacia la izquierda
|
|
194
|
-
* segun la posicion del parametro indicado
|
|
195
|
-
* ejemplo: <em>si en el parametro where se coloca grup by micampo entonces where sera tomado
|
|
196
|
-
* como group y group como having y asi sucesivamente </em>
|
|
197
|
-
* @param {string} group - clausula group
|
|
198
|
-
* @param {string} having - clausula having
|
|
199
|
-
* @param {string} order - clausula order
|
|
200
|
-
* @return {Promise} la sentencia sql generada
|
|
201
|
-
*/
|
|
202
|
-
selectOne(campos,joins,where,group,having,order)
|
|
203
|
-
{
|
|
204
|
-
return this.select(campos,joins,where,group,having,order,1).then(d=>typeof d[0]!==undefined?d[0]:null)
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
/**
|
|
208
|
-
* selecciona un elemento de la tabla comparando la primera clave primaria con el
|
|
209
|
-
* parametro proporcionado
|
|
210
|
-
* @param {array|string} campos - campos de la tabla que se seleccionaran si es un
|
|
211
|
-
* object entonces se utilisara como la clausula join y todos los parametros se
|
|
212
|
-
* correran hacia la izquierda
|
|
213
|
-
* @param {object|string} joins - clausulas jois utilizando un object con las tablas que se
|
|
214
|
-
* relacionaran ateponiendo los singnos <,>,= al nombre de la tabla foranea
|
|
215
|
-
* < sera para left join
|
|
216
|
-
* > sera para ringt join
|
|
217
|
-
* = sera innert join
|
|
218
|
-
* si no se antepone nada sera natural join
|
|
219
|
-
* sera tomado de las claves primarias que contengan las tablas para la clausula using
|
|
220
|
-
* para la clausula 'on' o 'using' se coloca el nombre de la tabla como atributo
|
|
221
|
-
* y el valor 'using' si es uno como valor string si son varios en un array
|
|
222
|
-
* y para la exprecion 'on' un string con la exprecion
|
|
223
|
-
* si joins no es un objeto entonces se utilisara como el parametro id
|
|
224
|
-
* @param {string|numeric} id - clave primaria a buscar
|
|
225
|
-
* @return {Promise}
|
|
226
|
-
*/
|
|
227
|
-
selectById(campos,joins,id)
|
|
228
|
-
{
|
|
229
|
-
if(typeof campos==="string" || typeof campos==="number")
|
|
230
|
-
{
|
|
231
|
-
id=campos
|
|
232
|
-
joins=undefined
|
|
233
|
-
campos=undefined
|
|
234
|
-
}
|
|
235
|
-
if(typeof campos==="string" || typeof campos==="number")
|
|
236
|
-
{
|
|
237
|
-
id=joins
|
|
238
|
-
joins=campos
|
|
239
|
-
campos=undefined
|
|
240
|
-
}
|
|
241
|
-
if(id===undefined)
|
|
242
|
-
{
|
|
243
|
-
throw new dbTablaError("falta el parametro id no es opcional")
|
|
244
|
-
}
|
|
245
|
-
return this.selectOne(campos,joins,this.sql.whereId(id))
|
|
246
|
-
}
|
|
247
|
-
/**
|
|
248
|
-
* envia una sentencia select a la base de datos con un sencillo algoritmo de busqueda
|
|
249
|
-
* @param {string} cadena - palabra o serie de palabras a buscar en la tabla
|
|
250
|
-
* @param {array} campo_bus - campos en los que se buscara el contenido del primer parametro
|
|
251
|
-
* @param {array|object|string} campos - campos de la tabla que se seleccionaran si es un object
|
|
252
|
-
* se tomara con el parametro join si es un string entonces se utilisara como el parametro
|
|
253
|
-
* where y todos los parametros se correran hacia la izquierda
|
|
254
|
-
* @param {object|string} joins - clausulas jois utilizando un object con las tablas que se
|
|
255
|
-
* relacionaran ateponiendo los singnos <,>,= al nombre de la tabla foranea
|
|
256
|
-
* < sera para left join
|
|
257
|
-
* > sera para ringt join
|
|
258
|
-
* = sera innert join
|
|
259
|
-
* si no se antepone nada sera natural join
|
|
260
|
-
* sera tomado de las claves primarias que contengan las tablas para la clausula using
|
|
261
|
-
* para la clausula 'on' o 'using' se coloca el nombre de la tabla como atributo
|
|
262
|
-
* y el valor 'using' si es uno como valor string si son varios en un array
|
|
263
|
-
* y para la exprecion 'on' un string con la exprecion
|
|
264
|
-
* si joins es un string entonces se utilisara el parametro where y
|
|
265
|
-
* todos los parametros se correran hacia la izquierda
|
|
266
|
-
* @param {string|object} where - clausula where desde este parametro se puede indicar
|
|
267
|
-
* explicitamente las clausulas GROUP BY, OERDER BY, HAVING, LIMIT y los parametros siguentes se correran hacia la izquierda
|
|
268
|
-
* segun la posicion del parametro indicado
|
|
269
|
-
* ejemplo: <em>si en el parametro where se coloca grup by micampo entonces where sera tomado
|
|
270
|
-
* como group y group como having y asi sucesivamente </em>
|
|
271
|
-
* @param {string} group - clausula group
|
|
272
|
-
* @param {string} having - clausula having
|
|
273
|
-
* @param {string} order - clausula order
|
|
274
|
-
* @param {string} limit - clausula limit
|
|
275
|
-
* @return {string} la sentencia sql generada
|
|
276
|
-
*/
|
|
277
|
-
busqueda(cadena,campo_bus,campos,where,joins,group,having,order,limit)
|
|
278
|
-
{
|
|
279
|
-
return new Promise((resolve,reject)=>
|
|
280
|
-
{
|
|
281
|
-
this._verifyKeys(()=>
|
|
282
|
-
{
|
|
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
|
-
}).catch(e=>reject(e))
|
|
287
|
-
})
|
|
288
|
-
|
|
289
|
-
})
|
|
290
|
-
}
|
|
291
|
-
/**
|
|
292
|
-
* envia una sentencia update en la base de datos
|
|
293
|
-
* que edita la fila con la clave primaria pasada en el segundo parametro
|
|
294
|
-
* @param {object} sets - elementos a modificar
|
|
295
|
-
* @param {string|numeric} id - clave primaria
|
|
296
|
-
* @return {Promise}
|
|
297
|
-
*/
|
|
298
|
-
updateById(sets,id)
|
|
299
|
-
{
|
|
300
|
-
if(sets===undefined )
|
|
301
|
-
{
|
|
302
|
-
throw new dbTablaError("Falta el parametro numero 1 sets no es opcional")
|
|
303
|
-
}else if(typeof sets!=="object")
|
|
304
|
-
{
|
|
305
|
-
throw new dbTablaError("El parametro numero 1 sets debe ser un de tipo object")
|
|
306
|
-
}
|
|
307
|
-
if(id===undefined)
|
|
308
|
-
{
|
|
309
|
-
throw new dbTablaError("falta el parametro numero 2 id no es opcional")
|
|
310
|
-
}
|
|
311
|
-
return this.update(sets,this.sql.whereId(id))
|
|
312
|
-
}
|
|
313
|
-
/**
|
|
314
|
-
* envia una sentencia update en la base de datos
|
|
315
|
-
* @param {object} sets - elementos a modificar
|
|
316
|
-
* @param {string|object} where - exprecion booleana sql
|
|
317
|
-
* @return {Promise}
|
|
318
|
-
*/
|
|
319
|
-
update(sets,where)
|
|
320
|
-
{
|
|
321
|
-
if(sets===undefined )
|
|
322
|
-
{
|
|
323
|
-
throw new dbTablaError("Falta el parametro numero 1 sets no es opcional")
|
|
324
|
-
}else if(typeof sets!=="object")
|
|
325
|
-
{
|
|
326
|
-
throw new dbTablaError("El parametro numero 1 sets debe ser un de tipo object")
|
|
327
|
-
}
|
|
328
|
-
if(where===undefined)
|
|
329
|
-
{
|
|
330
|
-
throw new dbTablaError("falta el parametro numero 2 where no es opcional")
|
|
331
|
-
}
|
|
332
|
-
return new Promise((resolve,reject)=>
|
|
333
|
-
{
|
|
334
|
-
this._verifyKeys(e=>
|
|
335
|
-
{
|
|
336
|
-
if(e)
|
|
337
|
-
{
|
|
338
|
-
return reject(e)
|
|
339
|
-
}
|
|
340
|
-
this._lastSql =this.sql.update(sets,where,reject)
|
|
341
|
-
if( this._lastSql)
|
|
342
|
-
{
|
|
343
|
-
this._connection.query(this._lastSql).then(d=>{
|
|
344
|
-
|
|
345
|
-
resolve(this._PropertyOk(d))
|
|
346
|
-
}).catch(e=>reject(e))
|
|
347
|
-
}
|
|
348
|
-
})
|
|
349
|
-
})
|
|
350
|
-
}
|
|
351
|
-
/**
|
|
352
|
-
* genera una sentencia sql deleteen la base de datos
|
|
353
|
-
* que elimina la fila con la clave primaria pasada en el segundo parametro
|
|
354
|
-
* @param {object} sets - elementos a modificar
|
|
355
|
-
* @param {string|numeric} id - clave prpimaria a eliminar
|
|
356
|
-
* @return {Promise}
|
|
357
|
-
*/
|
|
358
|
-
deleteById(sets,id)
|
|
359
|
-
{
|
|
360
|
-
if(sets===undefined )
|
|
361
|
-
{
|
|
362
|
-
throw new dbTablaError("Falta el parametro numero1 sets no es opcional")
|
|
363
|
-
}else if(typeof sets!=="object")
|
|
364
|
-
{
|
|
365
|
-
throw new dbTablaError("El parametro numero 1 sets debe ser un de tipo object")
|
|
366
|
-
}
|
|
367
|
-
if(id===undefined)
|
|
368
|
-
{
|
|
369
|
-
throw new dbTablaError("falta el parametro numero 2 id no es opcional")
|
|
370
|
-
}
|
|
371
|
-
return this.delete(sets,this.sql.whereId(id))
|
|
372
|
-
}
|
|
373
|
-
/**
|
|
374
|
-
* genera una sentencia sql delete en la base de datos
|
|
375
|
-
* @param {string|object} where - exprecion booleana sql
|
|
376
|
-
* @return {Promise} - sentencia sql
|
|
377
|
-
*/
|
|
378
|
-
|
|
379
|
-
delete(where)
|
|
380
|
-
{
|
|
381
|
-
if(where===undefined)
|
|
382
|
-
{
|
|
383
|
-
throw new dbTablaError("falta el parametro 1 where no es opcional")
|
|
384
|
-
}
|
|
385
|
-
return new Promise((resolve,reject)=>
|
|
386
|
-
{
|
|
387
|
-
this._verifyKeys(e=>
|
|
388
|
-
{
|
|
389
|
-
if(e)
|
|
390
|
-
{
|
|
391
|
-
return reject(e)
|
|
392
|
-
}
|
|
393
|
-
this._lastSql =this.sql.delete(where)
|
|
394
|
-
this._connection.query(this._lastSql).then(d=>{
|
|
395
|
-
|
|
396
|
-
resolve(this._PropertyOk(d))
|
|
397
|
-
}).catch(e=>reject(e))
|
|
398
|
-
})
|
|
399
|
-
})
|
|
400
|
-
}
|
|
401
|
-
|
|
402
|
-
}
|
|
403
|
-
|
|
404
|
-
module.exports=dbTabla
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* @param {object} tabla, connection, callback, config
|
|
19
|
+
* @param {boolean} ret indica si verificara la existencia de la tabla o esperara la primera consulta
|
|
20
|
+
*/
|
|
21
|
+
constructor({tabla,connection,callback,config},ret=false)
|
|
22
|
+
{
|
|
23
|
+
this._connection=connection
|
|
24
|
+
this._lastSql=""
|
|
25
|
+
this._keys=false
|
|
26
|
+
this.tabla=tabla || null
|
|
27
|
+
this._callbackKey=callback ||( e=>e)
|
|
28
|
+
this._config=config || {}
|
|
29
|
+
this.typeDB=connection.typeDB
|
|
30
|
+
if(ret)
|
|
31
|
+
this._verifyKeys()
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* agrega la propiedad $sql que continen
|
|
35
|
+
* el sql de la ultima consulta al objeto pasado
|
|
36
|
+
* @param {object}
|
|
37
|
+
* @return {object}
|
|
38
|
+
*/
|
|
39
|
+
_PropertyOk(ok)
|
|
40
|
+
{
|
|
41
|
+
if(typeof ok ==="object")
|
|
42
|
+
Object.defineProperty(ok, "$sql", {
|
|
43
|
+
configurable : true,
|
|
44
|
+
enumerable : false,
|
|
45
|
+
value : this._lastSql,
|
|
46
|
+
})
|
|
47
|
+
return ok
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* verifica la existencia de la tabla y obtiene los metadatos
|
|
51
|
+
* @param {function} call
|
|
52
|
+
*/
|
|
53
|
+
_verifyKeys(call=e=>e)
|
|
54
|
+
{
|
|
55
|
+
if(!this._keys)
|
|
56
|
+
{
|
|
57
|
+
this._connection._keysInTable(this.tabla)
|
|
58
|
+
.then(keys=>
|
|
59
|
+
{
|
|
60
|
+
this.colum=keys.colum
|
|
61
|
+
this.sql=new procesingSql(keys, this._config)
|
|
62
|
+
this._keys=true
|
|
63
|
+
if(keys.methods)
|
|
64
|
+
for(let i in keys.methods)
|
|
65
|
+
{
|
|
66
|
+
this[i]=(...params)=>keys.methods[i](this,this._connection,...params)
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
this._callbackKey(this)
|
|
70
|
+
call()
|
|
71
|
+
}).catch(e=>{
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
this._callbackKey(null,e)
|
|
75
|
+
call(e)
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
}else {
|
|
81
|
+
call()
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* construlle el objeto de resultado para consultas select
|
|
86
|
+
* @param {array|object} resultado obtenido de la consulta ejecutada
|
|
87
|
+
* @return {dbResult}
|
|
88
|
+
*/
|
|
89
|
+
_formatResult(result)
|
|
90
|
+
{
|
|
91
|
+
return new dbResult(this,result)
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* envia una sentencia insert a la base de datos
|
|
97
|
+
* @param {array|object|string} ...campos - campos a insertar
|
|
98
|
+
* @retuen {Promise}
|
|
99
|
+
*/
|
|
100
|
+
insert(...params)
|
|
101
|
+
{
|
|
102
|
+
if(params.length===0)
|
|
103
|
+
{
|
|
104
|
+
throw new dbTablaError("debe pasar almenos un parametro")
|
|
105
|
+
}
|
|
106
|
+
return new Promise((resolve,reject)=>
|
|
107
|
+
{
|
|
108
|
+
|
|
109
|
+
this._verifyKeys(e=>
|
|
110
|
+
{
|
|
111
|
+
if(e)
|
|
112
|
+
{
|
|
113
|
+
return reject(e)
|
|
114
|
+
}
|
|
115
|
+
if(params instanceof Array && params.length==1)
|
|
116
|
+
{
|
|
117
|
+
this._lastSql=this.sql.insert(params[0])
|
|
118
|
+
}else {
|
|
119
|
+
this._lastSql=this.sql.insert(params)
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
this._connection.query(this._lastSql).then(d=>{
|
|
123
|
+
|
|
124
|
+
resolve(this._PropertyOk(d))
|
|
125
|
+
}).catch(e=>reject(e))
|
|
126
|
+
})
|
|
127
|
+
})
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* envia una sentencia select a la base de datos
|
|
131
|
+
* @param {array|object|string} campos - campos de la tabla que se seleccionaran si es un object
|
|
132
|
+
* se tomara con el parametro join si es un string entonces se utilisara como el parametro
|
|
133
|
+
* where y todos los parametros se correran hacia la izquierda
|
|
134
|
+
* @param {object|string} joins - clausulas jois utilizando un object con las tablas que se
|
|
135
|
+
* relacionaran ateponiendo los singnos <,>,= al nombre de la tabla foranea
|
|
136
|
+
* < sera para left join
|
|
137
|
+
* > sera para ringt join
|
|
138
|
+
* = sera innert join
|
|
139
|
+
* si no se antepone nada sera natural join
|
|
140
|
+
* sera tomado de las claves primarias que contengan las tablas para la clausula using
|
|
141
|
+
* para la clausula 'on' o 'using' se coloca el nombre de la tabla como atributo
|
|
142
|
+
* y el valor 'using' si es uno como valor string si son varios en un array
|
|
143
|
+
* y para la exprecion 'on' un string con la exprecion
|
|
144
|
+
* si joins es un string entonces se utilisara el parametro where y
|
|
145
|
+
* todos los parametros se correran hacia la izquierda
|
|
146
|
+
* @param {string|object} where - clausula where desde este parametro se puede indicar
|
|
147
|
+
* explicitamente las clausulas GROUP BY, OERDER BY, HAVING, LIMIT y los parametros siguentes se correran hacia la izquierda
|
|
148
|
+
* segun la posicion del parametro indicado
|
|
149
|
+
* ejemplo: <em>si en el parametro where se coloca grup by micampo entonces where sera tomado
|
|
150
|
+
* como group y group como having y asi sucesivamente </em>
|
|
151
|
+
* @param {string} group - clausula group
|
|
152
|
+
* @param {string} having - clausula having
|
|
153
|
+
* @param {string} order - clausula order
|
|
154
|
+
* @param {string} limit - clausula limit
|
|
155
|
+
* @return {Promise} la sentencia sql generada
|
|
156
|
+
*/
|
|
157
|
+
select(...params)
|
|
158
|
+
{
|
|
159
|
+
return new Promise((resolve,reject)=>
|
|
160
|
+
{
|
|
161
|
+
this._verifyKeys(e=>
|
|
162
|
+
{
|
|
163
|
+
if(e)
|
|
164
|
+
{
|
|
165
|
+
return reject(e)
|
|
166
|
+
}
|
|
167
|
+
this._lastSql = this.sql.select(...params)
|
|
168
|
+
this._connection.query(this._lastSql).then(d=>{
|
|
169
|
+
resolve(this._formatResult(d))
|
|
170
|
+
}).catch(e=>reject(e))
|
|
171
|
+
})
|
|
172
|
+
|
|
173
|
+
})
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* envia una sentencia select a la base de datos y obtiene la primera fila
|
|
177
|
+
* @param {array|object|string} campos - campos de la tabla que se seleccionaran si es un object
|
|
178
|
+
* se tomara con el parametro join si es un string entonces se utilisara como el parametro
|
|
179
|
+
* where y todos los parametros se correran hacia la izquierda
|
|
180
|
+
* @param {object|string} joins - clausulas jois utilizando un object con las tablas que se
|
|
181
|
+
* relacionaran ateponiendo los singnos <,>,= al nombre de la tabla foranea
|
|
182
|
+
* < sera para left join
|
|
183
|
+
* > sera para ringt join
|
|
184
|
+
* = sera innert join
|
|
185
|
+
* si no se antepone nada sera natural join
|
|
186
|
+
* sera tomado de las claves primarias que contengan las tablas para la clausula using
|
|
187
|
+
* para la clausula 'on' o 'using' se coloca el nombre de la tabla como atributo
|
|
188
|
+
* y el valor 'using' si es uno como valor string si son varios en un array
|
|
189
|
+
* y para la exprecion 'on' un string con la exprecion
|
|
190
|
+
* si joins es un string entonces se utilisara el parametro where y
|
|
191
|
+
* todos los parametros se correran hacia la izquierda
|
|
192
|
+
* @param {string|object} where - clausula where desde este parametro se puede indicar
|
|
193
|
+
* explicitamente las clausulas GROUP BY, OERDER BY, HAVING, LIMIT y los parametros siguentes se correran hacia la izquierda
|
|
194
|
+
* segun la posicion del parametro indicado
|
|
195
|
+
* ejemplo: <em>si en el parametro where se coloca grup by micampo entonces where sera tomado
|
|
196
|
+
* como group y group como having y asi sucesivamente </em>
|
|
197
|
+
* @param {string} group - clausula group
|
|
198
|
+
* @param {string} having - clausula having
|
|
199
|
+
* @param {string} order - clausula order
|
|
200
|
+
* @return {Promise} la sentencia sql generada
|
|
201
|
+
*/
|
|
202
|
+
selectOne(campos,joins,where,group,having,order)
|
|
203
|
+
{
|
|
204
|
+
return this.select(campos,joins,where,group,having,order,1).then(d=>typeof d[0]!==undefined?d[0]:null)
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* selecciona un elemento de la tabla comparando la primera clave primaria con el
|
|
209
|
+
* parametro proporcionado
|
|
210
|
+
* @param {array|string} campos - campos de la tabla que se seleccionaran si es un
|
|
211
|
+
* object entonces se utilisara como la clausula join y todos los parametros se
|
|
212
|
+
* correran hacia la izquierda
|
|
213
|
+
* @param {object|string} joins - clausulas jois utilizando un object con las tablas que se
|
|
214
|
+
* relacionaran ateponiendo los singnos <,>,= al nombre de la tabla foranea
|
|
215
|
+
* < sera para left join
|
|
216
|
+
* > sera para ringt join
|
|
217
|
+
* = sera innert join
|
|
218
|
+
* si no se antepone nada sera natural join
|
|
219
|
+
* sera tomado de las claves primarias que contengan las tablas para la clausula using
|
|
220
|
+
* para la clausula 'on' o 'using' se coloca el nombre de la tabla como atributo
|
|
221
|
+
* y el valor 'using' si es uno como valor string si son varios en un array
|
|
222
|
+
* y para la exprecion 'on' un string con la exprecion
|
|
223
|
+
* si joins no es un objeto entonces se utilisara como el parametro id
|
|
224
|
+
* @param {string|numeric} id - clave primaria a buscar
|
|
225
|
+
* @return {Promise}
|
|
226
|
+
*/
|
|
227
|
+
selectById(campos,joins,id)
|
|
228
|
+
{
|
|
229
|
+
if(typeof campos==="string" || typeof campos==="number")
|
|
230
|
+
{
|
|
231
|
+
id=campos
|
|
232
|
+
joins=undefined
|
|
233
|
+
campos=undefined
|
|
234
|
+
}
|
|
235
|
+
if(typeof campos==="string" || typeof campos==="number")
|
|
236
|
+
{
|
|
237
|
+
id=joins
|
|
238
|
+
joins=campos
|
|
239
|
+
campos=undefined
|
|
240
|
+
}
|
|
241
|
+
if(id===undefined)
|
|
242
|
+
{
|
|
243
|
+
throw new dbTablaError("falta el parametro id no es opcional")
|
|
244
|
+
}
|
|
245
|
+
return this.selectOne(campos,joins,this.sql.whereId(id))
|
|
246
|
+
}
|
|
247
|
+
/**
|
|
248
|
+
* envia una sentencia select a la base de datos con un sencillo algoritmo de busqueda
|
|
249
|
+
* @param {string} cadena - palabra o serie de palabras a buscar en la tabla
|
|
250
|
+
* @param {array} campo_bus - campos en los que se buscara el contenido del primer parametro
|
|
251
|
+
* @param {array|object|string} campos - campos de la tabla que se seleccionaran si es un object
|
|
252
|
+
* se tomara con el parametro join si es un string entonces se utilisara como el parametro
|
|
253
|
+
* where y todos los parametros se correran hacia la izquierda
|
|
254
|
+
* @param {object|string} joins - clausulas jois utilizando un object con las tablas que se
|
|
255
|
+
* relacionaran ateponiendo los singnos <,>,= al nombre de la tabla foranea
|
|
256
|
+
* < sera para left join
|
|
257
|
+
* > sera para ringt join
|
|
258
|
+
* = sera innert join
|
|
259
|
+
* si no se antepone nada sera natural join
|
|
260
|
+
* sera tomado de las claves primarias que contengan las tablas para la clausula using
|
|
261
|
+
* para la clausula 'on' o 'using' se coloca el nombre de la tabla como atributo
|
|
262
|
+
* y el valor 'using' si es uno como valor string si son varios en un array
|
|
263
|
+
* y para la exprecion 'on' un string con la exprecion
|
|
264
|
+
* si joins es un string entonces se utilisara el parametro where y
|
|
265
|
+
* todos los parametros se correran hacia la izquierda
|
|
266
|
+
* @param {string|object} where - clausula where desde este parametro se puede indicar
|
|
267
|
+
* explicitamente las clausulas GROUP BY, OERDER BY, HAVING, LIMIT y los parametros siguentes se correran hacia la izquierda
|
|
268
|
+
* segun la posicion del parametro indicado
|
|
269
|
+
* ejemplo: <em>si en el parametro where se coloca grup by micampo entonces where sera tomado
|
|
270
|
+
* como group y group como having y asi sucesivamente </em>
|
|
271
|
+
* @param {string} group - clausula group
|
|
272
|
+
* @param {string} having - clausula having
|
|
273
|
+
* @param {string} order - clausula order
|
|
274
|
+
* @param {string} limit - clausula limit
|
|
275
|
+
* @return {string} la sentencia sql generada
|
|
276
|
+
*/
|
|
277
|
+
busqueda(cadena,campo_bus,campos,where,joins,group,having,order,limit)
|
|
278
|
+
{
|
|
279
|
+
return new Promise((resolve,reject)=>
|
|
280
|
+
{
|
|
281
|
+
this._verifyKeys(()=>
|
|
282
|
+
{
|
|
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
|
+
}).catch(e=>reject(e))
|
|
287
|
+
})
|
|
288
|
+
|
|
289
|
+
})
|
|
290
|
+
}
|
|
291
|
+
/**
|
|
292
|
+
* envia una sentencia update en la base de datos
|
|
293
|
+
* que edita la fila con la clave primaria pasada en el segundo parametro
|
|
294
|
+
* @param {object} sets - elementos a modificar
|
|
295
|
+
* @param {string|numeric} id - clave primaria
|
|
296
|
+
* @return {Promise}
|
|
297
|
+
*/
|
|
298
|
+
updateById(sets,id)
|
|
299
|
+
{
|
|
300
|
+
if(sets===undefined )
|
|
301
|
+
{
|
|
302
|
+
throw new dbTablaError("Falta el parametro numero 1 sets no es opcional")
|
|
303
|
+
}else if(typeof sets!=="object")
|
|
304
|
+
{
|
|
305
|
+
throw new dbTablaError("El parametro numero 1 sets debe ser un de tipo object")
|
|
306
|
+
}
|
|
307
|
+
if(id===undefined)
|
|
308
|
+
{
|
|
309
|
+
throw new dbTablaError("falta el parametro numero 2 id no es opcional")
|
|
310
|
+
}
|
|
311
|
+
return this.update(sets,this.sql.whereId(id))
|
|
312
|
+
}
|
|
313
|
+
/**
|
|
314
|
+
* envia una sentencia update en la base de datos
|
|
315
|
+
* @param {object} sets - elementos a modificar
|
|
316
|
+
* @param {string|object} where - exprecion booleana sql
|
|
317
|
+
* @return {Promise}
|
|
318
|
+
*/
|
|
319
|
+
update(sets,where)
|
|
320
|
+
{
|
|
321
|
+
if(sets===undefined )
|
|
322
|
+
{
|
|
323
|
+
throw new dbTablaError("Falta el parametro numero 1 sets no es opcional")
|
|
324
|
+
}else if(typeof sets!=="object")
|
|
325
|
+
{
|
|
326
|
+
throw new dbTablaError("El parametro numero 1 sets debe ser un de tipo object")
|
|
327
|
+
}
|
|
328
|
+
if(where===undefined)
|
|
329
|
+
{
|
|
330
|
+
throw new dbTablaError("falta el parametro numero 2 where no es opcional")
|
|
331
|
+
}
|
|
332
|
+
return new Promise((resolve,reject)=>
|
|
333
|
+
{
|
|
334
|
+
this._verifyKeys(e=>
|
|
335
|
+
{
|
|
336
|
+
if(e)
|
|
337
|
+
{
|
|
338
|
+
return reject(e)
|
|
339
|
+
}
|
|
340
|
+
this._lastSql =this.sql.update(sets,where,reject)
|
|
341
|
+
if( this._lastSql)
|
|
342
|
+
{
|
|
343
|
+
this._connection.query(this._lastSql).then(d=>{
|
|
344
|
+
|
|
345
|
+
resolve(this._PropertyOk(d))
|
|
346
|
+
}).catch(e=>reject(e))
|
|
347
|
+
}
|
|
348
|
+
})
|
|
349
|
+
})
|
|
350
|
+
}
|
|
351
|
+
/**
|
|
352
|
+
* genera una sentencia sql deleteen la base de datos
|
|
353
|
+
* que elimina la fila con la clave primaria pasada en el segundo parametro
|
|
354
|
+
* @param {object} sets - elementos a modificar
|
|
355
|
+
* @param {string|numeric} id - clave prpimaria a eliminar
|
|
356
|
+
* @return {Promise}
|
|
357
|
+
*/
|
|
358
|
+
deleteById(sets,id)
|
|
359
|
+
{
|
|
360
|
+
if(sets===undefined )
|
|
361
|
+
{
|
|
362
|
+
throw new dbTablaError("Falta el parametro numero1 sets no es opcional")
|
|
363
|
+
}else if(typeof sets!=="object")
|
|
364
|
+
{
|
|
365
|
+
throw new dbTablaError("El parametro numero 1 sets debe ser un de tipo object")
|
|
366
|
+
}
|
|
367
|
+
if(id===undefined)
|
|
368
|
+
{
|
|
369
|
+
throw new dbTablaError("falta el parametro numero 2 id no es opcional")
|
|
370
|
+
}
|
|
371
|
+
return this.delete(sets,this.sql.whereId(id))
|
|
372
|
+
}
|
|
373
|
+
/**
|
|
374
|
+
* genera una sentencia sql delete en la base de datos
|
|
375
|
+
* @param {string|object} where - exprecion booleana sql
|
|
376
|
+
* @return {Promise} - sentencia sql
|
|
377
|
+
*/
|
|
378
|
+
|
|
379
|
+
delete(where)
|
|
380
|
+
{
|
|
381
|
+
if(where===undefined)
|
|
382
|
+
{
|
|
383
|
+
throw new dbTablaError("falta el parametro 1 where no es opcional")
|
|
384
|
+
}
|
|
385
|
+
return new Promise((resolve,reject)=>
|
|
386
|
+
{
|
|
387
|
+
this._verifyKeys(e=>
|
|
388
|
+
{
|
|
389
|
+
if(e)
|
|
390
|
+
{
|
|
391
|
+
return reject(e)
|
|
392
|
+
}
|
|
393
|
+
this._lastSql =this.sql.delete(where)
|
|
394
|
+
this._connection.query(this._lastSql).then(d=>{
|
|
395
|
+
|
|
396
|
+
resolve(this._PropertyOk(d))
|
|
397
|
+
}).catch(e=>reject(e))
|
|
398
|
+
})
|
|
399
|
+
})
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
module.exports=dbTabla
|
package/package.json
CHANGED
package/test_results.txt
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
|
|
2
|
+
> dbtabla@2.1.1 test
|
|
3
|
+
> node --test ./test/index.js
|
|
4
|
+
|
|
5
|
+
ÔûÂ test de dbTabla
|
|
6
|
+
ÔûÂ Test de la clase prosessingSql
|
|
7
|
+
Ô£ö verificacion de metodos (1.2094ms)
|
|
8
|
+
Ô£ö metodo busqueda (2.2858ms)
|
|
9
|
+
Ô£ö metodo select (1.1186ms)
|
|
10
|
+
Ô£ö metodo select (0.2998ms)
|
|
11
|
+
Ô£ö metodo insert (0.6902ms)
|
|
12
|
+
Ô£ö metodo update (0.4324ms)
|
|
13
|
+
Ô£ö metodo delete (0.3132ms)
|
|
14
|
+
Ô£ö Test de la clase prosessingSql (7.4947ms)
|
|
15
|
+
ÔûÂ Test de la clase Connect
|
|
16
|
+
Ô£ö verificacion de metodos (0.2097ms)
|
|
17
|
+
Ô£ö verificacion de modelos (2.2262ms)
|
|
18
|
+
Ô£ö verificacion de tabla (0.3958ms)
|
|
19
|
+
Ô£ö Test de la clase Connect (3.0674ms)
|
|
20
|
+
ÔûÂ Test de la clase dbRow
|
|
21
|
+
Ô£ö verificacion de metodos (0.2598ms)
|
|
22
|
+
Ô£û metodo update (1.4786ms)
|
|
23
|
+
Ô£ö metodo update sin claves primarias (0.367ms)
|
|
24
|
+
Ô£û metodo delete (0.6469ms)
|
|
25
|
+
Ô£ö metodo delete sin claves primarias (0.246ms)
|
|
26
|
+
Ô£û Test de la clase dbRow (3.3226ms)
|
|
27
|
+
Ô£û test de dbTabla (14.5938ms)
|
|
28
|
+
Ôä╣ tests 15
|
|
29
|
+
Ôä╣ suites 4
|
|
30
|
+
Ôä╣ pass 13
|
|
31
|
+
Ôä╣ fail 2
|
|
32
|
+
Ôä╣ cancelled 0
|
|
33
|
+
Ôä╣ skipped 0
|
|
34
|
+
Ôä╣ todo 0
|
|
35
|
+
Ôä╣ duration_ms 168.9837
|
|
36
|
+
|
|
37
|
+
Ô£û failing tests:
|
|
38
|
+
|
|
39
|
+
test at test\dbRow.js:74:5
|
|
40
|
+
Ô£û metodo update (1.4786ms)
|
|
41
|
+
AssertionError [ERR_ASSERTION]: "UPDATE test1 SET col1=2343 WHERE id=1 AND col1=234 AND col2='row';" == 'UPDATE test1 SET col1=2343 WHERE id=1;'
|
|
42
|
+
at C:\programacion\dbtabla\dbtabla\test\dbRow.js:90:20
|
|
43
|
+
at async Test.run (node:internal/test_runner/test:1125:7)
|
|
44
|
+
at async Suite.processPendingSubtests (node:internal/test_runner/test:787:7) {
|
|
45
|
+
generatedMessage: true,
|
|
46
|
+
code: 'ERR_ASSERTION',
|
|
47
|
+
actual: "UPDATE test1 SET col1=2343 WHERE id=1 AND col1=234 AND col2='row';",
|
|
48
|
+
expected: 'UPDATE test1 SET col1=2343 WHERE id=1;',
|
|
49
|
+
operator: '==',
|
|
50
|
+
diff: 'simple'
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
test at test\dbRow.js:111:5
|
|
54
|
+
Ô£û metodo delete (0.6469ms)
|
|
55
|
+
AssertionError [ERR_ASSERTION]: "DELETE FROM test1 WHERE id=1 AND col1=234 AND col2='row';" == 'DELETE FROM test1 WHERE id=1;'
|
|
56
|
+
at C:\programacion\dbtabla\dbtabla\test\dbRow.js:122:20
|
|
57
|
+
at async Test.run (node:internal/test_runner/test:1125:7)
|
|
58
|
+
at async Suite.processPendingSubtests (node:internal/test_runner/test:787:7) {
|
|
59
|
+
generatedMessage: true,
|
|
60
|
+
code: 'ERR_ASSERTION',
|
|
61
|
+
actual: "DELETE FROM test1 WHERE id=1 AND col1=234 AND col2='row';",
|
|
62
|
+
expected: 'DELETE FROM test1 WHERE id=1;',
|
|
63
|
+
operator: '==',
|
|
64
|
+
diff: 'simple'
|
|
65
|
+
}
|