groupcore-utils 2.2.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 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. shape {field, value}
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
@@ -121,39 +142,4 @@ module.exports = class {
121
142
  query({ statement }) {
122
143
  return this.db.query(statement);
123
144
  }
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
145
  };
@@ -1,36 +1,124 @@
1
+ const mysql = require('mysql');
1
2
  const Crud = require('./crud');
3
+ const Init = require('./init');
2
4
 
3
- test('should generate the join right query - 2 tables', () => {
4
- const tables = [
5
- { table: 'users', fields: ['name', 'address'] },
6
- { table: 'categories', fields: ['name'] },
7
- ];
5
+ jest.spyOn(mysql, 'createConnection').mockImplementation(() => {
6
+ console.log('connected');
7
+ });
8
8
 
9
- const joins = [{ table: 'categories', id: 'user' }];
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
- const whereClauses = [{ table: 'users', key: 'id', value: 10 }];
12
- const query = new Crud({ dbTable: 'test', db: 'test' }).join({ tables, joins, whereClauses, mainTable: 'users', mainId: 'id' });
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
- expect(query).toBe('select users.name, users.address, categories.name from users inner join categories on users.id = categories.user where users.id = 10');
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();
16
81
 
17
- test('should generate the right join query - 3 tables', () => {
18
- const tables = [
19
- { table: 'users', fields: ['name', 'address'] },
20
- { table: 'categories', fields: ['name'] },
21
- { table: 'ages', fields: ['age'], id: 'user' },
22
- ];
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
+ });
23
98
 
24
- const joins = [
25
- { table: 'categories', id: 'user' },
26
- { table: 'ages', id: 'user' },
27
- ];
99
+ it('should work without any conditions', async () => {
100
+ expect.hasAssertions();
28
101
 
29
- const whereClauses = [{ table: 'users', key: 'id', value: 10 }];
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
+ });
30
111
 
31
- const query = new Crud({ dbTable: 'test', db: 'test' }).join({ tables, joins, whereClauses, mainTable: 'users', mainId: 'id' });
112
+ it('should support deprecated usage', async () => {
113
+ expect.hasAssertions();
32
114
 
33
- expect(query).toBe(
34
- 'select users.name, users.address, categories.name, ages.age from users inner join categories on users.id = categories.user inner join ages on users.id = ages.user where users.id = 10'
35
- );
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
+ });
36
124
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "groupcore-utils",
3
- "version": "2.2.0",
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
  },