@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 +26 -10
- package/dist/antity-pgsql.d.ts +17 -7
- package/dist/antity-pgsql.js +51 -35
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -171,17 +171,33 @@ class SQLEntity {
|
|
|
171
171
|
set table(table: string);
|
|
172
172
|
|
|
173
173
|
query: {
|
|
174
|
-
select: (
|
|
175
|
-
|
|
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:
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
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>;
|
package/dist/antity-pgsql.d.ts
CHANGED
|
@@ -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: (
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
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
|
-
|
|
86
|
-
|
|
87
|
-
|
|
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;
|
package/dist/antity-pgsql.js
CHANGED
|
@@ -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,
|
|
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
|
|
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 = [
|
|
257
|
-
this.
|
|
258
|
-
this._nbProps =
|
|
256
|
+
this._props = [];
|
|
257
|
+
this._quotedProps = [];
|
|
258
|
+
this._nbProps = 0;
|
|
259
|
+
this._cols = "";
|
|
259
260
|
}
|
|
260
261
|
addProp(prop) {
|
|
261
|
-
this._props
|
|
262
|
-
this.
|
|
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
|
-
|
|
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
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
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 +=
|
|
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 = [
|
|
314
|
+
this._props = [];
|
|
303
315
|
}
|
|
304
316
|
addProp(prop) {
|
|
305
|
-
this._props
|
|
317
|
+
this._props.push(quoteIfUppercase(prop));
|
|
306
318
|
}
|
|
307
319
|
query(table, rows, consumerId, consumerName) {
|
|
308
|
-
|
|
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
|
|
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
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
lines.push(`│
|
|
476
|
-
lines.push(`│
|
|
477
|
-
lines.push(`│
|
|
478
|
-
lines.push(`│
|
|
479
|
-
lines.push(`│
|
|
480
|
-
lines.push(`│
|
|
481
|
-
lines.push(`│
|
|
482
|
-
lines.push(`│
|
|
483
|
-
lines.push(`│
|
|
484
|
-
lines.push(`│
|
|
485
|
-
lines.push(`│
|
|
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
|
|
571
|
-
this.sel.execute(
|
|
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;
|