pg-mvc-service 1.0.9 → 1.0.11

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.
@@ -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;
@@ -108,7 +108,7 @@ class TableModel {
108
108
  this.groupExpression = [];
109
109
  this.sortExpression = [];
110
110
  this.vars = [];
111
- this.errorMessages = {
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
- throw new Exception_1.UnprocessableException("001", this.errorMessages.null.replace('{name}', name));
416
+ this.throwException("001", "null", key, value);
385
417
  }
386
418
  if (ValidateValueUtil_1.default.isErrorValue(column.type, value)) {
387
- throw new Exception_1.UnprocessableException("002", this.errorMessages[column.type].replace('{name}', name));
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
- throw new Exception_1.UnprocessableException("003", this.errorMessages.length.replace('{name}', name).replace('{length}', column.length.toString()));
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
- throw new Exception_1.UnprocessableException("004", this.errorMessages.length.replace('{name}', name).replace('{length}', column.length.toString()));
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("004", this.errorMessages.fk.replace('{name}', name));
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.UnprocessableException("004", this.errorMessages.fk.replace('{name}', name));
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
- throw new Exception_1.UnprocessableException("101", this.errorMessages.notInput.replace('{name}', name));
473
+ this.throwException("101", "notInput", key, options[key]);
442
474
  }
443
475
  }
444
476
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pg-mvc-service",
3
- "version": "1.0.9",
3
+ "version": "1.0.11",
4
4
  "description": "",
5
5
  "homepage": "https://github.com/n-daira/npm-pack_mvc-service#readme",
6
6
  "bugs": {
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';
@@ -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 = "";
@@ -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
- protected readonly errorMessages: Record<TColumnType | TColumnArrayType | 'length' | 'null' | 'notInput' | 'fk' | 'idNotExist', string> = {
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
- throw new UnprocessableException("001", this.errorMessages.null.replace('{name}', name));
479
+ this.throwException("001", "null", key, value);
443
480
  }
444
481
 
445
482
  if (ValidateValueUtil.isErrorValue(column.type, value)) {
446
- throw new UnprocessableException("002", this.errorMessages[column.type].replace('{name}', name));
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
- throw new UnprocessableException("003", this.errorMessages.length.replace('{name}', name).replace('{length}', column.length.toString()));
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
- throw new UnprocessableException("004", this.errorMessages.length.replace('{name}', name).replace('{length}', column.length.toString()));
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("004", this.errorMessages.fk.replace('{name}', name));
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 UnprocessableException("004", this.errorMessages.fk.replace('{name}', name));
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
- throw new UnprocessableException("101", this.errorMessages.notInput.replace('{name}', name));
541
+ this.throwException("101", "notInput", key, options[key]);
505
542
  }
506
543
  }
507
544
  }
@@ -1,4 +1,5 @@
1
1
  import { UnprocessableException } from "../exceptions/Exception";
2
+ import ValidateValueUtil from "./SqlUtils/ValidateValueUtil";
2
3
  import { TableModel } from "./TableModel";
3
4
  import { TOption, TSqlValue } from "./Type";
4
5