pg-mvc-service 1.0.8 → 1.0.10
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/clients/AwsS3Client.js +34 -12
- package/dist/exceptions/Exception.js +13 -1
- package/dist/index.js +2 -2
- package/dist/models/TableModel.js +41 -9
- package/package.json +1 -1
- package/src/Service.ts +1 -1
- package/src/clients/AwsS3Client.ts +15 -7
- package/src/exceptions/Exception.ts +13 -0
- package/src/models/TableModel.ts +47 -10
- package/src/models/ValidateClient.ts +1 -0
|
@@ -136,7 +136,8 @@ class AwsS3Client {
|
|
|
136
136
|
}
|
|
137
137
|
getText(path, fileName) {
|
|
138
138
|
return __awaiter(this, void 0, void 0, function* () {
|
|
139
|
-
var _a;
|
|
139
|
+
var _a, e_1, _b, _c;
|
|
140
|
+
var _d;
|
|
140
141
|
try {
|
|
141
142
|
const command = new client_s3_1.GetObjectCommand({
|
|
142
143
|
Bucket: this.bucketName,
|
|
@@ -146,18 +147,39 @@ class AwsS3Client {
|
|
|
146
147
|
if (res.Body === undefined) {
|
|
147
148
|
throw new Error(`Failed to get text data. Response body is undefined.`);
|
|
148
149
|
}
|
|
149
|
-
if (((
|
|
150
|
+
if (((_d = res.ContentType) === null || _d === void 0 ? void 0 : _d.startsWith('text/')) === false) {
|
|
150
151
|
throw new Error(`Cannot get text data from non-text file. ContentType: ${res.ContentType}`);
|
|
151
152
|
}
|
|
152
153
|
// v3ではBodyがReadableStreamなので、変換が必要
|
|
153
|
-
const stream = res.Body;
|
|
154
|
-
const reader = stream.getReader();
|
|
155
154
|
const chunks = [];
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
155
|
+
if (res.Body && typeof res.Body === 'object' && 'getReader' in res.Body) {
|
|
156
|
+
// ReadableStreamの場合
|
|
157
|
+
const stream = res.Body;
|
|
158
|
+
const reader = stream.getReader();
|
|
159
|
+
while (true) {
|
|
160
|
+
const { done, value } = yield reader.read();
|
|
161
|
+
if (done)
|
|
162
|
+
break;
|
|
163
|
+
chunks.push(value);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
else {
|
|
167
|
+
try {
|
|
168
|
+
// Node.js Readableの場合
|
|
169
|
+
for (var _e = true, _f = __asyncValues(res.Body), _g; _g = yield _f.next(), _a = _g.done, !_a; _e = true) {
|
|
170
|
+
_c = _g.value;
|
|
171
|
+
_e = false;
|
|
172
|
+
const chunk = _c;
|
|
173
|
+
chunks.push(chunk);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
177
|
+
finally {
|
|
178
|
+
try {
|
|
179
|
+
if (!_e && !_a && (_b = _f.return)) yield _b.call(_f);
|
|
180
|
+
}
|
|
181
|
+
finally { if (e_1) throw e_1.error; }
|
|
182
|
+
}
|
|
161
183
|
}
|
|
162
184
|
const buffer = Buffer.concat(chunks);
|
|
163
185
|
return buffer.toString('utf-8');
|
|
@@ -183,7 +205,7 @@ class AwsS3Client {
|
|
|
183
205
|
}
|
|
184
206
|
getDataFronJson(path, fileName) {
|
|
185
207
|
return __awaiter(this, void 0, void 0, function* () {
|
|
186
|
-
var _a,
|
|
208
|
+
var _a, e_2, _b, _c;
|
|
187
209
|
const command = new client_s3_1.GetObjectCommand({
|
|
188
210
|
Bucket: this.bucketName,
|
|
189
211
|
Key: this.makeKey(path, fileName),
|
|
@@ -205,12 +227,12 @@ class AwsS3Client {
|
|
|
205
227
|
chunks.push(chunk);
|
|
206
228
|
}
|
|
207
229
|
}
|
|
208
|
-
catch (
|
|
230
|
+
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
209
231
|
finally {
|
|
210
232
|
try {
|
|
211
233
|
if (!_d && !_a && (_b = _e.return)) yield _b.call(_e);
|
|
212
234
|
}
|
|
213
|
-
finally { if (
|
|
235
|
+
finally { if (e_2) throw e_2.error; }
|
|
214
236
|
}
|
|
215
237
|
const buffer = Buffer.concat(chunks);
|
|
216
238
|
const jsonString = buffer.toString('utf-8');
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.UnprocessableException = exports.DbConflictException = exports.MaintenanceException = exports.InputErrorException = exports.ForbiddenException = exports.AuthException = void 0;
|
|
3
|
+
exports.UnprocessableException = exports.DbConflictException = exports.NotFoundException = exports.MaintenanceException = exports.InputErrorException = exports.ForbiddenException = exports.AuthException = void 0;
|
|
4
4
|
class AuthException extends Error {
|
|
5
5
|
get Id() {
|
|
6
6
|
return this.id;
|
|
@@ -32,6 +32,18 @@ class MaintenanceException extends Error {
|
|
|
32
32
|
}
|
|
33
33
|
}
|
|
34
34
|
exports.MaintenanceException = MaintenanceException;
|
|
35
|
+
class NotFoundException extends Error {
|
|
36
|
+
get ErrorId() {
|
|
37
|
+
return this.errorId;
|
|
38
|
+
}
|
|
39
|
+
constructor(errorId, message = "") {
|
|
40
|
+
super(message);
|
|
41
|
+
// for 404 Not Found
|
|
42
|
+
this.errorId = "";
|
|
43
|
+
this.errorId = errorId;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
exports.NotFoundException = NotFoundException;
|
|
35
47
|
class DbConflictException extends Error {
|
|
36
48
|
get ErrorId() {
|
|
37
49
|
return this.errorId;
|
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.
|
|
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.UnprocessableException = 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");
|
|
@@ -9,7 +9,7 @@ Object.defineProperty(exports, "AuthException", { enumerable: true, get: functio
|
|
|
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
11
|
Object.defineProperty(exports, "DbConflictException", { enumerable: true, get: function () { return Exception_1.DbConflictException; } });
|
|
12
|
-
Object.defineProperty(exports, "
|
|
12
|
+
Object.defineProperty(exports, "UnprocessableException", { enumerable: true, get: function () { return Exception_1.UnprocessableException; } });
|
|
13
13
|
var Swagger_1 = require("./documents/Swagger");
|
|
14
14
|
Object.defineProperty(exports, "createSwagger", { enumerable: true, get: function () { return Swagger_1.createSwagger; } });
|
|
15
15
|
var AwsS3Client_1 = require("./clients/AwsS3Client");
|
|
@@ -108,7 +108,7 @@ class TableModel {
|
|
|
108
108
|
this.groupExpression = [];
|
|
109
109
|
this.sortExpression = [];
|
|
110
110
|
this.vars = [];
|
|
111
|
-
this.
|
|
111
|
+
this.errorMessageEnglish = {
|
|
112
112
|
'string': '{name} should be entered as a string or number type.',
|
|
113
113
|
'string[]': '{name} should be entered as an array of string or number types.',
|
|
114
114
|
'uuid': '{name} should be entered as a UUID.',
|
|
@@ -129,6 +129,28 @@ class TableModel {
|
|
|
129
129
|
'fk': 'The value of {name} does not exist in the table.',
|
|
130
130
|
'idNotExist': 'The specified ID({id}) does not exist in the table.',
|
|
131
131
|
};
|
|
132
|
+
this.errorMessageJapan = {
|
|
133
|
+
'string': '{name}はstringかnumberで入力してください。',
|
|
134
|
+
'string[]': '{name}はstringかnumberの配列で入力してください。',
|
|
135
|
+
'uuid': '{name}はuuidで入力してください。',
|
|
136
|
+
'uuid[]': '{name}はuuidの配列で入力してください。',
|
|
137
|
+
'number': '{name}はnumberか半角数字のstring型で入力してください。',
|
|
138
|
+
'number[]': '{name}はnumberか半角数字のstring型の配列で入力してください。',
|
|
139
|
+
'bool': '{name}はbool型、"true"、"false"、0、または1で入力してください。',
|
|
140
|
+
'bool[]': '{name}はbool型、"true"、"false"、0、または1の配列で入力してください。',
|
|
141
|
+
'date': '{name}は"YYYY-MM-DD"形式、"YYYY-MM-DD hh:mi:ss"形式、またはDate型で入力してください。',
|
|
142
|
+
'date[]': '{name}は"YYYY-MM-DD"形式、"YYYY-MM-DD hh:mi:ss"形式、またはDate型の配列で入力してください。',
|
|
143
|
+
'time': '{name}は"hh:mi"形式または"hh:mi:ss"形式で入力してください。',
|
|
144
|
+
'time[]': '{name}は"hh:mi"形式または"hh:mi:ss"形式の配列で入力してください。',
|
|
145
|
+
'timestamp': '{name}は"YYYY-MM-DD"形式、"YYYY-MM-DD hh:mi:ss"形式、"YYYY-MM-DDThh:mi:ss"形式、またはDate型で入力してください。',
|
|
146
|
+
'timestamp[]': '{name}は"YYYY-MM-DD"形式、"YYYY-MM-DD hh:mi:ss"形式、"YYYY-MM-DDThh:mi:ss"形式、またはDate型の配列で入力してください。',
|
|
147
|
+
'length': '{name}は{length}文字以内で入力してください。',
|
|
148
|
+
'null': '{name}はnullを許可されていません。',
|
|
149
|
+
'notInput': '{name}を入力してください。',
|
|
150
|
+
'fk': '{name}の値がテーブルに存在しません。',
|
|
151
|
+
'idNotExist': '指定されたID({id})はテーブルに存在しません。',
|
|
152
|
+
};
|
|
153
|
+
this.errorMessages = process.env.TZ === 'Asia/Tokyo' ? this.errorMessageJapan : this.errorMessageEnglish;
|
|
132
154
|
this.client = client;
|
|
133
155
|
if (tableAlias !== undefined && tableAlias.trim() !== '') {
|
|
134
156
|
this.tableAlias = tableAlias;
|
|
@@ -366,6 +388,17 @@ class TableModel {
|
|
|
366
388
|
return { datas: data.rows, count: Number(countData.rows[0].count), lastPage: Math.ceil(Number(countData.rows[0].count) / this.PageCount) };
|
|
367
389
|
});
|
|
368
390
|
}
|
|
391
|
+
throwException(code, type, columnName, vallue) {
|
|
392
|
+
var _a;
|
|
393
|
+
const column = this.getColumn(columnName);
|
|
394
|
+
let message = this.errorMessages[type];
|
|
395
|
+
const name = (column.alias === undefined || column.alias === '') ? columnName : column.alias;
|
|
396
|
+
message = message.replace('{name}', name);
|
|
397
|
+
if (message.includes("{length}") && (column.type === 'string' || column.type !== 'string[]')) {
|
|
398
|
+
message = message.replace('{length}', ((_a = column.length) !== null && _a !== void 0 ? _a : '未設定').toString());
|
|
399
|
+
}
|
|
400
|
+
throw new Exception_1.UnprocessableException(code, message);
|
|
401
|
+
}
|
|
369
402
|
validateOptions(options, isInsert) {
|
|
370
403
|
return __awaiter(this, void 0, void 0, function* () {
|
|
371
404
|
if (Object.keys(options).length === 0) {
|
|
@@ -376,22 +409,21 @@ class TableModel {
|
|
|
376
409
|
if (isInsert === false && column.attribute === 'primary') {
|
|
377
410
|
throw new Error(`${this.TableName}.${key} cannot be modified because it is a primary key.`);
|
|
378
411
|
}
|
|
379
|
-
const name = (column.alias === undefined || column.alias === '') ? key : column.alias;
|
|
380
412
|
if (value === null) {
|
|
381
413
|
if (column.attribute === 'nullable') {
|
|
382
414
|
continue;
|
|
383
415
|
}
|
|
384
|
-
|
|
416
|
+
this.throwException("001", "null", key, value);
|
|
385
417
|
}
|
|
386
418
|
if (ValidateValueUtil_1.default.isErrorValue(column.type, value)) {
|
|
387
|
-
|
|
419
|
+
this.throwException("002", column.type, key, value);
|
|
388
420
|
}
|
|
389
421
|
if (column.type === 'string') {
|
|
390
422
|
if (Number.isInteger(column.length) === false) {
|
|
391
423
|
throw new Error(`For strings, please specify the length of the column.(column: ${column.columnName})`);
|
|
392
424
|
}
|
|
393
425
|
if (value.toString().length > column.length) {
|
|
394
|
-
|
|
426
|
+
this.throwException("003", "length", key, value);
|
|
395
427
|
}
|
|
396
428
|
}
|
|
397
429
|
else if (column.type === 'string[]') {
|
|
@@ -401,7 +433,7 @@ class TableModel {
|
|
|
401
433
|
// ValidateValueUtil.isErrorValue(column.type, value)で型チェックしてるのでas []にしている
|
|
402
434
|
for (const v of value) {
|
|
403
435
|
if (v.toString().length > column.length) {
|
|
404
|
-
|
|
436
|
+
this.throwException("004", "length", key, value);
|
|
405
437
|
}
|
|
406
438
|
}
|
|
407
439
|
}
|
|
@@ -417,14 +449,14 @@ class TableModel {
|
|
|
417
449
|
// 一部の値がnullの場合はエラー
|
|
418
450
|
if (refValues.some(value => value === null || value === undefined)) {
|
|
419
451
|
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(',');
|
|
420
|
-
throw new Exception_1.UnprocessableException("
|
|
452
|
+
throw new Exception_1.UnprocessableException("005", this.errorMessages.null.replace('{name}', name));
|
|
421
453
|
}
|
|
422
454
|
let refIndex = 1;
|
|
423
455
|
const sql = `SELECT COUNT(*) as count FROM ${ref.table} WHERE ${ref.columns.map(col => `${col.ref} = $${refIndex++}`).join(" AND ")}`;
|
|
424
456
|
const datas = yield this.clientQuery(sql, refValues);
|
|
425
457
|
if (datas.rows[0].count == "0") {
|
|
426
458
|
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(',');
|
|
427
|
-
throw new Exception_1.
|
|
459
|
+
throw new Exception_1.DbConflictException("006", this.errorMessages.fk.replace('{name}', name));
|
|
428
460
|
}
|
|
429
461
|
}
|
|
430
462
|
}
|
|
@@ -438,7 +470,7 @@ class TableModel {
|
|
|
438
470
|
if (options[key] === undefined || options[key] === null) {
|
|
439
471
|
// Null許容されていないカラムにNULLを入れようとしているか?
|
|
440
472
|
if (column.attribute === "primary" || column.attribute === "noDefault") {
|
|
441
|
-
|
|
473
|
+
this.throwException("101", "notInput", key, options[key]);
|
|
442
474
|
}
|
|
443
475
|
}
|
|
444
476
|
}
|
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, DbConflictException, UnprocessableException } from './exceptions/Exception';
|
|
4
|
+
import { MaintenanceException, AuthException, InputErrorException, ForbiddenException, DbConflictException, UnprocessableException, NotFoundException } from './exceptions/Exception';
|
|
5
5
|
import { RequestType } from './reqestResponse/RequestType';
|
|
6
6
|
import { ResponseType } from './reqestResponse/ResponseType';
|
|
7
7
|
import { AwsS3Client } from './clients/AwsS3Client';
|
|
@@ -147,14 +147,22 @@ export class AwsS3Client {
|
|
|
147
147
|
}
|
|
148
148
|
|
|
149
149
|
// v3ではBodyがReadableStreamなので、変換が必要
|
|
150
|
-
const stream = res.Body as ReadableStream;
|
|
151
|
-
const reader = stream.getReader();
|
|
152
150
|
const chunks: Uint8Array[] = [];
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
const
|
|
156
|
-
|
|
157
|
-
|
|
151
|
+
if (res.Body && typeof res.Body === 'object' && 'getReader' in res.Body) {
|
|
152
|
+
// ReadableStreamの場合
|
|
153
|
+
const stream = res.Body as ReadableStream;
|
|
154
|
+
const reader = stream.getReader();
|
|
155
|
+
|
|
156
|
+
while (true) {
|
|
157
|
+
const { done, value } = await reader.read();
|
|
158
|
+
if (done) break;
|
|
159
|
+
chunks.push(value);
|
|
160
|
+
}
|
|
161
|
+
} else {
|
|
162
|
+
// Node.js Readableの場合
|
|
163
|
+
for await (const chunk of res.Body as any) {
|
|
164
|
+
chunks.push(chunk);
|
|
165
|
+
}
|
|
158
166
|
}
|
|
159
167
|
|
|
160
168
|
const buffer = Buffer.concat(chunks);
|
|
@@ -32,6 +32,19 @@ export class MaintenanceException extends Error {
|
|
|
32
32
|
}
|
|
33
33
|
}
|
|
34
34
|
|
|
35
|
+
export class NotFoundException extends Error {
|
|
36
|
+
// for 404 Not Found
|
|
37
|
+
private errorId: string = "";
|
|
38
|
+
get ErrorId(): string {
|
|
39
|
+
return this.errorId;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
constructor(errorId: string, message: string = "") {
|
|
43
|
+
super(message);
|
|
44
|
+
this.errorId = errorId;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
35
48
|
export class DbConflictException extends Error {
|
|
36
49
|
// for 409 Conflict
|
|
37
50
|
private errorId: string = "";
|
package/src/models/TableModel.ts
CHANGED
|
@@ -4,7 +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 { UnprocessableException } from '../exceptions/Exception';
|
|
7
|
+
import { DbConflictException, UnprocessableException } from '../exceptions/Exception';
|
|
8
8
|
|
|
9
9
|
export class TableModel {
|
|
10
10
|
|
|
@@ -401,7 +401,7 @@ export class TableModel {
|
|
|
401
401
|
return { datas: data.rows as Array<T>, count: Number(countData.rows[0].count), lastPage: Math.ceil(Number(countData.rows[0].count) / this.PageCount)};
|
|
402
402
|
}
|
|
403
403
|
|
|
404
|
-
|
|
404
|
+
private readonly errorMessageEnglish: Record<TColumnType | TColumnArrayType | 'length' | 'null' | 'notInput' | 'fk' | 'idNotExist', string> = {
|
|
405
405
|
'string': '{name} should be entered as a string or number type.',
|
|
406
406
|
'string[]': '{name} should be entered as an array of string or number types.',
|
|
407
407
|
'uuid': '{name} should be entered as a UUID.',
|
|
@@ -422,6 +422,44 @@ export class TableModel {
|
|
|
422
422
|
'fk': 'The value of {name} does not exist in the table.',
|
|
423
423
|
'idNotExist': 'The specified ID({id}) does not exist in the table.',
|
|
424
424
|
}
|
|
425
|
+
private readonly errorMessageJapan: Record<TColumnType | TColumnArrayType | 'length' | 'null' | 'notInput' | 'fk' | 'idNotExist', string> = {
|
|
426
|
+
'string': '{name}はstringかnumberで入力してください。',
|
|
427
|
+
'string[]': '{name}はstringかnumberの配列で入力してください。',
|
|
428
|
+
'uuid': '{name}はuuidで入力してください。',
|
|
429
|
+
'uuid[]': '{name}はuuidの配列で入力してください。',
|
|
430
|
+
'number': '{name}はnumberか半角数字のstring型で入力してください。',
|
|
431
|
+
'number[]': '{name}はnumberか半角数字のstring型の配列で入力してください。',
|
|
432
|
+
'bool': '{name}はbool型、"true"、"false"、0、または1で入力してください。',
|
|
433
|
+
'bool[]': '{name}はbool型、"true"、"false"、0、または1の配列で入力してください。',
|
|
434
|
+
'date': '{name}は"YYYY-MM-DD"形式、"YYYY-MM-DD hh:mi:ss"形式、またはDate型で入力してください。',
|
|
435
|
+
'date[]': '{name}は"YYYY-MM-DD"形式、"YYYY-MM-DD hh:mi:ss"形式、またはDate型の配列で入力してください。',
|
|
436
|
+
'time': '{name}は"hh:mi"形式または"hh:mi:ss"形式で入力してください。',
|
|
437
|
+
'time[]': '{name}は"hh:mi"形式または"hh:mi:ss"形式の配列で入力してください。',
|
|
438
|
+
'timestamp': '{name}は"YYYY-MM-DD"形式、"YYYY-MM-DD hh:mi:ss"形式、"YYYY-MM-DDThh:mi:ss"形式、またはDate型で入力してください。',
|
|
439
|
+
'timestamp[]': '{name}は"YYYY-MM-DD"形式、"YYYY-MM-DD hh:mi:ss"形式、"YYYY-MM-DDThh:mi:ss"形式、またはDate型の配列で入力してください。',
|
|
440
|
+
'length': '{name}は{length}文字以内で入力してください。',
|
|
441
|
+
'null': '{name}はnullを許可されていません。',
|
|
442
|
+
'notInput': '{name}を入力してください。',
|
|
443
|
+
'fk': '{name}の値がテーブルに存在しません。',
|
|
444
|
+
'idNotExist': '指定されたID({id})はテーブルに存在しません。',
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
private throwException(code: string, type: TColumnType | TColumnArrayType | 'length' | 'null' | 'notInput' | 'fk' | 'idNotExist', columnName: string, vallue: TSqlValue): never {
|
|
448
|
+
const column = this.getColumn(columnName);
|
|
449
|
+
|
|
450
|
+
let message = this.errorMessages[type];
|
|
451
|
+
|
|
452
|
+
const name = (column.alias === undefined || column.alias === '') ? columnName : column.alias;
|
|
453
|
+
message = message.replace('{name}', name);
|
|
454
|
+
if (message.includes("{length}") && (column.type === 'string' || column.type !== 'string[]')) {
|
|
455
|
+
message = message.replace('{length}', (column.length ?? '未設定').toString());
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
throw new UnprocessableException(code, message);
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
protected readonly errorMessages: Record<TColumnType | TColumnArrayType | 'length' | 'null' | 'notInput' | 'fk' | 'idNotExist', string> =
|
|
462
|
+
process.env.TZ === 'Asia/Tokyo' ? this.errorMessageJapan : this.errorMessageEnglish;
|
|
425
463
|
|
|
426
464
|
protected async validateOptions(options: TOption, isInsert: boolean): Promise<void> {
|
|
427
465
|
if (Object.keys(options).length === 0) {
|
|
@@ -434,16 +472,15 @@ export class TableModel {
|
|
|
434
472
|
throw new Error(`${this.TableName}.${key} cannot be modified because it is a primary key.`);
|
|
435
473
|
}
|
|
436
474
|
|
|
437
|
-
const name = (column.alias === undefined || column.alias === '') ? key : column.alias;
|
|
438
475
|
if (value === null) {
|
|
439
476
|
if (column.attribute === 'nullable') {
|
|
440
477
|
continue;
|
|
441
478
|
}
|
|
442
|
-
|
|
479
|
+
this.throwException("001", "null", key, value);
|
|
443
480
|
}
|
|
444
481
|
|
|
445
482
|
if (ValidateValueUtil.isErrorValue(column.type, value)) {
|
|
446
|
-
|
|
483
|
+
this.throwException("002", column.type, key, value);
|
|
447
484
|
}
|
|
448
485
|
|
|
449
486
|
if (column.type === 'string') {
|
|
@@ -452,7 +489,7 @@ export class TableModel {
|
|
|
452
489
|
}
|
|
453
490
|
|
|
454
491
|
if (value.toString().length > column.length) {
|
|
455
|
-
|
|
492
|
+
this.throwException("003", "length", key, value);
|
|
456
493
|
}
|
|
457
494
|
} else if (column.type === 'string[]') {
|
|
458
495
|
if (Number.isInteger(column.length) === false) {
|
|
@@ -462,7 +499,7 @@ export class TableModel {
|
|
|
462
499
|
// ValidateValueUtil.isErrorValue(column.type, value)で型チェックしてるのでas []にしている
|
|
463
500
|
for (const v of value as Array<string | number | boolean>) {
|
|
464
501
|
if (v.toString().length > column.length) {
|
|
465
|
-
|
|
502
|
+
this.throwException("004", "length", key, value);
|
|
466
503
|
}
|
|
467
504
|
}
|
|
468
505
|
}
|
|
@@ -480,7 +517,7 @@ export class TableModel {
|
|
|
480
517
|
// 一部の値がnullの場合はエラー
|
|
481
518
|
if (refValues.some(value => value === null || value === undefined)) {
|
|
482
519
|
const name = ref.columns.map(col => this.getColumn(col.target).alias ?? this.getColumn(col.target).columnName).join(',');
|
|
483
|
-
throw new UnprocessableException("
|
|
520
|
+
throw new UnprocessableException("005", this.errorMessages.null.replace('{name}', name));
|
|
484
521
|
}
|
|
485
522
|
|
|
486
523
|
let refIndex = 1;
|
|
@@ -488,7 +525,7 @@ export class TableModel {
|
|
|
488
525
|
const datas = await this.clientQuery(sql, refValues);
|
|
489
526
|
if (datas.rows[0].count == "0") {
|
|
490
527
|
const name = ref.columns.map(col => this.getColumn(col.target).alias ?? this.getColumn(col.target).columnName).join(',');
|
|
491
|
-
throw new
|
|
528
|
+
throw new DbConflictException("006", this.errorMessages.fk.replace('{name}', name));
|
|
492
529
|
}
|
|
493
530
|
}
|
|
494
531
|
}
|
|
@@ -501,7 +538,7 @@ export class TableModel {
|
|
|
501
538
|
if (options[key] === undefined || options[key] === null) {
|
|
502
539
|
// Null許容されていないカラムにNULLを入れようとしているか?
|
|
503
540
|
if (column.attribute === "primary" || column.attribute === "noDefault") {
|
|
504
|
-
|
|
541
|
+
this.throwException("101", "notInput", key, options[key]);
|
|
505
542
|
}
|
|
506
543
|
}
|
|
507
544
|
}
|