@dwtechs/antity-pgsql 0.4.0 → 0.5.1

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,39 @@ 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._quotedProps = [];
258
+ this._nbProps = 0;
259
+ this._cols = "";
259
260
  }
260
261
  addProp(prop) {
261
- this._props = add$1(this._props, quoteIfUppercase(prop), this._props.length - 2);
262
- this._cols = this._props.join(", ");
262
+ this._props.push(prop);
263
+ this._quotedProps.push(quoteIfUppercase(prop));
263
264
  this._nbProps++;
265
+ this._cols = this._quotedProps.join(", ");
264
266
  }
265
267
  query(table, rows, consumerId, consumerName, rtn = "") {
266
- let query = `INSERT INTO ${quoteIfUppercase(table)} (${this._cols}) VALUES `;
268
+ const propsToUse = [...this._props];
269
+ let nbProps = this._nbProps;
270
+ let cols = this._cols;
271
+ if (consumerId !== undefined && consumerName !== undefined) {
272
+ propsToUse.push("consumerId", "consumerName");
273
+ nbProps += 2;
274
+ cols += `, "consumerId", "consumerName"`;
275
+ }
276
+ let query = `INSERT INTO ${quoteIfUppercase(table)} (${cols}) VALUES `;
267
277
  const args = [];
268
278
  let i = 0;
269
279
  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) {
280
+ if (consumerId !== undefined && consumerName !== undefined) {
281
+ row.consumerId = consumerId;
282
+ row.consumerName = consumerName;
283
+ }
284
+ query += `${$i(nbProps, i)}, `;
285
+ for (const prop of propsToUse) {
274
286
  args.push(row[prop]);
275
287
  }
276
- i += this._nbProps;
288
+ i += nbProps;
277
289
  }
278
290
  query = query.slice(0, -2);
279
291
  if (rtn)
@@ -299,18 +311,24 @@ var __awaiter$2 = (undefined && undefined.__awaiter) || function (thisArg, _argu
299
311
  };
300
312
  class Update {
301
313
  constructor() {
302
- this._props = ["consumerId", "consumerName"];
314
+ this._props = [];
303
315
  }
304
316
  addProp(prop) {
305
- this._props = add$1(this._props, quoteIfUppercase(prop), this._props.length - 2);
317
+ this._props.push(quoteIfUppercase(prop));
306
318
  }
307
319
  query(table, rows, consumerId, consumerName) {
308
- rows = this.addConsumer(rows, consumerId, consumerName);
320
+ if (consumerId !== undefined && consumerName !== undefined) {
321
+ rows = this.addConsumer(rows, consumerId, consumerName);
322
+ }
323
+ const propsToUse = [...this._props];
324
+ if (consumerId !== undefined && consumerName !== undefined) {
325
+ propsToUse.push("consumerId", "consumerName");
326
+ }
309
327
  const l = rows.length;
310
328
  const args = rows.map(row => row.id);
311
329
  let query = `UPDATE "${quoteIfUppercase(table)}" SET `;
312
330
  let i = args.length + 1;
313
- for (const p of this._props) {
331
+ for (const p of propsToUse) {
314
332
  if (rows[0][p] === undefined)
315
333
  continue;
316
334
  query += `${p} = CASE `;
@@ -469,22 +487,20 @@ function generateSummary(name, table, properties) {
469
487
  lines.push(`│ └─ ${op}: ${count} properties`);
470
488
  });
471
489
  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}`);
490
+ properties.forEach((p) => {
491
+ lines.push(`│ ├─ ${p.key}:`);
492
+ lines.push(`│ ├─ Type: ${p.type}`);
493
+ lines.push(`│ │ ├─ Min: ${p.min}`);
494
+ lines.push(`│ ├─ Max: ${p.max}`);
495
+ lines.push(`│ ├─ Required: ${p.required}`);
496
+ lines.push(`│ ├─ Safe: ${p.safe}`);
497
+ lines.push(`│ ├─ TypeCheck: ${p.typeCheck}`);
498
+ lines.push(`│ ├─ Filter: ${p.filter}`);
499
+ lines.push(`│ ├─ Methods: [${p.methods.join(', ')}]`);
500
+ lines.push(`│ ├─ Operations: [${p.operations.join(', ')}]`);
501
+ lines.push(`│ ├─ Sanitize: ${p.sanitize}`);
502
+ lines.push(`│ ├─ Normalize: ${p.normalize}`);
503
+ lines.push(`│ ├─ Validate: ${p.validate}`);
488
504
  });
489
505
  const crudMappings = getCrudMappings(properties);
490
506
  lines.push(`├─ CRUD Mappings:`);
@@ -567,8 +583,8 @@ class SQLEntity extends Entity {
567
583
  log.debug(`get(first='${first}', rows='${rows}',
568
584
  sortOrder='${sortOrder}', sortField='${sortField}',
569
585
  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)
586
+ const { query, args } = this.sel.query(this._table, pagination, first, rows, sortField, sortOrder, filters);
587
+ this.sel.execute(query, args, dbClient)
572
588
  .then((r) => {
573
589
  l.rows = r.rows;
574
590
  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.1",
4
4
  "description": "Open source library to add PostgreSQL support to @dwtechs/Antity entities.",
5
5
  "keywords": [
6
6
  "entities"