ismx-nexo-node-app 0.3.35 → 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 +15 -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 +16 -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,13 +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);
|
|
25
|
+
this.introspect = this.introspect.bind(this);
|
|
26
26
|
}
|
|
27
27
|
init(chain) {
|
|
28
28
|
super.init(chain);
|
|
29
|
-
this.schema = chain.schema || "public";
|
|
30
29
|
this.connect(chain);
|
|
31
30
|
}
|
|
32
31
|
addOnIntrospectedListener(listener) {
|
|
@@ -72,24 +71,12 @@ class RepositoryDatabasePostgres extends RepositoryDatabase_1.default {
|
|
|
72
71
|
return __awaiter(this, void 0, void 0, function* () {
|
|
73
72
|
if (!this.tables[tableName])
|
|
74
73
|
throw new Error(`table ${tableName} does not exist`);
|
|
74
|
+
let schema = this.tables[tableName][0].schema;
|
|
75
75
|
let { where, values } = this.toWhere(tableName, filters);
|
|
76
|
-
let query = `SELECT * FROM ${
|
|
76
|
+
let query = `SELECT * FROM ${schema}.${tableName} WHERE ${where}`;
|
|
77
77
|
return this.query(query, values);
|
|
78
78
|
});
|
|
79
79
|
}
|
|
80
|
-
all_depr(tableName_1) {
|
|
81
|
-
return __awaiter(this, arguments, void 0, function* (tableName, options = {}) {
|
|
82
|
-
var _a, _b;
|
|
83
|
-
if (!this.tables[tableName])
|
|
84
|
-
throw new Error(`table ${tableName} does not exist`);
|
|
85
|
-
let table = this.tables[tableName];
|
|
86
|
-
let schema = (_a = options.schema) !== null && _a !== void 0 ? _a : this.schema;
|
|
87
|
-
return this.query(`
|
|
88
|
-
SELECT * FROM ${schema}.${tableName}
|
|
89
|
-
WHERE ${(_b = options.filters) !== null && _b !== void 0 ? _b : "TRUE"}
|
|
90
|
-
`);
|
|
91
|
-
});
|
|
92
|
-
}
|
|
93
80
|
add(tableName, object, id) {
|
|
94
81
|
return __awaiter(this, void 0, void 0, function* () {
|
|
95
82
|
return this.addAll(tableName, [Object.assign({ id: id }, object)]).then((rows) => rows[0]);
|
|
@@ -100,8 +87,9 @@ class RepositoryDatabasePostgres extends RepositoryDatabase_1.default {
|
|
|
100
87
|
if (!this.tables[tableName])
|
|
101
88
|
throw new Error(`table ${tableName} does not exist`);
|
|
102
89
|
let table = this.tables[tableName];
|
|
90
|
+
let schema = table[0].schema;
|
|
103
91
|
let columns = PostgresUtils_1.default.columns(table);
|
|
104
|
-
let query = `INSERT INTO ${
|
|
92
|
+
let query = `INSERT INTO ${schema}.${tableName} (${columns.join(",")}) VALUES `;
|
|
105
93
|
let values = [];
|
|
106
94
|
let index = 1;
|
|
107
95
|
for (let object of objects) {
|
|
@@ -122,10 +110,11 @@ class RepositoryDatabasePostgres extends RepositoryDatabase_1.default {
|
|
|
122
110
|
if (!this.tables[tableName])
|
|
123
111
|
throw new Error(`table ${tableName} does not exist`);
|
|
124
112
|
let table = this.tables[tableName];
|
|
113
|
+
let schema = table[0].schema;
|
|
125
114
|
let columns = PostgresUtils_1.default.columns(table);
|
|
126
115
|
let params = columns.map((column, index) => `\$${index + 2}`).join(",");
|
|
127
116
|
let values = [id, ...columns.map((column) => object[PostgresUtils_1.default.stringToCamel(column)])];
|
|
128
|
-
let query = `UPDATE ${
|
|
117
|
+
let query = `UPDATE ${schema}.${tableName} SET (${columns.join(",")}) = (${params}) WHERE id = $1 `;
|
|
129
118
|
query += `RETURNING *`;
|
|
130
119
|
return this.query(query, values).then((result) => result[0]);
|
|
131
120
|
});
|
|
@@ -135,10 +124,11 @@ class RepositoryDatabasePostgres extends RepositoryDatabase_1.default {
|
|
|
135
124
|
if (!this.tables[tableName])
|
|
136
125
|
throw new Error(`table ${tableName} does not exist`);
|
|
137
126
|
let table = this.tables[tableName];
|
|
127
|
+
let schema = table[0].schema;
|
|
138
128
|
let columns = PostgresUtils_1.default.columns(table);
|
|
139
129
|
let offset = maxResults * pageNumber;
|
|
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(query, [sortKey, maxResults, offset, ...values]);
|
|
143
133
|
let total = this.count(tableName, filters);
|
|
144
134
|
return Promise.all([total, elements]).then(([total, elements]) => ({ total, elements }));
|
|
@@ -148,8 +138,9 @@ class RepositoryDatabasePostgres extends RepositoryDatabase_1.default {
|
|
|
148
138
|
return __awaiter(this, void 0, void 0, function* () {
|
|
149
139
|
if (!this.tables[tableName])
|
|
150
140
|
throw new Error(`table ${tableName} does not exist`);
|
|
141
|
+
let schema = this.tables[tableName][0].schema;
|
|
151
142
|
let { where, values } = this.toWhere(tableName, filters);
|
|
152
|
-
let query = `SELECT count(*) as count FROM ${
|
|
143
|
+
let query = `SELECT count(*) as count FROM ${schema}.${tableName} WHERE ${where}`;
|
|
153
144
|
return this.query(query, values).then((result) => result[0].count);
|
|
154
145
|
});
|
|
155
146
|
}
|
|
@@ -157,8 +148,9 @@ class RepositoryDatabasePostgres extends RepositoryDatabase_1.default {
|
|
|
157
148
|
return __awaiter(this, void 0, void 0, function* () {
|
|
158
149
|
if (!this.tables[tableName])
|
|
159
150
|
throw new Error(`table ${tableName} does not exist`);
|
|
151
|
+
let schema = this.tables[tableName][0].schema;
|
|
160
152
|
let { where, values } = this.toWhere(tableName, filters);
|
|
161
|
-
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}`;
|
|
162
154
|
return this.query(query, values).then((result) => result[0].list);
|
|
163
155
|
});
|
|
164
156
|
}
|
|
@@ -192,11 +184,10 @@ class RepositoryDatabasePostgres extends RepositoryDatabase_1.default {
|
|
|
192
184
|
return __awaiter(this, void 0, void 0, function* () {
|
|
193
185
|
let result = yield this.native(`
|
|
194
186
|
SELECT table_name as name,
|
|
195
|
-
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
|
|
196
188
|
FROM information_schema.columns
|
|
197
|
-
WHERE (table_schema = $1 or table_schema = 'public')
|
|
198
189
|
GROUP BY table_name
|
|
199
|
-
|
|
190
|
+
`);
|
|
200
191
|
for (let table of result.rows)
|
|
201
192
|
this.tables[table["name"]] = table["columns"];
|
|
202
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,17 +16,15 @@ 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);
|
|
22
|
+
this.introspect = this.introspect.bind(this);
|
|
23
23
|
}
|
|
24
24
|
|
|
25
25
|
init(chain: Chain)
|
|
26
26
|
{
|
|
27
27
|
super.init(chain);
|
|
28
|
-
this.schema = chain.schema || "public";
|
|
29
28
|
this.connect(chain);
|
|
30
29
|
}
|
|
31
30
|
|
|
@@ -72,23 +71,12 @@ export default class RepositoryDatabasePostgres extends RepositoryDatabase
|
|
|
72
71
|
async find<T>(tableName: string, filters: { [key: string]: Primitive | Array<any> }): Promise<T[]>
|
|
73
72
|
{
|
|
74
73
|
if (!this.tables[tableName]) throw new Error(`table ${tableName} does not exist`);
|
|
74
|
+
let schema = this.tables[tableName][0].schema;
|
|
75
75
|
let { where, values } = this.toWhere(tableName, filters);
|
|
76
|
-
let query = `SELECT * FROM ${
|
|
76
|
+
let query = `SELECT * FROM ${schema}.${tableName} WHERE ${where}`
|
|
77
77
|
return this.query<T>(query, values);
|
|
78
78
|
}
|
|
79
79
|
|
|
80
|
-
async all_depr<E>(tableName: string, options: QueryOptions = {}): Promise<E[]>
|
|
81
|
-
{
|
|
82
|
-
if (!this.tables[tableName]) throw new Error(`table ${tableName} does not exist`);
|
|
83
|
-
let table = this.tables[tableName];
|
|
84
|
-
let schema = options.schema ?? this.schema;
|
|
85
|
-
|
|
86
|
-
return this.query(`
|
|
87
|
-
SELECT * FROM ${schema}.${tableName}
|
|
88
|
-
WHERE ${ options.filters ?? "TRUE" }
|
|
89
|
-
`);
|
|
90
|
-
}
|
|
91
|
-
|
|
92
80
|
async add<E>(tableName: string, object: {[key:string]:Primitive}|any, id?: string): Promise<E>
|
|
93
81
|
{
|
|
94
82
|
return this.addAll<E>(tableName, [ { id: id, ...object } ]).then((rows) => rows[0]);
|
|
@@ -98,8 +86,9 @@ export default class RepositoryDatabasePostgres extends RepositoryDatabase
|
|
|
98
86
|
{
|
|
99
87
|
if (!this.tables[tableName]) throw new Error(`table ${tableName} does not exist`);
|
|
100
88
|
let table = this.tables[tableName];
|
|
89
|
+
let schema = table[0].schema;
|
|
101
90
|
let columns = PostgresUtils.columns(table);
|
|
102
|
-
let query = `INSERT INTO ${
|
|
91
|
+
let query = `INSERT INTO ${schema}.${tableName} (${columns.join(",")}) VALUES `;
|
|
103
92
|
|
|
104
93
|
let values = []; let index = 1;
|
|
105
94
|
for (let object of objects) {
|
|
@@ -119,11 +108,12 @@ export default class RepositoryDatabasePostgres extends RepositoryDatabase
|
|
|
119
108
|
if (!id) throw new Error(`field 'id' is mandatory when updating ${tableName}`);
|
|
120
109
|
if (!this.tables[tableName]) throw new Error(`table ${tableName} does not exist`);
|
|
121
110
|
let table = this.tables[tableName];
|
|
111
|
+
let schema = table[0].schema;
|
|
122
112
|
let columns = PostgresUtils.columns(table);
|
|
123
113
|
|
|
124
114
|
let params = columns.map((column, index) => `\$${index+2}`).join(",");
|
|
125
115
|
let values = [ id, ...columns.map((column) => object[PostgresUtils.stringToCamel(column)]) ];
|
|
126
|
-
let query = `UPDATE ${
|
|
116
|
+
let query = `UPDATE ${schema}.${tableName} SET (${columns.join(",")}) = (${params}) WHERE id = $1 `
|
|
127
117
|
query += `RETURNING *`;
|
|
128
118
|
|
|
129
119
|
return this.query<T>(query, values).then((result) => result[0]);
|
|
@@ -133,11 +123,12 @@ export default class RepositoryDatabasePostgres extends RepositoryDatabase
|
|
|
133
123
|
{
|
|
134
124
|
if (!this.tables[tableName]) throw new Error(`table ${tableName} does not exist`);
|
|
135
125
|
let table = this.tables[tableName];
|
|
126
|
+
let schema = table[0].schema;
|
|
136
127
|
let columns = PostgresUtils.columns(table);
|
|
137
128
|
let offset = maxResults * pageNumber;
|
|
138
129
|
|
|
139
130
|
let { where, values } = this.toWhere(tableName, filters, "", 4);
|
|
140
|
-
let query = `SELECT * FROM ${
|
|
131
|
+
let query = `SELECT * FROM ${schema}.${tableName} WHERE ${ where } ORDER BY $1 LIMIT $2 OFFSET $3`;
|
|
141
132
|
let elements = this.query<T>(query, [sortKey, maxResults, offset, ...values])
|
|
142
133
|
|
|
143
134
|
let total = this.count(tableName, filters)
|
|
@@ -149,16 +140,18 @@ export default class RepositoryDatabasePostgres extends RepositoryDatabase
|
|
|
149
140
|
async count(tableName: string, filters?: { [key: string]: Primitive | Array<Primitive> }): Promise<number>
|
|
150
141
|
{
|
|
151
142
|
if (!this.tables[tableName]) throw new Error(`table ${tableName} does not exist`);
|
|
143
|
+
let schema = this.tables[tableName][0].schema;
|
|
152
144
|
let { where, values } = this.toWhere(tableName, filters);
|
|
153
|
-
let query = `SELECT count(*) as count FROM ${
|
|
145
|
+
let query = `SELECT count(*) as count FROM ${schema}.${tableName} WHERE ${where}`
|
|
154
146
|
return this.query<{count:number}>(query, values).then((result) => result[0].count);
|
|
155
147
|
}
|
|
156
148
|
|
|
157
149
|
async select(tableName: string, select: string, filters?: { [key: string]: Primitive | Array<Primitive> }): Promise<string[]>
|
|
158
150
|
{
|
|
159
151
|
if (!this.tables[tableName]) throw new Error(`table ${tableName} does not exist`);
|
|
152
|
+
let schema = this.tables[tableName][0].schema;
|
|
160
153
|
let { where, values } = this.toWhere(tableName, filters);
|
|
161
|
-
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}`
|
|
162
155
|
return this.query<{list:string[]}>(query, values).then((result) => result[0].list);
|
|
163
156
|
}
|
|
164
157
|
|
|
@@ -189,11 +182,10 @@ export default class RepositoryDatabasePostgres extends RepositoryDatabase
|
|
|
189
182
|
private async introspect() {
|
|
190
183
|
let result = await this.native(`
|
|
191
184
|
SELECT table_name as name,
|
|
192
|
-
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
|
|
193
186
|
FROM information_schema.columns
|
|
194
|
-
WHERE (table_schema = $1 or table_schema = 'public')
|
|
195
187
|
GROUP BY table_name
|
|
196
|
-
|
|
188
|
+
`);
|
|
197
189
|
|
|
198
190
|
for (let table of result.rows)
|
|
199
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
|
|