pg-mvc-service 1.0.4 → 1.0.5
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/dist/Service.js +14 -0
- package/dist/exceptions/Exception.js +15 -10
- package/dist/index.js +3 -1
- package/dist/models/TableModel.js +5 -4
- package/package.json +1 -1
- package/src/Service.ts +14 -2
- package/src/exceptions/Exception.ts +15 -10
- package/src/index.ts +1 -1
- package/src/models/TableModel.ts +5 -4
package/dist/Service.js
CHANGED
|
@@ -120,6 +120,20 @@ class Service {
|
|
|
120
120
|
});
|
|
121
121
|
return;
|
|
122
122
|
}
|
|
123
|
+
else if (ex instanceof Exception_1.DbConflictException) {
|
|
124
|
+
this.res.status(409).json({
|
|
125
|
+
errorCode: `${this.apiCode}-${ex.ErrorId}`,
|
|
126
|
+
errorMessage: ex.message
|
|
127
|
+
});
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
else if (ex instanceof Exception_1.BusinessLogicException) {
|
|
131
|
+
this.res.status(422).json({
|
|
132
|
+
errorCode: `${this.apiCode}-${ex.ErrorId}`,
|
|
133
|
+
errorMessage: ex.message
|
|
134
|
+
});
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
123
137
|
else if (ex instanceof Exception_1.MaintenanceException) {
|
|
124
138
|
this.res.status(503).json({
|
|
125
139
|
errorMessage: ex.message
|
|
@@ -19,15 +19,10 @@ class InputErrorException extends Error {
|
|
|
19
19
|
get ErrorId() {
|
|
20
20
|
return this.errorId;
|
|
21
21
|
}
|
|
22
|
-
|
|
23
|
-
return this.errorLog;
|
|
24
|
-
}
|
|
25
|
-
constructor(errorId, message = "", errorLog = "") {
|
|
22
|
+
constructor(errorId, message = "") {
|
|
26
23
|
super(message);
|
|
27
24
|
this.errorId = "";
|
|
28
|
-
this.errorLog = "";
|
|
29
25
|
this.errorId = errorId;
|
|
30
|
-
this.errorLog = errorLog;
|
|
31
26
|
}
|
|
32
27
|
}
|
|
33
28
|
exports.InputErrorException = InputErrorException;
|
|
@@ -38,16 +33,26 @@ class MaintenanceException extends Error {
|
|
|
38
33
|
}
|
|
39
34
|
exports.MaintenanceException = MaintenanceException;
|
|
40
35
|
class DbConflictException extends Error {
|
|
41
|
-
|
|
42
|
-
|
|
36
|
+
get ErrorId() {
|
|
37
|
+
return this.errorId;
|
|
38
|
+
}
|
|
39
|
+
constructor(errorId, message = "") {
|
|
43
40
|
super(message);
|
|
41
|
+
// for 409 Conflict
|
|
42
|
+
this.errorId = "";
|
|
43
|
+
this.errorId = errorId;
|
|
44
44
|
}
|
|
45
45
|
}
|
|
46
46
|
exports.DbConflictException = DbConflictException;
|
|
47
47
|
class BusinessLogicException extends Error {
|
|
48
|
-
|
|
49
|
-
|
|
48
|
+
get ErrorId() {
|
|
49
|
+
return this.errorId;
|
|
50
|
+
}
|
|
51
|
+
constructor(errorId, message = "") {
|
|
50
52
|
super(message);
|
|
53
|
+
// for 422 Unprocessable Entity
|
|
54
|
+
this.errorId = "";
|
|
55
|
+
this.errorId = errorId;
|
|
51
56
|
}
|
|
52
57
|
}
|
|
53
58
|
exports.BusinessLogicException = BusinessLogicException;
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.rollback = exports.migrate = exports.MigrateDatabase = exports.MigrateTable = exports.createTableDoc = exports.TableModel = exports.ResponseType = exports.RequestType = exports.EncryptClient = exports.StringClient = exports.Base64Client = exports.AwsS3Client = exports.createSwagger = exports.ForbiddenException = exports.InputErrorException = exports.AuthException = exports.MaintenanceException = exports.Service = void 0;
|
|
3
|
+
exports.rollback = exports.migrate = exports.MigrateDatabase = exports.MigrateTable = exports.createTableDoc = exports.TableModel = exports.ResponseType = exports.RequestType = exports.EncryptClient = exports.StringClient = exports.Base64Client = exports.AwsS3Client = exports.createSwagger = exports.BusinessLogicException = exports.DbConflictException = exports.ForbiddenException = exports.InputErrorException = exports.AuthException = exports.MaintenanceException = exports.Service = void 0;
|
|
4
4
|
var Service_1 = require("./Service");
|
|
5
5
|
Object.defineProperty(exports, "Service", { enumerable: true, get: function () { return Service_1.Service; } });
|
|
6
6
|
var Exception_1 = require("./exceptions/Exception");
|
|
@@ -8,6 +8,8 @@ Object.defineProperty(exports, "MaintenanceException", { enumerable: true, get:
|
|
|
8
8
|
Object.defineProperty(exports, "AuthException", { enumerable: true, get: function () { return Exception_1.AuthException; } });
|
|
9
9
|
Object.defineProperty(exports, "InputErrorException", { enumerable: true, get: function () { return Exception_1.InputErrorException; } });
|
|
10
10
|
Object.defineProperty(exports, "ForbiddenException", { enumerable: true, get: function () { return Exception_1.ForbiddenException; } });
|
|
11
|
+
Object.defineProperty(exports, "DbConflictException", { enumerable: true, get: function () { return Exception_1.DbConflictException; } });
|
|
12
|
+
Object.defineProperty(exports, "BusinessLogicException", { enumerable: true, get: function () { return Exception_1.BusinessLogicException; } });
|
|
11
13
|
var Swagger_1 = require("./documents/Swagger");
|
|
12
14
|
Object.defineProperty(exports, "createSwagger", { enumerable: true, get: function () { return Swagger_1.createSwagger; } });
|
|
13
15
|
var AwsS3Client_1 = require("./clients/AwsS3Client");
|
|
@@ -17,6 +17,7 @@ const ValidateValueUtil_1 = __importDefault(require("./SqlUtils/ValidateValueUti
|
|
|
17
17
|
const SelectExpression_1 = __importDefault(require("./SqlUtils/SelectExpression"));
|
|
18
18
|
const WhereExpression_1 = __importDefault(require("./SqlUtils/WhereExpression"));
|
|
19
19
|
const ValidateClient_1 = __importDefault(require("./ValidateClient"));
|
|
20
|
+
const Exception_1 = require("../exceptions/Exception");
|
|
20
21
|
class TableModel {
|
|
21
22
|
get DbName() { return this.dbName; }
|
|
22
23
|
get TableName() {
|
|
@@ -366,7 +367,7 @@ class TableModel {
|
|
|
366
367
|
});
|
|
367
368
|
}
|
|
368
369
|
throwValidationError(code, message) {
|
|
369
|
-
throw new
|
|
370
|
+
throw new Exception_1.InputErrorException(code, message);
|
|
370
371
|
}
|
|
371
372
|
validateOptions(options, isInsert) {
|
|
372
373
|
return __awaiter(this, void 0, void 0, function* () {
|
|
@@ -422,7 +423,7 @@ class TableModel {
|
|
|
422
423
|
this.throwValidationError("004", this.errorMessages.fk.replace('{name}', name));
|
|
423
424
|
}
|
|
424
425
|
let refIndex = 1;
|
|
425
|
-
const sql = `SELECT COUNT(*) as count FROM ${ref.table} WHERE ${ref.columns.map(col => `${col.ref} = $${refIndex++}`)}`;
|
|
426
|
+
const sql = `SELECT COUNT(*) as count FROM ${ref.table} WHERE ${ref.columns.map(col => `${col.ref} = $${refIndex++}`).join(" AND ")}`;
|
|
426
427
|
const datas = yield this.clientQuery(sql, refValues);
|
|
427
428
|
if (datas.rows[0].count == "0") {
|
|
428
429
|
const name = ref.columns.map(col => { var _a; return (_a = this.getColumn(col.target).alias) !== null && _a !== void 0 ? _a : this.getColumn(col.target).columnName; }).join(',');
|
|
@@ -530,7 +531,7 @@ class TableModel {
|
|
|
530
531
|
const sql = `UPDATE ${this.TableName} SET ${updateExpressions.join(',')} WHERE id = $${vars.length}`;
|
|
531
532
|
const data = yield this.executeQuery(sql, vars);
|
|
532
533
|
if (data.rowCount !== 1) {
|
|
533
|
-
|
|
534
|
+
throw new Exception_1.BusinessLogicException("201", this.errorMessages.idNotExist.replace('{id}', id));
|
|
534
535
|
}
|
|
535
536
|
});
|
|
536
537
|
}
|
|
@@ -564,7 +565,7 @@ class TableModel {
|
|
|
564
565
|
let sql = `DELETE FROM ${this.TableName} WHERE id = $1`;
|
|
565
566
|
const datas = yield this.executeQuery(sql, [id]);
|
|
566
567
|
if (datas.rowCount !== 1) {
|
|
567
|
-
|
|
568
|
+
throw new Exception_1.BusinessLogicException("301", this.errorMessages.idNotExist.replace('{id}', id));
|
|
568
569
|
}
|
|
569
570
|
});
|
|
570
571
|
}
|
package/package.json
CHANGED
package/src/Service.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import axios, { AxiosResponse } from "axios";
|
|
2
2
|
import { Request, Response } from 'express';
|
|
3
3
|
import { Pool, type PoolClient } from 'pg';
|
|
4
|
-
import { MaintenanceException, AuthException, InputErrorException, ForbiddenException } from './exceptions/Exception';
|
|
4
|
+
import { MaintenanceException, AuthException, InputErrorException, ForbiddenException, DbConflictException, BusinessLogicException } from './exceptions/Exception';
|
|
5
5
|
import { RequestType } from './reqestResponse/RequestType';
|
|
6
6
|
import { ResponseType } from './reqestResponse/ResponseType';
|
|
7
7
|
import { AwsS3Client } from './clients/AwsS3Client';
|
|
@@ -104,6 +104,18 @@ export class Service {
|
|
|
104
104
|
errorMessage : ex.message
|
|
105
105
|
});
|
|
106
106
|
return;
|
|
107
|
+
} else if (ex instanceof DbConflictException) {
|
|
108
|
+
this.res.status(409).json({
|
|
109
|
+
errorCode : `${this.apiCode}-${ex.ErrorId}`,
|
|
110
|
+
errorMessage : ex.message
|
|
111
|
+
});
|
|
112
|
+
return;
|
|
113
|
+
} else if (ex instanceof BusinessLogicException) {
|
|
114
|
+
this.res.status(422).json({
|
|
115
|
+
errorCode : `${this.apiCode}-${ex.ErrorId}`,
|
|
116
|
+
errorMessage : ex.message
|
|
117
|
+
});
|
|
118
|
+
return;
|
|
107
119
|
} else if (ex instanceof MaintenanceException) {
|
|
108
120
|
this.res.status(503).json({
|
|
109
121
|
errorMessage : ex.message
|
|
@@ -242,7 +254,7 @@ export class Service {
|
|
|
242
254
|
}
|
|
243
255
|
} catch (ex) {
|
|
244
256
|
let response = (ex as any).response as AxiosResponse<TResponse>;
|
|
245
|
-
if (response && [400,401,403,409,422].includes(response.status)) {
|
|
257
|
+
if (response && [400, 401, 403, 409, 422].includes(response.status)) {
|
|
246
258
|
return response;
|
|
247
259
|
}
|
|
248
260
|
throw ex;
|
|
@@ -14,21 +14,14 @@ export class ForbiddenException extends Error {
|
|
|
14
14
|
}
|
|
15
15
|
|
|
16
16
|
export class InputErrorException extends Error {
|
|
17
|
-
|
|
18
17
|
private errorId: string = "";
|
|
19
18
|
get ErrorId(): string {
|
|
20
19
|
return this.errorId;
|
|
21
20
|
}
|
|
22
21
|
|
|
23
|
-
|
|
24
|
-
get ErrorLog(): string {
|
|
25
|
-
return this.errorLog;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
constructor(errorId: string, message: string = "", errorLog: string = "") {
|
|
22
|
+
constructor(errorId: string, message: string = "") {
|
|
29
23
|
super(message);
|
|
30
24
|
this.errorId = errorId;
|
|
31
|
-
this.errorLog = errorLog;
|
|
32
25
|
}
|
|
33
26
|
}
|
|
34
27
|
|
|
@@ -41,14 +34,26 @@ export class MaintenanceException extends Error {
|
|
|
41
34
|
|
|
42
35
|
export class DbConflictException extends Error {
|
|
43
36
|
// for 409 Conflict
|
|
44
|
-
|
|
37
|
+
private errorId: string = "";
|
|
38
|
+
get ErrorId(): string {
|
|
39
|
+
return this.errorId;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
constructor(errorId: string, message: string = "") {
|
|
45
43
|
super(message);
|
|
44
|
+
this.errorId = errorId;
|
|
46
45
|
}
|
|
47
46
|
}
|
|
48
47
|
|
|
49
48
|
export class BusinessLogicException extends Error {
|
|
50
49
|
// for 422 Unprocessable Entity
|
|
51
|
-
|
|
50
|
+
private errorId: string = "";
|
|
51
|
+
get ErrorId(): string {
|
|
52
|
+
return this.errorId;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
constructor(errorId: string, message: string = "") {
|
|
52
56
|
super(message);
|
|
57
|
+
this.errorId = errorId;
|
|
53
58
|
}
|
|
54
59
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export { Service } from './Service';
|
|
2
|
-
export { MaintenanceException, AuthException, InputErrorException, ForbiddenException } from './exceptions/Exception';
|
|
2
|
+
export { MaintenanceException, AuthException, InputErrorException, ForbiddenException, DbConflictException, BusinessLogicException } from './exceptions/Exception';
|
|
3
3
|
export { createSwagger } from './documents/Swagger';
|
|
4
4
|
export { AwsS3Client } from './clients/AwsS3Client';
|
|
5
5
|
export { Base64Client } from './clients/Base64Client';
|
package/src/models/TableModel.ts
CHANGED
|
@@ -4,6 +4,7 @@ import ValidateValueUtil from './SqlUtils/ValidateValueUtil';
|
|
|
4
4
|
import SelectExpression from './SqlUtils/SelectExpression';
|
|
5
5
|
import WhereExpression from './SqlUtils/WhereExpression';
|
|
6
6
|
import ValidateClient from './ValidateClient';
|
|
7
|
+
import { BusinessLogicException, InputErrorException } from '../exceptions/Exception';
|
|
7
8
|
|
|
8
9
|
export class TableModel {
|
|
9
10
|
|
|
@@ -422,7 +423,7 @@ export class TableModel {
|
|
|
422
423
|
'idNotExist': 'The specified ID({id}) does not exist in the table.',
|
|
423
424
|
}
|
|
424
425
|
public throwValidationError(code: string, message: string): never {
|
|
425
|
-
throw new
|
|
426
|
+
throw new InputErrorException(code, message);
|
|
426
427
|
}
|
|
427
428
|
|
|
428
429
|
protected async validateOptions(options: TOption, isInsert: boolean): Promise<void> {
|
|
@@ -486,7 +487,7 @@ export class TableModel {
|
|
|
486
487
|
}
|
|
487
488
|
|
|
488
489
|
let refIndex = 1;
|
|
489
|
-
const sql = `SELECT COUNT(*) as count FROM ${ref.table} WHERE ${ref.columns.map(col => `${col.ref} = $${refIndex++}`)}`;
|
|
490
|
+
const sql = `SELECT COUNT(*) as count FROM ${ref.table} WHERE ${ref.columns.map(col => `${col.ref} = $${refIndex++}`).join(" AND ")}`;
|
|
490
491
|
const datas = await this.clientQuery(sql, refValues);
|
|
491
492
|
if (datas.rows[0].count == "0") {
|
|
492
493
|
const name = ref.columns.map(col => this.getColumn(col.target).alias ?? this.getColumn(col.target).columnName).join(',');
|
|
@@ -598,7 +599,7 @@ export class TableModel {
|
|
|
598
599
|
const sql = `UPDATE ${this.TableName} SET ${updateExpressions.join(',')} WHERE id = $${vars.length}`;
|
|
599
600
|
const data = await this.executeQuery(sql, vars);
|
|
600
601
|
if (data.rowCount !== 1) {
|
|
601
|
-
|
|
602
|
+
throw new BusinessLogicException("201", this.errorMessages.idNotExist.replace('{id}', id));
|
|
602
603
|
}
|
|
603
604
|
}
|
|
604
605
|
|
|
@@ -635,7 +636,7 @@ export class TableModel {
|
|
|
635
636
|
|
|
636
637
|
const datas = await this.executeQuery(sql, [id]);
|
|
637
638
|
if (datas.rowCount !== 1) {
|
|
638
|
-
|
|
639
|
+
throw new BusinessLogicException("301", this.errorMessages.idNotExist.replace('{id}', id));
|
|
639
640
|
}
|
|
640
641
|
}
|
|
641
642
|
|