ismx-nexo-node-app 0.4.18 → 0.4.19

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.
@@ -152,11 +152,15 @@ class RepositoryDatabasePostgres extends RepositoryDatabase_1.default {
152
152
  throw new Error(`table ${tableName} does not exist`);
153
153
  let table = this.tables[tableName];
154
154
  let schema = table[0].schema;
155
- let columns = PostgresUtils_1.default.columns(table);
156
- let params = columns.map((column, index) => `\$${index + 2}`).join(",");
157
- let values = [id, ...columns.map((column) => object[PostgresUtils_1.default.stringToCamel(column)])];
158
- let query = `UPDATE ${schema}.${tableName} SET (${columns.join(",")}) = (${params}) WHERE id = $1 `;
159
- query += `RETURNING *`;
155
+ const columns = PostgresUtils_1.default.columns(table).filter(column => {
156
+ const camelKey = PostgresUtils_1.default.stringToCamel(column);
157
+ return Object.prototype.hasOwnProperty.call(object, camelKey);
158
+ });
159
+ if (columns.length === 0)
160
+ throw new Error(`No valid columns provided to update for table ${tableName}`);
161
+ const params = columns.map((_, index) => `\$${index + 2}`).join(",");
162
+ const values = [id, ...columns.map((column) => object[PostgresUtils_1.default.stringToCamel(column)])];
163
+ const query = `UPDATE ${schema}.${tableName} SET (${columns.join(",")}) = (${params}) WHERE id = $1 RETURNING *`;
160
164
  return this.query(query, values).then((result) => result[0]);
161
165
  });
162
166
  }
@@ -36,9 +36,7 @@ export default class RepositoryDatabasePostgres extends RepositoryDatabase {
36
36
  addAll<T>(tableName: string, objects: ({
37
37
  [key: string]: Primitive;
38
38
  } | any)[]): Promise<T[]>;
39
- update<T>(tableName: string, id: string, object: {
40
- [key: string]: Primitive;
41
- } | any): Promise<T>;
39
+ update<T>(tableName: string, id: string, object: Partial<T>): Promise<T>;
42
40
  page<T>(tableName: string, sortKey: string, maxResults?: number, pageNumber?: number, filters?: {
43
41
  [key: string]: Primitive | Array<Primitive>;
44
42
  }): Promise<Pagination<T>>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ismx-nexo-node-app",
3
- "version": "0.4.18",
3
+ "version": "0.4.19",
4
4
  "description": "",
5
5
  "scripts": {
6
6
  "build": "rm -rf ./dist && npx tsc",
@@ -120,18 +120,22 @@ export default class RepositoryDatabasePostgres extends RepositoryDatabase
120
120
  return this.query(query, values);
121
121
  }
122
122
 
123
- async update<T>(tableName: string, id: string, object: {[key:string]:Primitive}|any): Promise<T>
123
+ async update<T>(tableName: string, id: string, object: Partial<T>): Promise<T>
124
124
  {
125
125
  if (!id) throw new Error(`field 'id' is mandatory when updating ${tableName}`);
126
126
  if (!this.tables[tableName]) throw new Error(`table ${tableName} does not exist`);
127
127
  let table = this.tables[tableName];
128
128
  let schema = table[0].schema;
129
- let columns = PostgresUtils.columns(table);
129
+ const columns = PostgresUtils.columns(table).filter(column => {
130
+ const camelKey = PostgresUtils.stringToCamel(column);
131
+ return Object.prototype.hasOwnProperty.call(object, camelKey);
132
+ });
133
+
134
+ if (columns.length === 0) throw new Error(`No valid columns provided to update for table ${tableName}`);
130
135
 
131
- let params = columns.map((column, index) => `\$${index+2}`).join(",");
132
- let values = [ id, ...columns.map((column) => object[PostgresUtils.stringToCamel(column)]) ];
133
- let query = `UPDATE ${schema}.${tableName} SET (${columns.join(",")}) = (${params}) WHERE id = $1 `
134
- query += `RETURNING *`;
136
+ const params = columns.map((_, index) => `\$${index + 2}`).join(",");
137
+ const values = [ id, ...columns.map((column) => object[PostgresUtils.stringToCamel(column) as keyof T]) ];
138
+ const query = `UPDATE ${schema}.${tableName} SET (${columns.join(",")}) = (${params}) WHERE id = $1 RETURNING *`;
135
139
 
136
140
  return this.query<T>(query, values).then((result) => result[0]);
137
141
  }