pg-mvc-service 2.0.43 → 2.0.45
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/models/TableDoc.js +1 -1
- package/index.d.ts +189 -0
- package/package.json +1 -5
- package/src/PoolManager.ts +48 -0
- package/src/Service.ts +276 -0
- package/src/Utils/DateTimeUtil.ts +146 -0
- package/src/Utils/NumberUtil.ts +23 -0
- package/src/Utils/StringUtil.ts +33 -0
- package/src/assets/favicon.ico +0 -0
- package/src/clients/AwsS3Client.ts +310 -0
- package/src/clients/Base64Client.ts +305 -0
- package/src/clients/EncryptClient.ts +100 -0
- package/src/clients/StringClient.ts +19 -0
- package/src/cron/BaseCron.ts +122 -0
- package/src/cron/CronExecuter.ts +34 -0
- package/src/cron/CronType.ts +25 -0
- package/src/documents/Swagger.ts +105 -0
- package/src/exceptions/Exception.ts +72 -0
- package/src/index.ts +23 -0
- package/src/models/ExpressionClient.ts +72 -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/UpdateExpression.ts +29 -0
- package/src/models/SqlUtils/ValidateValueUtil.ts +354 -0
- package/src/models/SqlUtils/WhereExpression.ts +421 -0
- package/src/models/TableDoc.ts +369 -0
- package/src/models/TableModel.ts +701 -0
- package/src/models/Type.ts +62 -0
- package/src/models/Utils/MessageUtil.ts +60 -0
- package/src/models/ValidateClient.ts +182 -0
- package/src/reqestResponse/ReqResType.ts +170 -0
- package/src/reqestResponse/RequestType.ts +918 -0
- package/src/reqestResponse/ResponseType.ts +420 -0
- package/tsconfig.json +14 -0
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import { Pool } from "pg";
|
|
2
|
+
|
|
3
|
+
export class MigrateDatabase {
|
|
4
|
+
|
|
5
|
+
private dbName: string;
|
|
6
|
+
get DbName(): string { return this.dbName; }
|
|
7
|
+
private userName: string;
|
|
8
|
+
get UserName(): string { return this.userName; }
|
|
9
|
+
private password: string | null = null;
|
|
10
|
+
get Password(): string | null {
|
|
11
|
+
return this.password;
|
|
12
|
+
}
|
|
13
|
+
private pool: Pool;
|
|
14
|
+
|
|
15
|
+
constructor (dbName: string, userName: string, pool: Pool) {
|
|
16
|
+
this.dbName = dbName;
|
|
17
|
+
this.userName = userName;
|
|
18
|
+
this.pool = pool;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
public async IsExistUser(): Promise<boolean> {
|
|
22
|
+
const sql = `
|
|
23
|
+
SELECT count(*) > 0 as is_exist
|
|
24
|
+
FROM pg_roles
|
|
25
|
+
WHERE rolname = '${this.UserName}';
|
|
26
|
+
`;
|
|
27
|
+
const datas = await this.pool.query(sql);
|
|
28
|
+
return datas.rows[0].is_exist;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
public async CreateUser(password: string = ''): Promise<void> {
|
|
32
|
+
if (password.trim() === '') {
|
|
33
|
+
password = '';
|
|
34
|
+
|
|
35
|
+
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@$%^&*_+|;:.<>?';
|
|
36
|
+
for (let i = 0; i < 36; i++) {
|
|
37
|
+
const randomIndex = Math.floor(Math.random() * characters.length);
|
|
38
|
+
password += characters[randomIndex];
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
this.password = password;
|
|
42
|
+
|
|
43
|
+
const sql = `
|
|
44
|
+
DO $$
|
|
45
|
+
BEGIN
|
|
46
|
+
IF NOT EXISTS (
|
|
47
|
+
SELECT FROM pg_catalog.pg_roles WHERE rolname = '${this.UserName}'
|
|
48
|
+
) THEN
|
|
49
|
+
CREATE USER ${this.UserName} WITH PASSWORD '${password}';
|
|
50
|
+
END IF;
|
|
51
|
+
END
|
|
52
|
+
$$;
|
|
53
|
+
|
|
54
|
+
GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA public TO ${this.UserName};
|
|
55
|
+
|
|
56
|
+
ALTER DEFAULT PRIVILEGES IN SCHEMA public
|
|
57
|
+
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLES TO ${this.UserName};
|
|
58
|
+
`;
|
|
59
|
+
await this.pool.query(sql);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
public async IsExistDb(): Promise<boolean> {
|
|
63
|
+
const sql = `
|
|
64
|
+
SELECT count(*) > 0 as is_exist
|
|
65
|
+
FROM pg_database
|
|
66
|
+
WHERE datname = '${this.DbName}';
|
|
67
|
+
`;
|
|
68
|
+
const datas = await this.pool.query(sql);
|
|
69
|
+
return datas.rows[0].is_exist;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
public async CreateDb(collateType: string = 'C'): Promise<void> {
|
|
73
|
+
const sql = `
|
|
74
|
+
CREATE DATABASE ${this.DbName}
|
|
75
|
+
WITH OWNER = ${this.UserName}
|
|
76
|
+
ENCODING = 'UTF8'
|
|
77
|
+
LC_COLLATE = '${collateType}'
|
|
78
|
+
LC_CTYPE = '${collateType}'
|
|
79
|
+
CONNECTION LIMIT = -1;
|
|
80
|
+
`;
|
|
81
|
+
await this.pool.query(sql);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
public RollbackDbSql(): string {
|
|
85
|
+
const sql = `
|
|
86
|
+
-- ${this.DbName}データベースに接続しているすべてのセッションを強制終了
|
|
87
|
+
SELECT pg_terminate_backend(pid)
|
|
88
|
+
FROM pg_stat_activity
|
|
89
|
+
WHERE datname = '${this.DbName}';
|
|
90
|
+
|
|
91
|
+
-- DB削除
|
|
92
|
+
DROP DATABASE IF EXISTS ${this.DbName};`;
|
|
93
|
+
return this.trimSpaceLineSql(sql);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
public RollbackUserSql(otherUserName: string): string {
|
|
97
|
+
const sql = `
|
|
98
|
+
-- 1. すべてのセッションを強制終了
|
|
99
|
+
SELECT pg_terminate_backend(pg_stat_activity.pid)
|
|
100
|
+
FROM pg_stat_activity
|
|
101
|
+
WHERE usename = '${this.UserName}';
|
|
102
|
+
|
|
103
|
+
-- 2. 所有オブジェクトを ${otherUserName} に移行
|
|
104
|
+
REASSIGN OWNED BY ${this.UserName} TO ${otherUserName};
|
|
105
|
+
|
|
106
|
+
-- 2. すべての権限を削除
|
|
107
|
+
DROP OWNED BY ${this.UserName} CASCADE;
|
|
108
|
+
|
|
109
|
+
-- 3. ロールを削除
|
|
110
|
+
DROP ROLE IF EXISTS ${this.UserName};`;
|
|
111
|
+
return this.trimSpaceLineSql(sql);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
private trimSpaceLineSql(str: string) {
|
|
115
|
+
const splitLines = str.split('\n');
|
|
116
|
+
let sql = '';
|
|
117
|
+
for (let line of splitLines) {
|
|
118
|
+
line = line.replace(/\s+/g, ' ').trim(); // 複数のスペースを一つに置き換え
|
|
119
|
+
|
|
120
|
+
if (line.startsWith('--') && sql[sql.length - 1] != '\n') {
|
|
121
|
+
line = '\n' + line;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
if (line.length > 0) {
|
|
125
|
+
if (line.includes('--') === false) {
|
|
126
|
+
sql += line + ' ';
|
|
127
|
+
} else {
|
|
128
|
+
sql += line + '\n';
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
return sql;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
@@ -0,0 +1,151 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
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
|
+
static createDateTime(column: string | TColumnInfo, to: 'date' | 'time' | 'datetime') {
|
|
87
|
+
const columnQuery = typeof column === 'string' ? column : column.model.getColumn(column.name).expression;
|
|
88
|
+
switch (to) {
|
|
89
|
+
case 'date':
|
|
90
|
+
return `to_char(${columnQuery}, 'YYYY-MM-DD')`;
|
|
91
|
+
case 'datetime':
|
|
92
|
+
return `to_char(${columnQuery}, 'YYYY-MM-DD HH24:mi:ss')`;
|
|
93
|
+
case 'time':
|
|
94
|
+
return `to_char(${columnQuery}, 'HH24:mi:ss')`;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
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
|
+
}
|