pg-mvc-service 2.0.24 → 2.0.26
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/cron/BaseCron.js +106 -0
- package/dist/cron/CronExecuter.js +81 -0
- package/dist/cron/CronType.js +2 -0
- package/dist/index.js +5 -1
- package/index.d.ts +1 -0
- package/package.json +1 -1
- package/src/cron/BaseCron.ts +56 -2
- package/src/index.ts +1 -0
- /package/src/cron/{TimeType.ts → CronType.ts} +0 -0
|
@@ -0,0 +1,106 @@
|
|
|
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
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.BaseCron = void 0;
|
|
16
|
+
const PoolManager_1 = __importDefault(require("../PoolManager"));
|
|
17
|
+
class BaseCron {
|
|
18
|
+
constructor() {
|
|
19
|
+
this.isTest = process.env.NODE_ENV === 'test';
|
|
20
|
+
this.dbUser = this.isTest ? process.env.TEST_DB_USER : process.env.DB_USER;
|
|
21
|
+
this.dbHost = this.isTest ? process.env.TEST_DB_HOST : process.env.DB_HOST;
|
|
22
|
+
this.dbName = this.isTest ? process.env.TEST_DB_DATABASE : process.env.DB_DATABASE;
|
|
23
|
+
this.dbPassword = this.isTest ? process.env.TEST_DB_PASSWORD : process.env.DB_PASSWORD;
|
|
24
|
+
this.dbPort = this.isTest ? process.env.TEST_DB_PORT : process.env.DB_PORT;
|
|
25
|
+
this.dbIsSslConnect = (this.isTest ? process.env.TEST_DB_IS_SSL : process.env.DB_IS_SSL) === 'true';
|
|
26
|
+
this.isExecuteRollback = false;
|
|
27
|
+
// **********************************************************************
|
|
28
|
+
// こちらのメソッド、プロパティを各サブクラスで設定してください
|
|
29
|
+
// **********************************************************************
|
|
30
|
+
this.cronCode = '';
|
|
31
|
+
this.minute = '*';
|
|
32
|
+
this.hour = '*';
|
|
33
|
+
this.date = '*';
|
|
34
|
+
this.month = '*';
|
|
35
|
+
this.day = '*';
|
|
36
|
+
}
|
|
37
|
+
get Client() {
|
|
38
|
+
if (this.client === undefined) {
|
|
39
|
+
throw new Error("Please call this.PoolClient after using the setClient method.");
|
|
40
|
+
}
|
|
41
|
+
return this.client;
|
|
42
|
+
}
|
|
43
|
+
commit() {
|
|
44
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
45
|
+
yield this.Client.query('COMMIT');
|
|
46
|
+
this.isExecuteRollback = false;
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
rollback() {
|
|
50
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
51
|
+
if (this.isExecuteRollback) {
|
|
52
|
+
yield this.Client.query('ROLLBACK');
|
|
53
|
+
}
|
|
54
|
+
this.isExecuteRollback = false;
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
run() {
|
|
58
|
+
return __awaiter(this, void 0, void 0, function* () { });
|
|
59
|
+
}
|
|
60
|
+
// **********************************************************************
|
|
61
|
+
// ベースクラスで設定
|
|
62
|
+
// **********************************************************************
|
|
63
|
+
get CronSchedule() {
|
|
64
|
+
let schedule = '';
|
|
65
|
+
schedule += this.minute.toString() + ' ';
|
|
66
|
+
schedule += this.hour.toString() + ' ';
|
|
67
|
+
schedule += this.date.toString() + ' ';
|
|
68
|
+
schedule += this.month.toString() + ' ';
|
|
69
|
+
schedule += this.day.toString();
|
|
70
|
+
return schedule;
|
|
71
|
+
}
|
|
72
|
+
get CronCode() { return this.cronCode; }
|
|
73
|
+
setUp() {
|
|
74
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
75
|
+
var _a;
|
|
76
|
+
if (this.dbUser === undefined) {
|
|
77
|
+
throw new Error("Database user is not configured");
|
|
78
|
+
}
|
|
79
|
+
if (this.dbHost === undefined) {
|
|
80
|
+
throw new Error("Database host is not configured");
|
|
81
|
+
}
|
|
82
|
+
if (this.dbName === undefined) {
|
|
83
|
+
throw new Error("Database name is not configured");
|
|
84
|
+
}
|
|
85
|
+
if (this.dbPassword === undefined) {
|
|
86
|
+
throw new Error("Database password is not configured");
|
|
87
|
+
}
|
|
88
|
+
if (this.dbPort === undefined) {
|
|
89
|
+
throw new Error("Database port is not configured");
|
|
90
|
+
}
|
|
91
|
+
this.pool = PoolManager_1.default.getPool(this.dbUser, this.dbHost, this.dbName, this.dbPassword, this.dbPort, this.dbIsSslConnect);
|
|
92
|
+
this.pool.query(`SET TIME ZONE '${(_a = process.env.TZ) !== null && _a !== void 0 ? _a : 'Asia/Tokyo'}';`);
|
|
93
|
+
this.client = yield this.pool.connect();
|
|
94
|
+
yield this.Client.query('BEGIN');
|
|
95
|
+
this.isExecuteRollback = true;
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
tearDown() {
|
|
99
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
100
|
+
if (this.isExecuteRollback === false) {
|
|
101
|
+
this.rollback();
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
exports.BaseCron = BaseCron;
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
36
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
37
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
38
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
39
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
40
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
41
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
42
|
+
});
|
|
43
|
+
};
|
|
44
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
45
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
46
|
+
};
|
|
47
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
48
|
+
exports.runCron = void 0;
|
|
49
|
+
const cron = require('node-cron');
|
|
50
|
+
const fs_1 = __importDefault(require("fs"));
|
|
51
|
+
const path_1 = __importDefault(require("path"));
|
|
52
|
+
const runCron = (dir, logCallback) => __awaiter(void 0, void 0, void 0, function* () {
|
|
53
|
+
const files = fs_1.default.readdirSync(dir);
|
|
54
|
+
for (let file of files) {
|
|
55
|
+
if (['BaseCron.ts'].includes(file)) {
|
|
56
|
+
continue;
|
|
57
|
+
}
|
|
58
|
+
const filePath = path_1.default.join(dir, file);
|
|
59
|
+
const module = yield Promise.resolve(`${`./${filePath.replace('src/', '').replace('.ts', '')}`}`).then(s => __importStar(require(s)));
|
|
60
|
+
const cronClass = module.default;
|
|
61
|
+
const cronInstance = new cronClass();
|
|
62
|
+
cron.schedule(cronInstance.CronSchedule, () => __awaiter(void 0, void 0, void 0, function* () {
|
|
63
|
+
try {
|
|
64
|
+
yield cronInstance.setUp();
|
|
65
|
+
yield cronInstance.run();
|
|
66
|
+
}
|
|
67
|
+
catch (ex) {
|
|
68
|
+
if (logCallback !== undefined && ex instanceof Error) {
|
|
69
|
+
yield logCallback(ex).
|
|
70
|
+
catch(() => {
|
|
71
|
+
// ログ送信時にエラーになっても握りつぶす
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
finally {
|
|
76
|
+
yield cronInstance.tearDown();
|
|
77
|
+
}
|
|
78
|
+
}));
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
exports.runCron = runCron;
|
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.UnprocessableException = exports.DbConflictException = exports.ForbiddenException = exports.InputErrorException = exports.AuthException = exports.MaintenanceException = exports.Service = void 0;
|
|
3
|
+
exports.runCron = exports.BaseCron = 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");
|
|
@@ -36,3 +36,7 @@ Object.defineProperty(exports, "MigrateDatabase", { enumerable: true, get: funct
|
|
|
36
36
|
var MigrateRollback_1 = require("./models/MigrateRollback");
|
|
37
37
|
Object.defineProperty(exports, "migrate", { enumerable: true, get: function () { return MigrateRollback_1.migrate; } });
|
|
38
38
|
Object.defineProperty(exports, "rollback", { enumerable: true, get: function () { return MigrateRollback_1.rollback; } });
|
|
39
|
+
var BaseCron_1 = require("./cron/BaseCron");
|
|
40
|
+
Object.defineProperty(exports, "BaseCron", { enumerable: true, get: function () { return BaseCron_1.BaseCron; } });
|
|
41
|
+
var CronExecuter_1 = require("./cron/CronExecuter");
|
|
42
|
+
Object.defineProperty(exports, "runCron", { enumerable: true, get: function () { return CronExecuter_1.runCron; } });
|
package/index.d.ts
CHANGED
|
@@ -22,6 +22,7 @@ export { TableModel } from "./src/models/TableModel";
|
|
|
22
22
|
|
|
23
23
|
export { TColumnAttribute, TColumnType, TColumnArrayType, TColumn, TColumnDetail, TOperator, TColumnInfo, TQuery, TSelectExpression, TAggregateFuncType, TCondition, TNestedCondition, TSortKeyword, TKeyFormat } from './src/models/Type';
|
|
24
24
|
|
|
25
|
+
export { DayType, MonthType, DateType, HourType, MinuteSecondType } from './src/cron/CronType';
|
|
25
26
|
export { BaseCron } from "./src/cron/BaseCron";
|
|
26
27
|
export { runCron } from "./src/cron/CronExecuter";
|
|
27
28
|
|
package/package.json
CHANGED
package/src/cron/BaseCron.ts
CHANGED
|
@@ -1,7 +1,39 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Pool, PoolClient } from "pg";
|
|
2
|
+
import { DateType, DayType, HourType, MinuteSecondType, MonthType } from "./CronType";
|
|
3
|
+
import PoolManager from "../PoolManager";
|
|
2
4
|
|
|
3
5
|
export class BaseCron {
|
|
4
6
|
|
|
7
|
+
protected readonly isTest: boolean = process.env.NODE_ENV === 'test';
|
|
8
|
+
protected dbUser?: string = this.isTest ? process.env.TEST_DB_USER : process.env.DB_USER;
|
|
9
|
+
protected dbHost?: string = this.isTest ? process.env.TEST_DB_HOST : process.env.DB_HOST;
|
|
10
|
+
protected dbName?: string = this.isTest ? process.env.TEST_DB_DATABASE : process.env.DB_DATABASE;
|
|
11
|
+
protected dbPassword?: string = this.isTest ? process.env.TEST_DB_PASSWORD : process.env.DB_PASSWORD;
|
|
12
|
+
protected dbPort?: string | number = this.isTest ? process.env.TEST_DB_PORT : process.env.DB_PORT;
|
|
13
|
+
protected dbIsSslConnect: boolean = (this.isTest ? process.env.TEST_DB_IS_SSL : process.env.DB_IS_SSL) === 'true';
|
|
14
|
+
|
|
15
|
+
private isExecuteRollback: boolean = false;
|
|
16
|
+
private pool?: Pool;
|
|
17
|
+
private client?: PoolClient;
|
|
18
|
+
protected get Client(): PoolClient {
|
|
19
|
+
if (this.client === undefined) {
|
|
20
|
+
throw new Error("Please call this.PoolClient after using the setClient method.");
|
|
21
|
+
}
|
|
22
|
+
return this.client;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
protected async commit(): Promise<void> {
|
|
26
|
+
await this.Client.query('COMMIT');
|
|
27
|
+
this.isExecuteRollback = false;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
protected async rollback(): Promise<void> {
|
|
31
|
+
if (this.isExecuteRollback) {
|
|
32
|
+
await this.Client.query('ROLLBACK');
|
|
33
|
+
}
|
|
34
|
+
this.isExecuteRollback = false;
|
|
35
|
+
}
|
|
36
|
+
|
|
5
37
|
// **********************************************************************
|
|
6
38
|
// こちらのメソッド、プロパティを各サブクラスで設定してください
|
|
7
39
|
// **********************************************************************
|
|
@@ -28,10 +60,32 @@ export class BaseCron {
|
|
|
28
60
|
get CronCode(): string { return this.cronCode; }
|
|
29
61
|
|
|
30
62
|
public async setUp() {
|
|
63
|
+
if (this.dbUser === undefined) {
|
|
64
|
+
throw new Error("Database user is not configured");
|
|
65
|
+
}
|
|
66
|
+
if (this.dbHost === undefined) {
|
|
67
|
+
throw new Error("Database host is not configured");
|
|
68
|
+
}
|
|
69
|
+
if (this.dbName === undefined) {
|
|
70
|
+
throw new Error("Database name is not configured");
|
|
71
|
+
}
|
|
72
|
+
if (this.dbPassword === undefined) {
|
|
73
|
+
throw new Error("Database password is not configured");
|
|
74
|
+
}
|
|
75
|
+
if (this.dbPort === undefined) {
|
|
76
|
+
throw new Error("Database port is not configured");
|
|
77
|
+
}
|
|
31
78
|
|
|
79
|
+
this.pool = PoolManager.getPool(this.dbUser, this.dbHost, this.dbName, this.dbPassword, this.dbPort, this.dbIsSslConnect);
|
|
80
|
+
this.pool.query(`SET TIME ZONE '${process.env.TZ ?? 'Asia/Tokyo'}';`);
|
|
81
|
+
this.client = await this.pool.connect();
|
|
82
|
+
await this.Client.query('BEGIN');
|
|
83
|
+
this.isExecuteRollback = true;
|
|
32
84
|
}
|
|
33
85
|
|
|
34
86
|
public async tearDown() {
|
|
35
|
-
|
|
87
|
+
if (this.isExecuteRollback === false) {
|
|
88
|
+
this.rollback();
|
|
89
|
+
}
|
|
36
90
|
}
|
|
37
91
|
}
|
package/src/index.ts
CHANGED
|
@@ -18,5 +18,6 @@ export { MigrateDatabase } from './models/MigrateDatabase';
|
|
|
18
18
|
export { migrate, rollback } from './models/MigrateRollback';
|
|
19
19
|
|
|
20
20
|
// cron
|
|
21
|
+
export { DayType, MonthType, DateType, HourType, MinuteSecondType } from './cron/CronType';
|
|
21
22
|
export { BaseCron } from './cron/BaseCron';
|
|
22
23
|
export { runCron } from './cron/CronExecuter';
|
|
File without changes
|