groupcore-utils 2.1.0 → 2.2.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
@@ -121,4 +121,39 @@ module.exports = class {
121
121
  query({ statement }) {
122
122
  return this.db.query(statement);
123
123
  }
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
+ }
124
159
  };
@@ -0,0 +1,36 @@
1
+ const Crud = require('./crud');
2
+
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
+ ];
8
+
9
+ const joins = [{ table: 'categories', id: 'user' }];
10
+
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' });
13
+
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
+ });
16
+
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
+ ];
23
+
24
+ const joins = [
25
+ { table: 'categories', id: 'user' },
26
+ { table: 'ages', id: 'user' },
27
+ ];
28
+
29
+ const whereClauses = [{ table: 'users', key: 'id', value: 10 }];
30
+
31
+ const query = new Crud({ dbTable: 'test', db: 'test' }).join({ tables, joins, whereClauses, mainTable: 'users', mainId: 'id' });
32
+
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
+ );
36
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "groupcore-utils",
3
- "version": "2.1.0",
3
+ "version": "2.2.0",
4
4
  "description": "Utilities for working with some core features",
5
5
  "main": "Utils.js",
6
6
  "scripts": {