pg-mvc-service 2.1.0 → 2.1.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/dist/PoolManager.d.ts +7 -0
- package/dist/PoolManager.d.ts.map +1 -0
- package/dist/Service.d.ts +82 -0
- package/dist/Service.d.ts.map +1 -0
- 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 +35 -0
- package/dist/clients/AwsS3Client.d.ts.map +1 -0
- package/dist/clients/Base64Client.d.ts +31 -0
- package/dist/clients/Base64Client.d.ts.map +1 -0
- 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 +35 -0
- package/dist/cron/BaseCron.d.ts.map +1 -0
- 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} +2 -6
- package/dist/index.d.ts.map +1 -0
- 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 -1
- 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
|
@@ -1,151 +0,0 @@
|
|
|
1
|
-
import { Pool } from "pg";
|
|
2
|
-
import { MigrateTable } from "./MigrateTable";
|
|
3
|
-
|
|
4
|
-
export const migrate = async (migrates: Array<MigrateTable>, poolParam: {
|
|
5
|
-
host: string, user: string, dbName: string, password: string, port?: number, isSsl?: boolean
|
|
6
|
-
}): Promise<void> => {
|
|
7
|
-
|
|
8
|
-
const pool = new Pool({
|
|
9
|
-
user: poolParam.user,
|
|
10
|
-
password: poolParam.password,
|
|
11
|
-
host: poolParam.host,
|
|
12
|
-
database: poolParam.dbName,
|
|
13
|
-
port: poolParam.port ?? 5432,
|
|
14
|
-
ssl: poolParam.isSsl === true ? {
|
|
15
|
-
rejectUnauthorized: false
|
|
16
|
-
} : false
|
|
17
|
-
});
|
|
18
|
-
|
|
19
|
-
// create migration table
|
|
20
|
-
try {
|
|
21
|
-
if (await isExistMigrationTable(pool) == false) {
|
|
22
|
-
const sql = `
|
|
23
|
-
CREATE TABLE migrations (
|
|
24
|
-
migration_number int,
|
|
25
|
-
script_file VARCHAR(50),
|
|
26
|
-
rollback_script VARCHAR(5000),
|
|
27
|
-
create_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
|
|
28
|
-
);`;
|
|
29
|
-
await pool.query(sql);
|
|
30
|
-
}
|
|
31
|
-
} catch (ex) {
|
|
32
|
-
console.error('An error occurred related to the Migrate table:', ex);
|
|
33
|
-
await pool.end();
|
|
34
|
-
throw ex;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
const client = await pool.connect();
|
|
38
|
-
try {
|
|
39
|
-
client.query('BEGIN');
|
|
40
|
-
|
|
41
|
-
const datas = await getMigrations(pool);
|
|
42
|
-
let maxNumber = datas.maxNumber;
|
|
43
|
-
for (const migrate of migrates) {
|
|
44
|
-
const className = migrate.constructor.name;
|
|
45
|
-
if (datas.datas.filter(data => data.script_file === className).length > 0) {
|
|
46
|
-
console.log(`Already executed: ${className}`);
|
|
47
|
-
continue;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
await client.query(migrate.MigrateSql);
|
|
51
|
-
|
|
52
|
-
const grantSql = migrate.AddGrantSql;
|
|
53
|
-
if (grantSql !== null) {
|
|
54
|
-
await client.query(grantSql);
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
const migrateInsertSql = `
|
|
58
|
-
INSERT INTO migrations
|
|
59
|
-
(migration_number, script_file, rollback_script)
|
|
60
|
-
VALUES (${maxNumber + 1}, '${className}', '${migrate.RollbackSql}');
|
|
61
|
-
`;
|
|
62
|
-
maxNumber++;
|
|
63
|
-
|
|
64
|
-
await client.query(migrateInsertSql);
|
|
65
|
-
|
|
66
|
-
console.log(`Execution completed: ${className}`);
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
await client.query('COMMIT');
|
|
70
|
-
|
|
71
|
-
console.log('Migration completed');
|
|
72
|
-
} catch (ex) {
|
|
73
|
-
await client.query('ROLLBACK');
|
|
74
|
-
console.log('Migration failed.', ex);
|
|
75
|
-
} finally {
|
|
76
|
-
client.release();
|
|
77
|
-
await pool.end();
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
export const rollback = async (toNumber: number, poolParam: {
|
|
82
|
-
host: string, user: string, dbName: string, password: string, port?: number, isSsl?: boolean
|
|
83
|
-
}): Promise<void> => {
|
|
84
|
-
|
|
85
|
-
const pool = new Pool({
|
|
86
|
-
user: poolParam.user,
|
|
87
|
-
password: poolParam.password,
|
|
88
|
-
host: poolParam.host,
|
|
89
|
-
database: poolParam.dbName,
|
|
90
|
-
port: poolParam.port ?? 5432,
|
|
91
|
-
ssl: poolParam.isSsl === true ? {
|
|
92
|
-
rejectUnauthorized: false
|
|
93
|
-
} : false
|
|
94
|
-
});
|
|
95
|
-
|
|
96
|
-
try {
|
|
97
|
-
// If the migration table does not exist, there is no target for rollback, so do not perform it
|
|
98
|
-
if (await isExistMigrationTable(pool) == false) {
|
|
99
|
-
return;
|
|
100
|
-
}
|
|
101
|
-
} catch (ex) {
|
|
102
|
-
console.error('An error occurred related to the Migrate table:', ex);
|
|
103
|
-
await pool.end();
|
|
104
|
-
return;
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
const client = await pool.connect();
|
|
108
|
-
try {
|
|
109
|
-
await client.query('BEGIN');
|
|
110
|
-
|
|
111
|
-
const datas = await getMigrations(pool);
|
|
112
|
-
for (const data of datas.datas) {
|
|
113
|
-
if (data.migration_number < toNumber) {
|
|
114
|
-
break;
|
|
115
|
-
}
|
|
116
|
-
await client.query(data.rollback_script);
|
|
117
|
-
await client.query(`DELETE FROM migrations WHERE migration_number = ${data.migration_number}`);
|
|
118
|
-
|
|
119
|
-
console.log(`Execution completed: ${data.script_file}`);
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
await client.query('COMMIT');
|
|
123
|
-
|
|
124
|
-
console.log('Rollback completed');
|
|
125
|
-
} catch (ex) {
|
|
126
|
-
await client.query('ROLLBACK');
|
|
127
|
-
console.error('Rollback failed', ex);
|
|
128
|
-
} finally {
|
|
129
|
-
client.release();
|
|
130
|
-
await pool.end();
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
const isExistMigrationTable = async (pool: Pool) => {
|
|
135
|
-
const existMigrationTableSql = `
|
|
136
|
-
SELECT EXISTS (
|
|
137
|
-
SELECT FROM information_schema.tables
|
|
138
|
-
WHERE table_name = 'migrations'
|
|
139
|
-
);
|
|
140
|
-
`;
|
|
141
|
-
const res = await pool.query(existMigrationTableSql);
|
|
142
|
-
return res.rows[0].exists;
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
const getMigrations = async (pool: Pool): Promise<{datas: Array<{migration_number: number, script_file: string, rollback_script: string}>, maxNumber: number}> => {
|
|
146
|
-
const datas = await pool.query("SELECT * FROM migrations ORDER BY migration_number DESC;");
|
|
147
|
-
return {
|
|
148
|
-
maxNumber: datas.rows.reduce((max, data) => data.migration_number > max ? data.migration_number : max, 0),
|
|
149
|
-
datas: datas.rows
|
|
150
|
-
}
|
|
151
|
-
}
|
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
export class MigrateTable {
|
|
2
|
-
|
|
3
|
-
protected readonly migrateSql: string = '';
|
|
4
|
-
protected readonly rollbackSql: string = '';
|
|
5
|
-
protected readonly addGrantTables: Array<string> = [];
|
|
6
|
-
protected readonly user: string = '';
|
|
7
|
-
|
|
8
|
-
get MigrateSql(): string { return this.trimSpaceLineSql(this.migrateSql); }
|
|
9
|
-
get RollbackSql(): string { return this.trimSpaceLineSql(this.rollbackSql); }
|
|
10
|
-
get AddGrantSql(): string | null {
|
|
11
|
-
if ((this.user ?? "").trim() === "") {
|
|
12
|
-
return null;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
const tables = this.addGrantTables.filter(table => table.trim() !== '');
|
|
16
|
-
if (tables.length === 0) {
|
|
17
|
-
return null;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
let sql = "";
|
|
21
|
-
for (const table of tables) {
|
|
22
|
-
sql += `GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE public.${table} TO ${this.user};`;
|
|
23
|
-
}
|
|
24
|
-
return sql;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
constructor();
|
|
28
|
-
constructor(user: string);
|
|
29
|
-
constructor(user?: string) {
|
|
30
|
-
if (user !== undefined) {
|
|
31
|
-
this.user = user;
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
private trimSpaceLineSql(str: string) {
|
|
36
|
-
const splitLines = str.split('\n');
|
|
37
|
-
let sql = '';
|
|
38
|
-
for (let line of splitLines) {
|
|
39
|
-
line = line.replace(/\s+/g, ' ').trim(); // 複数のスペースを一つに置き換え
|
|
40
|
-
|
|
41
|
-
if (line.startsWith('--') && sql[sql.length - 1] != '\n') {
|
|
42
|
-
line = '\n' + line;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
if (line.length > 0) {
|
|
46
|
-
if (line.includes('--') === false) {
|
|
47
|
-
sql += line + ' ';
|
|
48
|
-
} else {
|
|
49
|
-
sql += line + '\n';
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
return sql;
|
|
55
|
-
}
|
|
56
|
-
}
|
|
@@ -1,102 +0,0 @@
|
|
|
1
|
-
import { TableModel } from "../TableModel";
|
|
2
|
-
import { TAggregateFuncType, TColumnInfo, TKeyFormat } from "../Type";
|
|
3
|
-
import StringUtil from "../../Utils/StringUtil";
|
|
4
|
-
|
|
5
|
-
export default class SelectExpression {
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* 指定されたカラム情報と関数を使用して、SQLのSELECT文を作成します。
|
|
9
|
-
* @param columnInfoType カラム情報を含むオブジェクト。
|
|
10
|
-
* @param func カラムに適用する関数名。nullの場合は関数を適用しません。
|
|
11
|
-
* @returns SQLのSELECT文の文字列。
|
|
12
|
-
*/
|
|
13
|
-
static create(columnInfo: TColumnInfo, func: TAggregateFuncType | null = null, alias: string | null = null, keyFormat: TKeyFormat = 'snake') : string {
|
|
14
|
-
|
|
15
|
-
const column = columnInfo.model.getColumn(columnInfo.name);
|
|
16
|
-
let select = ''
|
|
17
|
-
switch (column.type) {
|
|
18
|
-
case 'date':
|
|
19
|
-
select = this.createDateTime(columnInfo, 'date');
|
|
20
|
-
break;
|
|
21
|
-
case 'time':
|
|
22
|
-
select = this.createDateTime(columnInfo, 'time');
|
|
23
|
-
break;
|
|
24
|
-
case 'timestamp':
|
|
25
|
-
select = this.createDateTime(columnInfo, 'datetime');
|
|
26
|
-
break;
|
|
27
|
-
default:
|
|
28
|
-
select = column.expression;
|
|
29
|
-
break;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
let aliasName = alias ?? '';
|
|
33
|
-
if (func !== null) {
|
|
34
|
-
if (aliasName.trim() === '') {
|
|
35
|
-
const snakeAlias = func + '_' + columnInfo.name;
|
|
36
|
-
aliasName = keyFormat === 'snake' ? snakeAlias : StringUtil.formatFromSnakeToCamel(snakeAlias);
|
|
37
|
-
}
|
|
38
|
-
select = `${func}(${select})`;
|
|
39
|
-
switch (func) {
|
|
40
|
-
case 'sum':
|
|
41
|
-
case 'max':
|
|
42
|
-
case 'min':
|
|
43
|
-
case 'avg':
|
|
44
|
-
case 'count':
|
|
45
|
-
// なぜかStringで返却されるため、INTでキャスト
|
|
46
|
-
select = `CAST(${select} as INTEGER)`;
|
|
47
|
-
break;
|
|
48
|
-
default:
|
|
49
|
-
break;
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
if (aliasName.trim() === '') {
|
|
54
|
-
aliasName = keyFormat === 'snake' ? columnInfo.name : StringUtil.formatFromSnakeToCamel(columnInfo.name);
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
return `${select} as "${aliasName}"`;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* BaseModelからSELECTクエリを作成します。
|
|
62
|
-
* @param baseModel クエリを作成するためのBaseModelオブジェクト。
|
|
63
|
-
* @param isExcludeId trueの場合、idカラムを除外します。
|
|
64
|
-
* @param isExcludeSystemTime trueの場合、システム時間のカラムを除外します。
|
|
65
|
-
* @returns 作成されたSELECTクエリの文字列。
|
|
66
|
-
*/
|
|
67
|
-
static createFromModel(model: TableModel) {
|
|
68
|
-
const queries: Array<string> = [];
|
|
69
|
-
for (const key of Object.keys(model.Columns)) {
|
|
70
|
-
queries.push(this.create({model: model, name: key}));
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
return queries.join(',');
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
/**
|
|
77
|
-
* Converts the specified column to a SQL string format.
|
|
78
|
-
* 指定されたカラムをSQLの文字列形式に変換します。
|
|
79
|
-
* @param column - The column information or a string containing the column name.
|
|
80
|
-
* 変換するカラム情報またはカラム名を含む文字列。
|
|
81
|
-
* @param to - Specifies the target format. Either 'date', 'time', or 'datetime'.
|
|
82
|
-
* 変換先の形式を指定します。'date'、'time'、または'datetime'のいずれか。
|
|
83
|
-
* @returns The SQL string converted to the specified format.
|
|
84
|
-
* 指定された形式に変換されたSQLの文字列。
|
|
85
|
-
*/
|
|
86
|
-
public static createDateTime(column: TColumnInfo, to: 'date' | 'time' | 'datetime') {
|
|
87
|
-
const columnInfo = column.model.getColumn(column.name);
|
|
88
|
-
|
|
89
|
-
if (['date', 'time', 'timestamp'].includes(columnInfo.type) === false) {
|
|
90
|
-
return '';
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
switch (to) {
|
|
94
|
-
case 'date':
|
|
95
|
-
return `to_char(${columnInfo.expression}, 'YYYY-MM-DD')`;
|
|
96
|
-
case 'datetime':
|
|
97
|
-
return `to_char(${columnInfo.expression}, 'YYYY-MM-DD HH24:mi:ss')`;
|
|
98
|
-
case 'time':
|
|
99
|
-
return `to_char(${columnInfo.expression}, 'HH24:mi:ss')`;
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
}
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import { TableModel } from "../TableModel";
|
|
2
|
-
import { TQuery } from "../Type";
|
|
3
|
-
|
|
4
|
-
export default class UpdateExpression {
|
|
5
|
-
|
|
6
|
-
static createUpdateSet(model: TableModel, options: {[key: string]: any}): TQuery {
|
|
7
|
-
const expressions = [];
|
|
8
|
-
const vars = [];
|
|
9
|
-
for (const [key, value] of Object.entries(options)) {
|
|
10
|
-
if (value === undefined) {
|
|
11
|
-
throw new Error(`The update option ${key} is undefined.`);
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
const column = model.getColumn(key);
|
|
15
|
-
if (column.attribute === 'primary') {
|
|
16
|
-
throw new Error(`The primary key ${model.TableName}.${key} cannot be changed.`);
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
vars.push(value);
|
|
20
|
-
expressions.push(`${key} = $${vars.length}`);
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
return {
|
|
24
|
-
expression: `UPDATE ${model.TableName} SET ${expressions.join(',')}`,
|
|
25
|
-
vars: vars
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
}
|
|
@@ -1,354 +0,0 @@
|
|
|
1
|
-
import { TColumn, TColumnArrayType, TColumnType } from "../Type";
|
|
2
|
-
import DateTimeUtil from "../../Utils/DateTimeUtil";
|
|
3
|
-
import StringUtil from "../../Utils/StringUtil";
|
|
4
|
-
|
|
5
|
-
export default class ValidateValueUtil {
|
|
6
|
-
|
|
7
|
-
static validateId(columns: {[key: string]: TColumn}, id: any) {
|
|
8
|
-
if ('id' in columns === false) {
|
|
9
|
-
throw new Error("The 'id' is not set in Columns.");
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
const pkColumnsArray = Object.entries(columns).filter(([key, column]) => column.attribute === 'primary');
|
|
13
|
-
const pkColumns = Object.fromEntries(pkColumnsArray);
|
|
14
|
-
if ('id' in pkColumns === false) {
|
|
15
|
-
throw new Error("The 'id' is not set as a Primary Key.");
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
if (Object.keys(pkColumns).length > 1) {
|
|
19
|
-
throw new Error("This method cannot be used because there are other Primary Keys set besides 'id'.");
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
ValidateValueUtil.validateValue(pkColumns['id'], id);
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
static validateValue(column: TColumn, value: any) {
|
|
27
|
-
if (value === undefined) {
|
|
28
|
-
throw new Error(`The value is undefined.`);
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
if (value === null) {
|
|
32
|
-
if (column.attribute === 'nullable') {
|
|
33
|
-
return null;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
throw new Error(`The specified column does not allow null values. (${column.attribute})`);
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
// // 配列のチェック
|
|
40
|
-
// if (column.type.endsWith("[]")) {
|
|
41
|
-
// if (Array.isArray(value) === false) {
|
|
42
|
-
// throw new Error('The value must be an array.');
|
|
43
|
-
// }
|
|
44
|
-
|
|
45
|
-
// for (const v of value) {
|
|
46
|
-
// switch (column.type) {
|
|
47
|
-
// case "string[]":
|
|
48
|
-
// if (this.isErrorString(v)) {
|
|
49
|
-
// throw new Error('Please enter a value of type string.');
|
|
50
|
-
// }
|
|
51
|
-
// break;
|
|
52
|
-
// case "uuid[]":
|
|
53
|
-
// if (this.isErrorUUID(v)) {
|
|
54
|
-
// throw new Error('Please enter a value in UUID string format.');
|
|
55
|
-
// }
|
|
56
|
-
// break;
|
|
57
|
-
// case "date[]":
|
|
58
|
-
// if (this.isErrorDate(v)) {
|
|
59
|
-
// throw new Error('Please enter a valid date in "YYYY-MM-DD" or "YYYY-MM-DD hh:mi:ss" format or as a Date type.');
|
|
60
|
-
// }
|
|
61
|
-
// break;
|
|
62
|
-
// case "time[]":
|
|
63
|
-
// if (this.isErrorTime(v)) {
|
|
64
|
-
// throw new Error('Please enter a valid time in "hh:mi" or "hh:mi:ss" format.');
|
|
65
|
-
// }
|
|
66
|
-
// break;
|
|
67
|
-
// case "timestamp[]":
|
|
68
|
-
// if (this.isErrorTimestamp(v)) {
|
|
69
|
-
// throw new Error('Please enter a valid timestamp in "YYYY-MM-DD", "YYYY-MM-DD hh:mi:ss", or "YYYY-MM-DDThh:mi:ss" format or as a Date type.');
|
|
70
|
-
// }
|
|
71
|
-
// break;
|
|
72
|
-
// case "number[]":
|
|
73
|
-
// if (this.isErrorNumber(v)) {
|
|
74
|
-
// throw new Error('Please enter a value of type number or a string of half-width digits.');
|
|
75
|
-
// }
|
|
76
|
-
// break;
|
|
77
|
-
// case "bool[]":
|
|
78
|
-
// if (this.isErrorBool(v)) {
|
|
79
|
-
// throw new Error('Please enter a value of type bool, or a string "true" or "false", or a number 0 or 1.');
|
|
80
|
-
// }
|
|
81
|
-
// break;
|
|
82
|
-
// default:
|
|
83
|
-
// throw new Error(`The specified ColumnTypeEnum does not exist. (${column.type})`);
|
|
84
|
-
// }
|
|
85
|
-
|
|
86
|
-
// if (v === true) {
|
|
87
|
-
// return true;
|
|
88
|
-
// }
|
|
89
|
-
// }
|
|
90
|
-
|
|
91
|
-
// return false;
|
|
92
|
-
// }
|
|
93
|
-
|
|
94
|
-
switch (column.type) {
|
|
95
|
-
case "string":
|
|
96
|
-
if (this.isErrorString(value)) {
|
|
97
|
-
throw new Error('Please enter a value of type string or number.');
|
|
98
|
-
}
|
|
99
|
-
break;
|
|
100
|
-
case "uuid":
|
|
101
|
-
if (this.isErrorUUID(value)) {
|
|
102
|
-
throw new Error('Please enter a value in UUID string format.');
|
|
103
|
-
}
|
|
104
|
-
break;
|
|
105
|
-
case "date":
|
|
106
|
-
if (this.isErrorDate(value)) {
|
|
107
|
-
throw new Error('Please enter a valid date in "YYYY-MM-DD" or "YYYY-MM-DD hh:mi:ss" format or as a Date type.');
|
|
108
|
-
}
|
|
109
|
-
break;
|
|
110
|
-
case "time":
|
|
111
|
-
if (this.isErrorTime(value)) {
|
|
112
|
-
throw new Error('Please enter a valid time in "hh:mi" or "hh:mi:ss" format.');
|
|
113
|
-
}
|
|
114
|
-
break;
|
|
115
|
-
case "timestamp":
|
|
116
|
-
if (this.isErrorTimestamp(value)) {
|
|
117
|
-
throw new Error('Please enter a valid timestamp in "YYYY-MM-DD", "YYYY-MM-DD hh:mi:ss", or "YYYY-MM-DDThh:mi:ss" format or as a Date type.');
|
|
118
|
-
}
|
|
119
|
-
break;
|
|
120
|
-
case "integer":
|
|
121
|
-
if (this.isErrorNumber(value)) {
|
|
122
|
-
throw new Error('Please enter a value of type number or a string of half-width digits.');
|
|
123
|
-
}
|
|
124
|
-
break;
|
|
125
|
-
case "bool":
|
|
126
|
-
if (this.isErrorBool(value)) {
|
|
127
|
-
throw new Error('Please enter a value of type bool, or a string "true" or "false", or a number 0 or 1.');
|
|
128
|
-
}
|
|
129
|
-
break;
|
|
130
|
-
case "json":
|
|
131
|
-
case "jsonb":
|
|
132
|
-
if (this.isErrorJson(value)) {
|
|
133
|
-
throw new Error('Please enter a value as an Object or JSON string.');
|
|
134
|
-
}
|
|
135
|
-
break;
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
static isErrorValue(columnType: TColumnType | TColumnArrayType, value: any) {
|
|
140
|
-
|
|
141
|
-
// 配列のチェック
|
|
142
|
-
if (columnType.endsWith("[]")) {
|
|
143
|
-
if (Array.isArray(value) === false) {
|
|
144
|
-
return true;
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
for (const v of value) {
|
|
148
|
-
let isError = false;
|
|
149
|
-
switch (columnType) {
|
|
150
|
-
case "string[]":
|
|
151
|
-
isError = this.isErrorString(v);
|
|
152
|
-
break;
|
|
153
|
-
case "uuid[]":
|
|
154
|
-
isError = this.isErrorUUID(v);
|
|
155
|
-
break;
|
|
156
|
-
case "date[]":
|
|
157
|
-
isError = this.isErrorDate(v);
|
|
158
|
-
break;
|
|
159
|
-
case "time[]":
|
|
160
|
-
isError = this.isErrorTime(v);
|
|
161
|
-
break;
|
|
162
|
-
case "timestamp[]":
|
|
163
|
-
isError = this.isErrorTimestamp(v);
|
|
164
|
-
break;
|
|
165
|
-
case "integer[]": // TODO: ここ最大最小のチェックもしないと
|
|
166
|
-
case "real[]": // TODO: ここ最大最小 + 桁数のチェックもしないと
|
|
167
|
-
isError = this.isErrorNumber(v);
|
|
168
|
-
break;
|
|
169
|
-
case "bool[]":
|
|
170
|
-
isError = this.isErrorBool(v);
|
|
171
|
-
break;
|
|
172
|
-
case "json[]":
|
|
173
|
-
case "jsonb[]":
|
|
174
|
-
isError = this.isErrorJson(v);
|
|
175
|
-
break;
|
|
176
|
-
default:
|
|
177
|
-
throw new Error(`The specified ColumnTypeEnum does not exist. (${columnType})`);
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
if (isError) {
|
|
181
|
-
return true;
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
return false;
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
switch (columnType) {
|
|
189
|
-
case "string":
|
|
190
|
-
return this.isErrorString(value);
|
|
191
|
-
case "uuid":
|
|
192
|
-
return this.isErrorUUID(value);
|
|
193
|
-
case "date":
|
|
194
|
-
return this.isErrorDate(value);
|
|
195
|
-
case "time":
|
|
196
|
-
return this.isErrorTime(value);
|
|
197
|
-
case "timestamp":
|
|
198
|
-
return this.isErrorTimestamp(value);
|
|
199
|
-
case "integer": // TODO: ここ最大最小のチェックもしないと
|
|
200
|
-
case "real": // TODO: ここ最大最小 + 桁数のチェックもしないと
|
|
201
|
-
return this.isErrorNumber(value);
|
|
202
|
-
case "bool":
|
|
203
|
-
return this.isErrorBool(value);
|
|
204
|
-
case "json":
|
|
205
|
-
case "jsonb":
|
|
206
|
-
return this.isErrorJson(value);
|
|
207
|
-
default:
|
|
208
|
-
throw new Error(`The specified ColumnTypeEnum does not exist. (${columnType})`);
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
static isErrorString(value: any): boolean {
|
|
213
|
-
if (typeof(value) == 'string' || typeof(value) == 'number') {
|
|
214
|
-
return false;
|
|
215
|
-
}
|
|
216
|
-
return true;
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
static isErrorInteger(value: any): boolean {
|
|
220
|
-
let numberValue: number;
|
|
221
|
-
if (typeof value === 'string') {
|
|
222
|
-
if (value.trim() === "" || isNaN(Number(value))) {
|
|
223
|
-
return true;
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
numberValue = Number(value);
|
|
227
|
-
} else if (typeof value === 'number') {
|
|
228
|
-
numberValue = value;
|
|
229
|
-
} else {
|
|
230
|
-
return true;
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
if (Number.isInteger(numberValue) === false) {
|
|
234
|
-
return true;
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
if (numberValue < -2147483648 || numberValue > 2147483647) {
|
|
238
|
-
return true;
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
return false;
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
static isErrorReal(value: any): boolean {
|
|
245
|
-
let numberValue: number;
|
|
246
|
-
if (typeof value === 'string') {
|
|
247
|
-
if (value.trim() === "" || isNaN(Number(value))) {
|
|
248
|
-
return true;
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
numberValue = Number(value);
|
|
252
|
-
} else if (typeof value === 'number') {
|
|
253
|
-
numberValue = value;
|
|
254
|
-
} else {
|
|
255
|
-
return true;
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
// 特殊値チェック
|
|
259
|
-
if (isFinite(numberValue) === false) {
|
|
260
|
-
return true;
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
// 範囲チェック(real型の範囲)
|
|
264
|
-
if (numberValue < -3.4e38 || numberValue > 3.4e38) {
|
|
265
|
-
return true;
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
return false;
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
static isErrorNumber(value: any): boolean {
|
|
272
|
-
if (typeof value === 'string') {
|
|
273
|
-
if (value.trim() === "" || isNaN(Number(value))) {
|
|
274
|
-
return true;
|
|
275
|
-
}
|
|
276
|
-
return false;
|
|
277
|
-
} else if (typeof value === 'number') {
|
|
278
|
-
return false;
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
return true;
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
static isErrorBool(value: any): boolean {
|
|
285
|
-
switch (typeof(value)) {
|
|
286
|
-
case 'string':
|
|
287
|
-
return value !== 'true' && value !== 'false';
|
|
288
|
-
case 'number':
|
|
289
|
-
return value !== 0 && value !== 1;
|
|
290
|
-
case 'boolean':
|
|
291
|
-
return false;
|
|
292
|
-
default:
|
|
293
|
-
return true;
|
|
294
|
-
}
|
|
295
|
-
}
|
|
296
|
-
|
|
297
|
-
static isErrorUUID(value: any) {
|
|
298
|
-
return StringUtil.isUUID(value) === false;
|
|
299
|
-
}
|
|
300
|
-
|
|
301
|
-
static isErrorDate(value: any): boolean {
|
|
302
|
-
if (value instanceof Date) {
|
|
303
|
-
return false;
|
|
304
|
-
} else if (DateTimeUtil.isYYYYMMDD(value)) {
|
|
305
|
-
return false;
|
|
306
|
-
} else if (DateTimeUtil.isYYYYMMDDhhmiss(value)) {
|
|
307
|
-
return false;
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
return true;
|
|
311
|
-
}
|
|
312
|
-
|
|
313
|
-
static isErrorTimestamp(value: any): boolean {
|
|
314
|
-
if (value instanceof Date) {
|
|
315
|
-
return false
|
|
316
|
-
} else if (DateTimeUtil.isYYYYMMDD(`${value}`)) {
|
|
317
|
-
return false;
|
|
318
|
-
} else if (DateTimeUtil.isYYYYMMDDhhmiss(value)) {
|
|
319
|
-
return false;
|
|
320
|
-
}
|
|
321
|
-
|
|
322
|
-
return true;
|
|
323
|
-
}
|
|
324
|
-
|
|
325
|
-
static isErrorTime(value: any): boolean {
|
|
326
|
-
if (value instanceof Date) {
|
|
327
|
-
return false
|
|
328
|
-
}
|
|
329
|
-
if (DateTimeUtil.isHHMMSS(value)) {
|
|
330
|
-
return false;
|
|
331
|
-
} else if (DateTimeUtil.isHHMM(value)) {
|
|
332
|
-
return false;
|
|
333
|
-
}
|
|
334
|
-
|
|
335
|
-
return true;
|
|
336
|
-
}
|
|
337
|
-
|
|
338
|
-
static isErrorJson(value: any): boolean {
|
|
339
|
-
if (typeof value === 'object' && value !== null && Array.isArray(value) === false) {
|
|
340
|
-
return false;
|
|
341
|
-
}
|
|
342
|
-
|
|
343
|
-
if (typeof value === 'string') {
|
|
344
|
-
try {
|
|
345
|
-
JSON.parse(value);
|
|
346
|
-
return false;
|
|
347
|
-
} catch {
|
|
348
|
-
return true;
|
|
349
|
-
}
|
|
350
|
-
}
|
|
351
|
-
|
|
352
|
-
return true;
|
|
353
|
-
}
|
|
354
|
-
}
|