groupcore-utils 2.2.0 → 3.0.1
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 +37 -49
- package/database/crud.spec.js +133 -25
- package/package.json +3 -2
package/database/crud.js
CHANGED
@@ -39,11 +39,47 @@ 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 if (where.conditions) {
|
59
|
+
whereClause += `${field.field} = '${field.value}' ${where.conditions[i]} `;
|
60
|
+
} else {
|
61
|
+
whereClause += `${field.field} = '${field.value}' ${where.condition} `;
|
62
|
+
}
|
63
|
+
});
|
64
|
+
}
|
65
|
+
|
66
|
+
if (orderBy) {
|
67
|
+
if (where) {
|
68
|
+
query = `select * from ${this.dbTable} where ${whereClause} order by ${orderBy}`;
|
69
|
+
} else {
|
70
|
+
query = `select * from ${this.dbTable} order by ${orderBy}`;
|
71
|
+
}
|
72
|
+
} else if (where) {
|
73
|
+
query = `select * from ${this.dbTable} where ${whereClause}`;
|
74
|
+
} else {
|
75
|
+
query = `select * from ${this.dbTable}`;
|
76
|
+
}
|
77
|
+
|
78
|
+
return this.db.query(query);
|
79
|
+
}
|
80
|
+
|
81
|
+
// should be deprecated soon
|
82
|
+
readV1({ where, orderBy }) {
|
47
83
|
let query;
|
48
84
|
|
49
85
|
if (orderBy) {
|
@@ -61,19 +97,6 @@ module.exports = class {
|
|
61
97
|
return this.db.query(query);
|
62
98
|
}
|
63
99
|
|
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
100
|
/**
|
78
101
|
* @method update
|
79
102
|
* @description update db table by id
|
@@ -121,39 +144,4 @@ module.exports = class {
|
|
121
144
|
query({ statement }) {
|
122
145
|
return this.db.query(statement);
|
123
146
|
}
|
124
|
-
|
125
|
-
/**
|
126
|
-
* @method join
|
127
|
-
* @description join two or more tables during db query
|
128
|
-
* @param tables {array} - list of tables to join, object in the shape {table: <string>, fields: <array>}
|
129
|
-
* @param joins {array} - list of the joins in the shape {table: <string>, id: <string>}
|
130
|
-
* @param whereClauses {array} - list of where clauses in the shape {table: <string>, key: <string>, value: <string|integer>}
|
131
|
-
* @param mainTable {string} - the main table from the to join others
|
132
|
-
* @param mainId {string} - the id to join on the main table, expected to be the primary key if the main table
|
133
|
-
*/
|
134
|
-
join({ tables = [], joins = [], whereClauses = [], mainTable, mainId }) {
|
135
|
-
// build join query
|
136
|
-
let query = 'select ';
|
137
|
-
tables.forEach((table) => {
|
138
|
-
table.fields.forEach((field) => {
|
139
|
-
query += `${table.table}.${field}, `;
|
140
|
-
});
|
141
|
-
});
|
142
|
-
|
143
|
-
query += `from ${mainTable} `;
|
144
|
-
|
145
|
-
joins.forEach((join) => {
|
146
|
-
query += `inner join ${join.table} on ${mainTable}.${mainId} = ${join.table}.${join.id} `;
|
147
|
-
});
|
148
|
-
|
149
|
-
query += ' where ';
|
150
|
-
whereClauses.forEach((clause) => {
|
151
|
-
query += `${clause.table}.${clause.key} = ${clause.value} `;
|
152
|
-
});
|
153
|
-
|
154
|
-
query = query.replace(', from', ' from');
|
155
|
-
query = query.replace(' where', 'where');
|
156
|
-
|
157
|
-
return query.trim();
|
158
|
-
}
|
159
147
|
};
|
package/database/crud.spec.js
CHANGED
@@ -1,36 +1,144 @@
|
|
1
|
+
const mysql = require('mysql');
|
1
2
|
const Crud = require('./crud');
|
3
|
+
const Init = require('./init');
|
2
4
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
{ table: 'categories', fields: ['name'] },
|
7
|
-
];
|
5
|
+
jest.spyOn(mysql, 'createConnection').mockImplementation(() => {
|
6
|
+
console.log('connected');
|
7
|
+
});
|
8
8
|
|
9
|
-
|
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();
|
10
16
|
|
11
|
-
|
12
|
-
|
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
|
+
});
|
13
24
|
|
14
|
-
|
15
|
-
|
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 when "where" is an array of objects with one condition', async () => {
|
100
|
+
expect.hasAssertions();
|
16
101
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
{
|
21
|
-
|
22
|
-
|
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
|
+
});
|
23
118
|
|
24
|
-
|
25
|
-
|
26
|
-
{ table: 'ages', id: 'user' },
|
27
|
-
];
|
119
|
+
it('should work without any conditions', async () => {
|
120
|
+
expect.hasAssertions();
|
28
121
|
|
29
|
-
|
122
|
+
const querySpy = jest.spyOn(Init.prototype, 'query').mockImplementation(() => {
|
123
|
+
console.log('query called');
|
124
|
+
});
|
125
|
+
await new Crud({ dbTable: 'test', db }).read({
|
126
|
+
where: null,
|
127
|
+
orderBy: null,
|
128
|
+
});
|
129
|
+
expect(querySpy).toHaveBeenCalledWith('select * from test');
|
130
|
+
});
|
30
131
|
|
31
|
-
|
132
|
+
it('should support deprecated usage', async () => {
|
133
|
+
expect.hasAssertions();
|
32
134
|
|
33
|
-
|
34
|
-
|
35
|
-
|
135
|
+
const querySpy = jest.spyOn(Init.prototype, 'query').mockImplementation(() => {
|
136
|
+
console.log('query called');
|
137
|
+
});
|
138
|
+
await new Crud({ dbTable: 'test', db }).read({
|
139
|
+
where: { field: 'id', value: 'test' },
|
140
|
+
orderBy: null,
|
141
|
+
});
|
142
|
+
expect(querySpy).toHaveBeenCalledWith("select * from test where id = 'test'");
|
143
|
+
});
|
36
144
|
});
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "groupcore-utils",
|
3
|
-
"version": "
|
3
|
+
"version": "3.0.1",
|
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
|
},
|