groupcore-utils 3.0.1 → 3.0.3
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/database/crud.js +11 -9
- package/database/crud.spec.js +17 -0
- package/package.json +1 -1
package/database/crud.js
CHANGED
@@ -109,15 +109,17 @@ module.exports = class {
|
|
109
109
|
let updateQuery = '';
|
110
110
|
|
111
111
|
// set this to be used inside the map function to check when we get to the last index key so that we dont add the comma to the last key in the generated sql query
|
112
|
-
const count = Object.keys(data).length;
|
113
|
-
Object.keys(data)
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
112
|
+
const count = Object.keys(data).length - 1; // adding -1 because we are taking out 'id' in the filter method
|
113
|
+
Object.keys(data)
|
114
|
+
.filter((key) => key !== 'id')
|
115
|
+
.forEach((key, i) => {
|
116
|
+
if (count - i === 1) {
|
117
|
+
updateQuery += `${key} = '${escapeQuotes(data[key])}'`;
|
118
|
+
} else {
|
119
|
+
updateQuery += `${key} = '${escapeQuotes(data[key])}', `;
|
120
|
+
}
|
121
|
+
return true;
|
122
|
+
});
|
121
123
|
|
122
124
|
const query = `update ${this.dbTable} set ${updateQuery} where id = '${id}'`;
|
123
125
|
|
package/database/crud.spec.js
CHANGED
@@ -142,3 +142,20 @@ describe('read()', () => {
|
|
142
142
|
expect(querySpy).toHaveBeenCalledWith("select * from test where id = 'test'");
|
143
143
|
});
|
144
144
|
});
|
145
|
+
|
146
|
+
describe('update()', () => {
|
147
|
+
beforeEach(() => {
|
148
|
+
jest.clearAllMocks();
|
149
|
+
});
|
150
|
+
it('should generate the right query', async () => {
|
151
|
+
expect.hasAssertions();
|
152
|
+
|
153
|
+
const querySpy = jest.spyOn(Init.prototype, 'query').mockImplementation(() => {
|
154
|
+
console.log('query called');
|
155
|
+
});
|
156
|
+
|
157
|
+
await new Crud({ dbTable: 'test', db }).update({ data: { id: 1, tag: 'test', slug: 'test' }, id: 1 });
|
158
|
+
expect(querySpy).toHaveBeenCalledWith("update test set tag = 'test', slug = 'test' where id = '1'");
|
159
|
+
expect(querySpy).toHaveBeenCalledTimes(1);
|
160
|
+
});
|
161
|
+
});
|