ismx-nexo-node-app 0.3.36 → 0.3.37
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/js/index.js +24 -8
- package/dist/js/repository/RepositoryDatabasePostgres.js +14 -24
- package/dist/js/repository/RepositoryRestFormal.js +3 -3
- package/dist/types/index.d.ts +16 -8
- package/dist/types/repository/RepositoryDatabase.d.ts +0 -2
- package/dist/types/repository/RepositoryDatabasePostgres.d.ts +1 -2
- package/package.json +1 -1
- package/src/main/node/index.ts +8 -8
- package/src/main/node/repository/RepositoryDatabase.ts +0 -3
- package/src/main/node/repository/RepositoryDatabasePostgres.ts +15 -24
- package/src/main/node/repository/RepositoryRestFormal.ts +0 -1
package/dist/js/index.js
CHANGED
|
@@ -54,19 +54,33 @@ exports.BusinessState = BusinessState_1.default;
|
|
|
54
54
|
const BusinessProxy_1 = __importDefault(require("./business/BusinessProxy"));
|
|
55
55
|
exports.BusinessProxy = BusinessProxy_1.default;
|
|
56
56
|
const BusinessServer_1 = __importDefault(require("./business/BusinessServer"));
|
|
57
|
-
|
|
57
|
+
class BusinessServer extends BusinessServer_1.default {
|
|
58
|
+
}
|
|
59
|
+
exports.BusinessServer = BusinessServer;
|
|
58
60
|
const BusinessThread_1 = __importDefault(require("./business/BusinessThread"));
|
|
59
|
-
|
|
61
|
+
class BusinessThread extends BusinessThread_1.default {
|
|
62
|
+
}
|
|
63
|
+
exports.BusinessThread = BusinessThread;
|
|
60
64
|
const BusinessErrors_1 = __importDefault(require("./business/BusinessErrors"));
|
|
61
|
-
|
|
65
|
+
class BusinessErrors extends BusinessErrors_1.default {
|
|
66
|
+
}
|
|
67
|
+
exports.BusinessErrors = BusinessErrors;
|
|
62
68
|
const BusinessLogger_1 = __importDefault(require("./business/BusinessLogger"));
|
|
63
|
-
|
|
69
|
+
class BusinessLogger extends BusinessLogger_1.default {
|
|
70
|
+
}
|
|
71
|
+
exports.BusinessLogger = BusinessLogger;
|
|
64
72
|
const CryptoUtils_1 = __importDefault(require("./business/utils/CryptoUtils"));
|
|
65
|
-
|
|
73
|
+
class CryptoUtils extends CryptoUtils_1.default {
|
|
74
|
+
}
|
|
75
|
+
exports.CryptoUtils = CryptoUtils;
|
|
66
76
|
const NumberUtils_1 = __importDefault(require("./business/utils/NumberUtils"));
|
|
67
|
-
|
|
77
|
+
class NumberUtils extends NumberUtils_1.default {
|
|
78
|
+
}
|
|
79
|
+
exports.NumberUtils = NumberUtils;
|
|
68
80
|
const StringUtils_1 = __importDefault(require("./business/utils/StringUtils"));
|
|
69
|
-
|
|
81
|
+
class StringUtils extends StringUtils_1.default {
|
|
82
|
+
}
|
|
83
|
+
exports.StringUtils = StringUtils;
|
|
70
84
|
/**************************************************************/
|
|
71
85
|
const Repository_1 = __importDefault(require("./repository/Repository"));
|
|
72
86
|
exports.Repository = Repository_1.default;
|
|
@@ -79,4 +93,6 @@ class RepositoryDatabasePostgres extends RepositoryDatabasePostgres_1.default {
|
|
|
79
93
|
}
|
|
80
94
|
exports.RepositoryDatabasePostgres = RepositoryDatabasePostgres;
|
|
81
95
|
const QueryUtils_1 = __importDefault(require("./repository/utils/QueryUtils"));
|
|
82
|
-
|
|
96
|
+
class QueryUtils extends QueryUtils_1.default {
|
|
97
|
+
}
|
|
98
|
+
exports.QueryUtils = QueryUtils;
|
|
@@ -20,14 +20,12 @@ class RepositoryDatabasePostgres extends RepositoryDatabase_1.default {
|
|
|
20
20
|
super();
|
|
21
21
|
this.introspectIntervalTime = 60 * 60 * 1000;
|
|
22
22
|
this.tables = {};
|
|
23
|
-
this.schema = "public";
|
|
24
23
|
this.onIntrospectedListeners = [];
|
|
25
24
|
this.native = this.native.bind(this);
|
|
26
25
|
this.introspect = this.introspect.bind(this);
|
|
27
26
|
}
|
|
28
27
|
init(chain) {
|
|
29
28
|
super.init(chain);
|
|
30
|
-
this.schema = chain.schema || "public";
|
|
31
29
|
this.connect(chain);
|
|
32
30
|
}
|
|
33
31
|
addOnIntrospectedListener(listener) {
|
|
@@ -73,24 +71,12 @@ class RepositoryDatabasePostgres extends RepositoryDatabase_1.default {
|
|
|
73
71
|
return __awaiter(this, void 0, void 0, function* () {
|
|
74
72
|
if (!this.tables[tableName])
|
|
75
73
|
throw new Error(`table ${tableName} does not exist`);
|
|
74
|
+
let schema = this.tables[tableName][0].schema;
|
|
76
75
|
let { where, values } = this.toWhere(tableName, filters);
|
|
77
|
-
let query = `SELECT * FROM ${
|
|
76
|
+
let query = `SELECT * FROM ${schema}.${tableName} WHERE ${where}`;
|
|
78
77
|
return this.query(query, values);
|
|
79
78
|
});
|
|
80
79
|
}
|
|
81
|
-
all_depr(tableName_1) {
|
|
82
|
-
return __awaiter(this, arguments, void 0, function* (tableName, options = {}) {
|
|
83
|
-
var _a, _b;
|
|
84
|
-
if (!this.tables[tableName])
|
|
85
|
-
throw new Error(`table ${tableName} does not exist`);
|
|
86
|
-
let table = this.tables[tableName];
|
|
87
|
-
let schema = (_a = options.schema) !== null && _a !== void 0 ? _a : this.schema;
|
|
88
|
-
return this.query(`
|
|
89
|
-
SELECT * FROM ${schema}.${tableName}
|
|
90
|
-
WHERE ${(_b = options.filters) !== null && _b !== void 0 ? _b : "TRUE"}
|
|
91
|
-
`);
|
|
92
|
-
});
|
|
93
|
-
}
|
|
94
80
|
add(tableName, object, id) {
|
|
95
81
|
return __awaiter(this, void 0, void 0, function* () {
|
|
96
82
|
return this.addAll(tableName, [Object.assign({ id: id }, object)]).then((rows) => rows[0]);
|
|
@@ -101,8 +87,9 @@ class RepositoryDatabasePostgres extends RepositoryDatabase_1.default {
|
|
|
101
87
|
if (!this.tables[tableName])
|
|
102
88
|
throw new Error(`table ${tableName} does not exist`);
|
|
103
89
|
let table = this.tables[tableName];
|
|
90
|
+
let schema = table[0].schema;
|
|
104
91
|
let columns = PostgresUtils_1.default.columns(table);
|
|
105
|
-
let query = `INSERT INTO ${
|
|
92
|
+
let query = `INSERT INTO ${schema}.${tableName} (${columns.join(",")}) VALUES `;
|
|
106
93
|
let values = [];
|
|
107
94
|
let index = 1;
|
|
108
95
|
for (let object of objects) {
|
|
@@ -123,10 +110,11 @@ class RepositoryDatabasePostgres extends RepositoryDatabase_1.default {
|
|
|
123
110
|
if (!this.tables[tableName])
|
|
124
111
|
throw new Error(`table ${tableName} does not exist`);
|
|
125
112
|
let table = this.tables[tableName];
|
|
113
|
+
let schema = table[0].schema;
|
|
126
114
|
let columns = PostgresUtils_1.default.columns(table);
|
|
127
115
|
let params = columns.map((column, index) => `\$${index + 2}`).join(",");
|
|
128
116
|
let values = [id, ...columns.map((column) => object[PostgresUtils_1.default.stringToCamel(column)])];
|
|
129
|
-
let query = `UPDATE ${
|
|
117
|
+
let query = `UPDATE ${schema}.${tableName} SET (${columns.join(",")}) = (${params}) WHERE id = $1 `;
|
|
130
118
|
query += `RETURNING *`;
|
|
131
119
|
return this.query(query, values).then((result) => result[0]);
|
|
132
120
|
});
|
|
@@ -136,10 +124,11 @@ class RepositoryDatabasePostgres extends RepositoryDatabase_1.default {
|
|
|
136
124
|
if (!this.tables[tableName])
|
|
137
125
|
throw new Error(`table ${tableName} does not exist`);
|
|
138
126
|
let table = this.tables[tableName];
|
|
127
|
+
let schema = table[0].schema;
|
|
139
128
|
let columns = PostgresUtils_1.default.columns(table);
|
|
140
129
|
let offset = maxResults * pageNumber;
|
|
141
130
|
let { where, values } = this.toWhere(tableName, filters, "", 4);
|
|
142
|
-
let query = `SELECT * FROM ${
|
|
131
|
+
let query = `SELECT * FROM ${schema}.${tableName} WHERE ${where} ORDER BY $1 LIMIT $2 OFFSET $3`;
|
|
143
132
|
let elements = this.query(query, [sortKey, maxResults, offset, ...values]);
|
|
144
133
|
let total = this.count(tableName, filters);
|
|
145
134
|
return Promise.all([total, elements]).then(([total, elements]) => ({ total, elements }));
|
|
@@ -149,8 +138,9 @@ class RepositoryDatabasePostgres extends RepositoryDatabase_1.default {
|
|
|
149
138
|
return __awaiter(this, void 0, void 0, function* () {
|
|
150
139
|
if (!this.tables[tableName])
|
|
151
140
|
throw new Error(`table ${tableName} does not exist`);
|
|
141
|
+
let schema = this.tables[tableName][0].schema;
|
|
152
142
|
let { where, values } = this.toWhere(tableName, filters);
|
|
153
|
-
let query = `SELECT count(*) as count FROM ${
|
|
143
|
+
let query = `SELECT count(*) as count FROM ${schema}.${tableName} WHERE ${where}`;
|
|
154
144
|
return this.query(query, values).then((result) => result[0].count);
|
|
155
145
|
});
|
|
156
146
|
}
|
|
@@ -158,8 +148,9 @@ class RepositoryDatabasePostgres extends RepositoryDatabase_1.default {
|
|
|
158
148
|
return __awaiter(this, void 0, void 0, function* () {
|
|
159
149
|
if (!this.tables[tableName])
|
|
160
150
|
throw new Error(`table ${tableName} does not exist`);
|
|
151
|
+
let schema = this.tables[tableName][0].schema;
|
|
161
152
|
let { where, values } = this.toWhere(tableName, filters);
|
|
162
|
-
let query = `SELECT json_agg(u.${select}) as list FROM ${
|
|
153
|
+
let query = `SELECT json_agg(u.${select}) as list FROM ${schema}.${tableName} u WHERE ${where}`;
|
|
163
154
|
return this.query(query, values).then((result) => result[0].list);
|
|
164
155
|
});
|
|
165
156
|
}
|
|
@@ -193,11 +184,10 @@ class RepositoryDatabasePostgres extends RepositoryDatabase_1.default {
|
|
|
193
184
|
return __awaiter(this, void 0, void 0, function* () {
|
|
194
185
|
let result = yield this.native(`
|
|
195
186
|
SELECT table_name as name,
|
|
196
|
-
json_agg(json_build_object('name', column_name, 'default', column_default)) as columns
|
|
187
|
+
json_agg(json_build_object('name', column_name, 'schema', table_schema, 'default', column_default)) as columns
|
|
197
188
|
FROM information_schema.columns
|
|
198
|
-
WHERE (table_schema = $1 or table_schema = 'public')
|
|
199
189
|
GROUP BY table_name
|
|
200
|
-
|
|
190
|
+
`);
|
|
201
191
|
for (let table of result.rows)
|
|
202
192
|
this.tables[table["name"]] = table["columns"];
|
|
203
193
|
this.onIntrospectedListeners.forEach((l) => l(this.tables));
|
|
@@ -29,15 +29,15 @@ class RepositoryRestFormal extends RepositoryRest_1.default {
|
|
|
29
29
|
if (json.code === "0")
|
|
30
30
|
return json.data;
|
|
31
31
|
else { // @ts-ignore
|
|
32
|
-
throw new
|
|
32
|
+
throw new FormalError(json.code, json.description, call);
|
|
33
33
|
}
|
|
34
34
|
}))
|
|
35
35
|
.catch((error) => {
|
|
36
36
|
// @ts-ignore
|
|
37
|
-
if (error instanceof
|
|
37
|
+
if (error instanceof FormalError)
|
|
38
38
|
throw error;
|
|
39
39
|
else { // @ts-ignore
|
|
40
|
-
throw new
|
|
40
|
+
throw new FormalError("-1", "ConnectionError", undefined, error);
|
|
41
41
|
}
|
|
42
42
|
});
|
|
43
43
|
});
|
package/dist/types/index.d.ts
CHANGED
|
@@ -26,21 +26,28 @@ export declare const BusinessProxy: typeof BaseBusinessProxy;
|
|
|
26
26
|
export interface Module extends BaseModule {
|
|
27
27
|
}
|
|
28
28
|
import BaseBusinessServer from "./business/BusinessServer";
|
|
29
|
-
export declare
|
|
29
|
+
export declare class BusinessServer extends BaseBusinessServer {
|
|
30
|
+
}
|
|
30
31
|
import BaseBusinessThread from "./business/BusinessThread";
|
|
31
|
-
export declare
|
|
32
|
+
export declare abstract class BusinessThread extends BaseBusinessThread {
|
|
33
|
+
}
|
|
32
34
|
import BaseBusinessErrors, { FormalError as BaseFormalError } from "./business/BusinessErrors";
|
|
33
|
-
export declare
|
|
35
|
+
export declare class BusinessErrors extends BaseBusinessErrors {
|
|
36
|
+
}
|
|
34
37
|
export interface FormalError extends BaseFormalError {
|
|
35
38
|
}
|
|
36
39
|
import BaseBusinessLogger from "./business/BusinessLogger";
|
|
37
|
-
export declare
|
|
40
|
+
export declare class BusinessLogger extends BaseBusinessLogger {
|
|
41
|
+
}
|
|
38
42
|
import _CryptoUtils from "./business/utils/CryptoUtils";
|
|
39
|
-
export declare
|
|
43
|
+
export declare abstract class CryptoUtils extends _CryptoUtils {
|
|
44
|
+
}
|
|
40
45
|
import _NumberUtils from "./business/utils/NumberUtils";
|
|
41
|
-
export declare
|
|
46
|
+
export declare abstract class NumberUtils extends _NumberUtils {
|
|
47
|
+
}
|
|
42
48
|
import _StringUtils from "./business/utils/StringUtils";
|
|
43
|
-
export declare
|
|
49
|
+
export declare abstract class StringUtils extends _StringUtils {
|
|
50
|
+
}
|
|
44
51
|
/**************************************************************/
|
|
45
52
|
import BaseRepository from "./repository/Repository";
|
|
46
53
|
export declare const Repository: typeof BaseRepository;
|
|
@@ -51,4 +58,5 @@ import _RepositoryDatabasePostgres from "./repository/RepositoryDatabasePostgres
|
|
|
51
58
|
export declare class RepositoryDatabasePostgres extends _RepositoryDatabasePostgres {
|
|
52
59
|
}
|
|
53
60
|
import _QueryUtils from "./repository/utils/QueryUtils";
|
|
54
|
-
export declare
|
|
61
|
+
export declare abstract class QueryUtils extends _QueryUtils {
|
|
62
|
+
}
|
|
@@ -10,7 +10,6 @@ export interface Chain {
|
|
|
10
10
|
user: string;
|
|
11
11
|
password: string;
|
|
12
12
|
database: string;
|
|
13
|
-
schema: string;
|
|
14
13
|
}
|
|
15
14
|
export interface Pagination<T> {
|
|
16
15
|
total: number;
|
|
@@ -38,7 +37,6 @@ export default abstract class RepositoryDatabase extends Repository {
|
|
|
38
37
|
abstract find<E>(tableName: string, filters: {
|
|
39
38
|
[key: string]: Primitive | Array<any>;
|
|
40
39
|
}): Promise<E[]>;
|
|
41
|
-
abstract all_depr<E>(tableName: string, options: QueryOptions): Promise<E[]>;
|
|
42
40
|
abstract add<E>(tableName: string, object: {
|
|
43
41
|
[key: string]: Primitive;
|
|
44
42
|
} | any, id?: string): Promise<E>;
|
|
@@ -2,13 +2,13 @@ import pg, { QueryResult } from "pg";
|
|
|
2
2
|
import RepositoryDatabase, { Chain, Pagination, Primitive, QueryOptions } from "./RepositoryDatabase";
|
|
3
3
|
export interface Column {
|
|
4
4
|
name: string;
|
|
5
|
+
schema: string;
|
|
5
6
|
default?: string;
|
|
6
7
|
}
|
|
7
8
|
export default class RepositoryDatabasePostgres extends RepositoryDatabase {
|
|
8
9
|
private readonly introspectIntervalTime;
|
|
9
10
|
protected client: pg.Client;
|
|
10
11
|
private tables;
|
|
11
|
-
private schema;
|
|
12
12
|
constructor();
|
|
13
13
|
init(chain: Chain): void;
|
|
14
14
|
private onIntrospectedListeners;
|
|
@@ -25,7 +25,6 @@ export default class RepositoryDatabasePostgres extends RepositoryDatabase {
|
|
|
25
25
|
find<T>(tableName: string, filters: {
|
|
26
26
|
[key: string]: Primitive | Array<any>;
|
|
27
27
|
}): Promise<T[]>;
|
|
28
|
-
all_depr<E>(tableName: string, options?: QueryOptions): Promise<E[]>;
|
|
29
28
|
add<E>(tableName: string, object: {
|
|
30
29
|
[key: string]: Primitive;
|
|
31
30
|
} | any, id?: string): Promise<E>;
|
package/package.json
CHANGED
package/src/main/node/index.ts
CHANGED
|
@@ -26,26 +26,26 @@ export const BusinessProxy = BaseBusinessProxy;
|
|
|
26
26
|
export interface Module extends BaseModule {}
|
|
27
27
|
|
|
28
28
|
import BaseBusinessServer from "./business/BusinessServer";
|
|
29
|
-
export
|
|
29
|
+
export class BusinessServer extends BaseBusinessServer {}
|
|
30
30
|
|
|
31
31
|
import BaseBusinessThread from "./business/BusinessThread";
|
|
32
|
-
export
|
|
32
|
+
export abstract class BusinessThread extends BaseBusinessThread {}
|
|
33
33
|
|
|
34
34
|
import BaseBusinessErrors, { FormalError as BaseFormalError } from "./business/BusinessErrors";
|
|
35
|
-
export
|
|
35
|
+
export class BusinessErrors extends BaseBusinessErrors {}
|
|
36
36
|
export interface FormalError extends BaseFormalError {}
|
|
37
37
|
|
|
38
38
|
import BaseBusinessLogger from "./business/BusinessLogger";
|
|
39
|
-
export
|
|
39
|
+
export class BusinessLogger extends BaseBusinessLogger {}
|
|
40
40
|
|
|
41
41
|
import _CryptoUtils from "./business/utils/CryptoUtils";
|
|
42
|
-
export
|
|
42
|
+
export abstract class CryptoUtils extends _CryptoUtils {}
|
|
43
43
|
|
|
44
44
|
import _NumberUtils from "./business/utils/NumberUtils";
|
|
45
|
-
export
|
|
45
|
+
export abstract class NumberUtils extends _NumberUtils {}
|
|
46
46
|
|
|
47
47
|
import _StringUtils from "./business/utils/StringUtils";
|
|
48
|
-
export
|
|
48
|
+
export abstract class StringUtils extends _StringUtils {}
|
|
49
49
|
|
|
50
50
|
/**************************************************************/
|
|
51
51
|
|
|
@@ -59,4 +59,4 @@ import _RepositoryDatabasePostgres from "./repository/RepositoryDatabasePostgres
|
|
|
59
59
|
export class RepositoryDatabasePostgres extends _RepositoryDatabasePostgres {}
|
|
60
60
|
|
|
61
61
|
import _QueryUtils from "./repository/utils/QueryUtils";
|
|
62
|
-
export
|
|
62
|
+
export abstract class QueryUtils extends _QueryUtils {}
|
|
@@ -12,7 +12,6 @@ export interface Chain {
|
|
|
12
12
|
user: string;
|
|
13
13
|
password: string;
|
|
14
14
|
database: string;
|
|
15
|
-
schema: string;
|
|
16
15
|
}
|
|
17
16
|
|
|
18
17
|
export interface Pagination<T> {
|
|
@@ -63,8 +62,6 @@ export default abstract class RepositoryDatabase extends Repository
|
|
|
63
62
|
|
|
64
63
|
abstract find<E>(tableName: string, filters: { [key: string]: Primitive | Array<any> }): Promise<E[]>;
|
|
65
64
|
|
|
66
|
-
abstract all_depr<E>(tableName: string, options: QueryOptions): Promise<E[]>;
|
|
67
|
-
|
|
68
65
|
abstract add<E>(tableName: string, object: {[key:string]:Primitive}|any, id?: string): Promise<E>;
|
|
69
66
|
|
|
70
67
|
abstract addAll<T>(tableName: string, objects: ({[key:string]:Primitive }|any)[]): Promise<T[]>;
|
|
@@ -4,6 +4,7 @@ import RepositoryDatabase, {Chain, Pagination, Primitive, QueryOptions} from "./
|
|
|
4
4
|
|
|
5
5
|
export interface Column {
|
|
6
6
|
name: string
|
|
7
|
+
schema: string
|
|
7
8
|
default?: string
|
|
8
9
|
}
|
|
9
10
|
|
|
@@ -15,8 +16,6 @@ export default class RepositoryDatabasePostgres extends RepositoryDatabase
|
|
|
15
16
|
|
|
16
17
|
private tables: { [key: string]: Column[] } = {};
|
|
17
18
|
|
|
18
|
-
private schema: string = "public";
|
|
19
|
-
|
|
20
19
|
constructor() {
|
|
21
20
|
super();
|
|
22
21
|
this.native = this.native.bind(this);
|
|
@@ -26,7 +25,6 @@ export default class RepositoryDatabasePostgres extends RepositoryDatabase
|
|
|
26
25
|
init(chain: Chain)
|
|
27
26
|
{
|
|
28
27
|
super.init(chain);
|
|
29
|
-
this.schema = chain.schema || "public";
|
|
30
28
|
this.connect(chain);
|
|
31
29
|
}
|
|
32
30
|
|
|
@@ -73,23 +71,12 @@ export default class RepositoryDatabasePostgres extends RepositoryDatabase
|
|
|
73
71
|
async find<T>(tableName: string, filters: { [key: string]: Primitive | Array<any> }): Promise<T[]>
|
|
74
72
|
{
|
|
75
73
|
if (!this.tables[tableName]) throw new Error(`table ${tableName} does not exist`);
|
|
74
|
+
let schema = this.tables[tableName][0].schema;
|
|
76
75
|
let { where, values } = this.toWhere(tableName, filters);
|
|
77
|
-
let query = `SELECT * FROM ${
|
|
76
|
+
let query = `SELECT * FROM ${schema}.${tableName} WHERE ${where}`
|
|
78
77
|
return this.query<T>(query, values);
|
|
79
78
|
}
|
|
80
79
|
|
|
81
|
-
async all_depr<E>(tableName: string, options: QueryOptions = {}): Promise<E[]>
|
|
82
|
-
{
|
|
83
|
-
if (!this.tables[tableName]) throw new Error(`table ${tableName} does not exist`);
|
|
84
|
-
let table = this.tables[tableName];
|
|
85
|
-
let schema = options.schema ?? this.schema;
|
|
86
|
-
|
|
87
|
-
return this.query(`
|
|
88
|
-
SELECT * FROM ${schema}.${tableName}
|
|
89
|
-
WHERE ${ options.filters ?? "TRUE" }
|
|
90
|
-
`);
|
|
91
|
-
}
|
|
92
|
-
|
|
93
80
|
async add<E>(tableName: string, object: {[key:string]:Primitive}|any, id?: string): Promise<E>
|
|
94
81
|
{
|
|
95
82
|
return this.addAll<E>(tableName, [ { id: id, ...object } ]).then((rows) => rows[0]);
|
|
@@ -99,8 +86,9 @@ export default class RepositoryDatabasePostgres extends RepositoryDatabase
|
|
|
99
86
|
{
|
|
100
87
|
if (!this.tables[tableName]) throw new Error(`table ${tableName} does not exist`);
|
|
101
88
|
let table = this.tables[tableName];
|
|
89
|
+
let schema = table[0].schema;
|
|
102
90
|
let columns = PostgresUtils.columns(table);
|
|
103
|
-
let query = `INSERT INTO ${
|
|
91
|
+
let query = `INSERT INTO ${schema}.${tableName} (${columns.join(",")}) VALUES `;
|
|
104
92
|
|
|
105
93
|
let values = []; let index = 1;
|
|
106
94
|
for (let object of objects) {
|
|
@@ -120,11 +108,12 @@ export default class RepositoryDatabasePostgres extends RepositoryDatabase
|
|
|
120
108
|
if (!id) throw new Error(`field 'id' is mandatory when updating ${tableName}`);
|
|
121
109
|
if (!this.tables[tableName]) throw new Error(`table ${tableName} does not exist`);
|
|
122
110
|
let table = this.tables[tableName];
|
|
111
|
+
let schema = table[0].schema;
|
|
123
112
|
let columns = PostgresUtils.columns(table);
|
|
124
113
|
|
|
125
114
|
let params = columns.map((column, index) => `\$${index+2}`).join(",");
|
|
126
115
|
let values = [ id, ...columns.map((column) => object[PostgresUtils.stringToCamel(column)]) ];
|
|
127
|
-
let query = `UPDATE ${
|
|
116
|
+
let query = `UPDATE ${schema}.${tableName} SET (${columns.join(",")}) = (${params}) WHERE id = $1 `
|
|
128
117
|
query += `RETURNING *`;
|
|
129
118
|
|
|
130
119
|
return this.query<T>(query, values).then((result) => result[0]);
|
|
@@ -134,11 +123,12 @@ export default class RepositoryDatabasePostgres extends RepositoryDatabase
|
|
|
134
123
|
{
|
|
135
124
|
if (!this.tables[tableName]) throw new Error(`table ${tableName} does not exist`);
|
|
136
125
|
let table = this.tables[tableName];
|
|
126
|
+
let schema = table[0].schema;
|
|
137
127
|
let columns = PostgresUtils.columns(table);
|
|
138
128
|
let offset = maxResults * pageNumber;
|
|
139
129
|
|
|
140
130
|
let { where, values } = this.toWhere(tableName, filters, "", 4);
|
|
141
|
-
let query = `SELECT * FROM ${
|
|
131
|
+
let query = `SELECT * FROM ${schema}.${tableName} WHERE ${ where } ORDER BY $1 LIMIT $2 OFFSET $3`;
|
|
142
132
|
let elements = this.query<T>(query, [sortKey, maxResults, offset, ...values])
|
|
143
133
|
|
|
144
134
|
let total = this.count(tableName, filters)
|
|
@@ -150,16 +140,18 @@ export default class RepositoryDatabasePostgres extends RepositoryDatabase
|
|
|
150
140
|
async count(tableName: string, filters?: { [key: string]: Primitive | Array<Primitive> }): Promise<number>
|
|
151
141
|
{
|
|
152
142
|
if (!this.tables[tableName]) throw new Error(`table ${tableName} does not exist`);
|
|
143
|
+
let schema = this.tables[tableName][0].schema;
|
|
153
144
|
let { where, values } = this.toWhere(tableName, filters);
|
|
154
|
-
let query = `SELECT count(*) as count FROM ${
|
|
145
|
+
let query = `SELECT count(*) as count FROM ${schema}.${tableName} WHERE ${where}`
|
|
155
146
|
return this.query<{count:number}>(query, values).then((result) => result[0].count);
|
|
156
147
|
}
|
|
157
148
|
|
|
158
149
|
async select(tableName: string, select: string, filters?: { [key: string]: Primitive | Array<Primitive> }): Promise<string[]>
|
|
159
150
|
{
|
|
160
151
|
if (!this.tables[tableName]) throw new Error(`table ${tableName} does not exist`);
|
|
152
|
+
let schema = this.tables[tableName][0].schema;
|
|
161
153
|
let { where, values } = this.toWhere(tableName, filters);
|
|
162
|
-
let query = `SELECT json_agg(u.${select}) as list FROM ${
|
|
154
|
+
let query = `SELECT json_agg(u.${select}) as list FROM ${schema}.${tableName} u WHERE ${where}`
|
|
163
155
|
return this.query<{list:string[]}>(query, values).then((result) => result[0].list);
|
|
164
156
|
}
|
|
165
157
|
|
|
@@ -190,11 +182,10 @@ export default class RepositoryDatabasePostgres extends RepositoryDatabase
|
|
|
190
182
|
private async introspect() {
|
|
191
183
|
let result = await this.native(`
|
|
192
184
|
SELECT table_name as name,
|
|
193
|
-
json_agg(json_build_object('name', column_name, 'default', column_default)) as columns
|
|
185
|
+
json_agg(json_build_object('name', column_name, 'schema', table_schema, 'default', column_default)) as columns
|
|
194
186
|
FROM information_schema.columns
|
|
195
|
-
WHERE (table_schema = $1 or table_schema = 'public')
|
|
196
187
|
GROUP BY table_name
|
|
197
|
-
|
|
188
|
+
`);
|
|
198
189
|
|
|
199
190
|
for (let table of result.rows)
|
|
200
191
|
this.tables[table["name"]] = table["columns"];
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import RepositoryRest from "./RepositoryRest";
|
|
2
2
|
import {Wrapper} from "../api/ServiceRestFormal";
|
|
3
3
|
import {HttpRequest} from "../api/Service";
|
|
4
|
-
import { FormalError } from "../business/BusinessErrors";
|
|
5
4
|
|
|
6
5
|
export default class RepositoryRestFormal<S=any> extends RepositoryRest<Wrapper<S>> {
|
|
7
6
|
|