zormz 1.4.0 → 1.4.2
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 +126 -74
- package/dist/index.d.mts +36 -20
- package/dist/index.d.ts +36 -20
- package/dist/index.js +111 -22
- package/dist/index.mjs +111 -22
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -7,17 +7,17 @@ Permite conectarse a una base de datos, definir tablas desde TypeScript con auto
|
|
|
7
7
|
|
|
8
8
|
## Características
|
|
9
9
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
10
|
+
- Compatible con **ESM** y **CommonJS**
|
|
11
|
+
- Tipado completo en **TypeScript**
|
|
12
|
+
- Builder con sintaxis encadenada: `select().from().where().execute()`
|
|
13
|
+
- Insert múltiple con arrays
|
|
14
|
+
- Update y Delete con condiciones
|
|
15
|
+
- Definición de columnas con encadenamiento:
|
|
16
|
+
- `int().Pk().$()`
|
|
17
|
+
- `varchar(200).Default("hola").$()`
|
|
18
|
+
- Generación automática de tablas en MySQL y PostgreSQL
|
|
19
|
+
- Sin dependencias pesadas
|
|
20
|
+
- Fácil de extender
|
|
21
21
|
|
|
22
22
|
---
|
|
23
23
|
|
|
@@ -29,20 +29,43 @@ npm install zormz
|
|
|
29
29
|
```
|
|
30
30
|
|
|
31
31
|
## Uso basico
|
|
32
|
-
### importacion ESM
|
|
33
|
-
```ts
|
|
34
32
|
|
|
35
|
-
|
|
33
|
+
### importacion ESM
|
|
36
34
|
|
|
35
|
+
```ts
|
|
36
|
+
import {
|
|
37
|
+
connecionLocal,
|
|
38
|
+
getConexion,
|
|
39
|
+
defineTable,
|
|
40
|
+
generateTable,
|
|
41
|
+
int,
|
|
42
|
+
varchar,
|
|
43
|
+
DB,
|
|
44
|
+
eq,
|
|
45
|
+
ORQ,
|
|
46
|
+
} from "zormz";
|
|
37
47
|
```
|
|
48
|
+
|
|
38
49
|
### importacion COMMONJS
|
|
39
50
|
|
|
40
51
|
```ts
|
|
41
|
-
const {
|
|
42
|
-
|
|
52
|
+
const {
|
|
53
|
+
connecionLocal,
|
|
54
|
+
getConexion,
|
|
55
|
+
defineTable,
|
|
56
|
+
generateTable,
|
|
57
|
+
int,
|
|
58
|
+
varchar,
|
|
59
|
+
DB,
|
|
60
|
+
eq,
|
|
61
|
+
ORQ,
|
|
62
|
+
} = require("zormz");
|
|
43
63
|
```
|
|
64
|
+
|
|
44
65
|
## Conexion a la base de datos
|
|
66
|
+
|
|
45
67
|
### MYSQL
|
|
68
|
+
|
|
46
69
|
```ts
|
|
47
70
|
const conexionMysql: connecionLocal = {
|
|
48
71
|
database: "pruebas",
|
|
@@ -53,15 +76,15 @@ const conexionMysql: connecionLocal = {
|
|
|
53
76
|
};
|
|
54
77
|
|
|
55
78
|
getConexion("mysql", conexionMysql);
|
|
56
|
-
|
|
57
79
|
```
|
|
58
80
|
|
|
59
81
|
### PG
|
|
82
|
+
|
|
60
83
|
```ts
|
|
61
84
|
//Conexion por red , aqui solo se pone la ruta que por defecto te arroja la base de datos de la nube
|
|
62
|
-
const conexion2:connecionRed = {
|
|
63
|
-
|
|
64
|
-
}
|
|
85
|
+
const conexion2: connecionRed = {
|
|
86
|
+
connectionString: "direccion",
|
|
87
|
+
};
|
|
65
88
|
|
|
66
89
|
const conexion: connecionLocal = {
|
|
67
90
|
database: "pruebamaster",
|
|
@@ -72,10 +95,9 @@ const conexion: connecionLocal = {
|
|
|
72
95
|
};
|
|
73
96
|
|
|
74
97
|
getConexion("pg", conexion);
|
|
75
|
-
|
|
76
98
|
```
|
|
77
99
|
|
|
78
|
-
## Definicion de Tablas
|
|
100
|
+
## Definicion de Tablas
|
|
79
101
|
|
|
80
102
|
```ts
|
|
81
103
|
export const prueba1 = defineTable("prueba1", {
|
|
@@ -83,11 +105,12 @@ export const prueba1 = defineTable("prueba1", {
|
|
|
83
105
|
valor: varchar(200).Default("hola").$(),
|
|
84
106
|
resultado: int().Default(0).$(),
|
|
85
107
|
fechaRegistro: timestamp().required().now().$(),
|
|
86
|
-
fechaUpdate: timestamp().required().now().onUpdate().$()
|
|
108
|
+
fechaUpdate: timestamp().required().now().onUpdate().$(),
|
|
87
109
|
});
|
|
88
|
-
|
|
89
110
|
```
|
|
111
|
+
|
|
90
112
|
o
|
|
113
|
+
|
|
91
114
|
```ts
|
|
92
115
|
export function generateTables() {
|
|
93
116
|
return {
|
|
@@ -98,7 +121,7 @@ export function generateTables() {
|
|
|
98
121
|
dni: varchar(9).$(),
|
|
99
122
|
rol: varchar(50).$(),
|
|
100
123
|
diastrabajo: varchar(200).Default("sabado - domingo").$(),
|
|
101
|
-
|
|
124
|
+
estado: varchar().Check(["pendiente", "completado", "rechazado"]).$(),
|
|
102
125
|
}),
|
|
103
126
|
permisos: defineTable("permisos", {
|
|
104
127
|
idpermiso: int().Pk().$(),
|
|
@@ -116,21 +139,27 @@ export function generateTables() {
|
|
|
116
139
|
```
|
|
117
140
|
|
|
118
141
|
### Notas importantes
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
142
|
+
|
|
143
|
+
- **.pk()** define la columna como clave primaria
|
|
144
|
+
- **.Default()** entrega un valor por defecto
|
|
145
|
+
- **.$()** finaliza la definicion y entrega un valor compatible con _$columns_
|
|
146
|
+
- **.required()** Indica que los valores de la fila no podran ser null, si o si un valor obligatorio
|
|
147
|
+
- **.now()** Solo esta disponible para timestamp , indica fecha automatica de registra
|
|
148
|
+
- **.onUpdate()** Tomara la fecha actual cada ves que se haga un update
|
|
149
|
+
- **.Check()** Solo para varchar , puedes decirle que valores esta obligado a recibir y no otros
|
|
150
|
+
|
|
125
151
|
## Generacion de tablas
|
|
152
|
+
|
|
126
153
|
```ts
|
|
127
|
-
generateTable(prueba1(), prueba1.$columns)
|
|
154
|
+
generateTable(prueba1(), prueba1.$columns,true);//true para ver la definicion
|
|
128
155
|
```
|
|
156
|
+
|
|
129
157
|
Esto crea la tabla en la base de datos respetando tipos, claves primarias y valores por defecto.
|
|
130
158
|
|
|
131
159
|
---
|
|
132
160
|
|
|
133
161
|
## Eliminar tablas
|
|
162
|
+
|
|
134
163
|
```ts
|
|
135
164
|
await dropTable(prueba4());
|
|
136
165
|
```
|
|
@@ -139,10 +168,14 @@ await dropTable(prueba4());
|
|
|
139
168
|
|
|
140
169
|
```ts
|
|
141
170
|
await DB.Insert(prueba1(), [prueba1.valor, prueba1.resultado])
|
|
142
|
-
.Values([
|
|
171
|
+
.Values([
|
|
172
|
+
["hola mundo", 1],
|
|
173
|
+
["prueba", 0],
|
|
174
|
+
])
|
|
175
|
+
.Returning(prueba1.id)
|
|
143
176
|
.execute();
|
|
144
|
-
|
|
145
177
|
```
|
|
178
|
+
|
|
146
179
|
---
|
|
147
180
|
|
|
148
181
|
## Select de Datos
|
|
@@ -151,67 +184,82 @@ await DB.Insert(prueba1(), [prueba1.valor, prueba1.resultado])
|
|
|
151
184
|
const datos = await DB.select()
|
|
152
185
|
.from(prueba1())
|
|
153
186
|
.where(eq(prueba1.id, 1))
|
|
154
|
-
.execute();
|
|
155
|
-
|
|
187
|
+
.execute(true);
|
|
156
188
|
```
|
|
189
|
+
|
|
157
190
|
---
|
|
158
191
|
|
|
159
192
|
## Update de Datos
|
|
160
|
-
|
|
193
|
+
|
|
194
|
+
### .set() Recibe un objeto con clave valor de las tablas o un array con UP
|
|
195
|
+
|
|
161
196
|
```ts
|
|
162
197
|
await DB.Update(prueba1())
|
|
163
198
|
.set({ valor: "nuevo valor", resultado: 2 })
|
|
164
199
|
.where(eq(prueba1.id, 1))
|
|
165
200
|
.execute();
|
|
166
|
-
|
|
167
201
|
```
|
|
202
|
+
|
|
168
203
|
o
|
|
204
|
+
|
|
169
205
|
```ts
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
206
|
+
//Usar la funcion UP
|
|
207
|
+
const response = await DB.Update(producto())
|
|
208
|
+
.set([UP(producto.stockactual, `${producto.stockactual} - 4`, true)])
|
|
209
|
+
.where(eq(producto.idproducto, 4))
|
|
210
|
+
.execute();
|
|
176
211
|
```
|
|
177
212
|
|
|
178
213
|
---
|
|
214
|
+
|
|
179
215
|
## Delete de Datos
|
|
216
|
+
|
|
180
217
|
```ts
|
|
181
218
|
await DB.Delete(prueba1())
|
|
182
219
|
.where(ORQ(prueba1.id, 2, 3))
|
|
183
220
|
.execute();
|
|
184
|
-
|
|
185
221
|
```
|
|
222
|
+
|
|
186
223
|
## JOINS
|
|
224
|
+
|
|
187
225
|
```ts
|
|
188
226
|
const { detallespermisos, colaborador, permisos } = generateTables();
|
|
189
227
|
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
228
|
+
const response = await DB.Select([
|
|
229
|
+
detallespermisos.iddetallepermiso,
|
|
230
|
+
colaborador.nombrecompleto,
|
|
231
|
+
colaborador.dni,
|
|
232
|
+
permisos.nombre,
|
|
233
|
+
permisos.estado,
|
|
234
|
+
])
|
|
235
|
+
.from(detallespermisos())
|
|
236
|
+
.innerJOIN(
|
|
237
|
+
colaborador(),
|
|
238
|
+
eq(colaborador.idcolaborador, detallespermisos.idcolaborador, false),
|
|
239
|
+
)
|
|
240
|
+
.innerJOIN(
|
|
241
|
+
permisos(),
|
|
242
|
+
eq(permisos.idpermiso, detallespermisos.idppermiso, false),
|
|
243
|
+
)
|
|
244
|
+
.execute();
|
|
208
245
|
```
|
|
246
|
+
|
|
209
247
|
---
|
|
210
248
|
|
|
211
|
-
## Ejemplo de Uso
|
|
249
|
+
## Ejemplo de Uso
|
|
212
250
|
|
|
213
251
|
```ts
|
|
214
|
-
import {
|
|
252
|
+
import {
|
|
253
|
+
connecionLocal,
|
|
254
|
+
getConexion,
|
|
255
|
+
defineTable,
|
|
256
|
+
generateTable,
|
|
257
|
+
int,
|
|
258
|
+
varchar,
|
|
259
|
+
DB,
|
|
260
|
+
eq,
|
|
261
|
+
ORQ,
|
|
262
|
+
} from "zormz";
|
|
215
263
|
|
|
216
264
|
const conexion: connecionLocal = {
|
|
217
265
|
database: "pruebamaster",
|
|
@@ -228,14 +276,17 @@ getConexion("pg", conexion);
|
|
|
228
276
|
const prueba1 = defineTable("prueba1", {
|
|
229
277
|
id: int().Pk().$(),
|
|
230
278
|
valor: varchar(200).Default("hola").$(),
|
|
231
|
-
resultado: int().Default(0).$()
|
|
279
|
+
resultado: int().Default(0).$(),
|
|
232
280
|
});
|
|
233
281
|
|
|
234
282
|
generateTable(prueba1(), prueba1.$columns);
|
|
235
283
|
|
|
236
284
|
async function pruebaData() {
|
|
237
285
|
await DB.Insert(prueba1(), [prueba1.valor, prueba1.resultado])
|
|
238
|
-
.Values([
|
|
286
|
+
.Values([
|
|
287
|
+
["hola mundo", 1],
|
|
288
|
+
["prueba", 0],
|
|
289
|
+
])
|
|
239
290
|
.execute();
|
|
240
291
|
|
|
241
292
|
const datos = await DB.select().from(prueba1()).execute();
|
|
@@ -252,19 +303,20 @@ async function pruebaData() {
|
|
|
252
303
|
}
|
|
253
304
|
|
|
254
305
|
pruebaData().catch(console.error);
|
|
255
|
-
|
|
256
306
|
```
|
|
257
307
|
|
|
258
308
|
---
|
|
259
309
|
|
|
260
310
|
## Notas
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
311
|
+
|
|
312
|
+
- ORM en version inicial con enfoque de tipado y autompletado
|
|
313
|
+
- Compatible con **MYSQL** y **PG**
|
|
314
|
+
- Preparado para extenderse
|
|
315
|
+
- Las versiones +1.3.0 son mas estables , las anteriores estaban en desarrollo y presentan multiples errores
|
|
316
|
+
- version estable 1.4.0
|
|
317
|
+
|
|
266
318
|
## Licencia
|
|
267
319
|
|
|
268
320
|
ISC © Yukio-kayaba
|
|
269
321
|
|
|
270
|
-
[](https://github.com/yukio-kayaba/)
|
|
322
|
+
[](https://github.com/yukio-kayaba/)
|
package/dist/index.d.mts
CHANGED
|
@@ -12,6 +12,7 @@ interface ColumnDefinition<T extends ColumnTypes = ColumnTypes> {
|
|
|
12
12
|
typo: T;
|
|
13
13
|
pk?: boolean;
|
|
14
14
|
sqlz: string;
|
|
15
|
+
check?: string
|
|
15
16
|
}
|
|
16
17
|
|
|
17
18
|
interface VarcharType{
|
|
@@ -83,25 +84,17 @@ type TableProxy<TCols> = {
|
|
|
83
84
|
* @param {string} tableName
|
|
84
85
|
* @param {string} columns
|
|
85
86
|
* @example
|
|
86
|
-
* export const
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
},
|
|
93
|
-
edad: {
|
|
94
|
-
typo:"int",
|
|
95
|
-
default: 0,
|
|
96
|
-
},
|
|
97
|
-
apellido : {
|
|
98
|
-
typo:"varchar",
|
|
99
|
-
}
|
|
87
|
+
* export const categorias = defineTable("categorias", {
|
|
88
|
+
idcategoria: int().Pk().$(),
|
|
89
|
+
nombrecategoria: varchar(100).Check(['disponible','data']).Required().$(),
|
|
90
|
+
descripcion: varchar(250).$(),
|
|
91
|
+
estado: bool().default(true).$(),
|
|
92
|
+
})
|
|
100
93
|
* });
|
|
101
94
|
* @returns
|
|
102
95
|
*/
|
|
103
96
|
declare function defineTable<T extends Record<string, ColumnDefinition>>(tableName: string, columns: T): TableProxy<T>;
|
|
104
|
-
declare function generateTable<T extends Record<string, ColumnDefinition>>(tabla: string, columns: T): Promise<void>;
|
|
97
|
+
declare function generateTable<T extends Record<string, ColumnDefinition>>(tabla: string, columns: T, seeSQL?: boolean): Promise<void>;
|
|
105
98
|
declare function dropTable(nombreTabla: string): Promise<void>;
|
|
106
99
|
|
|
107
100
|
declare class DeleteR {
|
|
@@ -122,7 +115,7 @@ declare class DeleteR {
|
|
|
122
115
|
* DB.Delete(tabla).WHERE(AND(eq(valor,valor), eq(valor2,valor2)))
|
|
123
116
|
*/
|
|
124
117
|
where(condicion: string): this;
|
|
125
|
-
execute(): Promise<number | null>;
|
|
118
|
+
execute(seeQuery?: boolean): Promise<number | null>;
|
|
126
119
|
}
|
|
127
120
|
|
|
128
121
|
type arrayData = (string | number | boolean)[];
|
|
@@ -131,7 +124,7 @@ declare class QueryBuilder {
|
|
|
131
124
|
private tabla;
|
|
132
125
|
private parametros;
|
|
133
126
|
private valores;
|
|
134
|
-
private
|
|
127
|
+
private returning;
|
|
135
128
|
private conexion;
|
|
136
129
|
/**
|
|
137
130
|
* @throws {Error} Si la consulta es vacía.
|
|
@@ -154,7 +147,8 @@ declare class QueryBuilder {
|
|
|
154
147
|
* @returns
|
|
155
148
|
*/
|
|
156
149
|
Values(values: arrayData | arrayDatas): this;
|
|
157
|
-
|
|
150
|
+
Returning(idRetorno: string): this;
|
|
151
|
+
execute(see?: boolean): Promise<any[] | undefined>;
|
|
158
152
|
}
|
|
159
153
|
|
|
160
154
|
type valor = "ASC" | "DESC";
|
|
@@ -214,7 +208,7 @@ declare class Select {
|
|
|
214
208
|
/**
|
|
215
209
|
* @returns {Promise<Array<Object>>}
|
|
216
210
|
*/
|
|
217
|
-
execute(): Promise<any[] | undefined>;
|
|
211
|
+
execute(see?: boolean): Promise<any[] | undefined>;
|
|
218
212
|
}
|
|
219
213
|
|
|
220
214
|
type Valores = Record<string, string | number | undefined>;
|
|
@@ -245,7 +239,7 @@ declare class Update {
|
|
|
245
239
|
* where(ORQ())
|
|
246
240
|
*/
|
|
247
241
|
where(condicion: string): this;
|
|
248
|
-
execute(): Promise<any>;
|
|
242
|
+
execute(seeQuery?: boolean): Promise<any>;
|
|
249
243
|
}
|
|
250
244
|
|
|
251
245
|
declare class DB {
|
|
@@ -392,9 +386,11 @@ declare class IntColumn {
|
|
|
392
386
|
private defaultData?;
|
|
393
387
|
private pk?;
|
|
394
388
|
private unsingned;
|
|
389
|
+
private comentario;
|
|
395
390
|
constructor();
|
|
396
391
|
Required(): this;
|
|
397
392
|
Pk(): this;
|
|
393
|
+
Comment(comentario: string): this;
|
|
398
394
|
Unique(): this;
|
|
399
395
|
Unsingned(numeroPositivosInicio?: number): this;
|
|
400
396
|
AutoIncrement(): this;
|
|
@@ -409,9 +405,23 @@ declare class Varchar {
|
|
|
409
405
|
private defaultData;
|
|
410
406
|
private requerido;
|
|
411
407
|
private unique;
|
|
408
|
+
private enum;
|
|
409
|
+
private campo;
|
|
410
|
+
private comentarios;
|
|
412
411
|
constructor(cantidad?: number);
|
|
413
412
|
withType({ maxLenth, defaultData, requerido, unique, }: VarcharType): ColumnDefinition<ColumnTypes>;
|
|
413
|
+
/**
|
|
414
|
+
*
|
|
415
|
+
* @param {string} campo
|
|
416
|
+
* @param {string[]} datos
|
|
417
|
+
* @example
|
|
418
|
+
* Check("example", ["operativo","denegado","revisando"])
|
|
419
|
+
*
|
|
420
|
+
* @returns
|
|
421
|
+
*/
|
|
422
|
+
Check(datos: (string)[]): this;
|
|
414
423
|
Required(): this;
|
|
424
|
+
Comment(comentario: string): this;
|
|
415
425
|
Unique(): this;
|
|
416
426
|
Default(textoDefecto: string): this;
|
|
417
427
|
$(): ColumnDefinition<ColumnTypes>;
|
|
@@ -421,7 +431,9 @@ declare function varchar(cantidad?: number): Varchar;
|
|
|
421
431
|
declare class BoolColumn {
|
|
422
432
|
private requerido;
|
|
423
433
|
private defaultData?;
|
|
434
|
+
private comentario;
|
|
424
435
|
required(): this;
|
|
436
|
+
Comment(comentario: string): this;
|
|
425
437
|
default(value: boolean): this;
|
|
426
438
|
$(): ColumnDefinition;
|
|
427
439
|
}
|
|
@@ -431,7 +443,9 @@ declare class Timestamp {
|
|
|
431
443
|
private requerido;
|
|
432
444
|
private defaultNow;
|
|
433
445
|
private onUpdateNow;
|
|
446
|
+
private comentario;
|
|
434
447
|
required(): this;
|
|
448
|
+
comment(comentario: string): this;
|
|
435
449
|
now(): this;
|
|
436
450
|
onUpdate(): this;
|
|
437
451
|
$(): ColumnDefinition<ColumnTypes>;
|
|
@@ -443,9 +457,11 @@ declare class Money {
|
|
|
443
457
|
private scale;
|
|
444
458
|
private requerido;
|
|
445
459
|
private defaultData?;
|
|
460
|
+
private comentario;
|
|
446
461
|
constructor(presicion?: number, decimales?: number);
|
|
447
462
|
required(): this;
|
|
448
463
|
default(value: number): this;
|
|
464
|
+
comment(comentario: string): this;
|
|
449
465
|
$(): ColumnDefinition<ColumnTypes>;
|
|
450
466
|
}
|
|
451
467
|
declare function money(presicion?: number, decimales?: number): Money;
|
package/dist/index.d.ts
CHANGED
|
@@ -12,6 +12,7 @@ interface ColumnDefinition<T extends ColumnTypes = ColumnTypes> {
|
|
|
12
12
|
typo: T;
|
|
13
13
|
pk?: boolean;
|
|
14
14
|
sqlz: string;
|
|
15
|
+
check?: string
|
|
15
16
|
}
|
|
16
17
|
|
|
17
18
|
interface VarcharType{
|
|
@@ -83,25 +84,17 @@ type TableProxy<TCols> = {
|
|
|
83
84
|
* @param {string} tableName
|
|
84
85
|
* @param {string} columns
|
|
85
86
|
* @example
|
|
86
|
-
* export const
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
},
|
|
93
|
-
edad: {
|
|
94
|
-
typo:"int",
|
|
95
|
-
default: 0,
|
|
96
|
-
},
|
|
97
|
-
apellido : {
|
|
98
|
-
typo:"varchar",
|
|
99
|
-
}
|
|
87
|
+
* export const categorias = defineTable("categorias", {
|
|
88
|
+
idcategoria: int().Pk().$(),
|
|
89
|
+
nombrecategoria: varchar(100).Check(['disponible','data']).Required().$(),
|
|
90
|
+
descripcion: varchar(250).$(),
|
|
91
|
+
estado: bool().default(true).$(),
|
|
92
|
+
})
|
|
100
93
|
* });
|
|
101
94
|
* @returns
|
|
102
95
|
*/
|
|
103
96
|
declare function defineTable<T extends Record<string, ColumnDefinition>>(tableName: string, columns: T): TableProxy<T>;
|
|
104
|
-
declare function generateTable<T extends Record<string, ColumnDefinition>>(tabla: string, columns: T): Promise<void>;
|
|
97
|
+
declare function generateTable<T extends Record<string, ColumnDefinition>>(tabla: string, columns: T, seeSQL?: boolean): Promise<void>;
|
|
105
98
|
declare function dropTable(nombreTabla: string): Promise<void>;
|
|
106
99
|
|
|
107
100
|
declare class DeleteR {
|
|
@@ -122,7 +115,7 @@ declare class DeleteR {
|
|
|
122
115
|
* DB.Delete(tabla).WHERE(AND(eq(valor,valor), eq(valor2,valor2)))
|
|
123
116
|
*/
|
|
124
117
|
where(condicion: string): this;
|
|
125
|
-
execute(): Promise<number | null>;
|
|
118
|
+
execute(seeQuery?: boolean): Promise<number | null>;
|
|
126
119
|
}
|
|
127
120
|
|
|
128
121
|
type arrayData = (string | number | boolean)[];
|
|
@@ -131,7 +124,7 @@ declare class QueryBuilder {
|
|
|
131
124
|
private tabla;
|
|
132
125
|
private parametros;
|
|
133
126
|
private valores;
|
|
134
|
-
private
|
|
127
|
+
private returning;
|
|
135
128
|
private conexion;
|
|
136
129
|
/**
|
|
137
130
|
* @throws {Error} Si la consulta es vacía.
|
|
@@ -154,7 +147,8 @@ declare class QueryBuilder {
|
|
|
154
147
|
* @returns
|
|
155
148
|
*/
|
|
156
149
|
Values(values: arrayData | arrayDatas): this;
|
|
157
|
-
|
|
150
|
+
Returning(idRetorno: string): this;
|
|
151
|
+
execute(see?: boolean): Promise<any[] | undefined>;
|
|
158
152
|
}
|
|
159
153
|
|
|
160
154
|
type valor = "ASC" | "DESC";
|
|
@@ -214,7 +208,7 @@ declare class Select {
|
|
|
214
208
|
/**
|
|
215
209
|
* @returns {Promise<Array<Object>>}
|
|
216
210
|
*/
|
|
217
|
-
execute(): Promise<any[] | undefined>;
|
|
211
|
+
execute(see?: boolean): Promise<any[] | undefined>;
|
|
218
212
|
}
|
|
219
213
|
|
|
220
214
|
type Valores = Record<string, string | number | undefined>;
|
|
@@ -245,7 +239,7 @@ declare class Update {
|
|
|
245
239
|
* where(ORQ())
|
|
246
240
|
*/
|
|
247
241
|
where(condicion: string): this;
|
|
248
|
-
execute(): Promise<any>;
|
|
242
|
+
execute(seeQuery?: boolean): Promise<any>;
|
|
249
243
|
}
|
|
250
244
|
|
|
251
245
|
declare class DB {
|
|
@@ -392,9 +386,11 @@ declare class IntColumn {
|
|
|
392
386
|
private defaultData?;
|
|
393
387
|
private pk?;
|
|
394
388
|
private unsingned;
|
|
389
|
+
private comentario;
|
|
395
390
|
constructor();
|
|
396
391
|
Required(): this;
|
|
397
392
|
Pk(): this;
|
|
393
|
+
Comment(comentario: string): this;
|
|
398
394
|
Unique(): this;
|
|
399
395
|
Unsingned(numeroPositivosInicio?: number): this;
|
|
400
396
|
AutoIncrement(): this;
|
|
@@ -409,9 +405,23 @@ declare class Varchar {
|
|
|
409
405
|
private defaultData;
|
|
410
406
|
private requerido;
|
|
411
407
|
private unique;
|
|
408
|
+
private enum;
|
|
409
|
+
private campo;
|
|
410
|
+
private comentarios;
|
|
412
411
|
constructor(cantidad?: number);
|
|
413
412
|
withType({ maxLenth, defaultData, requerido, unique, }: VarcharType): ColumnDefinition<ColumnTypes>;
|
|
413
|
+
/**
|
|
414
|
+
*
|
|
415
|
+
* @param {string} campo
|
|
416
|
+
* @param {string[]} datos
|
|
417
|
+
* @example
|
|
418
|
+
* Check("example", ["operativo","denegado","revisando"])
|
|
419
|
+
*
|
|
420
|
+
* @returns
|
|
421
|
+
*/
|
|
422
|
+
Check(datos: (string)[]): this;
|
|
414
423
|
Required(): this;
|
|
424
|
+
Comment(comentario: string): this;
|
|
415
425
|
Unique(): this;
|
|
416
426
|
Default(textoDefecto: string): this;
|
|
417
427
|
$(): ColumnDefinition<ColumnTypes>;
|
|
@@ -421,7 +431,9 @@ declare function varchar(cantidad?: number): Varchar;
|
|
|
421
431
|
declare class BoolColumn {
|
|
422
432
|
private requerido;
|
|
423
433
|
private defaultData?;
|
|
434
|
+
private comentario;
|
|
424
435
|
required(): this;
|
|
436
|
+
Comment(comentario: string): this;
|
|
425
437
|
default(value: boolean): this;
|
|
426
438
|
$(): ColumnDefinition;
|
|
427
439
|
}
|
|
@@ -431,7 +443,9 @@ declare class Timestamp {
|
|
|
431
443
|
private requerido;
|
|
432
444
|
private defaultNow;
|
|
433
445
|
private onUpdateNow;
|
|
446
|
+
private comentario;
|
|
434
447
|
required(): this;
|
|
448
|
+
comment(comentario: string): this;
|
|
435
449
|
now(): this;
|
|
436
450
|
onUpdate(): this;
|
|
437
451
|
$(): ColumnDefinition<ColumnTypes>;
|
|
@@ -443,9 +457,11 @@ declare class Money {
|
|
|
443
457
|
private scale;
|
|
444
458
|
private requerido;
|
|
445
459
|
private defaultData?;
|
|
460
|
+
private comentario;
|
|
446
461
|
constructor(presicion?: number, decimales?: number);
|
|
447
462
|
required(): this;
|
|
448
463
|
default(value: number): this;
|
|
464
|
+
comment(comentario: string): this;
|
|
449
465
|
$(): ColumnDefinition<ColumnTypes>;
|
|
450
466
|
}
|
|
451
467
|
declare function money(presicion?: number, decimales?: number): Money;
|
package/dist/index.js
CHANGED
|
@@ -73,7 +73,7 @@ module.exports = __toCommonJS(index_exports);
|
|
|
73
73
|
var Validator = class {
|
|
74
74
|
};
|
|
75
75
|
Validator.isValidTable = (val) => /^(?![0-9_])[a-z][a-z0-9_]{0,62}$/.test(val);
|
|
76
|
-
Validator.isValidColumn = (val) => /^[a-
|
|
76
|
+
Validator.isValidColumn = (val) => /^[a-zA-Z][a-zA-Z0-9]{0,62}$/.test(val);
|
|
77
77
|
Validator.isValidUsername = (val) => /^[a-zA-Z0-9\-_]+$/.test(val);
|
|
78
78
|
var RESERVED_WORDS = /* @__PURE__ */ new Set([
|
|
79
79
|
"select",
|
|
@@ -145,7 +145,6 @@ var BDconnection = class {
|
|
|
145
145
|
mensaje,
|
|
146
146
|
alertar = true
|
|
147
147
|
}) {
|
|
148
|
-
console.log(query);
|
|
149
148
|
try {
|
|
150
149
|
if (this.tipo === "mysql") {
|
|
151
150
|
const pool = await this.conexionMysql();
|
|
@@ -176,7 +175,6 @@ var BDconnection = class {
|
|
|
176
175
|
return respuesta;
|
|
177
176
|
}
|
|
178
177
|
} catch (error) {
|
|
179
|
-
console.log(error);
|
|
180
178
|
if (!alertar) return;
|
|
181
179
|
if (typeof error === "object" && error !== null && "code" in error) {
|
|
182
180
|
if (error.code === "ETIMEDOUT") {
|
|
@@ -219,7 +217,7 @@ function defineTable(tableName, columns) {
|
|
|
219
217
|
const fn = () => tableName;
|
|
220
218
|
if (!Validator.isValidTable(tableName)) {
|
|
221
219
|
throw new Error(
|
|
222
|
-
"El nombre de la tabla no puede contener mayusculas o caracteres especiales"
|
|
220
|
+
"El nombre de la tabla no puede contener mayusculas o caracteres especiales : " + tableName
|
|
223
221
|
);
|
|
224
222
|
}
|
|
225
223
|
if (RESERVED_WORDS.has(tableName)) {
|
|
@@ -246,7 +244,7 @@ function defineTable(tableName, columns) {
|
|
|
246
244
|
if (prop === "toString") return () => tableName;
|
|
247
245
|
if (prop === "valueOf") return () => tableName;
|
|
248
246
|
if (prop in columns) {
|
|
249
|
-
return `${tableName}
|
|
247
|
+
return tipoConexionZORMZ3691 === "mysql" ? `${tableName}.\`${prop.toString()}\` ` : `${tableName}."${prop.toString()}" `;
|
|
250
248
|
}
|
|
251
249
|
return void 0;
|
|
252
250
|
},
|
|
@@ -268,14 +266,15 @@ function defineTable(tableName, columns) {
|
|
|
268
266
|
});
|
|
269
267
|
return proxy;
|
|
270
268
|
}
|
|
271
|
-
async function generateTable(tabla, columns) {
|
|
269
|
+
async function generateTable(tabla, columns, seeSQL = false) {
|
|
272
270
|
let queries = "";
|
|
273
271
|
let columnDefs = [];
|
|
272
|
+
let cheksOut = [];
|
|
274
273
|
let sql = "";
|
|
275
274
|
let id = false;
|
|
276
275
|
for (const columnName in columns) {
|
|
277
276
|
const col = columns[columnName];
|
|
278
|
-
sql = ` ${columnName} `;
|
|
277
|
+
sql = tipoConexionZORMZ3691 === "mysql" ? `\`${columnName}\` ` : `"${columnName}" `;
|
|
279
278
|
if (!col.typo) {
|
|
280
279
|
throw new Error(`La columna ${columnName} no tiene el tipo definido`);
|
|
281
280
|
}
|
|
@@ -289,16 +288,28 @@ async function generateTable(tabla, columns) {
|
|
|
289
288
|
id = col.pk === true ? true : false;
|
|
290
289
|
sql += col.sqlz;
|
|
291
290
|
columnDefs.push(sql);
|
|
291
|
+
if (col.check !== void 0) {
|
|
292
|
+
cheksOut.push(
|
|
293
|
+
`CONSTRAINT chk_${columnName} CHECK (${columnName} IN ( ${col.check}))`
|
|
294
|
+
);
|
|
295
|
+
}
|
|
292
296
|
}
|
|
293
297
|
queries += `CREATE TABLE IF NOT EXISTS ${tabla} (
|
|
294
298
|
`;
|
|
295
299
|
queries += columnDefs.join(", \n");
|
|
300
|
+
if (cheksOut.length > 0) {
|
|
301
|
+
queries += ",\n";
|
|
302
|
+
}
|
|
303
|
+
queries += cheksOut.join(",\n");
|
|
296
304
|
queries += ");";
|
|
297
305
|
if (!conexionZORMZ3691 || !tipoConexionZORMZ3691) {
|
|
298
306
|
throw new Error(
|
|
299
307
|
"La conexi\xF3n no ha sido inicializada. Por favor, llama a getConexion() primero."
|
|
300
308
|
);
|
|
301
309
|
}
|
|
310
|
+
if (seeSQL) {
|
|
311
|
+
console.log(queries);
|
|
312
|
+
}
|
|
302
313
|
if (tipoConexionZORMZ3691 === "mysql") {
|
|
303
314
|
const response = await conexionZORMZ3691.executeConsulta({
|
|
304
315
|
query: queries,
|
|
@@ -376,8 +387,9 @@ var DeleteR = class {
|
|
|
376
387
|
__privateSet(this, _condicion, ` where ${condicion}`);
|
|
377
388
|
return this;
|
|
378
389
|
}
|
|
379
|
-
async execute() {
|
|
390
|
+
async execute(seeQuery = false) {
|
|
380
391
|
const query = `DELETE FROM ${__privateGet(this, _tabla)} ${__privateGet(this, _condicion)}`;
|
|
392
|
+
if (seeQuery) console.log(query);
|
|
381
393
|
if (this.conexion.tipo === "mysql") {
|
|
382
394
|
const respuesta = await this.conexion.executeConsulta({
|
|
383
395
|
query,
|
|
@@ -418,7 +430,6 @@ var QueryBuilder = class {
|
|
|
418
430
|
this.tabla = tabla;
|
|
419
431
|
this.parametros = parametros;
|
|
420
432
|
this.valores = [];
|
|
421
|
-
this.valorRetorno = null;
|
|
422
433
|
this.conexion = conexion;
|
|
423
434
|
}
|
|
424
435
|
/**
|
|
@@ -439,8 +450,11 @@ var QueryBuilder = class {
|
|
|
439
450
|
this.valores = values;
|
|
440
451
|
return this;
|
|
441
452
|
}
|
|
442
|
-
|
|
443
|
-
this.
|
|
453
|
+
Returning(idRetorno) {
|
|
454
|
+
this.returning = idRetorno;
|
|
455
|
+
return this;
|
|
456
|
+
}
|
|
457
|
+
async execute(see = false) {
|
|
444
458
|
let query1 = `INSERT INTO ${this.tabla} `;
|
|
445
459
|
let param = "";
|
|
446
460
|
let arrayArrays = false;
|
|
@@ -493,21 +507,35 @@ var QueryBuilder = class {
|
|
|
493
507
|
} else {
|
|
494
508
|
query += paramespaces;
|
|
495
509
|
}
|
|
510
|
+
if (see) {
|
|
511
|
+
console.log(query);
|
|
512
|
+
}
|
|
496
513
|
if (this.conexion.tipo === "mysql") {
|
|
497
514
|
const respuesta = await this.conexion.executeConsulta({
|
|
498
515
|
query,
|
|
499
516
|
valores: arrayArrays ? [this.valores] : this.valores,
|
|
500
517
|
mensaje: `Ocurrio un error al ingresar datos a ${this.tabla} `
|
|
501
518
|
});
|
|
502
|
-
|
|
519
|
+
let response = [];
|
|
520
|
+
if (this.returning !== void 0) {
|
|
521
|
+
for (let i = 0; i < this.valores.length; i++) {
|
|
522
|
+
response.push(respuesta.insertId + i);
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
return response;
|
|
503
526
|
} else if (this.conexion.tipo === "pg") {
|
|
504
|
-
|
|
527
|
+
if (this.returning !== void 0)
|
|
528
|
+
query += ` RETURNING ${this.returning} `;
|
|
505
529
|
const respuesta = await this.conexion.executeConsulta({
|
|
506
530
|
query,
|
|
507
531
|
valores: this.valores,
|
|
508
532
|
mensaje: `Ocurrio un error al ingresar datos a ${this.tabla} `
|
|
509
533
|
});
|
|
510
|
-
|
|
534
|
+
let response = [];
|
|
535
|
+
if (respuesta.rows !== void 0) {
|
|
536
|
+
response = respuesta.rows.map((obj) => Object.values(obj)[0]);
|
|
537
|
+
}
|
|
538
|
+
return response;
|
|
511
539
|
}
|
|
512
540
|
}
|
|
513
541
|
};
|
|
@@ -620,7 +648,7 @@ var Select = class {
|
|
|
620
648
|
/**
|
|
621
649
|
* @returns {Promise<Array<Object>>}
|
|
622
650
|
*/
|
|
623
|
-
async execute() {
|
|
651
|
+
async execute(see = false) {
|
|
624
652
|
let selectFields = "*";
|
|
625
653
|
if (this.valores) {
|
|
626
654
|
if (typeof this.parametros !== "undefined" && typeof this.parametros !== "string") {
|
|
@@ -628,6 +656,9 @@ var Select = class {
|
|
|
628
656
|
}
|
|
629
657
|
}
|
|
630
658
|
const query = `SELECT ${selectFields} from ${this.tabla} ${this.innerJoin} ${this.leftjoins} ${this.rigthjoins} ${this.condicion} ${this.orderBy} ${this.limit};`;
|
|
659
|
+
if (see) {
|
|
660
|
+
console.log(query);
|
|
661
|
+
}
|
|
631
662
|
if (this.conexion.tipo === "mysql") {
|
|
632
663
|
const respuesta = await this.conexion.executeConsulta({
|
|
633
664
|
query,
|
|
@@ -750,7 +781,7 @@ var Update = class {
|
|
|
750
781
|
}
|
|
751
782
|
} else if (validate) {
|
|
752
783
|
valores.forEach((valors) => {
|
|
753
|
-
valor += eq(valors.campoUp, valors.newCampoUp, !valors.dataTable);
|
|
784
|
+
valor += eq(valors.campoUp.replace(`${this.nombreTabla}.`, ""), valors.newCampoUp.replace(`${this.nombreTabla}.`, ""), !valors.dataTable);
|
|
754
785
|
valor += ",";
|
|
755
786
|
});
|
|
756
787
|
}
|
|
@@ -778,8 +809,11 @@ var Update = class {
|
|
|
778
809
|
}
|
|
779
810
|
return this;
|
|
780
811
|
}
|
|
781
|
-
async execute() {
|
|
812
|
+
async execute(seeQuery = false) {
|
|
782
813
|
const query = `UPDATE ${this.nombreTabla} SET ${this.valores} ${this.condicion};`;
|
|
814
|
+
if (seeQuery) {
|
|
815
|
+
console.log(query);
|
|
816
|
+
}
|
|
783
817
|
const respuesta = await this.conexion.executeConsulta({
|
|
784
818
|
query,
|
|
785
819
|
mensaje: "Error Update"
|
|
@@ -787,9 +821,7 @@ var Update = class {
|
|
|
787
821
|
if (this.conexion.tipo === "mysql") {
|
|
788
822
|
return respuesta.affectedRows;
|
|
789
823
|
} else {
|
|
790
|
-
return
|
|
791
|
-
resultado: respuesta.rowCount
|
|
792
|
-
};
|
|
824
|
+
return respuesta.rowCount;
|
|
793
825
|
}
|
|
794
826
|
}
|
|
795
827
|
};
|
|
@@ -839,6 +871,7 @@ var IntColumn = class {
|
|
|
839
871
|
this.unique = false;
|
|
840
872
|
this.autoIncrement = false;
|
|
841
873
|
this.unsingned = { uso: false, valor: 0 };
|
|
874
|
+
this.comentario = "";
|
|
842
875
|
}
|
|
843
876
|
Required() {
|
|
844
877
|
this.requerido = true;
|
|
@@ -849,6 +882,10 @@ var IntColumn = class {
|
|
|
849
882
|
this.pk = true;
|
|
850
883
|
return this;
|
|
851
884
|
}
|
|
885
|
+
Comment(comentario) {
|
|
886
|
+
this.comentario = comentario;
|
|
887
|
+
return this;
|
|
888
|
+
}
|
|
852
889
|
Unique() {
|
|
853
890
|
this.unique = true;
|
|
854
891
|
return this;
|
|
@@ -907,6 +944,9 @@ var IntColumn = class {
|
|
|
907
944
|
parts.push(`DEFAULT ${this.defaultData}`);
|
|
908
945
|
}
|
|
909
946
|
if (this.unique) parts.push("UNIQUE");
|
|
947
|
+
if (db === "mysql" && this.comentario.length > 2) {
|
|
948
|
+
parts.push(` COMMENT '${this.comentario}' `);
|
|
949
|
+
}
|
|
910
950
|
const valor = {
|
|
911
951
|
pk: false,
|
|
912
952
|
typo: "int",
|
|
@@ -926,6 +966,9 @@ var Varchar = class {
|
|
|
926
966
|
this.defaultData = "";
|
|
927
967
|
this.requerido = false;
|
|
928
968
|
this.unique = false;
|
|
969
|
+
this.enum = "";
|
|
970
|
+
this.campo = "";
|
|
971
|
+
this.comentarios = "";
|
|
929
972
|
this.maxLenth = cantidad;
|
|
930
973
|
}
|
|
931
974
|
withType({
|
|
@@ -939,10 +982,27 @@ var Varchar = class {
|
|
|
939
982
|
this.unique = unique;
|
|
940
983
|
return this.Default(defaultData).$();
|
|
941
984
|
}
|
|
985
|
+
/**
|
|
986
|
+
*
|
|
987
|
+
* @param {string} campo
|
|
988
|
+
* @param {string[]} datos
|
|
989
|
+
* @example
|
|
990
|
+
* Check("example", ["operativo","denegado","revisando"])
|
|
991
|
+
*
|
|
992
|
+
* @returns
|
|
993
|
+
*/
|
|
994
|
+
Check(datos) {
|
|
995
|
+
this.enum = datos.map((caracter) => `'${caracter}'`).join(",");
|
|
996
|
+
return this;
|
|
997
|
+
}
|
|
942
998
|
Required() {
|
|
943
999
|
this.requerido = true;
|
|
944
1000
|
return this;
|
|
945
1001
|
}
|
|
1002
|
+
Comment(comentario) {
|
|
1003
|
+
this.comentarios = comentario;
|
|
1004
|
+
return this;
|
|
1005
|
+
}
|
|
946
1006
|
Unique() {
|
|
947
1007
|
this.unique = true;
|
|
948
1008
|
return this;
|
|
@@ -957,11 +1017,16 @@ var Varchar = class {
|
|
|
957
1017
|
return this;
|
|
958
1018
|
}
|
|
959
1019
|
$() {
|
|
960
|
-
let sqlz = `VARCHAR(${this.maxLenth}) ${this.requerido ? "NOT NULL" : "NULL"} ${this.defaultData.length > 0 ? ` DEFAULT ${this.defaultData} ` : " "} ${this.unique ? " UNIQUE" : " "}
|
|
1020
|
+
let sqlz = `VARCHAR(${this.maxLenth}) ${this.requerido ? "NOT NULL" : "NULL"} ${this.defaultData.length > 0 ? ` DEFAULT ${this.defaultData} ` : " "} ${this.unique ? " UNIQUE" : " "}`;
|
|
1021
|
+
const db = getTipoConexion();
|
|
1022
|
+
if (db === "mysql" && this.comentarios.length > 2) {
|
|
1023
|
+
sqlz += ` COMMENT '${this.comentarios}' `;
|
|
1024
|
+
}
|
|
961
1025
|
const response = {
|
|
962
1026
|
typo: "varchar",
|
|
963
1027
|
pk: false,
|
|
964
|
-
sqlz
|
|
1028
|
+
sqlz,
|
|
1029
|
+
check: this.enum.length > 2 ? this.enum : void 0
|
|
965
1030
|
};
|
|
966
1031
|
return response;
|
|
967
1032
|
}
|
|
@@ -974,11 +1039,16 @@ function varchar(cantidad = 100) {
|
|
|
974
1039
|
var BoolColumn = class {
|
|
975
1040
|
constructor() {
|
|
976
1041
|
this.requerido = false;
|
|
1042
|
+
this.comentario = "";
|
|
977
1043
|
}
|
|
978
1044
|
required() {
|
|
979
1045
|
this.requerido = true;
|
|
980
1046
|
return this;
|
|
981
1047
|
}
|
|
1048
|
+
Comment(comentario) {
|
|
1049
|
+
this.comentario = comentario;
|
|
1050
|
+
return this;
|
|
1051
|
+
}
|
|
982
1052
|
default(value) {
|
|
983
1053
|
this.defaultData = value;
|
|
984
1054
|
return this;
|
|
@@ -993,6 +1063,9 @@ var BoolColumn = class {
|
|
|
993
1063
|
`DEFAULT ${db === "mysql" ? this.defaultData ? 1 : 0 : this.defaultData}`
|
|
994
1064
|
);
|
|
995
1065
|
}
|
|
1066
|
+
if (db === "mysql" && this.comentario.length > 2) {
|
|
1067
|
+
parts.push(` COMMENT '${this.comentario}' `);
|
|
1068
|
+
}
|
|
996
1069
|
const response = {
|
|
997
1070
|
typo: "bool",
|
|
998
1071
|
pk: false,
|
|
@@ -1011,11 +1084,16 @@ var Timestamp = class {
|
|
|
1011
1084
|
this.requerido = false;
|
|
1012
1085
|
this.defaultNow = false;
|
|
1013
1086
|
this.onUpdateNow = false;
|
|
1087
|
+
this.comentario = "";
|
|
1014
1088
|
}
|
|
1015
1089
|
required() {
|
|
1016
1090
|
this.requerido = true;
|
|
1017
1091
|
return this;
|
|
1018
1092
|
}
|
|
1093
|
+
comment(comentario) {
|
|
1094
|
+
this.comentario = comentario;
|
|
1095
|
+
return this;
|
|
1096
|
+
}
|
|
1019
1097
|
now() {
|
|
1020
1098
|
this.defaultNow = true;
|
|
1021
1099
|
return this;
|
|
@@ -1035,6 +1113,9 @@ var Timestamp = class {
|
|
|
1035
1113
|
if (this.onUpdateNow && db === "mysql") {
|
|
1036
1114
|
parts.push("ON UPDATE CURRENT_TIMESTAMP");
|
|
1037
1115
|
}
|
|
1116
|
+
if (db === "mysql" && this.comentario.length > 2) {
|
|
1117
|
+
parts.push(` COMMENT '${this.comentario}' `);
|
|
1118
|
+
}
|
|
1038
1119
|
const response = {
|
|
1039
1120
|
typo: "timestamp",
|
|
1040
1121
|
pk: false,
|
|
@@ -1053,6 +1134,7 @@ var Money = class {
|
|
|
1053
1134
|
this.precision = 10;
|
|
1054
1135
|
this.scale = 2;
|
|
1055
1136
|
this.requerido = false;
|
|
1137
|
+
this.comentario = "";
|
|
1056
1138
|
this.precision = presicion;
|
|
1057
1139
|
this.scale = decimales;
|
|
1058
1140
|
}
|
|
@@ -1064,6 +1146,10 @@ var Money = class {
|
|
|
1064
1146
|
this.defaultData = value;
|
|
1065
1147
|
return this;
|
|
1066
1148
|
}
|
|
1149
|
+
comment(comentario) {
|
|
1150
|
+
this.comentario = comentario;
|
|
1151
|
+
return this;
|
|
1152
|
+
}
|
|
1067
1153
|
$() {
|
|
1068
1154
|
const parts = [];
|
|
1069
1155
|
const db = getTipoConexion();
|
|
@@ -1074,6 +1160,9 @@ var Money = class {
|
|
|
1074
1160
|
if (this.defaultData !== void 0) {
|
|
1075
1161
|
parts.push(`DEFAULT ${this.defaultData.toFixed(this.scale)}`);
|
|
1076
1162
|
}
|
|
1163
|
+
if (db === "mysql" && this.comentario.length > 2) {
|
|
1164
|
+
parts.push(` COMMENT '${this.comentario}' `);
|
|
1165
|
+
}
|
|
1077
1166
|
const response = {
|
|
1078
1167
|
typo: "money",
|
|
1079
1168
|
pk: false,
|
package/dist/index.mjs
CHANGED
|
@@ -10,7 +10,7 @@ var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "
|
|
|
10
10
|
var Validator = class {
|
|
11
11
|
};
|
|
12
12
|
Validator.isValidTable = (val) => /^(?![0-9_])[a-z][a-z0-9_]{0,62}$/.test(val);
|
|
13
|
-
Validator.isValidColumn = (val) => /^[a-
|
|
13
|
+
Validator.isValidColumn = (val) => /^[a-zA-Z][a-zA-Z0-9]{0,62}$/.test(val);
|
|
14
14
|
Validator.isValidUsername = (val) => /^[a-zA-Z0-9\-_]+$/.test(val);
|
|
15
15
|
var RESERVED_WORDS = /* @__PURE__ */ new Set([
|
|
16
16
|
"select",
|
|
@@ -82,7 +82,6 @@ var BDconnection = class {
|
|
|
82
82
|
mensaje,
|
|
83
83
|
alertar = true
|
|
84
84
|
}) {
|
|
85
|
-
console.log(query);
|
|
86
85
|
try {
|
|
87
86
|
if (this.tipo === "mysql") {
|
|
88
87
|
const pool = await this.conexionMysql();
|
|
@@ -113,7 +112,6 @@ var BDconnection = class {
|
|
|
113
112
|
return respuesta;
|
|
114
113
|
}
|
|
115
114
|
} catch (error) {
|
|
116
|
-
console.log(error);
|
|
117
115
|
if (!alertar) return;
|
|
118
116
|
if (typeof error === "object" && error !== null && "code" in error) {
|
|
119
117
|
if (error.code === "ETIMEDOUT") {
|
|
@@ -156,7 +154,7 @@ function defineTable(tableName, columns) {
|
|
|
156
154
|
const fn = () => tableName;
|
|
157
155
|
if (!Validator.isValidTable(tableName)) {
|
|
158
156
|
throw new Error(
|
|
159
|
-
"El nombre de la tabla no puede contener mayusculas o caracteres especiales"
|
|
157
|
+
"El nombre de la tabla no puede contener mayusculas o caracteres especiales : " + tableName
|
|
160
158
|
);
|
|
161
159
|
}
|
|
162
160
|
if (RESERVED_WORDS.has(tableName)) {
|
|
@@ -183,7 +181,7 @@ function defineTable(tableName, columns) {
|
|
|
183
181
|
if (prop === "toString") return () => tableName;
|
|
184
182
|
if (prop === "valueOf") return () => tableName;
|
|
185
183
|
if (prop in columns) {
|
|
186
|
-
return `${tableName}
|
|
184
|
+
return tipoConexionZORMZ3691 === "mysql" ? `${tableName}.\`${prop.toString()}\` ` : `${tableName}."${prop.toString()}" `;
|
|
187
185
|
}
|
|
188
186
|
return void 0;
|
|
189
187
|
},
|
|
@@ -205,14 +203,15 @@ function defineTable(tableName, columns) {
|
|
|
205
203
|
});
|
|
206
204
|
return proxy;
|
|
207
205
|
}
|
|
208
|
-
async function generateTable(tabla, columns) {
|
|
206
|
+
async function generateTable(tabla, columns, seeSQL = false) {
|
|
209
207
|
let queries = "";
|
|
210
208
|
let columnDefs = [];
|
|
209
|
+
let cheksOut = [];
|
|
211
210
|
let sql = "";
|
|
212
211
|
let id = false;
|
|
213
212
|
for (const columnName in columns) {
|
|
214
213
|
const col = columns[columnName];
|
|
215
|
-
sql = ` ${columnName} `;
|
|
214
|
+
sql = tipoConexionZORMZ3691 === "mysql" ? `\`${columnName}\` ` : `"${columnName}" `;
|
|
216
215
|
if (!col.typo) {
|
|
217
216
|
throw new Error(`La columna ${columnName} no tiene el tipo definido`);
|
|
218
217
|
}
|
|
@@ -226,16 +225,28 @@ async function generateTable(tabla, columns) {
|
|
|
226
225
|
id = col.pk === true ? true : false;
|
|
227
226
|
sql += col.sqlz;
|
|
228
227
|
columnDefs.push(sql);
|
|
228
|
+
if (col.check !== void 0) {
|
|
229
|
+
cheksOut.push(
|
|
230
|
+
`CONSTRAINT chk_${columnName} CHECK (${columnName} IN ( ${col.check}))`
|
|
231
|
+
);
|
|
232
|
+
}
|
|
229
233
|
}
|
|
230
234
|
queries += `CREATE TABLE IF NOT EXISTS ${tabla} (
|
|
231
235
|
`;
|
|
232
236
|
queries += columnDefs.join(", \n");
|
|
237
|
+
if (cheksOut.length > 0) {
|
|
238
|
+
queries += ",\n";
|
|
239
|
+
}
|
|
240
|
+
queries += cheksOut.join(",\n");
|
|
233
241
|
queries += ");";
|
|
234
242
|
if (!conexionZORMZ3691 || !tipoConexionZORMZ3691) {
|
|
235
243
|
throw new Error(
|
|
236
244
|
"La conexi\xF3n no ha sido inicializada. Por favor, llama a getConexion() primero."
|
|
237
245
|
);
|
|
238
246
|
}
|
|
247
|
+
if (seeSQL) {
|
|
248
|
+
console.log(queries);
|
|
249
|
+
}
|
|
239
250
|
if (tipoConexionZORMZ3691 === "mysql") {
|
|
240
251
|
const response = await conexionZORMZ3691.executeConsulta({
|
|
241
252
|
query: queries,
|
|
@@ -313,8 +324,9 @@ var DeleteR = class {
|
|
|
313
324
|
__privateSet(this, _condicion, ` where ${condicion}`);
|
|
314
325
|
return this;
|
|
315
326
|
}
|
|
316
|
-
async execute() {
|
|
327
|
+
async execute(seeQuery = false) {
|
|
317
328
|
const query = `DELETE FROM ${__privateGet(this, _tabla)} ${__privateGet(this, _condicion)}`;
|
|
329
|
+
if (seeQuery) console.log(query);
|
|
318
330
|
if (this.conexion.tipo === "mysql") {
|
|
319
331
|
const respuesta = await this.conexion.executeConsulta({
|
|
320
332
|
query,
|
|
@@ -355,7 +367,6 @@ var QueryBuilder = class {
|
|
|
355
367
|
this.tabla = tabla;
|
|
356
368
|
this.parametros = parametros;
|
|
357
369
|
this.valores = [];
|
|
358
|
-
this.valorRetorno = null;
|
|
359
370
|
this.conexion = conexion;
|
|
360
371
|
}
|
|
361
372
|
/**
|
|
@@ -376,8 +387,11 @@ var QueryBuilder = class {
|
|
|
376
387
|
this.valores = values;
|
|
377
388
|
return this;
|
|
378
389
|
}
|
|
379
|
-
|
|
380
|
-
this.
|
|
390
|
+
Returning(idRetorno) {
|
|
391
|
+
this.returning = idRetorno;
|
|
392
|
+
return this;
|
|
393
|
+
}
|
|
394
|
+
async execute(see = false) {
|
|
381
395
|
let query1 = `INSERT INTO ${this.tabla} `;
|
|
382
396
|
let param = "";
|
|
383
397
|
let arrayArrays = false;
|
|
@@ -430,21 +444,35 @@ var QueryBuilder = class {
|
|
|
430
444
|
} else {
|
|
431
445
|
query += paramespaces;
|
|
432
446
|
}
|
|
447
|
+
if (see) {
|
|
448
|
+
console.log(query);
|
|
449
|
+
}
|
|
433
450
|
if (this.conexion.tipo === "mysql") {
|
|
434
451
|
const respuesta = await this.conexion.executeConsulta({
|
|
435
452
|
query,
|
|
436
453
|
valores: arrayArrays ? [this.valores] : this.valores,
|
|
437
454
|
mensaje: `Ocurrio un error al ingresar datos a ${this.tabla} `
|
|
438
455
|
});
|
|
439
|
-
|
|
456
|
+
let response = [];
|
|
457
|
+
if (this.returning !== void 0) {
|
|
458
|
+
for (let i = 0; i < this.valores.length; i++) {
|
|
459
|
+
response.push(respuesta.insertId + i);
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
return response;
|
|
440
463
|
} else if (this.conexion.tipo === "pg") {
|
|
441
|
-
|
|
464
|
+
if (this.returning !== void 0)
|
|
465
|
+
query += ` RETURNING ${this.returning} `;
|
|
442
466
|
const respuesta = await this.conexion.executeConsulta({
|
|
443
467
|
query,
|
|
444
468
|
valores: this.valores,
|
|
445
469
|
mensaje: `Ocurrio un error al ingresar datos a ${this.tabla} `
|
|
446
470
|
});
|
|
447
|
-
|
|
471
|
+
let response = [];
|
|
472
|
+
if (respuesta.rows !== void 0) {
|
|
473
|
+
response = respuesta.rows.map((obj) => Object.values(obj)[0]);
|
|
474
|
+
}
|
|
475
|
+
return response;
|
|
448
476
|
}
|
|
449
477
|
}
|
|
450
478
|
};
|
|
@@ -557,7 +585,7 @@ var Select = class {
|
|
|
557
585
|
/**
|
|
558
586
|
* @returns {Promise<Array<Object>>}
|
|
559
587
|
*/
|
|
560
|
-
async execute() {
|
|
588
|
+
async execute(see = false) {
|
|
561
589
|
let selectFields = "*";
|
|
562
590
|
if (this.valores) {
|
|
563
591
|
if (typeof this.parametros !== "undefined" && typeof this.parametros !== "string") {
|
|
@@ -565,6 +593,9 @@ var Select = class {
|
|
|
565
593
|
}
|
|
566
594
|
}
|
|
567
595
|
const query = `SELECT ${selectFields} from ${this.tabla} ${this.innerJoin} ${this.leftjoins} ${this.rigthjoins} ${this.condicion} ${this.orderBy} ${this.limit};`;
|
|
596
|
+
if (see) {
|
|
597
|
+
console.log(query);
|
|
598
|
+
}
|
|
568
599
|
if (this.conexion.tipo === "mysql") {
|
|
569
600
|
const respuesta = await this.conexion.executeConsulta({
|
|
570
601
|
query,
|
|
@@ -687,7 +718,7 @@ var Update = class {
|
|
|
687
718
|
}
|
|
688
719
|
} else if (validate) {
|
|
689
720
|
valores.forEach((valors) => {
|
|
690
|
-
valor += eq(valors.campoUp, valors.newCampoUp, !valors.dataTable);
|
|
721
|
+
valor += eq(valors.campoUp.replace(`${this.nombreTabla}.`, ""), valors.newCampoUp.replace(`${this.nombreTabla}.`, ""), !valors.dataTable);
|
|
691
722
|
valor += ",";
|
|
692
723
|
});
|
|
693
724
|
}
|
|
@@ -715,8 +746,11 @@ var Update = class {
|
|
|
715
746
|
}
|
|
716
747
|
return this;
|
|
717
748
|
}
|
|
718
|
-
async execute() {
|
|
749
|
+
async execute(seeQuery = false) {
|
|
719
750
|
const query = `UPDATE ${this.nombreTabla} SET ${this.valores} ${this.condicion};`;
|
|
751
|
+
if (seeQuery) {
|
|
752
|
+
console.log(query);
|
|
753
|
+
}
|
|
720
754
|
const respuesta = await this.conexion.executeConsulta({
|
|
721
755
|
query,
|
|
722
756
|
mensaje: "Error Update"
|
|
@@ -724,9 +758,7 @@ var Update = class {
|
|
|
724
758
|
if (this.conexion.tipo === "mysql") {
|
|
725
759
|
return respuesta.affectedRows;
|
|
726
760
|
} else {
|
|
727
|
-
return
|
|
728
|
-
resultado: respuesta.rowCount
|
|
729
|
-
};
|
|
761
|
+
return respuesta.rowCount;
|
|
730
762
|
}
|
|
731
763
|
}
|
|
732
764
|
};
|
|
@@ -776,6 +808,7 @@ var IntColumn = class {
|
|
|
776
808
|
this.unique = false;
|
|
777
809
|
this.autoIncrement = false;
|
|
778
810
|
this.unsingned = { uso: false, valor: 0 };
|
|
811
|
+
this.comentario = "";
|
|
779
812
|
}
|
|
780
813
|
Required() {
|
|
781
814
|
this.requerido = true;
|
|
@@ -786,6 +819,10 @@ var IntColumn = class {
|
|
|
786
819
|
this.pk = true;
|
|
787
820
|
return this;
|
|
788
821
|
}
|
|
822
|
+
Comment(comentario) {
|
|
823
|
+
this.comentario = comentario;
|
|
824
|
+
return this;
|
|
825
|
+
}
|
|
789
826
|
Unique() {
|
|
790
827
|
this.unique = true;
|
|
791
828
|
return this;
|
|
@@ -844,6 +881,9 @@ var IntColumn = class {
|
|
|
844
881
|
parts.push(`DEFAULT ${this.defaultData}`);
|
|
845
882
|
}
|
|
846
883
|
if (this.unique) parts.push("UNIQUE");
|
|
884
|
+
if (db === "mysql" && this.comentario.length > 2) {
|
|
885
|
+
parts.push(` COMMENT '${this.comentario}' `);
|
|
886
|
+
}
|
|
847
887
|
const valor = {
|
|
848
888
|
pk: false,
|
|
849
889
|
typo: "int",
|
|
@@ -863,6 +903,9 @@ var Varchar = class {
|
|
|
863
903
|
this.defaultData = "";
|
|
864
904
|
this.requerido = false;
|
|
865
905
|
this.unique = false;
|
|
906
|
+
this.enum = "";
|
|
907
|
+
this.campo = "";
|
|
908
|
+
this.comentarios = "";
|
|
866
909
|
this.maxLenth = cantidad;
|
|
867
910
|
}
|
|
868
911
|
withType({
|
|
@@ -876,10 +919,27 @@ var Varchar = class {
|
|
|
876
919
|
this.unique = unique;
|
|
877
920
|
return this.Default(defaultData).$();
|
|
878
921
|
}
|
|
922
|
+
/**
|
|
923
|
+
*
|
|
924
|
+
* @param {string} campo
|
|
925
|
+
* @param {string[]} datos
|
|
926
|
+
* @example
|
|
927
|
+
* Check("example", ["operativo","denegado","revisando"])
|
|
928
|
+
*
|
|
929
|
+
* @returns
|
|
930
|
+
*/
|
|
931
|
+
Check(datos) {
|
|
932
|
+
this.enum = datos.map((caracter) => `'${caracter}'`).join(",");
|
|
933
|
+
return this;
|
|
934
|
+
}
|
|
879
935
|
Required() {
|
|
880
936
|
this.requerido = true;
|
|
881
937
|
return this;
|
|
882
938
|
}
|
|
939
|
+
Comment(comentario) {
|
|
940
|
+
this.comentarios = comentario;
|
|
941
|
+
return this;
|
|
942
|
+
}
|
|
883
943
|
Unique() {
|
|
884
944
|
this.unique = true;
|
|
885
945
|
return this;
|
|
@@ -894,11 +954,16 @@ var Varchar = class {
|
|
|
894
954
|
return this;
|
|
895
955
|
}
|
|
896
956
|
$() {
|
|
897
|
-
let sqlz = `VARCHAR(${this.maxLenth}) ${this.requerido ? "NOT NULL" : "NULL"} ${this.defaultData.length > 0 ? ` DEFAULT ${this.defaultData} ` : " "} ${this.unique ? " UNIQUE" : " "}
|
|
957
|
+
let sqlz = `VARCHAR(${this.maxLenth}) ${this.requerido ? "NOT NULL" : "NULL"} ${this.defaultData.length > 0 ? ` DEFAULT ${this.defaultData} ` : " "} ${this.unique ? " UNIQUE" : " "}`;
|
|
958
|
+
const db = getTipoConexion();
|
|
959
|
+
if (db === "mysql" && this.comentarios.length > 2) {
|
|
960
|
+
sqlz += ` COMMENT '${this.comentarios}' `;
|
|
961
|
+
}
|
|
898
962
|
const response = {
|
|
899
963
|
typo: "varchar",
|
|
900
964
|
pk: false,
|
|
901
|
-
sqlz
|
|
965
|
+
sqlz,
|
|
966
|
+
check: this.enum.length > 2 ? this.enum : void 0
|
|
902
967
|
};
|
|
903
968
|
return response;
|
|
904
969
|
}
|
|
@@ -911,11 +976,16 @@ function varchar(cantidad = 100) {
|
|
|
911
976
|
var BoolColumn = class {
|
|
912
977
|
constructor() {
|
|
913
978
|
this.requerido = false;
|
|
979
|
+
this.comentario = "";
|
|
914
980
|
}
|
|
915
981
|
required() {
|
|
916
982
|
this.requerido = true;
|
|
917
983
|
return this;
|
|
918
984
|
}
|
|
985
|
+
Comment(comentario) {
|
|
986
|
+
this.comentario = comentario;
|
|
987
|
+
return this;
|
|
988
|
+
}
|
|
919
989
|
default(value) {
|
|
920
990
|
this.defaultData = value;
|
|
921
991
|
return this;
|
|
@@ -930,6 +1000,9 @@ var BoolColumn = class {
|
|
|
930
1000
|
`DEFAULT ${db === "mysql" ? this.defaultData ? 1 : 0 : this.defaultData}`
|
|
931
1001
|
);
|
|
932
1002
|
}
|
|
1003
|
+
if (db === "mysql" && this.comentario.length > 2) {
|
|
1004
|
+
parts.push(` COMMENT '${this.comentario}' `);
|
|
1005
|
+
}
|
|
933
1006
|
const response = {
|
|
934
1007
|
typo: "bool",
|
|
935
1008
|
pk: false,
|
|
@@ -948,11 +1021,16 @@ var Timestamp = class {
|
|
|
948
1021
|
this.requerido = false;
|
|
949
1022
|
this.defaultNow = false;
|
|
950
1023
|
this.onUpdateNow = false;
|
|
1024
|
+
this.comentario = "";
|
|
951
1025
|
}
|
|
952
1026
|
required() {
|
|
953
1027
|
this.requerido = true;
|
|
954
1028
|
return this;
|
|
955
1029
|
}
|
|
1030
|
+
comment(comentario) {
|
|
1031
|
+
this.comentario = comentario;
|
|
1032
|
+
return this;
|
|
1033
|
+
}
|
|
956
1034
|
now() {
|
|
957
1035
|
this.defaultNow = true;
|
|
958
1036
|
return this;
|
|
@@ -972,6 +1050,9 @@ var Timestamp = class {
|
|
|
972
1050
|
if (this.onUpdateNow && db === "mysql") {
|
|
973
1051
|
parts.push("ON UPDATE CURRENT_TIMESTAMP");
|
|
974
1052
|
}
|
|
1053
|
+
if (db === "mysql" && this.comentario.length > 2) {
|
|
1054
|
+
parts.push(` COMMENT '${this.comentario}' `);
|
|
1055
|
+
}
|
|
975
1056
|
const response = {
|
|
976
1057
|
typo: "timestamp",
|
|
977
1058
|
pk: false,
|
|
@@ -990,6 +1071,7 @@ var Money = class {
|
|
|
990
1071
|
this.precision = 10;
|
|
991
1072
|
this.scale = 2;
|
|
992
1073
|
this.requerido = false;
|
|
1074
|
+
this.comentario = "";
|
|
993
1075
|
this.precision = presicion;
|
|
994
1076
|
this.scale = decimales;
|
|
995
1077
|
}
|
|
@@ -1001,6 +1083,10 @@ var Money = class {
|
|
|
1001
1083
|
this.defaultData = value;
|
|
1002
1084
|
return this;
|
|
1003
1085
|
}
|
|
1086
|
+
comment(comentario) {
|
|
1087
|
+
this.comentario = comentario;
|
|
1088
|
+
return this;
|
|
1089
|
+
}
|
|
1004
1090
|
$() {
|
|
1005
1091
|
const parts = [];
|
|
1006
1092
|
const db = getTipoConexion();
|
|
@@ -1011,6 +1097,9 @@ var Money = class {
|
|
|
1011
1097
|
if (this.defaultData !== void 0) {
|
|
1012
1098
|
parts.push(`DEFAULT ${this.defaultData.toFixed(this.scale)}`);
|
|
1013
1099
|
}
|
|
1100
|
+
if (db === "mysql" && this.comentario.length > 2) {
|
|
1101
|
+
parts.push(` COMMENT '${this.comentario}' `);
|
|
1102
|
+
}
|
|
1014
1103
|
const response = {
|
|
1015
1104
|
typo: "money",
|
|
1016
1105
|
pk: false,
|