@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 +26 -10
- package/dist/antity-pgsql.d.ts +17 -7
- package/dist/antity-pgsql.js +49 -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,37 @@ function $i(qty, start) {
|
|
|
253
253
|
|
|
254
254
|
class Insert {
|
|
255
255
|
constructor() {
|
|
256
|
-
this._props = [
|
|
257
|
-
this.
|
|
258
|
-
this.
|
|
256
|
+
this._props = [];
|
|
257
|
+
this._nbProps = 0;
|
|
258
|
+
this._cols = "";
|
|
259
259
|
}
|
|
260
260
|
addProp(prop) {
|
|
261
|
-
this._props
|
|
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
|
-
|
|
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
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
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 +=
|
|
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 = [
|
|
312
|
+
this._props = [];
|
|
303
313
|
}
|
|
304
314
|
addProp(prop) {
|
|
305
|
-
this._props
|
|
315
|
+
this._props.push(quoteIfUppercase(prop));
|
|
306
316
|
}
|
|
307
317
|
query(table, rows, consumerId, consumerName) {
|
|
308
|
-
|
|
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
|
|
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
|
|
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}`);
|
|
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
|
|
571
|
-
this.sel.execute(
|
|
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;
|