ismx-nexo-node-app 0.4.182 → 0.4.184
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/repository/RepositoryDatabasePostgres.js +23 -0
- package/dist/js/repository/RepositoryRest.js +11 -3
- package/dist/types/repository/RepositoryDatabasePostgres.d.ts +3 -0
- package/package.json +1 -1
- package/src/main/node/repository/RepositoryDatabasePostgres.ts +24 -0
- package/src/main/node/repository/RepositoryRest.ts +11 -1
|
@@ -175,6 +175,29 @@ class RepositoryDatabasePostgres extends RepositoryDatabase_1.default {
|
|
|
175
175
|
return this.query(query, values).then((result) => result[0]);
|
|
176
176
|
});
|
|
177
177
|
}
|
|
178
|
+
updateAll(tableName, filters, object) {
|
|
179
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
180
|
+
if (!this.tables[tableName])
|
|
181
|
+
throw new Error(`table ${tableName} does not exist`);
|
|
182
|
+
let table = this.tables[tableName];
|
|
183
|
+
let schema = table[0].schema;
|
|
184
|
+
const columns = PostgresUtils_1.default.columns(table).filter(column => {
|
|
185
|
+
const camelKey = PostgresUtils_1.default.snakeToCamel(column);
|
|
186
|
+
return Object.prototype.hasOwnProperty.call(object, camelKey);
|
|
187
|
+
});
|
|
188
|
+
if (columns.length === 0)
|
|
189
|
+
return this.find(tableName, filters);
|
|
190
|
+
let { where, values } = this.toWhere(tableName, filters);
|
|
191
|
+
const params = columns.map((_, index) => `\$${index + values.length + 1}`).join(",");
|
|
192
|
+
const query = `
|
|
193
|
+
UPDATE ${schema}.${tableName}
|
|
194
|
+
SET (${columns.join(",")}) = ROW(${params})
|
|
195
|
+
WHERE ${where} RETURNING *
|
|
196
|
+
`;
|
|
197
|
+
let allValues = [...values, ...columns.map((column) => object[PostgresUtils_1.default.snakeToCamel(column)])];
|
|
198
|
+
return this.query(query, allValues).then((result) => result);
|
|
199
|
+
});
|
|
200
|
+
}
|
|
178
201
|
delete(tableName, id) {
|
|
179
202
|
return __awaiter(this, void 0, void 0, function* () {
|
|
180
203
|
return (yield this.deleteAll(tableName, { id }))[0];
|
|
@@ -51,15 +51,23 @@ class RepositoryRest extends Repository_1.default {
|
|
|
51
51
|
// Añade información adicional a la respuesta y devuelve el resultado (o el error en su caso).
|
|
52
52
|
// @ts-ignore
|
|
53
53
|
return call.then((res) => __awaiter(this, void 0, void 0, function* () {
|
|
54
|
-
var _a, _b, _c, _d, _e;
|
|
54
|
+
var _a, _b, _c, _d, _e, _f;
|
|
55
|
+
// Captura la información de las cabeceras independientemente de si vienen en stream de datos o en bloque
|
|
55
56
|
let headers = {};
|
|
56
|
-
|
|
57
|
+
if (res.headers instanceof Headers)
|
|
58
|
+
res.headers.forEach((value, key) => headers[key.toLowerCase()] = value);
|
|
59
|
+
else
|
|
60
|
+
Object.entries(res.headers).forEach(([key, value]) => headers[key.toLowerCase()] = value);
|
|
61
|
+
// Interpreta el contenido del cuerpo resultado
|
|
57
62
|
let content = yield res.text();
|
|
58
63
|
(_b = (_a = this.options).onRawResponse) === null || _b === void 0 ? void 0 : _b.call(_a, request, content);
|
|
64
|
+
// Si el cuerpo resultado es un JSON, lo transforma a objeto
|
|
59
65
|
if ((_c = headers['content-type']) === null || _c === void 0 ? void 0 : _c.startsWith('application/json'))
|
|
60
66
|
content = JSON.parse(content, this.reviver);
|
|
67
|
+
if ((_d = headers['content-type']) === null || _d === void 0 ? void 0 : _d.startsWith('application/xml'))
|
|
68
|
+
content = content; //new XMLParser({ ignoreAttributes: false, attributeNamePrefix: '@_' }).parse(content);
|
|
61
69
|
let response = { httpCode: res.status, content: content, headers };
|
|
62
|
-
(
|
|
70
|
+
(_f = (_e = this.options) === null || _e === void 0 ? void 0 : _e.onResponse) === null || _f === void 0 ? void 0 : _f.call(_e, request, response);
|
|
63
71
|
return response;
|
|
64
72
|
})).catch((error) => {
|
|
65
73
|
var _a, _b;
|
|
@@ -42,6 +42,9 @@ export default class RepositoryDatabasePostgres extends RepositoryDatabase {
|
|
|
42
42
|
[key: string]: Valuable;
|
|
43
43
|
} | any)[]): Promise<T[]>;
|
|
44
44
|
update<T>(tableName: string, id: string, object: Partial<T>): Promise<T>;
|
|
45
|
+
updateAll<T>(tableName: string, filters: {
|
|
46
|
+
[p: string]: Valuable | Array<any>;
|
|
47
|
+
}, object: Partial<T>): Promise<T[]>;
|
|
45
48
|
delete<T>(tableName: string, id: string): Promise<T>;
|
|
46
49
|
deleteAll<T>(tableName: string, filters: Partial<T>): Promise<T[]>;
|
|
47
50
|
page<T>(tableName: string, sortKey: string, maxResults?: number, pageNumber?: number, filters?: {
|
package/package.json
CHANGED
|
@@ -150,6 +150,30 @@ export default class RepositoryDatabasePostgres extends RepositoryDatabase
|
|
|
150
150
|
return this.query<T>(query, values).then((result) => result[0]);
|
|
151
151
|
}
|
|
152
152
|
|
|
153
|
+
async updateAll<T>(tableName: string, filters: { [p: string]: Valuable | Array<any> }, object: Partial<T>): Promise<T[]> {
|
|
154
|
+
|
|
155
|
+
if (!this.tables[tableName]) throw new Error(`table ${tableName} does not exist`);
|
|
156
|
+
let table = this.tables[tableName];
|
|
157
|
+
let schema = table[0].schema;
|
|
158
|
+
const columns = PostgresUtils.columns(table).filter(column => {
|
|
159
|
+
const camelKey = PostgresUtils.snakeToCamel(column);
|
|
160
|
+
return Object.prototype.hasOwnProperty.call(object, camelKey);
|
|
161
|
+
});
|
|
162
|
+
if (columns.length === 0) return this.find(tableName, filters);
|
|
163
|
+
|
|
164
|
+
let { where, values } = this.toWhere(tableName, filters);
|
|
165
|
+
const params = columns.map((_, index) => `\$${index + values.length + 1}`).join(",");
|
|
166
|
+
|
|
167
|
+
const query = `
|
|
168
|
+
UPDATE ${schema}.${tableName}
|
|
169
|
+
SET (${columns.join(",")}) = ROW(${params})
|
|
170
|
+
WHERE ${where} RETURNING *
|
|
171
|
+
`;
|
|
172
|
+
|
|
173
|
+
let allValues = [ ...values, ...columns.map((column) => object[PostgresUtils.snakeToCamel(column) as keyof T]) ]
|
|
174
|
+
return this.query<T>(query, allValues).then((result) => result);
|
|
175
|
+
}
|
|
176
|
+
|
|
153
177
|
async delete<T>(tableName: string, id: string): Promise<T> {
|
|
154
178
|
return (await this.deleteAll(tableName, { id } as any))[0] as T;
|
|
155
179
|
}
|
|
@@ -66,14 +66,24 @@ export default class RepositoryRest<Body=any,Res=any> extends Repository
|
|
|
66
66
|
// Añade información adicional a la respuesta y devuelve el resultado (o el error en su caso).
|
|
67
67
|
// @ts-ignore
|
|
68
68
|
return call.then(async (res: Response) => {
|
|
69
|
+
|
|
70
|
+
// Captura la información de las cabeceras independientemente de si vienen en stream de datos o en bloque
|
|
69
71
|
let headers: any = {};
|
|
70
|
-
|
|
72
|
+
if (res.headers instanceof Headers)
|
|
73
|
+
res.headers.forEach((value, key) => headers[key.toLowerCase()] = value);
|
|
74
|
+
else Object.entries(res.headers).forEach(([key, value]) => headers[key.toLowerCase()] = value);
|
|
75
|
+
|
|
76
|
+
// Interpreta el contenido del cuerpo resultado
|
|
71
77
|
let content: string | E = await res.text();
|
|
72
78
|
this.options.onRawResponse?.(request, content);
|
|
73
79
|
|
|
80
|
+
// Si el cuerpo resultado es un JSON, lo transforma a objeto
|
|
74
81
|
if (headers['content-type']?.startsWith('application/json'))
|
|
75
82
|
content = JSON.parse(content, this.reviver) as E;
|
|
76
83
|
|
|
84
|
+
if (headers['content-type']?.startsWith('application/xml'))
|
|
85
|
+
content = content; //new XMLParser({ ignoreAttributes: false, attributeNamePrefix: '@_' }).parse(content);
|
|
86
|
+
|
|
77
87
|
let response: HttpResponse<E> = { httpCode: res.status, content: content as E, headers };
|
|
78
88
|
this.options?.onResponse?.(request, response);
|
|
79
89
|
return response;
|