ismx-nexo-node-app 0.4.183 → 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.
|
@@ -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];
|
|
@@ -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
|
}
|