zormz 1.4.1 → 1.4.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/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 productosPrueba = defineTable("productosPrueba",{
87
- * id:{typo:"int",id:true},
88
- dato: {
89
- typo:"varchar",
90
- maxLength:200,
91
- default:"hola mundo"
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 {
@@ -131,7 +124,7 @@ declare class QueryBuilder {
131
124
  private tabla;
132
125
  private parametros;
133
126
  private valores;
134
- private valorRetorno;
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
- execute(see?: boolean): Promise<number | any[] | undefined>;
150
+ Returning(idRetorno: string): this;
151
+ execute(see?: boolean): Promise<any[] | undefined>;
158
152
  }
159
153
 
160
154
  type valor = "ASC" | "DESC";
@@ -344,6 +338,19 @@ declare const NOTNULL: (variable: string) => string;
344
338
  * eq('id',valor1, false)
345
339
  */
346
340
  declare const eq: (valor1: Valor, valor2: Valor, literal?: boolean) => string;
341
+ /**
342
+ *
343
+ * @param {(string | number | boolean )} valor1 - primer valor
344
+ * @param {(string | number | boolean )} valor2 - segundo valor
345
+ * @param {(boolean )} literal
346
+ *
347
+ * @example
348
+ * //en caso sea un valores normalres
349
+ * neq('id','1')
350
+ * //en caso de que seaq un valor de tabla columna
351
+ * neq('id',valor1, false)
352
+ */
353
+ declare const neq: (valor1: Valor, valor2: Valor, literal?: boolean) => string;
347
354
  /**
348
355
  *
349
356
  * @param {string} valor
@@ -392,9 +399,11 @@ declare class IntColumn {
392
399
  private defaultData?;
393
400
  private pk?;
394
401
  private unsingned;
402
+ private comentario;
395
403
  constructor();
396
404
  Required(): this;
397
405
  Pk(): this;
406
+ Comment(comentario: string): this;
398
407
  Unique(): this;
399
408
  Unsingned(numeroPositivosInicio?: number): this;
400
409
  AutoIncrement(): this;
@@ -409,9 +418,23 @@ declare class Varchar {
409
418
  private defaultData;
410
419
  private requerido;
411
420
  private unique;
421
+ private enum;
422
+ private campo;
423
+ private comentarios;
412
424
  constructor(cantidad?: number);
413
425
  withType({ maxLenth, defaultData, requerido, unique, }: VarcharType): ColumnDefinition<ColumnTypes>;
426
+ /**
427
+ *
428
+ * @param {string} campo
429
+ * @param {string[]} datos
430
+ * @example
431
+ * Check("example", ["operativo","denegado","revisando"])
432
+ *
433
+ * @returns
434
+ */
435
+ Check(datos: (string)[]): this;
414
436
  Required(): this;
437
+ Comment(comentario: string): this;
415
438
  Unique(): this;
416
439
  Default(textoDefecto: string): this;
417
440
  $(): ColumnDefinition<ColumnTypes>;
@@ -421,7 +444,9 @@ declare function varchar(cantidad?: number): Varchar;
421
444
  declare class BoolColumn {
422
445
  private requerido;
423
446
  private defaultData?;
447
+ private comentario;
424
448
  required(): this;
449
+ Comment(comentario: string): this;
425
450
  default(value: boolean): this;
426
451
  $(): ColumnDefinition;
427
452
  }
@@ -431,7 +456,9 @@ declare class Timestamp {
431
456
  private requerido;
432
457
  private defaultNow;
433
458
  private onUpdateNow;
459
+ private comentario;
434
460
  required(): this;
461
+ comment(comentario: string): this;
435
462
  now(): this;
436
463
  onUpdate(): this;
437
464
  $(): ColumnDefinition<ColumnTypes>;
@@ -443,11 +470,13 @@ declare class Money {
443
470
  private scale;
444
471
  private requerido;
445
472
  private defaultData?;
473
+ private comentario;
446
474
  constructor(presicion?: number, decimales?: number);
447
475
  required(): this;
448
476
  default(value: number): this;
477
+ comment(comentario: string): this;
449
478
  $(): ColumnDefinition<ColumnTypes>;
450
479
  }
451
480
  declare function money(presicion?: number, decimales?: number): Money;
452
481
 
453
- export { AND, BDconnection, CURRENT_TIMESTAMP, type Consultas, DB, DeleteR, ILIKE, MAYOR, MENOR, NOTNULL, NOW, NULL, OR, ORQ, QueryBuilder, Select, type TableProxy, type Tipos, UP, Update, type Valores, type arrayData, type arrayDatas, bool, type connecionLocal, type connecionRed, type connectionDB, defineTable, dropTable, eq, generateTable, getConexion, getRed, getTipoConexion, int, money, timestamp, type valor, varchar };
482
+ export { AND, BDconnection, CURRENT_TIMESTAMP, type Consultas, DB, DeleteR, ILIKE, MAYOR, MENOR, NOTNULL, NOW, NULL, OR, ORQ, QueryBuilder, Select, type TableProxy, type Tipos, UP, Update, type Valores, type arrayData, type arrayDatas, bool, type connecionLocal, type connecionRed, type connectionDB, defineTable, dropTable, eq, generateTable, getConexion, getRed, getTipoConexion, int, money, neq, timestamp, type valor, varchar };
package/dist/index.js CHANGED
@@ -64,6 +64,7 @@ __export(index_exports, {
64
64
  getTipoConexion: () => getTipoConexion,
65
65
  int: () => int,
66
66
  money: () => money,
67
+ neq: () => neq,
67
68
  timestamp: () => timestamp,
68
69
  varchar: () => varchar
69
70
  });
@@ -73,7 +74,7 @@ module.exports = __toCommonJS(index_exports);
73
74
  var Validator = class {
74
75
  };
75
76
  Validator.isValidTable = (val) => /^(?![0-9_])[a-z][a-z0-9_]{0,62}$/.test(val);
76
- Validator.isValidColumn = (val) => /^[a-z][a-z0-9_]{0,62}$/.test(val);
77
+ Validator.isValidColumn = (val) => /^[a-zA-Z][a-zA-Z0-9]{0,62}$/.test(val);
77
78
  Validator.isValidUsername = (val) => /^[a-zA-Z0-9\-_]+$/.test(val);
78
79
  var RESERVED_WORDS = /* @__PURE__ */ new Set([
79
80
  "select",
@@ -217,7 +218,7 @@ function defineTable(tableName, columns) {
217
218
  const fn = () => tableName;
218
219
  if (!Validator.isValidTable(tableName)) {
219
220
  throw new Error(
220
- "El nombre de la tabla no puede contener mayusculas o caracteres especiales"
221
+ "El nombre de la tabla no puede contener mayusculas o caracteres especiales : " + tableName
221
222
  );
222
223
  }
223
224
  if (RESERVED_WORDS.has(tableName)) {
@@ -244,7 +245,7 @@ function defineTable(tableName, columns) {
244
245
  if (prop === "toString") return () => tableName;
245
246
  if (prop === "valueOf") return () => tableName;
246
247
  if (prop in columns) {
247
- return `${tableName}.${prop.toString()}`;
248
+ return tipoConexionZORMZ3691 === "mysql" ? `${tableName}.\`${prop.toString()}\` ` : `${tableName}."${prop.toString()}" `;
248
249
  }
249
250
  return void 0;
250
251
  },
@@ -266,14 +267,15 @@ function defineTable(tableName, columns) {
266
267
  });
267
268
  return proxy;
268
269
  }
269
- async function generateTable(tabla, columns) {
270
+ async function generateTable(tabla, columns, seeSQL = false) {
270
271
  let queries = "";
271
272
  let columnDefs = [];
273
+ let cheksOut = [];
272
274
  let sql = "";
273
275
  let id = false;
274
276
  for (const columnName in columns) {
275
277
  const col = columns[columnName];
276
- sql = ` ${columnName} `;
278
+ sql = tipoConexionZORMZ3691 === "mysql" ? `\`${columnName}\` ` : `"${columnName}" `;
277
279
  if (!col.typo) {
278
280
  throw new Error(`La columna ${columnName} no tiene el tipo definido`);
279
281
  }
@@ -287,16 +289,28 @@ async function generateTable(tabla, columns) {
287
289
  id = col.pk === true ? true : false;
288
290
  sql += col.sqlz;
289
291
  columnDefs.push(sql);
292
+ if (col.check !== void 0) {
293
+ cheksOut.push(
294
+ `CONSTRAINT chk_${columnName} CHECK (${columnName} IN ( ${col.check}))`
295
+ );
296
+ }
290
297
  }
291
298
  queries += `CREATE TABLE IF NOT EXISTS ${tabla} (
292
299
  `;
293
300
  queries += columnDefs.join(", \n");
301
+ if (cheksOut.length > 0) {
302
+ queries += ",\n";
303
+ }
304
+ queries += cheksOut.join(",\n");
294
305
  queries += ");";
295
306
  if (!conexionZORMZ3691 || !tipoConexionZORMZ3691) {
296
307
  throw new Error(
297
308
  "La conexi\xF3n no ha sido inicializada. Por favor, llama a getConexion() primero."
298
309
  );
299
310
  }
311
+ if (seeSQL) {
312
+ console.log(queries);
313
+ }
300
314
  if (tipoConexionZORMZ3691 === "mysql") {
301
315
  const response = await conexionZORMZ3691.executeConsulta({
302
316
  query: queries,
@@ -417,7 +431,6 @@ var QueryBuilder = class {
417
431
  this.tabla = tabla;
418
432
  this.parametros = parametros;
419
433
  this.valores = [];
420
- this.valorRetorno = null;
421
434
  this.conexion = conexion;
422
435
  }
423
436
  /**
@@ -438,8 +451,11 @@ var QueryBuilder = class {
438
451
  this.valores = values;
439
452
  return this;
440
453
  }
454
+ Returning(idRetorno) {
455
+ this.returning = idRetorno;
456
+ return this;
457
+ }
441
458
  async execute(see = false) {
442
- this.valorRetorno = 1;
443
459
  let query1 = `INSERT INTO ${this.tabla} `;
444
460
  let param = "";
445
461
  let arrayArrays = false;
@@ -501,15 +517,26 @@ var QueryBuilder = class {
501
517
  valores: arrayArrays ? [this.valores] : this.valores,
502
518
  mensaje: `Ocurrio un error al ingresar datos a ${this.tabla} `
503
519
  });
504
- return respuesta.insertId;
520
+ let response = [];
521
+ if (this.returning !== void 0) {
522
+ for (let i = 0; i < this.valores.length; i++) {
523
+ response.push(respuesta.insertId + i);
524
+ }
525
+ }
526
+ return response;
505
527
  } else if (this.conexion.tipo === "pg") {
506
- query += " RETURNING id ";
528
+ if (this.returning !== void 0)
529
+ query += ` RETURNING ${this.returning} `;
507
530
  const respuesta = await this.conexion.executeConsulta({
508
531
  query,
509
532
  valores: this.valores,
510
533
  mensaje: `Ocurrio un error al ingresar datos a ${this.tabla} `
511
534
  });
512
- return respuesta.rows;
535
+ let response = [];
536
+ if (respuesta.rows !== void 0) {
537
+ response = respuesta.rows.map((obj) => Object.values(obj)[0]);
538
+ }
539
+ return response;
513
540
  }
514
541
  }
515
542
  };
@@ -695,6 +722,14 @@ var eq = (valor1, valor2, literal = true) => {
695
722
  }
696
723
  return `${valor1} = '${valor2}'`;
697
724
  };
725
+ var neq = (valor1, valor2, literal = true) => {
726
+ if (typeof valor1 === "string") valor1 = valor1.trim();
727
+ if (typeof valor2 === "string") valor2 = valor2.trim();
728
+ if (!literal) {
729
+ return `${valor1} != ${valor2}`;
730
+ }
731
+ return `${valor1} != '${valor2}'`;
732
+ };
698
733
  var MAYOR = (valor, valor2) => {
699
734
  return ` ${valor} > ${valor2} `;
700
735
  };
@@ -755,7 +790,7 @@ var Update = class {
755
790
  }
756
791
  } else if (validate) {
757
792
  valores.forEach((valors) => {
758
- valor += eq(valors.campoUp, valors.newCampoUp, !valors.dataTable);
793
+ valor += eq(valors.campoUp.replace(`${this.nombreTabla}.`, ""), valors.newCampoUp.replace(`${this.nombreTabla}.`, ""), !valors.dataTable);
759
794
  valor += ",";
760
795
  });
761
796
  }
@@ -786,7 +821,7 @@ var Update = class {
786
821
  async execute(seeQuery = false) {
787
822
  const query = `UPDATE ${this.nombreTabla} SET ${this.valores} ${this.condicion};`;
788
823
  if (seeQuery) {
789
- console.log(seeQuery);
824
+ console.log(query);
790
825
  }
791
826
  const respuesta = await this.conexion.executeConsulta({
792
827
  query,
@@ -795,9 +830,7 @@ var Update = class {
795
830
  if (this.conexion.tipo === "mysql") {
796
831
  return respuesta.affectedRows;
797
832
  } else {
798
- return {
799
- resultado: respuesta.rowCount
800
- };
833
+ return respuesta.rowCount;
801
834
  }
802
835
  }
803
836
  };
@@ -847,6 +880,7 @@ var IntColumn = class {
847
880
  this.unique = false;
848
881
  this.autoIncrement = false;
849
882
  this.unsingned = { uso: false, valor: 0 };
883
+ this.comentario = "";
850
884
  }
851
885
  Required() {
852
886
  this.requerido = true;
@@ -857,6 +891,10 @@ var IntColumn = class {
857
891
  this.pk = true;
858
892
  return this;
859
893
  }
894
+ Comment(comentario) {
895
+ this.comentario = comentario;
896
+ return this;
897
+ }
860
898
  Unique() {
861
899
  this.unique = true;
862
900
  return this;
@@ -915,6 +953,9 @@ var IntColumn = class {
915
953
  parts.push(`DEFAULT ${this.defaultData}`);
916
954
  }
917
955
  if (this.unique) parts.push("UNIQUE");
956
+ if (db === "mysql" && this.comentario.length > 2) {
957
+ parts.push(` COMMENT '${this.comentario}' `);
958
+ }
918
959
  const valor = {
919
960
  pk: false,
920
961
  typo: "int",
@@ -934,6 +975,9 @@ var Varchar = class {
934
975
  this.defaultData = "";
935
976
  this.requerido = false;
936
977
  this.unique = false;
978
+ this.enum = "";
979
+ this.campo = "";
980
+ this.comentarios = "";
937
981
  this.maxLenth = cantidad;
938
982
  }
939
983
  withType({
@@ -947,10 +991,27 @@ var Varchar = class {
947
991
  this.unique = unique;
948
992
  return this.Default(defaultData).$();
949
993
  }
994
+ /**
995
+ *
996
+ * @param {string} campo
997
+ * @param {string[]} datos
998
+ * @example
999
+ * Check("example", ["operativo","denegado","revisando"])
1000
+ *
1001
+ * @returns
1002
+ */
1003
+ Check(datos) {
1004
+ this.enum = datos.map((caracter) => `'${caracter}'`).join(",");
1005
+ return this;
1006
+ }
950
1007
  Required() {
951
1008
  this.requerido = true;
952
1009
  return this;
953
1010
  }
1011
+ Comment(comentario) {
1012
+ this.comentarios = comentario;
1013
+ return this;
1014
+ }
954
1015
  Unique() {
955
1016
  this.unique = true;
956
1017
  return this;
@@ -965,11 +1026,16 @@ var Varchar = class {
965
1026
  return this;
966
1027
  }
967
1028
  $() {
968
- let sqlz = `VARCHAR(${this.maxLenth}) ${this.requerido ? "NOT NULL" : "NULL"} ${this.defaultData.length > 0 ? ` DEFAULT ${this.defaultData} ` : " "} ${this.unique ? " UNIQUE" : " "} `;
1029
+ let sqlz = `VARCHAR(${this.maxLenth}) ${this.requerido ? "NOT NULL" : "NULL"} ${this.defaultData.length > 0 ? ` DEFAULT ${this.defaultData} ` : " "} ${this.unique ? " UNIQUE" : " "}`;
1030
+ const db = getTipoConexion();
1031
+ if (db === "mysql" && this.comentarios.length > 2) {
1032
+ sqlz += ` COMMENT '${this.comentarios}' `;
1033
+ }
969
1034
  const response = {
970
1035
  typo: "varchar",
971
1036
  pk: false,
972
- sqlz
1037
+ sqlz,
1038
+ check: this.enum.length > 2 ? this.enum : void 0
973
1039
  };
974
1040
  return response;
975
1041
  }
@@ -982,11 +1048,16 @@ function varchar(cantidad = 100) {
982
1048
  var BoolColumn = class {
983
1049
  constructor() {
984
1050
  this.requerido = false;
1051
+ this.comentario = "";
985
1052
  }
986
1053
  required() {
987
1054
  this.requerido = true;
988
1055
  return this;
989
1056
  }
1057
+ Comment(comentario) {
1058
+ this.comentario = comentario;
1059
+ return this;
1060
+ }
990
1061
  default(value) {
991
1062
  this.defaultData = value;
992
1063
  return this;
@@ -1001,6 +1072,9 @@ var BoolColumn = class {
1001
1072
  `DEFAULT ${db === "mysql" ? this.defaultData ? 1 : 0 : this.defaultData}`
1002
1073
  );
1003
1074
  }
1075
+ if (db === "mysql" && this.comentario.length > 2) {
1076
+ parts.push(` COMMENT '${this.comentario}' `);
1077
+ }
1004
1078
  const response = {
1005
1079
  typo: "bool",
1006
1080
  pk: false,
@@ -1019,11 +1093,16 @@ var Timestamp = class {
1019
1093
  this.requerido = false;
1020
1094
  this.defaultNow = false;
1021
1095
  this.onUpdateNow = false;
1096
+ this.comentario = "";
1022
1097
  }
1023
1098
  required() {
1024
1099
  this.requerido = true;
1025
1100
  return this;
1026
1101
  }
1102
+ comment(comentario) {
1103
+ this.comentario = comentario;
1104
+ return this;
1105
+ }
1027
1106
  now() {
1028
1107
  this.defaultNow = true;
1029
1108
  return this;
@@ -1043,6 +1122,9 @@ var Timestamp = class {
1043
1122
  if (this.onUpdateNow && db === "mysql") {
1044
1123
  parts.push("ON UPDATE CURRENT_TIMESTAMP");
1045
1124
  }
1125
+ if (db === "mysql" && this.comentario.length > 2) {
1126
+ parts.push(` COMMENT '${this.comentario}' `);
1127
+ }
1046
1128
  const response = {
1047
1129
  typo: "timestamp",
1048
1130
  pk: false,
@@ -1061,6 +1143,7 @@ var Money = class {
1061
1143
  this.precision = 10;
1062
1144
  this.scale = 2;
1063
1145
  this.requerido = false;
1146
+ this.comentario = "";
1064
1147
  this.precision = presicion;
1065
1148
  this.scale = decimales;
1066
1149
  }
@@ -1072,6 +1155,10 @@ var Money = class {
1072
1155
  this.defaultData = value;
1073
1156
  return this;
1074
1157
  }
1158
+ comment(comentario) {
1159
+ this.comentario = comentario;
1160
+ return this;
1161
+ }
1075
1162
  $() {
1076
1163
  const parts = [];
1077
1164
  const db = getTipoConexion();
@@ -1082,6 +1169,9 @@ var Money = class {
1082
1169
  if (this.defaultData !== void 0) {
1083
1170
  parts.push(`DEFAULT ${this.defaultData.toFixed(this.scale)}`);
1084
1171
  }
1172
+ if (db === "mysql" && this.comentario.length > 2) {
1173
+ parts.push(` COMMENT '${this.comentario}' `);
1174
+ }
1085
1175
  const response = {
1086
1176
  typo: "money",
1087
1177
  pk: false,
@@ -1122,6 +1212,7 @@ function money(presicion = 10, decimales = 2) {
1122
1212
  getTipoConexion,
1123
1213
  int,
1124
1214
  money,
1215
+ neq,
1125
1216
  timestamp,
1126
1217
  varchar
1127
1218
  });