pg-mvc-service 1.0.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 +1 -0
- package/dist/PoolManager.js +57 -0
- package/dist/Service.js +257 -0
- package/dist/clients/AwsS3Client.js +249 -0
- package/dist/clients/Base64Client.js +153 -0
- package/dist/clients/EncryptClient.js +85 -0
- package/dist/clients/StringClient.js +13 -0
- package/dist/documents/Swagger.js +94 -0
- package/dist/exceptions/Exception.js +53 -0
- package/dist/index.js +16 -0
- package/dist/models/MigrateDatabase.js +138 -0
- package/dist/models/MigrateRollback.js +146 -0
- package/dist/models/MigrateTable.js +51 -0
- package/dist/models/SqlUtils/SelectExpression.js +92 -0
- package/dist/models/SqlUtils/ValidateValueUtil.js +250 -0
- package/dist/models/SqlUtils/WhereExpression.js +256 -0
- package/dist/models/TableDoc.js +353 -0
- package/dist/models/TableModel.js +636 -0
- package/dist/models/Type.js +2 -0
- package/dist/models/Utils/DateTimeUtil.js +134 -0
- package/dist/models/Utils/NumberUtil.js +28 -0
- package/dist/models/Utils/StringUtil.js +31 -0
- package/dist/models/ValidateClient.js +164 -0
- package/dist/models/index.js +14 -0
- package/dist/reqestResponse/ReqResType.js +196 -0
- package/dist/reqestResponse/RequestType.js +742 -0
- package/dist/reqestResponse/ResponseType.js +380 -0
- package/index.d.ts +306 -0
- package/package.json +36 -0
- package/src/PoolManager.ts +48 -0
- package/src/Service.ts +251 -0
- package/src/clients/AwsS3Client.ts +229 -0
- package/src/clients/Base64Client.ts +155 -0
- package/src/clients/EncryptClient.ts +100 -0
- package/src/clients/StringClient.ts +14 -0
- package/src/documents/Swagger.ts +111 -0
- package/src/exceptions/Exception.ts +54 -0
- package/src/index.ts +7 -0
- package/src/models/MigrateDatabase.ts +135 -0
- package/src/models/MigrateRollback.ts +151 -0
- package/src/models/MigrateTable.ts +56 -0
- package/src/models/SqlUtils/SelectExpression.ts +97 -0
- package/src/models/SqlUtils/ValidateValueUtil.ts +270 -0
- package/src/models/SqlUtils/WhereExpression.ts +286 -0
- package/src/models/TableDoc.ts +360 -0
- package/src/models/TableModel.ts +713 -0
- package/src/models/Type.ts +59 -0
- package/src/models/Utils/DateTimeUtil.ts +146 -0
- package/src/models/Utils/NumberUtil.ts +23 -0
- package/src/models/Utils/StringUtil.ts +33 -0
- package/src/models/ValidateClient.ts +182 -0
- package/src/models/index.ts +7 -0
- package/src/reqestResponse/ReqResType.ts +242 -0
- package/src/reqestResponse/RequestType.ts +851 -0
- package/src/reqestResponse/ResponseType.ts +418 -0
- package/tsconfig.json +14 -0
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.rollback = exports.migrate = void 0;
|
|
13
|
+
const pg_1 = require("pg");
|
|
14
|
+
const migrate = (migrates, poolParam) => __awaiter(void 0, void 0, void 0, function* () {
|
|
15
|
+
var _a;
|
|
16
|
+
const pool = new pg_1.Pool({
|
|
17
|
+
user: poolParam.user,
|
|
18
|
+
password: poolParam.password,
|
|
19
|
+
host: poolParam.host,
|
|
20
|
+
database: poolParam.dbName,
|
|
21
|
+
port: (_a = poolParam.port) !== null && _a !== void 0 ? _a : 5432,
|
|
22
|
+
ssl: poolParam.isSsl === true ? {
|
|
23
|
+
rejectUnauthorized: false
|
|
24
|
+
} : false
|
|
25
|
+
});
|
|
26
|
+
// create migration table
|
|
27
|
+
try {
|
|
28
|
+
if ((yield isExistMigrationTable(pool)) == false) {
|
|
29
|
+
const sql = `
|
|
30
|
+
CREATE TABLE migrations (
|
|
31
|
+
migration_number int,
|
|
32
|
+
script_file VARCHAR(50),
|
|
33
|
+
rollback_script VARCHAR(5000),
|
|
34
|
+
create_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
|
|
35
|
+
);`;
|
|
36
|
+
yield pool.query(sql);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
catch (ex) {
|
|
40
|
+
console.error('An error occurred related to the Migrate table:', ex);
|
|
41
|
+
yield pool.end();
|
|
42
|
+
throw ex;
|
|
43
|
+
}
|
|
44
|
+
const client = yield pool.connect();
|
|
45
|
+
try {
|
|
46
|
+
client.query('BEGIN');
|
|
47
|
+
const datas = yield getMigrations(pool);
|
|
48
|
+
let maxNumber = datas.maxNumber;
|
|
49
|
+
for (const migrate of migrates) {
|
|
50
|
+
const className = migrate.constructor.name;
|
|
51
|
+
if (datas.datas.filter(data => data.script_file === className).length > 0) {
|
|
52
|
+
console.log(`Already executed: ${className}`);
|
|
53
|
+
continue;
|
|
54
|
+
}
|
|
55
|
+
yield client.query(migrate.MigrateSql);
|
|
56
|
+
const grantSql = migrate.AddGrantSql;
|
|
57
|
+
if (grantSql !== null) {
|
|
58
|
+
yield client.query(grantSql);
|
|
59
|
+
}
|
|
60
|
+
const migrateInsertSql = `
|
|
61
|
+
INSERT INTO migrations
|
|
62
|
+
(migration_number, script_file, rollback_script)
|
|
63
|
+
VALUES (${maxNumber + 1}, '${className}', '${migrate.RollbackSql}');
|
|
64
|
+
`;
|
|
65
|
+
maxNumber++;
|
|
66
|
+
yield client.query(migrateInsertSql);
|
|
67
|
+
console.log(`Execution completed: ${className}`);
|
|
68
|
+
}
|
|
69
|
+
yield client.query('COMMIT');
|
|
70
|
+
console.log('Migration completed');
|
|
71
|
+
}
|
|
72
|
+
catch (ex) {
|
|
73
|
+
yield client.query('ROLLBACK');
|
|
74
|
+
console.log('Migration failed.', ex);
|
|
75
|
+
}
|
|
76
|
+
finally {
|
|
77
|
+
client.release();
|
|
78
|
+
yield pool.end();
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
exports.migrate = migrate;
|
|
82
|
+
const rollback = (toNumber, poolParam) => __awaiter(void 0, void 0, void 0, function* () {
|
|
83
|
+
var _a;
|
|
84
|
+
const pool = new pg_1.Pool({
|
|
85
|
+
user: poolParam.user,
|
|
86
|
+
password: poolParam.password,
|
|
87
|
+
host: poolParam.host,
|
|
88
|
+
database: poolParam.dbName,
|
|
89
|
+
port: (_a = poolParam.port) !== null && _a !== void 0 ? _a : 5432,
|
|
90
|
+
ssl: poolParam.isSsl === true ? {
|
|
91
|
+
rejectUnauthorized: false
|
|
92
|
+
} : false
|
|
93
|
+
});
|
|
94
|
+
try {
|
|
95
|
+
// If the migration table does not exist, there is no target for rollback, so do not perform it
|
|
96
|
+
if ((yield isExistMigrationTable(pool)) == false) {
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
catch (ex) {
|
|
101
|
+
console.error('An error occurred related to the Migrate table:', ex);
|
|
102
|
+
yield pool.end();
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
const client = yield pool.connect();
|
|
106
|
+
try {
|
|
107
|
+
yield client.query('BEGIN');
|
|
108
|
+
const datas = yield getMigrations(pool);
|
|
109
|
+
for (const data of datas.datas) {
|
|
110
|
+
if (data.migration_number < toNumber) {
|
|
111
|
+
break;
|
|
112
|
+
}
|
|
113
|
+
yield client.query(data.rollback_script);
|
|
114
|
+
yield client.query(`DELETE FROM migrations WHERE migration_number = ${data.migration_number}`);
|
|
115
|
+
console.log(`Execution completed: ${data.script_file}`);
|
|
116
|
+
}
|
|
117
|
+
yield client.query('COMMIT');
|
|
118
|
+
console.log('Rollback completed');
|
|
119
|
+
}
|
|
120
|
+
catch (ex) {
|
|
121
|
+
yield client.query('ROLLBACK');
|
|
122
|
+
console.error('Rollback failed', ex);
|
|
123
|
+
}
|
|
124
|
+
finally {
|
|
125
|
+
client.release();
|
|
126
|
+
yield pool.end();
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
exports.rollback = rollback;
|
|
130
|
+
const isExistMigrationTable = (pool) => __awaiter(void 0, void 0, void 0, function* () {
|
|
131
|
+
const existMigrationTableSql = `
|
|
132
|
+
SELECT EXISTS (
|
|
133
|
+
SELECT FROM information_schema.tables
|
|
134
|
+
WHERE table_name = 'migrations'
|
|
135
|
+
);
|
|
136
|
+
`;
|
|
137
|
+
const res = yield pool.query(existMigrationTableSql);
|
|
138
|
+
return res.rows[0].exists;
|
|
139
|
+
});
|
|
140
|
+
const getMigrations = (pool) => __awaiter(void 0, void 0, void 0, function* () {
|
|
141
|
+
const datas = yield pool.query("SELECT * FROM migrations ORDER BY migration_number DESC;");
|
|
142
|
+
return {
|
|
143
|
+
maxNumber: datas.rows.reduce((max, data) => data.migration_number > max ? data.migration_number : max, 0),
|
|
144
|
+
datas: datas.rows
|
|
145
|
+
};
|
|
146
|
+
});
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MigrateTable = void 0;
|
|
4
|
+
class MigrateTable {
|
|
5
|
+
get MigrateSql() { return this.trimSpaceLineSql(this.migrateSql); }
|
|
6
|
+
get RollbackSql() { return this.trimSpaceLineSql(this.rollbackSql); }
|
|
7
|
+
get AddGrantSql() {
|
|
8
|
+
var _a;
|
|
9
|
+
if (((_a = this.user) !== null && _a !== void 0 ? _a : "").trim() === "") {
|
|
10
|
+
return null;
|
|
11
|
+
}
|
|
12
|
+
const tables = this.addGrantTables.filter(table => table.trim() !== '');
|
|
13
|
+
if (tables.length === 0) {
|
|
14
|
+
return null;
|
|
15
|
+
}
|
|
16
|
+
let sql = "";
|
|
17
|
+
for (const table of tables) {
|
|
18
|
+
sql += `GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE public.${table} TO ${this.user};`;
|
|
19
|
+
}
|
|
20
|
+
return sql;
|
|
21
|
+
}
|
|
22
|
+
constructor(user) {
|
|
23
|
+
this.migrateSql = '';
|
|
24
|
+
this.rollbackSql = '';
|
|
25
|
+
this.addGrantTables = [];
|
|
26
|
+
this.user = '';
|
|
27
|
+
if (user !== undefined) {
|
|
28
|
+
this.user = user;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
trimSpaceLineSql(str) {
|
|
32
|
+
const splitLines = str.split('\n');
|
|
33
|
+
let sql = '';
|
|
34
|
+
for (let line of splitLines) {
|
|
35
|
+
line = line.replace(/\s+/g, ' ').trim(); // 複数のスペースを一つに置き換え
|
|
36
|
+
if (line.startsWith('--') && sql[sql.length - 1] != '\n') {
|
|
37
|
+
line = '\n' + line;
|
|
38
|
+
}
|
|
39
|
+
if (line.length > 0) {
|
|
40
|
+
if (line.includes('--') === false) {
|
|
41
|
+
sql += line + ' ';
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
sql += line + '\n';
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return sql;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
exports.MigrateTable = MigrateTable;
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const StringUtil_1 = __importDefault(require("../Utils/StringUtil"));
|
|
7
|
+
class SelectExpression {
|
|
8
|
+
/**
|
|
9
|
+
* 指定されたカラム情報と関数を使用して、SQLのSELECT文を作成します。
|
|
10
|
+
* @param columnInfoType カラム情報を含むオブジェクト。
|
|
11
|
+
* @param func カラムに適用する関数名。nullの場合は関数を適用しません。
|
|
12
|
+
* @returns SQLのSELECT文の文字列。
|
|
13
|
+
*/
|
|
14
|
+
static create(columnInfo, func = null, alias = null, keyFormat = 'snake') {
|
|
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
|
+
let aliasName = alias !== null && alias !== void 0 ? alias : '';
|
|
32
|
+
if (func !== null) {
|
|
33
|
+
if (aliasName.trim() === '') {
|
|
34
|
+
const snakeAlias = func + '_' + columnInfo.name;
|
|
35
|
+
aliasName = keyFormat === 'snake' ? snakeAlias : StringUtil_1.default.formatFromSnakeToCamel(snakeAlias);
|
|
36
|
+
}
|
|
37
|
+
select = `${func}(${select})`;
|
|
38
|
+
switch (func) {
|
|
39
|
+
case 'sum':
|
|
40
|
+
case 'max':
|
|
41
|
+
case 'min':
|
|
42
|
+
case 'avg':
|
|
43
|
+
case 'count':
|
|
44
|
+
// なぜかStringで返却されるため、INTでキャスト
|
|
45
|
+
select = `CAST(${select} as INTEGER)`;
|
|
46
|
+
break;
|
|
47
|
+
default:
|
|
48
|
+
break;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
if (aliasName.trim() === '') {
|
|
52
|
+
aliasName = keyFormat === 'snake' ? columnInfo.name : StringUtil_1.default.formatFromSnakeToCamel(columnInfo.name);
|
|
53
|
+
}
|
|
54
|
+
return `${select} as "${aliasName}"`;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* BaseModelからSELECTクエリを作成します。
|
|
58
|
+
* @param baseModel クエリを作成するためのBaseModelオブジェクト。
|
|
59
|
+
* @param isExcludeId trueの場合、idカラムを除外します。
|
|
60
|
+
* @param isExcludeSystemTime trueの場合、システム時間のカラムを除外します。
|
|
61
|
+
* @returns 作成されたSELECTクエリの文字列。
|
|
62
|
+
*/
|
|
63
|
+
static createFromModel(model) {
|
|
64
|
+
const queries = [];
|
|
65
|
+
for (const key of Object.keys(model.Columns)) {
|
|
66
|
+
queries.push(this.create({ model: model, name: key }));
|
|
67
|
+
}
|
|
68
|
+
return queries.join(',');
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Converts the specified column to a SQL string format.
|
|
72
|
+
* 指定されたカラムをSQLの文字列形式に変換します。
|
|
73
|
+
* @param column - The column information or a string containing the column name.
|
|
74
|
+
* 変換するカラム情報またはカラム名を含む文字列。
|
|
75
|
+
* @param to - Specifies the target format. Either 'date', 'time', or 'datetime'.
|
|
76
|
+
* 変換先の形式を指定します。'date'、'time'、または'datetime'のいずれか。
|
|
77
|
+
* @returns The SQL string converted to the specified format.
|
|
78
|
+
* 指定された形式に変換されたSQLの文字列。
|
|
79
|
+
*/
|
|
80
|
+
static createDateTime(column, to) {
|
|
81
|
+
const columnQuery = typeof column === 'string' ? column : column.model.getColumn(column.name).expression;
|
|
82
|
+
switch (to) {
|
|
83
|
+
case 'date':
|
|
84
|
+
return `to_char(${columnQuery}, 'YYYY-MM-DD')`;
|
|
85
|
+
case 'datetime':
|
|
86
|
+
return `to_char(${columnQuery}, 'YYYY-MM-DD HH24:mi:ss')`;
|
|
87
|
+
case 'time':
|
|
88
|
+
return `to_char(${columnQuery}, 'HH24:mi:ss')`;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
exports.default = SelectExpression;
|
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const DateTimeUtil_1 = __importDefault(require("../Utils/DateTimeUtil"));
|
|
7
|
+
const StringUtil_1 = __importDefault(require("../Utils/StringUtil"));
|
|
8
|
+
class ValidateValueUtil {
|
|
9
|
+
static validateId(columns, id) {
|
|
10
|
+
if ('id' in columns === false) {
|
|
11
|
+
throw new Error("The 'id' is not set in Columns.");
|
|
12
|
+
}
|
|
13
|
+
const pkColumnsArray = Object.entries(columns).filter(([key, column]) => column.attribute === 'primary');
|
|
14
|
+
const pkColumns = Object.fromEntries(pkColumnsArray);
|
|
15
|
+
if ('id' in pkColumns === false) {
|
|
16
|
+
throw new Error("The 'id' is not set as a Primary Key.");
|
|
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
|
+
ValidateValueUtil.validateValue(pkColumns['id'], id);
|
|
22
|
+
}
|
|
23
|
+
static validateValue(column, value) {
|
|
24
|
+
if (value === undefined) {
|
|
25
|
+
throw new Error(`The value is undefined.`);
|
|
26
|
+
}
|
|
27
|
+
if (value === null) {
|
|
28
|
+
if (column.attribute === 'nullable') {
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
31
|
+
throw new Error(`The specified column does not allow null values. (${column.attribute})`);
|
|
32
|
+
}
|
|
33
|
+
// // 配列のチェック
|
|
34
|
+
// if (column.type.endsWith("[]")) {
|
|
35
|
+
// if (Array.isArray(value) === false) {
|
|
36
|
+
// throw new Error('The value must be an array.');
|
|
37
|
+
// }
|
|
38
|
+
// for (const v of value) {
|
|
39
|
+
// switch (column.type) {
|
|
40
|
+
// case "string[]":
|
|
41
|
+
// if (this.isErrorString(v)) {
|
|
42
|
+
// throw new Error('Please enter a value of type string.');
|
|
43
|
+
// }
|
|
44
|
+
// break;
|
|
45
|
+
// case "uuid[]":
|
|
46
|
+
// if (this.isErrorUUID(v)) {
|
|
47
|
+
// throw new Error('Please enter a value in UUID string format.');
|
|
48
|
+
// }
|
|
49
|
+
// break;
|
|
50
|
+
// case "date[]":
|
|
51
|
+
// if (this.isErrorDate(v)) {
|
|
52
|
+
// 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.');
|
|
53
|
+
// }
|
|
54
|
+
// break;
|
|
55
|
+
// case "time[]":
|
|
56
|
+
// if (this.isErrorTime(v)) {
|
|
57
|
+
// throw new Error('Please enter a valid time in "hh:mi" or "hh:mi:ss" format.');
|
|
58
|
+
// }
|
|
59
|
+
// break;
|
|
60
|
+
// case "timestamp[]":
|
|
61
|
+
// if (this.isErrorTimestamp(v)) {
|
|
62
|
+
// 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.');
|
|
63
|
+
// }
|
|
64
|
+
// break;
|
|
65
|
+
// case "number[]":
|
|
66
|
+
// if (this.isErrorNumber(v)) {
|
|
67
|
+
// throw new Error('Please enter a value of type number or a string of half-width digits.');
|
|
68
|
+
// }
|
|
69
|
+
// break;
|
|
70
|
+
// case "bool[]":
|
|
71
|
+
// if (this.isErrorBool(v)) {
|
|
72
|
+
// throw new Error('Please enter a value of type bool, or a string "true" or "false", or a number 0 or 1.');
|
|
73
|
+
// }
|
|
74
|
+
// break;
|
|
75
|
+
// default:
|
|
76
|
+
// throw new Error(`The specified ColumnTypeEnum does not exist. (${column.type})`);
|
|
77
|
+
// }
|
|
78
|
+
// if (v === true) {
|
|
79
|
+
// return true;
|
|
80
|
+
// }
|
|
81
|
+
// }
|
|
82
|
+
// return false;
|
|
83
|
+
// }
|
|
84
|
+
switch (column.type) {
|
|
85
|
+
case "string":
|
|
86
|
+
if (this.isErrorString(value)) {
|
|
87
|
+
throw new Error('Please enter a value of type string or number.');
|
|
88
|
+
}
|
|
89
|
+
break;
|
|
90
|
+
case "uuid":
|
|
91
|
+
if (this.isErrorUUID(value)) {
|
|
92
|
+
throw new Error('Please enter a value in UUID string format.');
|
|
93
|
+
}
|
|
94
|
+
break;
|
|
95
|
+
case "date":
|
|
96
|
+
if (this.isErrorDate(value)) {
|
|
97
|
+
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.');
|
|
98
|
+
}
|
|
99
|
+
break;
|
|
100
|
+
case "time":
|
|
101
|
+
if (this.isErrorTime(value)) {
|
|
102
|
+
throw new Error('Please enter a valid time in "hh:mi" or "hh:mi:ss" format.');
|
|
103
|
+
}
|
|
104
|
+
break;
|
|
105
|
+
case "timestamp":
|
|
106
|
+
if (this.isErrorTimestamp(value)) {
|
|
107
|
+
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.');
|
|
108
|
+
}
|
|
109
|
+
break;
|
|
110
|
+
case "number":
|
|
111
|
+
if (this.isErrorNumber(value)) {
|
|
112
|
+
throw new Error('Please enter a value of type number or a string of half-width digits.');
|
|
113
|
+
}
|
|
114
|
+
break;
|
|
115
|
+
case "bool":
|
|
116
|
+
if (this.isErrorBool(value)) {
|
|
117
|
+
throw new Error('Please enter a value of type bool, or a string "true" or "false", or a number 0 or 1.');
|
|
118
|
+
}
|
|
119
|
+
break;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
static isErrorValue(columnType, value) {
|
|
123
|
+
// 配列のチェック
|
|
124
|
+
if (columnType.endsWith("[]")) {
|
|
125
|
+
if (Array.isArray(value) === false) {
|
|
126
|
+
return true;
|
|
127
|
+
}
|
|
128
|
+
for (const v of value) {
|
|
129
|
+
let isError = false;
|
|
130
|
+
switch (columnType) {
|
|
131
|
+
case "string[]":
|
|
132
|
+
isError = this.isErrorString(v);
|
|
133
|
+
break;
|
|
134
|
+
case "uuid[]":
|
|
135
|
+
isError = this.isErrorUUID(v);
|
|
136
|
+
break;
|
|
137
|
+
case "date[]":
|
|
138
|
+
isError = this.isErrorDate(v);
|
|
139
|
+
break;
|
|
140
|
+
case "time[]":
|
|
141
|
+
isError = this.isErrorTime(v);
|
|
142
|
+
break;
|
|
143
|
+
case "timestamp[]":
|
|
144
|
+
isError = this.isErrorTimestamp(v);
|
|
145
|
+
break;
|
|
146
|
+
case "number[]":
|
|
147
|
+
isError = this.isErrorNumber(v);
|
|
148
|
+
break;
|
|
149
|
+
case "bool[]":
|
|
150
|
+
isError = this.isErrorBool(v);
|
|
151
|
+
break;
|
|
152
|
+
default:
|
|
153
|
+
throw new Error(`The specified ColumnTypeEnum does not exist. (${columnType})`);
|
|
154
|
+
}
|
|
155
|
+
if (isError) {
|
|
156
|
+
return true;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
return false;
|
|
160
|
+
}
|
|
161
|
+
switch (columnType) {
|
|
162
|
+
case "string":
|
|
163
|
+
return this.isErrorString(value);
|
|
164
|
+
case "uuid":
|
|
165
|
+
return this.isErrorUUID(value);
|
|
166
|
+
case "date":
|
|
167
|
+
return this.isErrorDate(value);
|
|
168
|
+
case "time":
|
|
169
|
+
return this.isErrorTime(value);
|
|
170
|
+
case "timestamp":
|
|
171
|
+
return this.isErrorTimestamp(value);
|
|
172
|
+
case "number":
|
|
173
|
+
return this.isErrorNumber(value);
|
|
174
|
+
case "bool":
|
|
175
|
+
return this.isErrorBool(value);
|
|
176
|
+
default:
|
|
177
|
+
throw new Error(`The specified ColumnTypeEnum does not exist. (${columnType})`);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
static isErrorString(value) {
|
|
181
|
+
if (typeof (value) == 'string' || typeof (value) == 'number') {
|
|
182
|
+
return false;
|
|
183
|
+
}
|
|
184
|
+
return true;
|
|
185
|
+
}
|
|
186
|
+
static isErrorNumber(value) {
|
|
187
|
+
if (typeof value === 'string') {
|
|
188
|
+
if (value.trim() === "" || isNaN(Number(value))) {
|
|
189
|
+
return true;
|
|
190
|
+
}
|
|
191
|
+
return false;
|
|
192
|
+
}
|
|
193
|
+
else if (typeof value === 'number') {
|
|
194
|
+
return false;
|
|
195
|
+
}
|
|
196
|
+
return true;
|
|
197
|
+
}
|
|
198
|
+
static isErrorBool(value) {
|
|
199
|
+
switch (typeof (value)) {
|
|
200
|
+
case 'string':
|
|
201
|
+
return value !== 'true' && value !== 'false';
|
|
202
|
+
case 'number':
|
|
203
|
+
return value !== 0 && value !== 1;
|
|
204
|
+
case 'boolean':
|
|
205
|
+
return false;
|
|
206
|
+
default:
|
|
207
|
+
return true;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
static isErrorUUID(value) {
|
|
211
|
+
return StringUtil_1.default.isUUID(value) === false;
|
|
212
|
+
}
|
|
213
|
+
static isErrorDate(value) {
|
|
214
|
+
if (value instanceof Date) {
|
|
215
|
+
return false;
|
|
216
|
+
}
|
|
217
|
+
else if (DateTimeUtil_1.default.isYYYYMMDD(value)) {
|
|
218
|
+
return false;
|
|
219
|
+
}
|
|
220
|
+
else if (DateTimeUtil_1.default.isYYYYMMDDhhmiss(value)) {
|
|
221
|
+
return false;
|
|
222
|
+
}
|
|
223
|
+
return true;
|
|
224
|
+
}
|
|
225
|
+
static isErrorTimestamp(value) {
|
|
226
|
+
if (value instanceof Date) {
|
|
227
|
+
return false;
|
|
228
|
+
}
|
|
229
|
+
else if (DateTimeUtil_1.default.isYYYYMMDD(`${value}`)) {
|
|
230
|
+
return false;
|
|
231
|
+
}
|
|
232
|
+
else if (DateTimeUtil_1.default.isYYYYMMDDhhmiss(value)) {
|
|
233
|
+
return false;
|
|
234
|
+
}
|
|
235
|
+
return true;
|
|
236
|
+
}
|
|
237
|
+
static isErrorTime(value) {
|
|
238
|
+
if (value instanceof Date) {
|
|
239
|
+
return false;
|
|
240
|
+
}
|
|
241
|
+
if (DateTimeUtil_1.default.isHHMMSS(value)) {
|
|
242
|
+
return false;
|
|
243
|
+
}
|
|
244
|
+
else if (DateTimeUtil_1.default.isHHMM(value)) {
|
|
245
|
+
return false;
|
|
246
|
+
}
|
|
247
|
+
return true;
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
exports.default = ValidateValueUtil;
|