groupcore-utils 2.0.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/README.md CHANGED
@@ -324,6 +324,19 @@ Initialize db with this and pass the object to the CRUD class constructor
324
324
  */
325
325
  ```
326
326
 
327
+ ### RBAC
328
+ ```
329
+ require('@groupcollab/core-utils/rbac')
330
+
331
+ /**
332
+ * @method validateAction()
333
+ * @description validate the action to be carried out
334
+ * @param {number} id - the main id
335
+ * @param {number} compareId - id to be compared
336
+ * @returns {Promise<void>}
337
+ */
338
+ ```
339
+
327
340
  ### Testing
328
341
  ```
329
342
  npm run test
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.0.0",
3
+ "version": "2.2.0",
4
4
  "description": "Utilities for working with some core features",
5
5
  "main": "Utils.js",
6
6
  "scripts": {
package/rbac/index.js ADDED
@@ -0,0 +1,32 @@
1
+ const EasyRbac = require('easy-rbac');
2
+
3
+ module.exports = class {
4
+ /**
5
+ * @method validateAction()
6
+ * @description validate the action to be carried out
7
+ * @param {number} id - the main id
8
+ * @param {number} compareId - id to be compared
9
+ * @returns {Promise<void>}
10
+ */
11
+ async validateAction({ id, compareId }) {
12
+ const rbac = new EasyRbac({
13
+ user: {
14
+ can: [
15
+ {
16
+ name: 'do',
17
+ when: async (params) => params.id === params.compareId,
18
+ },
19
+ ],
20
+ },
21
+ });
22
+
23
+ const response = await rbac.can('user', 'do', {
24
+ id,
25
+ compareId,
26
+ });
27
+
28
+ if (!response) {
29
+ throw new Error('User not authorized to perform this action!');
30
+ }
31
+ }
32
+ };