@dwtechs/antity-pgsql 0.4.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -171,17 +171,33 @@ class SQLEntity {
171
171
  set table(table: string);
172
172
 
173
173
  query: {
174
- select: (paginate: boolean) => string;
175
- update: (rows: Record<string, unknown>[], consumerId: number | string, consumerName: string) => {
174
+ select: (
175
+ paginate: boolean,
176
+ first?: number,
177
+ rows?: number | null,
178
+ sortField?: string | null,
179
+ sortOrder?: "ASC" | "DESC" | null,
180
+ filters?: Filters | null) => {
176
181
  query: string;
177
- args: unknown[];
178
- };
179
- insert: (rows: Record<string, unknown>[], consumerId: number | string, consumerName: string, rtn?: string) => {
180
- query: string;
181
- args: unknown[];
182
- };
183
- delete: () => string;
184
- return: (prop: string) => string;
182
+ args: (Filter["value"])[];
183
+ };
184
+ update: (
185
+ rows: Record<string, unknown>[],
186
+ consumerId?: number | string,
187
+ consumerName?: string) => {
188
+ query: string;
189
+ args: unknown[];
190
+ };
191
+ insert: (
192
+ rows: Record<string, unknown>[],
193
+ consumerId?: number | string,
194
+ consumerName?: string,
195
+ rtn?: string) => {
196
+ query: string;
197
+ args: unknown[];
198
+ };
199
+ delete: () => string;
200
+ return: (prop: string) => string;
185
201
  };
186
202
  get: (req: Request, res: Response, next: NextFunction) => void;
187
203
  add: (req: Request, res: Response, next: NextFunction) => Promise<void>;
@@ -77,14 +77,24 @@ declare class SQLEntity extends Entity {
77
77
  get table(): string;
78
78
  set table(table: string);
79
79
  query: {
80
- select: (paginate: boolean) => string;
81
- update: (rows: Record<string, unknown>[], consumerId: number | string, consumerName: string) => {
82
- query: string;
83
- args: unknown[];
80
+ select: (
81
+ paginate: boolean,
82
+ first?: number,
83
+ rows?: number | null,
84
+ sortField?: string | null,
85
+ sortOrder?: "ASC" | "DESC" | null,
86
+ filters?: Filters | null) => {
87
+ query: string;
88
+ args: (Filter["value"])[];
84
89
  };
85
- insert: (rows: Record<string, unknown>[], consumerId: number | string, consumerName: string, rtn?: string) => {
86
- query: string;
87
- args: unknown[];
90
+ update: (rows: Record<string,
91
+ unknown>[], consumerId?: number | string, consumerName?: string) => {
92
+ query: string;
93
+ args: unknown[];
94
+ };
95
+ insert: (rows: Record<string, unknown>[], consumerId?: number | string, consumerName?: string, rtn?: string) => {
96
+ query: string;
97
+ args: unknown[];
88
98
  };
89
99
  delete: () => string;
90
100
  return: (prop: string) => string;
@@ -25,7 +25,7 @@ https://github.com/DWTechs/Antity-pgsql.js
25
25
  */
26
26
 
27
27
  import { isArray, isIn, isString } from '@dwtechs/checkard';
28
- import { deleteProps, add as add$1, chunk, flatten } from '@dwtechs/sparray';
28
+ import { deleteProps, chunk, flatten } from '@dwtechs/sparray';
29
29
  import { log } from '@dwtechs/winstan';
30
30
  import { Entity } from '@dwtechs/antity';
31
31
  import Pool from 'pg-pool';
@@ -229,7 +229,7 @@ class Select {
229
229
  const { filterClause, args } = filter(first, rows, sortField, sortOrder, filters);
230
230
  return {
231
231
  query: baseQuery + filterClause,
232
- args: args
232
+ args
233
233
  };
234
234
  }
235
235
  execute(query, args, client) {
@@ -253,27 +253,37 @@ function $i(qty, start) {
253
253
 
254
254
  class Insert {
255
255
  constructor() {
256
- this._props = ["consumerId", "consumerName"];
257
- this._cols = "*";
258
- this._nbProps = 2;
256
+ this._props = [];
257
+ this._nbProps = 0;
258
+ this._cols = "";
259
259
  }
260
260
  addProp(prop) {
261
- this._props = add$1(this._props, quoteIfUppercase(prop), this._props.length - 2);
262
- this._cols = this._props.join(", ");
261
+ this._props.push(quoteIfUppercase(prop));
263
262
  this._nbProps++;
263
+ this._cols = this._props.join(", ");
264
264
  }
265
265
  query(table, rows, consumerId, consumerName, rtn = "") {
266
- let query = `INSERT INTO ${quoteIfUppercase(table)} (${this._cols}) VALUES `;
266
+ const propsToUse = [...this._props];
267
+ let nbProps = this._nbProps;
268
+ let cols = this._cols;
269
+ if (consumerId !== undefined && consumerName !== undefined) {
270
+ propsToUse.push("consumerId", "consumerName");
271
+ nbProps += 2;
272
+ cols += ", consumerId, consumerName";
273
+ }
274
+ let query = `INSERT INTO ${quoteIfUppercase(table)} (${cols}) VALUES `;
267
275
  const args = [];
268
276
  let i = 0;
269
277
  for (const row of rows) {
270
- row.consumerId = consumerId;
271
- row.consumerName = consumerName;
272
- query += `${$i(this._nbProps, i)}, `;
273
- for (const prop of this._props) {
278
+ if (consumerId !== undefined && consumerName !== undefined) {
279
+ row.consumerId = consumerId;
280
+ row.consumerName = consumerName;
281
+ }
282
+ query += `${$i(nbProps, i)}, `;
283
+ for (const prop of propsToUse) {
274
284
  args.push(row[prop]);
275
285
  }
276
- i += this._nbProps;
286
+ i += nbProps;
277
287
  }
278
288
  query = query.slice(0, -2);
279
289
  if (rtn)
@@ -299,18 +309,24 @@ var __awaiter$2 = (undefined && undefined.__awaiter) || function (thisArg, _argu
299
309
  };
300
310
  class Update {
301
311
  constructor() {
302
- this._props = ["consumerId", "consumerName"];
312
+ this._props = [];
303
313
  }
304
314
  addProp(prop) {
305
- this._props = add$1(this._props, quoteIfUppercase(prop), this._props.length - 2);
315
+ this._props.push(quoteIfUppercase(prop));
306
316
  }
307
317
  query(table, rows, consumerId, consumerName) {
308
- rows = this.addConsumer(rows, consumerId, consumerName);
318
+ if (consumerId !== undefined && consumerName !== undefined) {
319
+ rows = this.addConsumer(rows, consumerId, consumerName);
320
+ }
321
+ const propsToUse = [...this._props];
322
+ if (consumerId !== undefined && consumerName !== undefined) {
323
+ propsToUse.push("consumerId", "consumerName");
324
+ }
309
325
  const l = rows.length;
310
326
  const args = rows.map(row => row.id);
311
327
  let query = `UPDATE "${quoteIfUppercase(table)}" SET `;
312
328
  let i = args.length + 1;
313
- for (const p of this._props) {
329
+ for (const p of propsToUse) {
314
330
  if (rows[0][p] === undefined)
315
331
  continue;
316
332
  query += `${p} = CASE `;
@@ -469,22 +485,20 @@ function generateSummary(name, table, properties) {
469
485
  lines.push(`│ └─ ${op}: ${count} properties`);
470
486
  });
471
487
  lines.push(`├─ Property Details:`);
472
- properties.forEach((p, index) => {
473
- const isLast = index === propLen - 1;
474
- const prefix = isLast ? '└─' : '├─';
475
- lines.push(`│ ${prefix} ${p.key}:`);
476
- lines.push(`│ ${isLast ? ' ' : '│'} ├─ Type: ${p.type}`);
477
- lines.push(`│ ${isLast ? ' ' : '│'} ├─ Min: ${p.min}`);
478
- lines.push(`│ ${isLast ? ' ' : '│'} ├─ Max: ${p.max}`);
479
- lines.push(`│ ${isLast ? ' ' : '│'} ├─ Required: ${p.required}`);
480
- lines.push(`│ ${isLast ? ' ' : '│'} ├─ Safe: ${p.safe}`);
481
- lines.push(`│ ${isLast ? ' ' : '│'} ├─ TypeCheck: ${p.typeCheck}`);
482
- lines.push(`│ ${isLast ? ' ' : '│'} ├─ Filter: ${p.filter}`);
483
- lines.push(`│ ${isLast ? ' ' : '│'} ├─ Methods: [${p.methods.join(', ')}]`);
484
- lines.push(`│ ${isLast ? ' ' : '│'} ├─ Operations: [${p.operations.join(', ')}]`);
485
- lines.push(`│ ${isLast ? ' ' : '│'} ├─ Sanitize: ${p.sanitize}`);
486
- lines.push(`│ ${isLast ? ' ' : '│'} ├─ Normalize: ${p.normalize}`);
487
- lines.push(`│ ${isLast ? ' ' : '│'} ├─ Validate: ${p.validate}`);
488
+ properties.forEach((p) => {
489
+ lines.push(`│ ├─ ${p.key}:`);
490
+ lines.push(`│ ├─ Type: ${p.type}`);
491
+ lines.push(`│ │ ├─ Min: ${p.min}`);
492
+ lines.push(`│ ├─ Max: ${p.max}`);
493
+ lines.push(`│ ├─ Required: ${p.required}`);
494
+ lines.push(`│ ├─ Safe: ${p.safe}`);
495
+ lines.push(`│ ├─ TypeCheck: ${p.typeCheck}`);
496
+ lines.push(`│ ├─ Filter: ${p.filter}`);
497
+ lines.push(`│ ├─ Methods: [${p.methods.join(', ')}]`);
498
+ lines.push(`│ ├─ Operations: [${p.operations.join(', ')}]`);
499
+ lines.push(`│ ├─ Sanitize: ${p.sanitize}`);
500
+ lines.push(`│ ├─ Normalize: ${p.normalize}`);
501
+ lines.push(`│ ├─ Validate: ${p.validate}`);
488
502
  });
489
503
  const crudMappings = getCrudMappings(properties);
490
504
  lines.push(`├─ CRUD Mappings:`);
@@ -567,8 +581,8 @@ class SQLEntity extends Entity {
567
581
  log.debug(`get(first='${first}', rows='${rows}',
568
582
  sortOrder='${sortOrder}', sortField='${sortField}',
569
583
  pagination=${pagination}, filters=${JSON.stringify(filters)}`);
570
- const { query: q, args } = this.sel.query(this._table, pagination, first, rows, sortField, sortOrder, filters);
571
- this.sel.execute(q, args, dbClient)
584
+ const { query, args } = this.sel.query(this._table, pagination, first, rows, sortField, sortOrder, filters);
585
+ this.sel.execute(query, args, dbClient)
572
586
  .then((r) => {
573
587
  l.rows = r.rows;
574
588
  l.total = r.total;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dwtechs/antity-pgsql",
3
- "version": "0.4.0",
3
+ "version": "0.5.0",
4
4
  "description": "Open source library to add PostgreSQL support to @dwtechs/Antity entities.",
5
5
  "keywords": [
6
6
  "entities"