@dwtechs/antity-pgsql 0.6.1 → 0.8.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 +30 -20
- package/dist/antity-pgsql.d.ts +7 -3
- package/dist/antity-pgsql.js +47 -23
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
[](https://opensource.org/licenses/MIT)
|
|
3
3
|
[](https://www.npmjs.com/package/@dwtechs/antity-pgsql)
|
|
4
4
|
[](https://www.npmjs.com/package/@dwtechs/antity-pgsql)
|
|
5
|
-

|
|
6
6
|
|
|
7
7
|
- [Synopsis](#synopsis)
|
|
8
8
|
- [Support](#support)
|
|
@@ -123,7 +123,9 @@ const entity = new Entity("consumers", [
|
|
|
123
123
|
router.get("/", ..., entity.get);
|
|
124
124
|
router.post("/", entity.normalizeArray, entity.validateArray, ..., entity.add);
|
|
125
125
|
router.put("/", entity.normalizeArray, entity.validateArray, ..., entity.update);
|
|
126
|
-
router.put("/", ..., entity.archive);
|
|
126
|
+
router.put("/archive", ..., entity.archive);
|
|
127
|
+
router.delete("/", ..., entity.delete);
|
|
128
|
+
router.delete("/archived", ..., entity.deleteArchive);
|
|
127
129
|
|
|
128
130
|
```
|
|
129
131
|
|
|
@@ -172,7 +174,6 @@ class SQLEntity {
|
|
|
172
174
|
|
|
173
175
|
query: {
|
|
174
176
|
select: (
|
|
175
|
-
paginate: boolean,
|
|
176
177
|
first?: number,
|
|
177
178
|
rows?: number | null,
|
|
178
179
|
sortField?: string | null,
|
|
@@ -181,29 +182,34 @@ class SQLEntity {
|
|
|
181
182
|
query: string;
|
|
182
183
|
args: (Filter["value"])[];
|
|
183
184
|
};
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
185
|
+
update: (
|
|
186
|
+
rows: Record<string, unknown>[],
|
|
187
|
+
consumerId?: number | string,
|
|
188
|
+
consumerName?: string) => {
|
|
189
|
+
query: string;
|
|
190
|
+
args: unknown[];
|
|
191
|
+
};
|
|
192
|
+
insert: (
|
|
193
|
+
rows: Record<string, unknown>[],
|
|
194
|
+
consumerId?: number | string,
|
|
195
|
+
consumerName?: string,
|
|
196
|
+
rtn?: string) => {
|
|
197
|
+
query: string;
|
|
189
198
|
args: unknown[];
|
|
190
199
|
};
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
args: unknown[];
|
|
198
|
-
};
|
|
199
|
-
delete: () => string;
|
|
200
|
-
return: (prop: string) => string;
|
|
200
|
+
delete: (ids: number[]) => {
|
|
201
|
+
query: string;
|
|
202
|
+
args: number[];
|
|
203
|
+
};
|
|
204
|
+
deleteArchive: () => string;
|
|
205
|
+
return: (prop: string) => string;
|
|
201
206
|
};
|
|
202
207
|
get: (req: Request, res: Response, next: NextFunction) => void;
|
|
203
208
|
add: (req: Request, res: Response, next: NextFunction) => Promise<void>;
|
|
204
209
|
update: (req: Request, res: Response, next: NextFunction) => Promise<void>;
|
|
205
210
|
archive: (req: Request, res: Response, next: NextFunction) => Promise<void>;
|
|
206
|
-
delete: (req: Request, res: Response, next: NextFunction) => void
|
|
211
|
+
delete: (req: Request, res: Response, next: NextFunction) => Promise<void>;
|
|
212
|
+
deleteArchive: (req: Request, res: Response, next: NextFunction) => void;
|
|
207
213
|
|
|
208
214
|
}
|
|
209
215
|
|
|
@@ -223,9 +229,13 @@ function execute(
|
|
|
223
229
|
|
|
224
230
|
|
|
225
231
|
```
|
|
226
|
-
get(), add(), update(), archive() and
|
|
232
|
+
get(), add(), update(), archive(), delete() and deleteArchive() methods are made to be used as Express.js middlewares.
|
|
227
233
|
Each method will look for data to work on in the **req.body.rows** parameter.
|
|
228
234
|
|
|
235
|
+
- **query.select()**: Generates a SELECT query. When the `rows` parameter is provided (not null), pagination is automatically enabled and the query includes `COUNT(*) OVER () AS total` to return the total number of rows. The total count is extracted from results and returned separately from the row data.
|
|
236
|
+
- **delete()**: Deletes rows by their IDs. Expects `req.body.rows` to be an array of objects with `id` property: `[{id: 1}, {id: 2}]`
|
|
237
|
+
- **deleteArchive()**: Deletes archived rows that were archived before a specific date. Expects `req.body.date` to be a Date object.
|
|
238
|
+
|
|
229
239
|
|
|
230
240
|
## Match modes
|
|
231
241
|
|
package/dist/antity-pgsql.d.ts
CHANGED
|
@@ -78,7 +78,6 @@ declare class SQLEntity extends Entity {
|
|
|
78
78
|
set table(table: string);
|
|
79
79
|
query: {
|
|
80
80
|
select: (
|
|
81
|
-
paginate: boolean,
|
|
82
81
|
first?: number,
|
|
83
82
|
rows?: number | null,
|
|
84
83
|
sortField?: string | null,
|
|
@@ -96,14 +95,19 @@ declare class SQLEntity extends Entity {
|
|
|
96
95
|
query: string;
|
|
97
96
|
args: unknown[];
|
|
98
97
|
};
|
|
99
|
-
delete: () =>
|
|
98
|
+
delete: (ids: number[]) => {
|
|
99
|
+
query: string;
|
|
100
|
+
args: number[];
|
|
101
|
+
};
|
|
102
|
+
deleteArchive: () => string;
|
|
100
103
|
return: (prop: string) => string;
|
|
101
104
|
};
|
|
102
105
|
get: (req: Request, res: Response, next: NextFunction) => void;
|
|
103
106
|
add: (req: Request, res: Response, next: NextFunction) => Promise<void>;
|
|
104
107
|
update: (req: Request, res: Response, next: NextFunction) => Promise<void>;
|
|
105
108
|
archive: (req: Request, res: Response, next: NextFunction) => Promise<void>;
|
|
106
|
-
delete: (req: Request, res: Response, next: NextFunction) => void
|
|
109
|
+
delete: (req: Request, res: Response, next: NextFunction) => Promise<void>;
|
|
110
|
+
deleteArchive: (req: Request, res: Response, next: NextFunction) => void;
|
|
107
111
|
}
|
|
108
112
|
|
|
109
113
|
declare function filter(
|
package/dist/antity-pgsql.js
CHANGED
|
@@ -58,7 +58,7 @@ var perf = {
|
|
|
58
58
|
end,
|
|
59
59
|
};
|
|
60
60
|
|
|
61
|
-
function execute
|
|
61
|
+
function execute(query, args, clt) {
|
|
62
62
|
const time = perf.start(query, args);
|
|
63
63
|
const client = clt || pool;
|
|
64
64
|
return client
|
|
@@ -222,8 +222,8 @@ class Select {
|
|
|
222
222
|
get props() {
|
|
223
223
|
return this._cols;
|
|
224
224
|
}
|
|
225
|
-
query(table,
|
|
226
|
-
const p =
|
|
225
|
+
query(table, first = 0, rows = null, sortField = null, sortOrder = null, filters = null) {
|
|
226
|
+
const p = rows ? this._count : '';
|
|
227
227
|
const c = this._cols ? this._cols : '*';
|
|
228
228
|
const baseQuery = `SELECT ${c}${p} FROM ${quoteIfUppercase(table)}`;
|
|
229
229
|
const { filterClause, args } = filter(first, rows, sortField, sortOrder, filters);
|
|
@@ -233,7 +233,7 @@ class Select {
|
|
|
233
233
|
};
|
|
234
234
|
}
|
|
235
235
|
execute(query, args, client) {
|
|
236
|
-
return execute
|
|
236
|
+
return execute(query, args, client)
|
|
237
237
|
.then((r) => {
|
|
238
238
|
if (!r.rowCount)
|
|
239
239
|
throw { status: 404, msg: "Resource not found" };
|
|
@@ -293,10 +293,10 @@ class Insert {
|
|
|
293
293
|
return { query, args };
|
|
294
294
|
}
|
|
295
295
|
rtn(prop) {
|
|
296
|
-
return `RETURNING
|
|
296
|
+
return `RETURNING ${quoteIfUppercase(prop)}`;
|
|
297
297
|
}
|
|
298
298
|
execute(query, args, client) {
|
|
299
|
-
return execute
|
|
299
|
+
return execute(query, args, client);
|
|
300
300
|
}
|
|
301
301
|
}
|
|
302
302
|
|
|
@@ -322,6 +322,7 @@ class Update {
|
|
|
322
322
|
const propsToUse = [...this._props];
|
|
323
323
|
if (consumerId !== undefined && consumerName !== undefined)
|
|
324
324
|
propsToUse.push("consumerId", "consumerName");
|
|
325
|
+
log.debug(`${LOGS_PREFIX}Update query input rows: ${JSON.stringify(rows, null, 2)}`);
|
|
325
326
|
const l = rows.length;
|
|
326
327
|
const args = rows.map(row => row.id);
|
|
327
328
|
let query = `UPDATE "${quoteIfUppercase(table)}" SET `;
|
|
@@ -342,7 +343,7 @@ class Update {
|
|
|
342
343
|
}
|
|
343
344
|
execute(query, args, client) {
|
|
344
345
|
return __awaiter$2(this, void 0, void 0, function* () {
|
|
345
|
-
return execute
|
|
346
|
+
return execute(query, args, client);
|
|
346
347
|
});
|
|
347
348
|
}
|
|
348
349
|
addConsumer(rows, consumerId, consumerName) {
|
|
@@ -360,14 +361,20 @@ var __awaiter$1 = (undefined && undefined.__awaiter) || function (thisArg, _argu
|
|
|
360
361
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
361
362
|
});
|
|
362
363
|
};
|
|
363
|
-
function
|
|
364
|
+
function queryById(table, ids) {
|
|
365
|
+
return {
|
|
366
|
+
query: `DELETE FROM ${quoteIfUppercase(table)} WHERE id = ANY($1)`,
|
|
367
|
+
args: [ids]
|
|
368
|
+
};
|
|
369
|
+
}
|
|
370
|
+
function queryArchived(table) {
|
|
364
371
|
return `DELETE FROM ${quoteIfUppercase(table)} WHERE "archivedAt" < $1`;
|
|
365
372
|
}
|
|
366
|
-
function
|
|
373
|
+
function executeArchived(date, query, client) {
|
|
367
374
|
return __awaiter$1(this, void 0, void 0, function* () {
|
|
368
375
|
let db;
|
|
369
376
|
try {
|
|
370
|
-
db = yield execute
|
|
377
|
+
db = yield execute(query, [date], client);
|
|
371
378
|
}
|
|
372
379
|
catch (err) {
|
|
373
380
|
throw err;
|
|
@@ -551,8 +558,8 @@ class SQLEntity extends Entity {
|
|
|
551
558
|
this.ins = new Insert();
|
|
552
559
|
this.upd = new Update();
|
|
553
560
|
this.query = {
|
|
554
|
-
select: (
|
|
555
|
-
return this.sel.query(this.table,
|
|
561
|
+
select: (first = 0, rows = null, sortField = null, sortOrder = null, filters = null) => {
|
|
562
|
+
return this.sel.query(this.table, first, rows, sortField, sortOrder, filters);
|
|
556
563
|
},
|
|
557
564
|
update: (rows, consumerId, consumerName) => {
|
|
558
565
|
return this.upd.query(this.table, rows, consumerId, consumerName);
|
|
@@ -560,8 +567,11 @@ class SQLEntity extends Entity {
|
|
|
560
567
|
insert: (rows, consumerId, consumerName, rtn = "") => {
|
|
561
568
|
return this.ins.query(this.table, rows, consumerId, consumerName, rtn);
|
|
562
569
|
},
|
|
563
|
-
delete: () => {
|
|
564
|
-
return
|
|
570
|
+
delete: (ids) => {
|
|
571
|
+
return queryById(this.table, ids);
|
|
572
|
+
},
|
|
573
|
+
deleteArchive: () => {
|
|
574
|
+
return queryArchived(this.table);
|
|
565
575
|
},
|
|
566
576
|
return: (prop) => {
|
|
567
577
|
return this.ins.rtn(prop);
|
|
@@ -581,7 +591,7 @@ class SQLEntity extends Entity {
|
|
|
581
591
|
log.debug(`get(first='${first}', rows='${rows}',
|
|
582
592
|
sortOrder='${sortOrder}', sortField='${sortField}',
|
|
583
593
|
pagination=${pagination}, filters=${JSON.stringify(filters)}`);
|
|
584
|
-
const { query, args } = this.sel.query(this._table,
|
|
594
|
+
const { query, args } = this.sel.query(this._table, first, rows, sortField, sortOrder, filters);
|
|
585
595
|
this.sel.execute(query, args, dbClient)
|
|
586
596
|
.then((r) => {
|
|
587
597
|
l.rows = r.rows;
|
|
@@ -603,7 +613,7 @@ class SQLEntity extends Entity {
|
|
|
603
613
|
const { query, args } = this.ins.query(this._table, c, cId, cName, rtn);
|
|
604
614
|
let db;
|
|
605
615
|
try {
|
|
606
|
-
db = yield execute
|
|
616
|
+
db = yield execute(query, args, dbClient);
|
|
607
617
|
}
|
|
608
618
|
catch (err) {
|
|
609
619
|
return next(err);
|
|
@@ -627,7 +637,7 @@ class SQLEntity extends Entity {
|
|
|
627
637
|
for (const c of chunks) {
|
|
628
638
|
const { query, args } = this.upd.query(this._table, c, cId, cName);
|
|
629
639
|
try {
|
|
630
|
-
yield execute
|
|
640
|
+
yield execute(query, args, dbClient);
|
|
631
641
|
}
|
|
632
642
|
catch (err) {
|
|
633
643
|
return next(err);
|
|
@@ -647,7 +657,7 @@ class SQLEntity extends Entity {
|
|
|
647
657
|
for (const c of chunks) {
|
|
648
658
|
const { query, args } = this.upd.query(this._table, c, cId, cName);
|
|
649
659
|
try {
|
|
650
|
-
yield execute
|
|
660
|
+
yield execute(query, args, dbClient);
|
|
651
661
|
}
|
|
652
662
|
catch (err) {
|
|
653
663
|
return next(err);
|
|
@@ -655,12 +665,26 @@ class SQLEntity extends Entity {
|
|
|
655
665
|
}
|
|
656
666
|
next();
|
|
657
667
|
});
|
|
658
|
-
this.delete = (req, res, next) => {
|
|
668
|
+
this.delete = (req, res, next) => __awaiter(this, void 0, void 0, function* () {
|
|
669
|
+
const rows = req.body.rows;
|
|
670
|
+
const dbClient = res.locals.dbClient || null;
|
|
671
|
+
const ids = rows.map((row) => row.id);
|
|
672
|
+
log.debug(`${LOGS_PREFIX}delete ${rows.length} rows : (${ids.join(", ")})`);
|
|
673
|
+
const { query, args } = queryById(this._table, ids);
|
|
674
|
+
try {
|
|
675
|
+
yield execute(query, args, dbClient);
|
|
676
|
+
}
|
|
677
|
+
catch (err) {
|
|
678
|
+
return next(err);
|
|
679
|
+
}
|
|
680
|
+
next();
|
|
681
|
+
});
|
|
682
|
+
this.deleteArchive = (req, res, next) => {
|
|
659
683
|
const date = req.body.date;
|
|
660
684
|
const dbClient = res.locals.dbClient || null;
|
|
661
|
-
log.debug(`${LOGS_PREFIX}
|
|
662
|
-
const q =
|
|
663
|
-
|
|
685
|
+
log.debug(`${LOGS_PREFIX}deleteArchive(date=${date})`);
|
|
686
|
+
const q = queryArchived(this._table);
|
|
687
|
+
executeArchived(date, q, dbClient)
|
|
664
688
|
.then(() => next())
|
|
665
689
|
.catch((err) => next(err));
|
|
666
690
|
};
|
|
@@ -696,4 +720,4 @@ class SQLEntity extends Entity {
|
|
|
696
720
|
}
|
|
697
721
|
}
|
|
698
722
|
|
|
699
|
-
export { SQLEntity, execute
|
|
723
|
+
export { SQLEntity, execute, filter };
|