groupcore-utils 3.0.0 → 3.0.2
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 +13 -9
- package/database/crud.spec.js +20 -0
- package/package.json +1 -1
package/database/crud.js
CHANGED
@@ -55,8 +55,10 @@ module.exports = class {
|
|
55
55
|
where.fields.forEach((field, i) => {
|
56
56
|
if (_.last(where.fields) === field) {
|
57
57
|
whereClause += `${field.field} = '${field.value}'`;
|
58
|
-
} else {
|
58
|
+
} else if (where.conditions) {
|
59
59
|
whereClause += `${field.field} = '${field.value}' ${where.conditions[i]} `;
|
60
|
+
} else {
|
61
|
+
whereClause += `${field.field} = '${field.value}' ${where.condition} `;
|
60
62
|
}
|
61
63
|
});
|
62
64
|
}
|
@@ -108,14 +110,16 @@ module.exports = class {
|
|
108
110
|
|
109
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
|
110
112
|
const count = Object.keys(data).length;
|
111
|
-
Object.keys(data)
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
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
|
+
});
|
119
123
|
|
120
124
|
const query = `update ${this.dbTable} set ${updateQuery} where id = '${id}'`;
|
121
125
|
|
package/database/crud.spec.js
CHANGED
@@ -96,6 +96,26 @@ describe('read()', () => {
|
|
96
96
|
expect(querySpy).toHaveBeenCalledWith("select * from test where id = '10' or tag = 'test' and slug = 'testslug'");
|
97
97
|
});
|
98
98
|
|
99
|
+
it('should work when "where" is an array of objects with one condition', async () => {
|
100
|
+
expect.hasAssertions();
|
101
|
+
|
102
|
+
const querySpy = jest.spyOn(Init.prototype, 'query').mockImplementation(() => {
|
103
|
+
console.log('query called');
|
104
|
+
});
|
105
|
+
await new Crud({ dbTable: 'test', db }).read({
|
106
|
+
where: {
|
107
|
+
condition: 'and',
|
108
|
+
fields: [
|
109
|
+
{ field: 'id', value: '10' },
|
110
|
+
{ field: 'tag', value: 'test' },
|
111
|
+
{ field: 'slug', value: 'testslug' },
|
112
|
+
],
|
113
|
+
},
|
114
|
+
orderBy: null,
|
115
|
+
});
|
116
|
+
expect(querySpy).toHaveBeenCalledWith("select * from test where id = '10' and tag = 'test' and slug = 'testslug'");
|
117
|
+
});
|
118
|
+
|
99
119
|
it('should work without any conditions', async () => {
|
100
120
|
expect.hasAssertions();
|
101
121
|
|