pg-mvc-service 1.0.22 → 1.0.24
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/ExpressionClient.js +56 -0
- package/dist/models/TableModel.js +10 -0
- package/index.d.ts +0 -81
- package/package.json +1 -1
- package/src/models/ExpressionClient.ts +61 -0
- package/src/models/TableModel.ts +13 -0
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
class ExpressionClient {
|
|
4
|
+
constructor(model) {
|
|
5
|
+
this.model = model;
|
|
6
|
+
}
|
|
7
|
+
toSqlStringValue(value) {
|
|
8
|
+
if (typeof value === 'string' || typeof value === 'number') {
|
|
9
|
+
return `"${value}"`;
|
|
10
|
+
}
|
|
11
|
+
throw new Error('Please enter a value of type string or number.');
|
|
12
|
+
}
|
|
13
|
+
toSqlNumberValue(value) {
|
|
14
|
+
if (typeof value === 'number') {
|
|
15
|
+
return value.toString();
|
|
16
|
+
}
|
|
17
|
+
else if (typeof value === 'string') {
|
|
18
|
+
if (value.trim() !== "" || isNaN(Number(value)) === false) {
|
|
19
|
+
return value;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
throw new Error('Please enter a value of type number or a string of half-width digits.');
|
|
23
|
+
}
|
|
24
|
+
createCaseFromObject(column, obj, elseValue) {
|
|
25
|
+
const columnInfo = typeof column === 'string' ? this.model.getColumn(column) : column.model.getColumn(column.name);
|
|
26
|
+
if (columnInfo.type !== 'string' && columnInfo.type !== 'integer') {
|
|
27
|
+
throw new Error('This method cannot be used for columns other than integer, real, or string.');
|
|
28
|
+
}
|
|
29
|
+
const whenExpression = Object.entries(obj).map(([key, value]) => {
|
|
30
|
+
let expression = `WHEN ${columnInfo.expression} = `;
|
|
31
|
+
// 今は文字列と数値だけだが、他のも対応しないといけないかも
|
|
32
|
+
if (columnInfo.type === 'string') {
|
|
33
|
+
expression += this.toSqlStringValue(key);
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
expression += this.toSqlNumberValue(key);
|
|
37
|
+
}
|
|
38
|
+
expression += ` THEN `;
|
|
39
|
+
if (value === null) {
|
|
40
|
+
expression += 'null';
|
|
41
|
+
}
|
|
42
|
+
else if (typeof value === 'string') {
|
|
43
|
+
expression += `"${value}"`;
|
|
44
|
+
}
|
|
45
|
+
else if (typeof value === 'boolean') {
|
|
46
|
+
expression += `${value}`;
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
expression += `${value}`;
|
|
50
|
+
}
|
|
51
|
+
return expression;
|
|
52
|
+
});
|
|
53
|
+
return `CASE ${whenExpression.join(' ')} END`;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
exports.default = ExpressionClient;
|
|
@@ -18,6 +18,7 @@ const SelectExpression_1 = __importDefault(require("./SqlUtils/SelectExpression"
|
|
|
18
18
|
const WhereExpression_1 = __importDefault(require("./SqlUtils/WhereExpression"));
|
|
19
19
|
const ValidateClient_1 = __importDefault(require("./ValidateClient"));
|
|
20
20
|
const Exception_1 = require("../exceptions/Exception");
|
|
21
|
+
const ExpressionClient_1 = __importDefault(require("./ExpressionClient"));
|
|
21
22
|
class TableModel {
|
|
22
23
|
get DbName() { return this.dbName; }
|
|
23
24
|
get TableName() {
|
|
@@ -92,6 +93,9 @@ class TableModel {
|
|
|
92
93
|
this.Offset = (value - 1) * this.PageCount;
|
|
93
94
|
}
|
|
94
95
|
}
|
|
96
|
+
get Client() {
|
|
97
|
+
return this.client;
|
|
98
|
+
}
|
|
95
99
|
constructor(client, tableAlias) {
|
|
96
100
|
this.dbName = "default";
|
|
97
101
|
this.tableName = "";
|
|
@@ -674,5 +678,11 @@ class TableModel {
|
|
|
674
678
|
}
|
|
675
679
|
return this.validateClient;
|
|
676
680
|
}
|
|
681
|
+
get ExpressionClient() {
|
|
682
|
+
if (this.expressionClient === undefined) {
|
|
683
|
+
this.expressionClient = new ExpressionClient_1.default(this);
|
|
684
|
+
}
|
|
685
|
+
return this.expressionClient;
|
|
686
|
+
}
|
|
677
687
|
}
|
|
678
688
|
exports.TableModel = TableModel;
|
package/index.d.ts
CHANGED
|
@@ -146,87 +146,6 @@ declare module 'pg-mvc-service' {
|
|
|
146
146
|
constructor(errorId: string, message?: string);
|
|
147
147
|
}
|
|
148
148
|
|
|
149
|
-
// export class TableModel {
|
|
150
|
-
// protected readonly dbName: string;
|
|
151
|
-
// get DbName(): string;
|
|
152
|
-
// protected readonly tableName: string;
|
|
153
|
-
// get TableName(): string;
|
|
154
|
-
// protected readonly tableDescription: string;
|
|
155
|
-
// get TableDescription(): string;
|
|
156
|
-
// protected readonly comment: string;
|
|
157
|
-
// get Comment(): string;
|
|
158
|
-
// protected readonly columns: { [key: string]: TColumn };
|
|
159
|
-
// get Columns(): { [key: string]: TColumn };
|
|
160
|
-
// protected readonly references: Array<{table: string, columns: Array<{target: string, ref: string}>}>;
|
|
161
|
-
// get References(): Array<{table: string, columns: Array<{target: string, ref: string}>}>;
|
|
162
|
-
// public GetReferences(columnName: string): Array<{table: string, columns: Array<{target: string, ref: string}>}>;
|
|
163
|
-
// get TableAlias(): string;
|
|
164
|
-
// public IsOutputLog: boolean;
|
|
165
|
-
// public SortKeyword: TSortKeyword;
|
|
166
|
-
// public Offset: number;
|
|
167
|
-
// public Limit: number;
|
|
168
|
-
// public PageCount: number;
|
|
169
|
-
// set OffsetPage(value: number);
|
|
170
|
-
|
|
171
|
-
// constructor(client: Pool);
|
|
172
|
-
// constructor(client: Pool, tableAlias: string);
|
|
173
|
-
// constructor(client: PoolClient);
|
|
174
|
-
// constructor(client: PoolClient, tableAlias: string);
|
|
175
|
-
|
|
176
|
-
// public select(): void;
|
|
177
|
-
// public select(columls: Array<string | {name: string, alias?: string, func?: TAggregateFuncType}> | '*'): void;
|
|
178
|
-
// public select(columls: Array<string | {name: string, alias?: string, func?: TAggregateFuncType}> | '*', model: TableModel): void;
|
|
179
|
-
// public select(columls: Array<string | {name: string, alias?: string, func?: TAggregateFuncType}> | '*', keyFormat: TKeyFormat): void;
|
|
180
|
-
// public select(columls: Array<string | {name: string, alias?: string, func?: TAggregateFuncType}> | '*', model: TableModel, keyFormat: TKeyFormat): void;
|
|
181
|
-
// public select(expression: string, alias: string): void;
|
|
182
|
-
|
|
183
|
-
// public join(joinType: 'left' | 'inner', joinModel: TableModel, conditions: Array<TNestedCondition>): void;
|
|
184
|
-
|
|
185
|
-
// public where(expression: string): void;
|
|
186
|
-
// public where(conditions: Array<TNestedCondition>): void;
|
|
187
|
-
// public where(left: string, operator: TOperator, right: any | TColumnInfo): void;
|
|
188
|
-
// public where(left: TColumnInfo, operator: TOperator, right: any | TColumnInfo): void;
|
|
189
|
-
|
|
190
|
-
// public find<T = {[key: string]: any}>(pk: {[key: string]: any}, selectColumns: Array<string> | "*" | null, selectExpressions: Array<TSelectExpression> | null, keyFormat: TKeyFormat): Promise<T | null>;
|
|
191
|
-
// public find<T = {[key: string]: any}>(pk: {[key: string]: any}, selectColumns: Array<string> | "*" | null, selectExpressions: Array<TSelectExpression> | null): Promise<T | null>;
|
|
192
|
-
// public find<T = {[key: string]: any}>(pk: {[key: string]: any}, selectColumns: Array<string> | "*" | null): Promise<T | null>;
|
|
193
|
-
// public find<T = {[key: string]: any}>(pk: {[key: string]: any}): Promise<T | null>;
|
|
194
|
-
|
|
195
|
-
// public findId<T = {[key: string]: any}>(id: any, selectColumns: Array<string> | "*" | null, selectExpressions: Array<TSelectExpression> | null, keyFormat: TKeyFormat): Promise<T | null>;
|
|
196
|
-
// public findId<T = {[key: string]: any}>(id: any, selectColumns: Array<string> | "*" | null, selectExpressions: Array<TSelectExpression> | null): Promise<T | null>;
|
|
197
|
-
// public findId<T = {[key: string]: any}>(id: any, selectColumns: Array<string> | "*" | null): Promise<T | null>;
|
|
198
|
-
// public findId<T = {[key: string]: any}>(id: any): Promise<T | null>;
|
|
199
|
-
|
|
200
|
-
// protected readonly errorMessages: Record<TColumnType | 'length' | 'null' | 'notInput', string>
|
|
201
|
-
// protected throwValidationError(code: string, message: string): never;
|
|
202
|
-
// protected validateOptions(options: {[key: string]: any}, isInsert: boolean) : Promise<void>;
|
|
203
|
-
// protected validateInsert(options: {[key: string]: any}) : Promise<void>;
|
|
204
|
-
// protected validateUpdate(options: {[key: string]: any}) : Promise<void>;
|
|
205
|
-
// protected validateUpdateId(id: any, options: {[key: string]: any}) : Promise<void>;
|
|
206
|
-
// protected validateDelete() : Promise<void>;
|
|
207
|
-
// protected validateDeleteId(id: any) : Promise<void>;
|
|
208
|
-
|
|
209
|
-
// public executeInsert(options: {[key: string]: any}) : Promise<void>;
|
|
210
|
-
// public executeUpdate(options: {[key: string]: any}) : Promise<number>;
|
|
211
|
-
// public executeUpdateId(id: any, options: {[key: string]: any}) : Promise<void>;
|
|
212
|
-
// public executeDelete() : Promise<number>;
|
|
213
|
-
// public executeDeleteId(id: any) : Promise<void>;
|
|
214
|
-
|
|
215
|
-
// public executeSelect<T = {[key: string]: any}>(): Promise<Array<T>>;
|
|
216
|
-
// public executeSelectWithCount<T = any>(): Promise<{ datas: Array<T>, count: number, lastPage: number}>;
|
|
217
|
-
|
|
218
|
-
// protected executeQuery(param1: string, vars?: Array<any>) : Promise<any>;
|
|
219
|
-
// protected executeQuery(param1: TQuery) : Promise<any>;
|
|
220
|
-
|
|
221
|
-
// public orderBy(column: string | TColumnInfo, sortKeyword: TSortKeyword): void;
|
|
222
|
-
// public orderByList(column: string | TColumnInfo, list: Array<string | number | boolean | null>, sortKeyword: TSortKeyword): void;
|
|
223
|
-
// public orderBySentence(query: string, sortKeyword: TSortKeyword): void;
|
|
224
|
-
|
|
225
|
-
// public groupBy(column: string | TColumnInfo): void;
|
|
226
|
-
|
|
227
|
-
// get ValidateClient(): ValidateClient;
|
|
228
|
-
// }
|
|
229
|
-
|
|
230
149
|
export function createTableDoc(models: Array<TableModel>, serviceName?: string): string;
|
|
231
150
|
export function migrate(migrates: Array<MigrateTable>, poolParam: {
|
|
232
151
|
host: string, user: string, dbName: string, password: string, port?: number, isSsl?: boolean
|
package/package.json
CHANGED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { TableModel } from "./TableModel";
|
|
2
|
+
import { TColumnInfo } from "./Type";
|
|
3
|
+
|
|
4
|
+
export default class ExpressionClient {
|
|
5
|
+
private model: TableModel;
|
|
6
|
+
constructor(model: TableModel) {
|
|
7
|
+
this.model = model;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
public toSqlStringValue(value: any): string {
|
|
11
|
+
if (typeof value === 'string' || typeof value === 'number') {
|
|
12
|
+
return `"${value}"`;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
throw new Error('Please enter a value of type string or number.');
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
public toSqlNumberValue(value: any): string {
|
|
19
|
+
if (typeof value === 'number') {
|
|
20
|
+
return value.toString();
|
|
21
|
+
} else if (typeof value === 'string') {
|
|
22
|
+
if (value.trim() !== "" || isNaN(Number(value)) === false) {
|
|
23
|
+
return value;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
throw new Error('Please enter a value of type number or a string of half-width digits.');
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
public createCaseFromObject(column: string | TColumnInfo, obj: {[key: string | number]: string | number | boolean | null}, elseValue: string | number | boolean | null): string {
|
|
31
|
+
const columnInfo = typeof column === 'string' ? this.model.getColumn(column) : column.model.getColumn(column.name);
|
|
32
|
+
if (columnInfo.type !== 'string' && columnInfo.type !== 'integer') {
|
|
33
|
+
throw new Error('This method cannot be used for columns other than integer, real, or string.');
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const whenExpression = Object.entries(obj).map(([key, value]) => {
|
|
37
|
+
let expression = `WHEN ${columnInfo.expression} = `;
|
|
38
|
+
// 今は文字列と数値だけだが、他のも対応しないといけないかも
|
|
39
|
+
if (columnInfo.type === 'string') {
|
|
40
|
+
expression += this.toSqlStringValue(key);
|
|
41
|
+
} else {
|
|
42
|
+
expression += this.toSqlNumberValue(key);
|
|
43
|
+
}
|
|
44
|
+
expression += ` THEN `;
|
|
45
|
+
|
|
46
|
+
if (value === null) {
|
|
47
|
+
expression += 'null';
|
|
48
|
+
} else if (typeof value === 'string') {
|
|
49
|
+
expression += `"${value}"`;
|
|
50
|
+
} else if (typeof value === 'boolean') {
|
|
51
|
+
expression += `${value}`;
|
|
52
|
+
} else {
|
|
53
|
+
expression += `${value}`;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return expression;
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
return `CASE ${whenExpression.join(' ')} END`;
|
|
60
|
+
}
|
|
61
|
+
}
|
package/src/models/TableModel.ts
CHANGED
|
@@ -5,6 +5,7 @@ import SelectExpression from './SqlUtils/SelectExpression';
|
|
|
5
5
|
import WhereExpression from './SqlUtils/WhereExpression';
|
|
6
6
|
import ValidateClient from './ValidateClient';
|
|
7
7
|
import { DbConflictException, UnprocessableException } from '../exceptions/Exception';
|
|
8
|
+
import ExpressionClient from './ExpressionClient';
|
|
8
9
|
|
|
9
10
|
export class TableModel {
|
|
10
11
|
|
|
@@ -122,6 +123,9 @@ export class TableModel {
|
|
|
122
123
|
}
|
|
123
124
|
|
|
124
125
|
private client: PoolClient | Pool;
|
|
126
|
+
get Client(): PoolClient | Pool {
|
|
127
|
+
return this.client;
|
|
128
|
+
}
|
|
125
129
|
constructor(client: Pool);
|
|
126
130
|
constructor(client: Pool, tableAlias: string);
|
|
127
131
|
constructor(client: PoolClient);
|
|
@@ -757,4 +761,13 @@ export class TableModel {
|
|
|
757
761
|
|
|
758
762
|
return this.validateClient;
|
|
759
763
|
}
|
|
764
|
+
|
|
765
|
+
private expressionClient?: ExpressionClient;
|
|
766
|
+
get ExpressionClient(): ExpressionClient {
|
|
767
|
+
if (this.expressionClient === undefined) {
|
|
768
|
+
this.expressionClient = new ExpressionClient(this);
|
|
769
|
+
}
|
|
770
|
+
|
|
771
|
+
return this.expressionClient;
|
|
772
|
+
}
|
|
760
773
|
}
|