groupcore-utils 2.1.0 → 3.0.0
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 +35 -14
- package/database/crud.spec.js +124 -0
- package/package.json +3 -2
package/database/crud.js
CHANGED
@@ -39,11 +39,45 @@ module.exports = class {
|
|
39
39
|
/**
|
40
40
|
* @method read()
|
41
41
|
* @description get data from table
|
42
|
-
* @param {Object} where - if no where, all the data will be returned from db table without any 'where' clause.
|
42
|
+
* @param {Object} where - if no where, all the data will be returned from db table without any 'where' clause.
|
43
|
+
* shape {fields (array of fields {field, value}), condition array ("and" or "or") to correspond with fields}
|
43
44
|
* @param {Array} orderBy - explicitly specify the order, either asc or desc
|
44
45
|
* @returns {Promise}
|
45
46
|
*/
|
46
47
|
read({ where, orderBy }) {
|
48
|
+
if (where && !_.has(where, 'fields')) {
|
49
|
+
return this.readV1({ where, orderBy });
|
50
|
+
}
|
51
|
+
|
52
|
+
let query;
|
53
|
+
let whereClause = '';
|
54
|
+
if (where) {
|
55
|
+
where.fields.forEach((field, i) => {
|
56
|
+
if (_.last(where.fields) === field) {
|
57
|
+
whereClause += `${field.field} = '${field.value}'`;
|
58
|
+
} else {
|
59
|
+
whereClause += `${field.field} = '${field.value}' ${where.conditions[i]} `;
|
60
|
+
}
|
61
|
+
});
|
62
|
+
}
|
63
|
+
|
64
|
+
if (orderBy) {
|
65
|
+
if (where) {
|
66
|
+
query = `select * from ${this.dbTable} where ${whereClause} order by ${orderBy}`;
|
67
|
+
} else {
|
68
|
+
query = `select * from ${this.dbTable} order by ${orderBy}`;
|
69
|
+
}
|
70
|
+
} else if (where) {
|
71
|
+
query = `select * from ${this.dbTable} where ${whereClause}`;
|
72
|
+
} else {
|
73
|
+
query = `select * from ${this.dbTable}`;
|
74
|
+
}
|
75
|
+
|
76
|
+
return this.db.query(query);
|
77
|
+
}
|
78
|
+
|
79
|
+
// should be deprecated soon
|
80
|
+
readV1({ where, orderBy }) {
|
47
81
|
let query;
|
48
82
|
|
49
83
|
if (orderBy) {
|
@@ -61,19 +95,6 @@ module.exports = class {
|
|
61
95
|
return this.db.query(query);
|
62
96
|
}
|
63
97
|
|
64
|
-
// with orderby
|
65
|
-
// readOrderBy(where = null) {
|
66
|
-
// let query;
|
67
|
-
//
|
68
|
-
// if (where) {
|
69
|
-
// query = `select * from ${this.dbTable} where ${where.field} = '${where.value}'`;
|
70
|
-
// } else {
|
71
|
-
// query = `select * from ${this.dbTable}`;
|
72
|
-
// }
|
73
|
-
//
|
74
|
-
// return new Db().query(query);
|
75
|
-
// }
|
76
|
-
|
77
98
|
/**
|
78
99
|
* @method update
|
79
100
|
* @description update db table by id
|
@@ -0,0 +1,124 @@
|
|
1
|
+
const mysql = require('mysql');
|
2
|
+
const Crud = require('./crud');
|
3
|
+
const Init = require('./init');
|
4
|
+
|
5
|
+
jest.spyOn(mysql, 'createConnection').mockImplementation(() => {
|
6
|
+
console.log('connected');
|
7
|
+
});
|
8
|
+
|
9
|
+
const db = new Init({ host: '', user: '', password: '', db: '' });
|
10
|
+
describe('read()', () => {
|
11
|
+
beforeEach(() => {
|
12
|
+
jest.clearAllMocks();
|
13
|
+
});
|
14
|
+
it('should work when "where" is an object', async () => {
|
15
|
+
expect.hasAssertions();
|
16
|
+
|
17
|
+
const querySpy = jest.spyOn(Init.prototype, 'query').mockImplementation(() => {
|
18
|
+
console.log('query called');
|
19
|
+
});
|
20
|
+
await new Crud({ dbTable: 'test', db }).read({ where: { field: 'id', value: '10' }, orderBy: null });
|
21
|
+
expect(querySpy).toHaveBeenCalledWith("select * from test where id = '10'");
|
22
|
+
expect(querySpy).toHaveBeenCalledTimes(1);
|
23
|
+
});
|
24
|
+
|
25
|
+
it('should work when "where" is an array of objects with "and" condition', async () => {
|
26
|
+
expect.hasAssertions();
|
27
|
+
|
28
|
+
const querySpy = jest.spyOn(Init.prototype, 'query').mockImplementation(() => {
|
29
|
+
console.log('query called');
|
30
|
+
});
|
31
|
+
await new Crud({ dbTable: 'test', db }).read({
|
32
|
+
where: {
|
33
|
+
conditions: ['and'],
|
34
|
+
fields: [
|
35
|
+
{ field: 'id', value: '10' },
|
36
|
+
{ field: 'username', value: 'test' },
|
37
|
+
],
|
38
|
+
},
|
39
|
+
orderBy: null,
|
40
|
+
});
|
41
|
+
expect(querySpy).toHaveBeenCalledWith("select * from test where id = '10' and username = 'test'");
|
42
|
+
});
|
43
|
+
|
44
|
+
it('should work when "where" is an array of objects with "or" condition', async () => {
|
45
|
+
expect.hasAssertions();
|
46
|
+
|
47
|
+
const querySpy = jest.spyOn(Init.prototype, 'query').mockImplementation(() => {
|
48
|
+
console.log('query called');
|
49
|
+
});
|
50
|
+
await new Crud({ dbTable: 'test', db }).read({
|
51
|
+
where: {
|
52
|
+
conditions: ['or'],
|
53
|
+
fields: [
|
54
|
+
{ field: 'id', value: '10' },
|
55
|
+
{ field: 'tag', value: 'test' },
|
56
|
+
],
|
57
|
+
},
|
58
|
+
orderBy: null,
|
59
|
+
});
|
60
|
+
expect(querySpy).toHaveBeenCalledWith("select * from test where id = '10' or tag = 'test'");
|
61
|
+
});
|
62
|
+
|
63
|
+
it('should work when "where" has just one in array', async () => {
|
64
|
+
expect.hasAssertions();
|
65
|
+
|
66
|
+
const querySpy = jest.spyOn(Init.prototype, 'query').mockImplementation(() => {
|
67
|
+
console.log('query called');
|
68
|
+
});
|
69
|
+
await new Crud({ dbTable: 'test', db }).read({
|
70
|
+
where: {
|
71
|
+
conditions: null,
|
72
|
+
fields: [{ field: 'id', value: '10' }],
|
73
|
+
},
|
74
|
+
orderBy: null,
|
75
|
+
});
|
76
|
+
expect(querySpy).toHaveBeenCalledWith("select * from test where id = '10'");
|
77
|
+
});
|
78
|
+
|
79
|
+
it('should work when "where" is an array of objects with multiple conditions', async () => {
|
80
|
+
expect.hasAssertions();
|
81
|
+
|
82
|
+
const querySpy = jest.spyOn(Init.prototype, 'query').mockImplementation(() => {
|
83
|
+
console.log('query called');
|
84
|
+
});
|
85
|
+
await new Crud({ dbTable: 'test', db }).read({
|
86
|
+
where: {
|
87
|
+
conditions: ['or', 'and'],
|
88
|
+
fields: [
|
89
|
+
{ field: 'id', value: '10' },
|
90
|
+
{ field: 'tag', value: 'test' },
|
91
|
+
{ field: 'slug', value: 'testslug' },
|
92
|
+
],
|
93
|
+
},
|
94
|
+
orderBy: null,
|
95
|
+
});
|
96
|
+
expect(querySpy).toHaveBeenCalledWith("select * from test where id = '10' or tag = 'test' and slug = 'testslug'");
|
97
|
+
});
|
98
|
+
|
99
|
+
it('should work without any conditions', 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: null,
|
107
|
+
orderBy: null,
|
108
|
+
});
|
109
|
+
expect(querySpy).toHaveBeenCalledWith('select * from test');
|
110
|
+
});
|
111
|
+
|
112
|
+
it('should support deprecated usage', async () => {
|
113
|
+
expect.hasAssertions();
|
114
|
+
|
115
|
+
const querySpy = jest.spyOn(Init.prototype, 'query').mockImplementation(() => {
|
116
|
+
console.log('query called');
|
117
|
+
});
|
118
|
+
await new Crud({ dbTable: 'test', db }).read({
|
119
|
+
where: { field: 'id', value: 'test' },
|
120
|
+
orderBy: null,
|
121
|
+
});
|
122
|
+
expect(querySpy).toHaveBeenCalledWith("select * from test where id = 'test'");
|
123
|
+
});
|
124
|
+
});
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "groupcore-utils",
|
3
|
-
"version": "
|
3
|
+
"version": "3.0.0",
|
4
4
|
"description": "Utilities for working with some core features",
|
5
5
|
"main": "Utils.js",
|
6
6
|
"scripts": {
|
@@ -17,13 +17,14 @@
|
|
17
17
|
},
|
18
18
|
"homepage": "https://bitbucket.org/thegroupc/groupcore-utils/src/master",
|
19
19
|
"dependencies": {
|
20
|
-
"lodash": "^4.17.21",
|
21
20
|
"axios": "^0.26.1",
|
22
21
|
"bcrypt": "^5.0.1",
|
23
22
|
"easy-rbac": "^3.2.0",
|
24
23
|
"escape-quotes": "^1.0.2",
|
25
24
|
"jsonwebtoken": "^8.5.1",
|
25
|
+
"lodash": "^4.17.21",
|
26
26
|
"moment": "^2.29.2",
|
27
|
+
"mysql": "^2.18.1",
|
27
28
|
"nodemailer": "^6.7.3",
|
28
29
|
"random-number": "^0.0.9"
|
29
30
|
},
|