pg-mvc-service 2.1.0 → 2.1.2
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/PoolManager.d.ts +7 -0
- package/dist/PoolManager.d.ts.map +1 -0
- package/dist/Service.d.ts +79 -0
- package/dist/Service.d.ts.map +1 -0
- package/dist/Service.js +0 -8
- package/dist/Utils/DateTimeUtil.d.ts +58 -0
- package/dist/Utils/DateTimeUtil.d.ts.map +1 -0
- package/dist/Utils/NumberUtil.d.ts +10 -0
- package/dist/Utils/NumberUtil.d.ts.map +1 -0
- package/dist/Utils/StringUtil.d.ts +16 -0
- package/dist/Utils/StringUtil.d.ts.map +1 -0
- package/dist/clients/AwsS3Client.d.ts +34 -0
- package/dist/clients/AwsS3Client.d.ts.map +1 -0
- package/dist/clients/AwsS3Client.js +13 -28
- package/dist/clients/EncryptClient.d.ts +18 -0
- package/dist/clients/EncryptClient.d.ts.map +1 -0
- package/dist/clients/StringClient.d.ts +6 -0
- package/dist/clients/StringClient.d.ts.map +1 -0
- package/dist/cron/BaseCron.d.ts +32 -0
- package/dist/cron/BaseCron.d.ts.map +1 -0
- package/dist/cron/BaseCron.js +0 -7
- package/dist/cron/CronExecuter.d.ts +2 -0
- package/dist/cron/CronExecuter.d.ts.map +1 -0
- package/dist/cron/CronType.d.ts +6 -0
- package/dist/cron/CronType.d.ts.map +1 -0
- package/dist/documents/Swagger.d.ts +10 -0
- package/dist/documents/Swagger.d.ts.map +1 -0
- package/dist/exceptions/Exception.d.ts +31 -0
- package/dist/exceptions/Exception.d.ts.map +1 -0
- package/{src/index.ts → dist/index.d.ts} +3 -7
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1 -3
- package/dist/models/ExpressionClient.d.ts +12 -0
- package/dist/models/ExpressionClient.d.ts.map +1 -0
- package/dist/models/MigrateDatabase.d.ts +19 -0
- package/dist/models/MigrateDatabase.d.ts.map +1 -0
- package/dist/models/MigrateRollback.d.ts +18 -0
- package/dist/models/MigrateRollback.d.ts.map +1 -0
- package/dist/models/MigrateTable.d.ts +13 -0
- package/dist/models/MigrateTable.d.ts.map +1 -0
- package/dist/models/SqlUtils/SelectExpression.d.ts +31 -0
- package/dist/models/SqlUtils/SelectExpression.d.ts.map +1 -0
- package/dist/models/SqlUtils/UpdateExpression.d.ts +8 -0
- package/dist/models/SqlUtils/UpdateExpression.d.ts.map +1 -0
- package/dist/models/SqlUtils/ValidateValueUtil.d.ts +19 -0
- package/dist/models/SqlUtils/ValidateValueUtil.d.ts.map +1 -0
- package/dist/models/SqlUtils/WhereExpression.d.ts +30 -0
- package/dist/models/SqlUtils/WhereExpression.d.ts.map +1 -0
- package/dist/models/TableDoc.d.ts +3 -0
- package/dist/models/TableDoc.d.ts.map +1 -0
- package/dist/models/TableModel.d.ts +196 -0
- package/dist/models/TableModel.d.ts.map +1 -0
- package/dist/models/Type.d.ts +64 -0
- package/dist/models/Type.d.ts.map +1 -0
- package/dist/models/Utils/MessageUtil.d.ts +7 -0
- package/dist/models/Utils/MessageUtil.d.ts.map +1 -0
- package/dist/models/ValidateClient.d.ts +27 -0
- package/dist/models/ValidateClient.d.ts.map +1 -0
- package/dist/reqestResponse/ReqResType.d.ts +113 -0
- package/dist/reqestResponse/ReqResType.d.ts.map +1 -0
- package/dist/reqestResponse/RequestType.d.ts +247 -0
- package/dist/reqestResponse/RequestType.d.ts.map +1 -0
- package/dist/reqestResponse/ResponseType.d.ts +85 -0
- package/dist/reqestResponse/ResponseType.d.ts.map +1 -0
- package/package.json +7 -2
- package/dist/clients/Base64Client.js +0 -292
- package/index.d.ts +0 -192
- package/src/PoolManager.ts +0 -48
- package/src/Service.ts +0 -307
- package/src/Utils/DateTimeUtil.ts +0 -146
- package/src/Utils/NumberUtil.ts +0 -23
- package/src/Utils/StringUtil.ts +0 -33
- package/src/clients/AwsS3Client.ts +0 -310
- package/src/clients/Base64Client.ts +0 -305
- package/src/clients/EncryptClient.ts +0 -100
- package/src/clients/StringClient.ts +0 -19
- package/src/cron/BaseCron.ts +0 -122
- package/src/cron/CronExecuter.ts +0 -34
- package/src/cron/CronType.ts +0 -25
- package/src/documents/Swagger.ts +0 -106
- package/src/exceptions/Exception.ts +0 -72
- package/src/models/ExpressionClient.ts +0 -72
- package/src/models/MigrateDatabase.ts +0 -135
- package/src/models/MigrateRollback.ts +0 -151
- package/src/models/MigrateTable.ts +0 -56
- package/src/models/SqlUtils/SelectExpression.ts +0 -102
- package/src/models/SqlUtils/UpdateExpression.ts +0 -29
- package/src/models/SqlUtils/ValidateValueUtil.ts +0 -354
- package/src/models/SqlUtils/WhereExpression.ts +0 -455
- package/src/models/TableDoc.ts +0 -372
- package/src/models/TableModel.ts +0 -749
- package/src/models/Type.ts +0 -62
- package/src/models/Utils/MessageUtil.ts +0 -60
- package/src/models/ValidateClient.ts +0 -182
- package/src/reqestResponse/ReqResType.ts +0 -241
- package/src/reqestResponse/RequestType.ts +0 -1619
- package/src/reqestResponse/ResponseType.ts +0 -549
- package/tsconfig.json +0 -14
package/index.d.ts
DELETED
|
@@ -1,192 +0,0 @@
|
|
|
1
|
-
import { AxiosResponse } from "axios";
|
|
2
|
-
import { Request, Response } from 'express';
|
|
3
|
-
import { Pool, PoolClient } from 'pg';
|
|
4
|
-
import { ErrorMessageType, IncomingHttpHeaders } from './src/reqestResponse/RequestType';
|
|
5
|
-
import { ArrayType, EnumType, MapType, NumberType, ObjectType, PrimitiveType, StringType } from './src/reqestResponse/ReqResType';
|
|
6
|
-
|
|
7
|
-
import { MethodType } from './src/Service';
|
|
8
|
-
export { MethodType } from './src/Service';
|
|
9
|
-
|
|
10
|
-
import { Base64Client } from './src/clients/Base64Client';
|
|
11
|
-
import { StringClient } from './src/clients/StringClient';
|
|
12
|
-
import { EncryptClient } from './src/clients/EncryptClient';
|
|
13
|
-
|
|
14
|
-
import { AwsS3Client } from './src/clients/AwsS3Client';
|
|
15
|
-
export { AwsS3Client } from './src/clients/AwsS3Client';
|
|
16
|
-
|
|
17
|
-
// models class
|
|
18
|
-
import ValidateClient from './src/models/ValidateClient';
|
|
19
|
-
|
|
20
|
-
import { TableModel } from "./src/models/TableModel";
|
|
21
|
-
export { TableModel } from "./src/models/TableModel";
|
|
22
|
-
|
|
23
|
-
import { WhereExpression } from './src/models/SqlUtils/WhereExpression';
|
|
24
|
-
export { WhereExpression } from './src/models/SqlUtils/WhereExpression';
|
|
25
|
-
|
|
26
|
-
export { TColumnAttribute, TColumnType, TColumnArrayType, TColumn, TColumnDetail, TOperator, TColumnInfo, TQuery, TSelectExpression, TAggregateFuncType, TCondition, TNestedCondition, TSortKeyword, TKeyFormat } from './src/models/Type';
|
|
27
|
-
|
|
28
|
-
export { DayType, MonthType, DateType, HourType, MinuteSecondType } from './src/cron/CronType';
|
|
29
|
-
export { BaseCron } from "./src/cron/BaseCron";
|
|
30
|
-
export { runCron } from "./src/cron/CronExecuter";
|
|
31
|
-
|
|
32
|
-
declare module 'pg-mvc-service' {
|
|
33
|
-
export class Service {
|
|
34
|
-
protected readonly method: MethodType;
|
|
35
|
-
get Method(): MethodType;
|
|
36
|
-
protected readonly endpoint: string;
|
|
37
|
-
get Endpoint(): string;
|
|
38
|
-
protected readonly apiCode: string;
|
|
39
|
-
get ApiCode(): string;
|
|
40
|
-
protected readonly summary: string;
|
|
41
|
-
get Summary(): string;
|
|
42
|
-
protected readonly apiUserAvailable: string;
|
|
43
|
-
get ApiUserAvailable(): string;
|
|
44
|
-
protected readonly request: RequestType;
|
|
45
|
-
get AuthToken(): string;
|
|
46
|
-
protected readonly response: ResponseType;
|
|
47
|
-
protected readonly isTest: boolean;
|
|
48
|
-
protected readonly tags: Array<string>;
|
|
49
|
-
get Tags(): Array<string>;
|
|
50
|
-
|
|
51
|
-
protected readonly req: Request;
|
|
52
|
-
protected readonly res: Response;
|
|
53
|
-
constructor(request: Request, response: Response);
|
|
54
|
-
|
|
55
|
-
public inintialize(): void;
|
|
56
|
-
|
|
57
|
-
protected dbUser?: string;
|
|
58
|
-
protected dbHost?: string;
|
|
59
|
-
protected dbName?: string;
|
|
60
|
-
protected dbPassword?: string;
|
|
61
|
-
protected dbPort?: number;
|
|
62
|
-
protected dbIsSslConnect?: boolean;
|
|
63
|
-
protected checkMaintenance(): Promise<void>;
|
|
64
|
-
protected middleware(): Promise<void>;
|
|
65
|
-
|
|
66
|
-
public resSuccess(): void;
|
|
67
|
-
public handleException(ex: any): void;
|
|
68
|
-
protected outputErrorLog(ex: any): Promise<void>;
|
|
69
|
-
|
|
70
|
-
protected get Pool(): Pool;
|
|
71
|
-
protected get Client(): PoolClient;
|
|
72
|
-
|
|
73
|
-
public startConnect(): Promise<void>;
|
|
74
|
-
public commit(): Promise<void>;
|
|
75
|
-
public rollback(): Promise<void>;
|
|
76
|
-
public release(): Promise<void>;
|
|
77
|
-
|
|
78
|
-
get S3Client(): AwsS3Client;
|
|
79
|
-
get Base64Client(): Base64Client;
|
|
80
|
-
get StringClient(): StringClient;
|
|
81
|
-
get EncryptClient(): EncryptClient;
|
|
82
|
-
|
|
83
|
-
public requestApi<TRequest=Record<string, any>, TResponse={[key: string]: any}>(
|
|
84
|
-
method: MethodType, url: string, params: TRequest, header: {[key: string]: any}): Promise<AxiosResponse<TResponse>>;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
export interface IParams {
|
|
88
|
-
in: 'header' | 'path',
|
|
89
|
-
name: string,
|
|
90
|
-
require?: boolean,
|
|
91
|
-
description?: string,
|
|
92
|
-
example?: string
|
|
93
|
-
}
|
|
94
|
-
export function createSwagger(services: Service[], name: string, url: string, params: Array<IParams>): string;
|
|
95
|
-
|
|
96
|
-
export type PropertyType = PrimitiveType | StringType | NumberType | ObjectType | ArrayType | EnumType | MapType;
|
|
97
|
-
|
|
98
|
-
export class RequestType {
|
|
99
|
-
constructor();
|
|
100
|
-
|
|
101
|
-
protected properties: { [key: string]: PropertyType; };
|
|
102
|
-
protected paramProperties: Array<(PrimitiveType | StringType | NumberType | EnumType) & { key: string }>;
|
|
103
|
-
protected readonly ERROR_MESSAGE: ErrorMessageType;
|
|
104
|
-
|
|
105
|
-
protected throwException(code: string, message: string): never;
|
|
106
|
-
|
|
107
|
-
public setRequest(request: Request): void;
|
|
108
|
-
get Data(): { [key: string]: any };
|
|
109
|
-
get Headers(): IncomingHttpHeaders;
|
|
110
|
-
get Params(): { [key: string]: any };
|
|
111
|
-
get RemoteAddress(): string | undefined;
|
|
112
|
-
get Authorization(): string | null;
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
export class ResponseType {
|
|
116
|
-
public Data: { [key: string]: any };
|
|
117
|
-
|
|
118
|
-
protected properties: { [key: string]: PropertyType; };
|
|
119
|
-
get ResponseData(): { [key: string]: any };
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
export class AuthException extends Error {
|
|
123
|
-
private id: string;
|
|
124
|
-
get Id(): string;
|
|
125
|
-
constructor(id: string, message?: string);
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
export class ForbiddenException extends Error {
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
export class InputErrorException extends Error {
|
|
132
|
-
private errorId: string;
|
|
133
|
-
get ErrorId(): string;
|
|
134
|
-
private errorLog: string;
|
|
135
|
-
get ErrorLog(): string;
|
|
136
|
-
constructor(errorId: string, message?: string, errorLog?: string);
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
export class MaintenanceException extends Error {
|
|
140
|
-
constructor(message?: string);
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
export class DbConflictException extends Error {
|
|
144
|
-
private errorId: string;
|
|
145
|
-
get ErrorId(): string;
|
|
146
|
-
constructor(errorId: string, message?: string);
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
export class NotFoundException extends Error {
|
|
150
|
-
private errorId: string;
|
|
151
|
-
get ErrorId(): string;
|
|
152
|
-
constructor(errorId: string, message?: string);
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
export class UnprocessableException extends Error {
|
|
156
|
-
private errorId: string;
|
|
157
|
-
get ErrorId(): string;
|
|
158
|
-
constructor(errorId: string, message?: string);
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
export function createTableDoc(models: Array<TableModel>, serviceName?: string): string;
|
|
162
|
-
export function migrate(migrates: Array<MigrateTable>, poolParam: {
|
|
163
|
-
host: string, user: string, dbName: string, password: string, port?: number, isSsl?: boolean
|
|
164
|
-
}): Promise<void>;
|
|
165
|
-
export function rollback(toNumber: number, poolParam: {
|
|
166
|
-
host: string, user: string, dbName: string, password: string, port?: number, isSsl?: boolean
|
|
167
|
-
}): Promise<void>;
|
|
168
|
-
export class MigrateTable {
|
|
169
|
-
protected readonly migrateSql: string;
|
|
170
|
-
protected readonly rollbackSql: string;
|
|
171
|
-
protected readonly addGrantTables: Array<string>;
|
|
172
|
-
protected readonly user: string;
|
|
173
|
-
|
|
174
|
-
constructor();
|
|
175
|
-
constructor(user: string);
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
export class MigrateDatabase {
|
|
179
|
-
constructor(dbName: string, userName: string, pool: Pool);
|
|
180
|
-
|
|
181
|
-
get DbName(): string;
|
|
182
|
-
get UserName(): string;
|
|
183
|
-
get Password(): string | null;
|
|
184
|
-
|
|
185
|
-
public IsExistUser(): Promise<boolean>;
|
|
186
|
-
public CreateUser(password?: string): Promise<void>;
|
|
187
|
-
public IsExistDb(): Promise<boolean>;
|
|
188
|
-
public CreateDb(collateType?: string): Promise<void>;
|
|
189
|
-
public RollbackDbSql(): string;
|
|
190
|
-
public RollbackUserSql(otherUserName: string): string;
|
|
191
|
-
}
|
|
192
|
-
}
|
package/src/PoolManager.ts
DELETED
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
// PoolManager.ts
|
|
2
|
-
import { Pool } from 'pg';
|
|
3
|
-
|
|
4
|
-
export default class PoolManager {
|
|
5
|
-
private static poolMap: Record<string, Pool> = {};
|
|
6
|
-
|
|
7
|
-
static getPool(user: string, host: string, database: string, password: string, port: number | string, isSslConnect: boolean): Pool {
|
|
8
|
-
const key = `${user}@${host}/${database}`;
|
|
9
|
-
if (!this.poolMap[key]) {
|
|
10
|
-
this.poolMap[key] = new Pool({
|
|
11
|
-
user: user,
|
|
12
|
-
host: host,
|
|
13
|
-
database: database,
|
|
14
|
-
password: password,
|
|
15
|
-
port: Number(port),
|
|
16
|
-
ssl: isSslConnect ? {
|
|
17
|
-
rejectUnauthorized: false
|
|
18
|
-
} : false
|
|
19
|
-
});
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
return this.poolMap[key];
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
static async shutdownAll(): Promise<void> {
|
|
26
|
-
for (const [key, pool] of Object.entries(this.poolMap)) {
|
|
27
|
-
try {
|
|
28
|
-
await pool.end();
|
|
29
|
-
console.log(`Closed pool: ${key}`);
|
|
30
|
-
} catch (e) {
|
|
31
|
-
console.error(`Error closing pool ${key}`, e);
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
// ✅ 自動実行されるシャットダウン登録
|
|
38
|
-
process.on('SIGINT', async () => {
|
|
39
|
-
console.log('🔌 SIGINT received. Closing all pools...');
|
|
40
|
-
await PoolManager.shutdownAll();
|
|
41
|
-
process.exit(0);
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
process.on('SIGTERM', async () => {
|
|
45
|
-
console.log('🔌 SIGTERM received. Closing all pools...');
|
|
46
|
-
await PoolManager.shutdownAll();
|
|
47
|
-
process.exit(0);
|
|
48
|
-
});
|
package/src/Service.ts
DELETED
|
@@ -1,307 +0,0 @@
|
|
|
1
|
-
import axios, { AxiosResponse } from "axios";
|
|
2
|
-
import { Request, Response } from 'express';
|
|
3
|
-
import { Hono, Context } from 'hono' // Context を追加
|
|
4
|
-
import { Pool, type PoolClient } from 'pg';
|
|
5
|
-
import { MaintenanceException, AuthException, InputErrorException, ForbiddenException, DbConflictException, UnprocessableException, NotFoundException } from './exceptions/Exception';
|
|
6
|
-
import { RequestType } from './reqestResponse/RequestType';
|
|
7
|
-
import { ResponseType } from './reqestResponse/ResponseType';
|
|
8
|
-
import { AwsS3Client } from './clients/AwsS3Client';
|
|
9
|
-
import { Base64Client } from './clients/Base64Client';
|
|
10
|
-
import { StringClient } from './clients/StringClient';
|
|
11
|
-
import { EncryptClient } from './clients/EncryptClient';
|
|
12
|
-
import PoolManager from './PoolManager';
|
|
13
|
-
|
|
14
|
-
type TStatusCode = 200 | 201 | 400 | 401 | 403 | 404 | 409 | 422 | 500 | 503;
|
|
15
|
-
|
|
16
|
-
export type MethodType = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
|
|
17
|
-
export interface IError {
|
|
18
|
-
status: TStatusCode,
|
|
19
|
-
code: string;
|
|
20
|
-
description: string;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
export class Service {
|
|
25
|
-
protected readonly module: 'express' | 'hono' = 'express';
|
|
26
|
-
protected readonly method: MethodType = 'GET';
|
|
27
|
-
get Method(): MethodType { return this.method; }
|
|
28
|
-
protected readonly endpoint: string = '';
|
|
29
|
-
get Endpoint(): string { return this.endpoint + this.request.paramPath; }
|
|
30
|
-
protected readonly apiCode: string = '';
|
|
31
|
-
get ApiCode(): string { return this.apiCode; }
|
|
32
|
-
protected readonly summary: string = '';
|
|
33
|
-
get Summary(): string { return `${this.ApiCode !== '' ? this.apiCode + ': ' : ''}${this.summary}`; }
|
|
34
|
-
protected readonly apiUserAvailable: string = '';
|
|
35
|
-
get ApiUserAvailable(): string { return this.apiUserAvailable; }
|
|
36
|
-
protected readonly request: RequestType = new RequestType();
|
|
37
|
-
get Request(): RequestType { return this.request }; // swaggerで必要なので、ここだけ宣言
|
|
38
|
-
get AuthToken(): string { return this.request.Authorization ?? ''; }
|
|
39
|
-
protected readonly response: ResponseType = new ResponseType();
|
|
40
|
-
get Response(): ResponseType { return this.response }; // swaggerで必要なので、ここだけ宣言
|
|
41
|
-
protected readonly tags: Array<string> = [];
|
|
42
|
-
get Tags(): Array<string> { return this.tags; }
|
|
43
|
-
protected readonly errorList: Array<IError> = [];
|
|
44
|
-
get ErrorList(): Array<IError> {
|
|
45
|
-
return [...this.errorList, {
|
|
46
|
-
status: 500,
|
|
47
|
-
code: '',
|
|
48
|
-
description: 'サーバー内部エラー(予期せぬエラー)'
|
|
49
|
-
}];
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
protected readonly req?: Request;
|
|
53
|
-
protected readonly res?: Response;
|
|
54
|
-
protected readonly c?: Context;
|
|
55
|
-
protected setResponse(status: TStatusCode, data: any) {
|
|
56
|
-
if (this.module === 'express') {
|
|
57
|
-
this.res?.status(status).json(data);
|
|
58
|
-
} else {
|
|
59
|
-
this.c?.json(data, status)
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
constructor(request: Request, response: Response);
|
|
64
|
-
constructor(c: Context);
|
|
65
|
-
constructor(param1: Request | Context, param2?: Response) {
|
|
66
|
-
if (param2 !== undefined) {
|
|
67
|
-
// Express の場合: (request, response)
|
|
68
|
-
this.module = 'express';
|
|
69
|
-
this.req = param1 as Request;
|
|
70
|
-
this.res = param2;
|
|
71
|
-
} else {
|
|
72
|
-
// Hono の場合: (c)
|
|
73
|
-
this.module = 'hono';
|
|
74
|
-
this.c = param1 as Context;
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
public async inintialize(): Promise<void> {
|
|
79
|
-
if (this.module === "express") {
|
|
80
|
-
await this.request.setRequest(this.module, this.req as Request);
|
|
81
|
-
} else {
|
|
82
|
-
await this.request.setRequest(this.module, this.c as Context);
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
await this.checkMaintenance();
|
|
86
|
-
await this.middleware();
|
|
87
|
-
}
|
|
88
|
-
// Context<{ Bindings: Bindings }>
|
|
89
|
-
|
|
90
|
-
protected dbUser?: string = process.env.DB_USER;
|
|
91
|
-
protected dbHost?: string = process.env.DB_HOST;
|
|
92
|
-
protected dbName?: string = process.env.DB_DATABASE;
|
|
93
|
-
protected dbPassword?: string = process.env.DB_PASSWORD;
|
|
94
|
-
protected dbPort?: string | number = process.env.DB_PORT;
|
|
95
|
-
protected dbIsSslConnect: boolean = process.env.DB_IS_SSL === 'true';
|
|
96
|
-
private setPool(): Pool {
|
|
97
|
-
if (this.dbUser === undefined) {
|
|
98
|
-
throw new Error("Database user is not configured");
|
|
99
|
-
}
|
|
100
|
-
if (this.dbHost === undefined) {
|
|
101
|
-
throw new Error("Database host is not configured");
|
|
102
|
-
}
|
|
103
|
-
if (this.dbName === undefined) {
|
|
104
|
-
throw new Error("Database name is not configured");
|
|
105
|
-
}
|
|
106
|
-
if (this.dbPassword === undefined) {
|
|
107
|
-
throw new Error("Database password is not configured");
|
|
108
|
-
}
|
|
109
|
-
if (this.dbPort === undefined) {
|
|
110
|
-
throw new Error("Database port is not configured");
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
try {
|
|
114
|
-
return PoolManager.getPool(this.dbUser, this.dbHost, this.dbName, this.dbPassword, this.dbPort, this.dbIsSslConnect);
|
|
115
|
-
} catch (ex) {
|
|
116
|
-
throw new Error("Failed to connect to the database. Please check the connection settings.");
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
protected async checkMaintenance(): Promise<void> { }
|
|
120
|
-
protected async middleware(): Promise<void>{ }
|
|
121
|
-
|
|
122
|
-
public resSuccess(): void {
|
|
123
|
-
this.setResponse(200, this.response.ResponseData);
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
protected async outputErrorLog(ex: any): Promise<void>{ }
|
|
127
|
-
public handleException(ex: any): void {
|
|
128
|
-
// To avoid slowing down the response, make this asynchronous
|
|
129
|
-
this.outputErrorLog(ex).catch((ex) => {
|
|
130
|
-
console.error(ex);
|
|
131
|
-
});
|
|
132
|
-
|
|
133
|
-
if (ex instanceof AuthException) {
|
|
134
|
-
this.setResponse(401, {
|
|
135
|
-
message : "Authentication expired. Please login again."
|
|
136
|
-
});
|
|
137
|
-
return;
|
|
138
|
-
} else if (ex instanceof ForbiddenException) {
|
|
139
|
-
this.setResponse(403, {
|
|
140
|
-
message : 'Forbidden error'
|
|
141
|
-
});
|
|
142
|
-
return;
|
|
143
|
-
} else if (ex instanceof InputErrorException) {
|
|
144
|
-
this.setResponse(400, {
|
|
145
|
-
errorCode : `${this.apiCode}-${ex.ErrorId}`,
|
|
146
|
-
errorMessage : ex.message
|
|
147
|
-
});
|
|
148
|
-
return;
|
|
149
|
-
} else if (ex instanceof DbConflictException) {
|
|
150
|
-
this.setResponse(409, {
|
|
151
|
-
errorCode : `${this.apiCode}-${ex.ErrorId}`,
|
|
152
|
-
errorMessage : ex.message
|
|
153
|
-
});
|
|
154
|
-
return;
|
|
155
|
-
} else if (ex instanceof UnprocessableException) {
|
|
156
|
-
this.setResponse(422, {
|
|
157
|
-
errorCode : `${this.apiCode}-${ex.ErrorId}`,
|
|
158
|
-
errorMessage : ex.message
|
|
159
|
-
});
|
|
160
|
-
return;
|
|
161
|
-
} else if (ex instanceof MaintenanceException) {
|
|
162
|
-
this.setResponse(503, {
|
|
163
|
-
errorMessage : ex.message
|
|
164
|
-
});
|
|
165
|
-
return;
|
|
166
|
-
} else if (ex instanceof NotFoundException) {
|
|
167
|
-
this.setResponse(404, {
|
|
168
|
-
errorCode : `${this.apiCode}-${ex.ErrorId}`,
|
|
169
|
-
errorMessage : ex.message
|
|
170
|
-
});
|
|
171
|
-
return;
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
this.setResponse(500, {
|
|
175
|
-
message : 'Internal server error'
|
|
176
|
-
});
|
|
177
|
-
|
|
178
|
-
return;
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
private pool?: Pool;
|
|
182
|
-
protected get Pool(): Pool {
|
|
183
|
-
if (this.pool === undefined) {
|
|
184
|
-
this.pool = this.setPool();
|
|
185
|
-
this.pool.query(`SET TIME ZONE '${process.env.TZ ?? 'Asia/Tokyo'}';`);
|
|
186
|
-
}
|
|
187
|
-
return this.pool;
|
|
188
|
-
}
|
|
189
|
-
private client?: PoolClient;
|
|
190
|
-
private isExecuteRollback: boolean = false;
|
|
191
|
-
protected get Client(): PoolClient {
|
|
192
|
-
if (this.client === undefined) {
|
|
193
|
-
throw new Error("Please call this.PoolClient after using the startConnect method.");
|
|
194
|
-
}
|
|
195
|
-
return this.client;
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
public async startConnect(): Promise<void> {
|
|
199
|
-
this.client = await this.Pool.connect();
|
|
200
|
-
await this.Client.query('BEGIN');
|
|
201
|
-
this.isExecuteRollback = true;
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
public async commit(): Promise<void> {
|
|
205
|
-
await this.Client.query('COMMIT');
|
|
206
|
-
this.isExecuteRollback = false;
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
public async rollback(): Promise<void> {
|
|
210
|
-
if (this.isExecuteRollback) {
|
|
211
|
-
await this.Client.query('ROLLBACK');
|
|
212
|
-
}
|
|
213
|
-
this.isExecuteRollback = false;
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
public async release(): Promise<void> {
|
|
217
|
-
await this.rollback();
|
|
218
|
-
if (this.client !== undefined) {
|
|
219
|
-
await this.client.release();
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
private s3Client?: AwsS3Client;
|
|
224
|
-
get S3Client(): AwsS3Client {
|
|
225
|
-
if (this.s3Client === undefined) {
|
|
226
|
-
this.s3Client = new AwsS3Client({
|
|
227
|
-
bucketName: process.env.S3_BUCKET_NAME,
|
|
228
|
-
region: process.env.S3_REGION,
|
|
229
|
-
accessKeyId: process.env.S3_ACCESS_KEY_ID,
|
|
230
|
-
secretAccessKey: process.env.S3_SECRET_ACCESS_KEY
|
|
231
|
-
});
|
|
232
|
-
}
|
|
233
|
-
return this.s3Client;
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
private base64Client? : Base64Client;
|
|
237
|
-
get Base64Client(): Base64Client {
|
|
238
|
-
if (this.base64Client === undefined) {
|
|
239
|
-
this.base64Client = new Base64Client();
|
|
240
|
-
}
|
|
241
|
-
return this.base64Client;
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
private stringClient? : StringClient;
|
|
245
|
-
get StringClient(): StringClient {
|
|
246
|
-
if (this.stringClient === undefined) {
|
|
247
|
-
this.stringClient = new StringClient();
|
|
248
|
-
}
|
|
249
|
-
return this.stringClient;
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
private encryptClient?: EncryptClient;
|
|
253
|
-
get EncryptClient(): EncryptClient {
|
|
254
|
-
if (this.encryptClient === undefined) {
|
|
255
|
-
this.encryptClient = new EncryptClient({
|
|
256
|
-
secretKeyHex: process.env.SECRET_KEY_HEX,
|
|
257
|
-
hmacKeyBase64: process.env.HMAC_KEY_BASE64
|
|
258
|
-
});
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
return this.encryptClient;
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
public async requestApi<TRequest=Record<string, any>, TResponse={[key: string]: any}>(
|
|
265
|
-
method: MethodType, url: string, params: TRequest, header: {[key: string]: any}): Promise<AxiosResponse<TResponse>> {
|
|
266
|
-
|
|
267
|
-
// GET,DELETEのparamをURLクエリに
|
|
268
|
-
if (method === 'GET' || method === 'DELETE') {
|
|
269
|
-
for (const [key, value] of Object.entries(params as Record<string, any>)) {
|
|
270
|
-
if (value === undefined || value === null) {
|
|
271
|
-
continue;
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
if (Array.isArray(value)) {
|
|
275
|
-
for (const arrayValue of value) {
|
|
276
|
-
url += url.includes('?') ? '&' : '?';
|
|
277
|
-
url += `${key}=${arrayValue.toString()}`;
|
|
278
|
-
}
|
|
279
|
-
} else {
|
|
280
|
-
url += url.includes('?') ? '&' : '?';
|
|
281
|
-
url += `${key}=${value.toString()}`;
|
|
282
|
-
}
|
|
283
|
-
}
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
try {
|
|
287
|
-
switch (method) {
|
|
288
|
-
case 'GET':
|
|
289
|
-
return await axios.get(url, header === undefined ? {} : { headers: header });
|
|
290
|
-
case 'POST':
|
|
291
|
-
return await axios.post(url, params, header === undefined ? {} : { headers: header });
|
|
292
|
-
case 'PUT':
|
|
293
|
-
return await axios.put(url, params, header === undefined ? {} : { headers: header });
|
|
294
|
-
case 'DELETE':
|
|
295
|
-
return await axios.delete(url, header === undefined ? {} : { headers: header });
|
|
296
|
-
case 'PATCH':
|
|
297
|
-
return await axios.patch(url, params, header === undefined ? {} : { headers: header });
|
|
298
|
-
}
|
|
299
|
-
} catch (ex) {
|
|
300
|
-
let response = (ex as any).response as AxiosResponse<TResponse>;
|
|
301
|
-
if (response && [400, 401, 403, 404, 409, 422].includes(response.status)) {
|
|
302
|
-
return response;
|
|
303
|
-
}
|
|
304
|
-
throw ex;
|
|
305
|
-
}
|
|
306
|
-
}
|
|
307
|
-
}
|
|
@@ -1,146 +0,0 @@
|
|
|
1
|
-
export default class DateTimeUtil {
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Checks if the value is a valid date-time format
|
|
5
|
-
* 値が有効な日付時間形式かどうかを確認します
|
|
6
|
-
* @param value - 検証する値, The value to be validated
|
|
7
|
-
* @returns {boolean} - 値が有効な日付時間形式であるかどうか, Whether the value is a valid date-time format
|
|
8
|
-
*/
|
|
9
|
-
private static isErrorDateTime(value: string): boolean {
|
|
10
|
-
try {
|
|
11
|
-
const [datePart, timePart] = value.split(' ');
|
|
12
|
-
const [year, month, day] = datePart.split('-').map(Number);
|
|
13
|
-
let [hour, minute, sec] = [0, 0, 0];
|
|
14
|
-
if (timePart !== undefined) {
|
|
15
|
-
[hour, minute, sec] = timePart.split(':').map(Number);
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
const date = new Date(year, month - 1, day, hour, minute, sec);
|
|
19
|
-
return year !== date.getFullYear() ||
|
|
20
|
-
month !== date.getMonth() + 1 ||
|
|
21
|
-
day !== date.getDate() ||
|
|
22
|
-
hour !== date.getHours() ||
|
|
23
|
-
minute !== date.getMinutes() ||
|
|
24
|
-
sec !== date.getSeconds()
|
|
25
|
-
} catch (error) {
|
|
26
|
-
return true;
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* Generates a Date object from a string.
|
|
32
|
-
* 文字列からDateオブジェクトを生成します。
|
|
33
|
-
* @param dateString A string representing the date and time (e.g., "2023-10-05 14:30:00")
|
|
34
|
-
* 日付と時間を表す文字列(例: "2023-10-05 14:30:00")
|
|
35
|
-
* @returns Date object
|
|
36
|
-
* Dateオブジェクト
|
|
37
|
-
*/
|
|
38
|
-
static toDateFromString(dateString: string): Date {
|
|
39
|
-
const [datePart, timePart] = dateString.split(' ');
|
|
40
|
-
const [year, month, day] = datePart.split('-').map(Number);
|
|
41
|
-
let [hours, minutes, seconds] = [0, 0, 0];
|
|
42
|
-
if (timePart !== undefined) {
|
|
43
|
-
[hours, minutes, seconds] = timePart.split(':').map(Number);
|
|
44
|
-
}
|
|
45
|
-
return new Date(year, month - 1, day, hours, minutes, seconds);
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* Formats the specified date.
|
|
50
|
-
* 指定された日付をフォーマットします。
|
|
51
|
-
* @param date The date object to be formatted.
|
|
52
|
-
* フォーマットする対象の日付オブジェクト
|
|
53
|
-
* @param type A string specifying the type of format.
|
|
54
|
-
* フォーマットの種類を指定する文字列
|
|
55
|
-
* @returns A formatted date string.
|
|
56
|
-
* フォーマットされた日付文字列
|
|
57
|
-
*/
|
|
58
|
-
static toStringFromDate(date: Date, type: 'datetime' | 'date' | 'time'): string {
|
|
59
|
-
|
|
60
|
-
const year = date.getFullYear().toString().padStart(4, '0');
|
|
61
|
-
const month = (date.getMonth() + 1).toString().padStart(2, '0');
|
|
62
|
-
const day = date.getDate().toString().padStart(2, '0');
|
|
63
|
-
const hour = date.getHours().toString().padStart(2, '0');
|
|
64
|
-
const minute = date.getMinutes().toString().padStart(2, '0');
|
|
65
|
-
const second = date.getSeconds().toString().padStart(2, '0');
|
|
66
|
-
|
|
67
|
-
switch (type) {
|
|
68
|
-
case 'datetime':
|
|
69
|
-
return `${year}-${month}-${day} ${hour}:${minute}:${second}`;
|
|
70
|
-
case 'date':
|
|
71
|
-
return `${year}-${month}-${day}`;
|
|
72
|
-
case 'time':
|
|
73
|
-
return `${hour}:${minute}:${second}`;
|
|
74
|
-
default:
|
|
75
|
-
throw new Error('Invalid type');
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
/**
|
|
80
|
-
* Validates if the given value is in the format YYYY-MM-DD
|
|
81
|
-
* 与えられた値がYYYY-MM-DD形式であるかどうかを検証します
|
|
82
|
-
* @param value - The value to be validated, 検証する値
|
|
83
|
-
* @returns {boolean} - Whether the value is in the format YYYY-MM-DD, 値がYYYY-MM-DD形式であるかどうか
|
|
84
|
-
*/
|
|
85
|
-
static isYYYYMMDD(value: any) {
|
|
86
|
-
if (typeof value !== 'string') {
|
|
87
|
-
return false;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
const pattern = new RegExp('^\\d{4}-\\d{2}-\\d{2}$');
|
|
91
|
-
if (pattern.test(value) === false) {
|
|
92
|
-
return false;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
return this.isErrorDateTime(value) === false;
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
/**
|
|
99
|
-
* Validates if the given value is in the format YYYY-MM-DD hh:mm:ss
|
|
100
|
-
* 与えられた値がYYYY-MM-DD hh:mm:ss形式であるかどうかを検証します
|
|
101
|
-
* @param value - The value to be validated, 検証する値
|
|
102
|
-
* @returns {boolean} - Whether the value is in the format YYYY-MM-DD hh:mm:ss, 値がYYYY-MM-DD hh:mm:ss形式であるかどうか
|
|
103
|
-
*/
|
|
104
|
-
static isYYYYMMDDhhmiss(value: any) {
|
|
105
|
-
if (typeof value !== 'string') {
|
|
106
|
-
return false;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
const pattern = new RegExp('^\\d{4}-\\d{2}-\\d{2}[ T]\\d{2}:\\d{2}:\\d{2}$');
|
|
110
|
-
if (pattern.test(value) === false) {
|
|
111
|
-
return false;
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
return this.isErrorDateTime(value) === false;
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
/**
|
|
118
|
-
* Validates if the given value is in the format YYYY-MM-DD hh:mm:ss
|
|
119
|
-
* 与えられた値がYYYY-MM-DD hh:mm:ss形式であるかどうかを検証します
|
|
120
|
-
* @param value - The value to be validated, 検証する値
|
|
121
|
-
* @returns {boolean} - Whether the value is in the format YYYY-MM-DD hh:mm:ss, 値がYYYY-MM-DD hh:mm:ss形式であるかどうか
|
|
122
|
-
*/
|
|
123
|
-
static isHHMM(value: any) {
|
|
124
|
-
if (typeof value !== 'string') {
|
|
125
|
-
return false;
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
const pattern = new RegExp('^(?:[01]\\d|2[0-3]):[0-5]\\d$');
|
|
129
|
-
return pattern.test(value);
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
/**
|
|
133
|
-
* Validates if the given value is in the format HH:MM:SS
|
|
134
|
-
* 与えられた値がHH:MM:SS形式であるかどうかを検証します
|
|
135
|
-
* @param value - The value to be validated, 検証する値
|
|
136
|
-
* @returns {boolean} - Whether the value is in the format HH:MM:SS, 値がHH:MM:SS形式であるかどうか
|
|
137
|
-
*/
|
|
138
|
-
static isHHMMSS(value: any) {
|
|
139
|
-
if (typeof value !== 'string') {
|
|
140
|
-
return false;
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
const pattern = new RegExp('^(?:[01]\\d|2[0-3]):[0-5]\\d:[0-5]\\d$');
|
|
144
|
-
return pattern.test(value);
|
|
145
|
-
}
|
|
146
|
-
}
|