@abtnode/rbac 1.15.17 → 1.16.0-beta-8ee536d7

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/lib/index.js CHANGED
@@ -70,6 +70,8 @@ const createRBAC = async ({ storage, data = {} } = {}) => {
70
70
  canAny: rbac.canAny.bind(rbac),
71
71
  canAll: rbac.canAll.bind(rbac),
72
72
  can: rbac.can.bind(rbac),
73
+
74
+ storage: rbac.storage,
73
75
  };
74
76
  };
75
77
 
package/lib/store/nedb.js CHANGED
@@ -1,7 +1,6 @@
1
- const util = require('util');
2
1
  const pick = require('lodash/pick');
3
- const DataStore =
4
- process.env.NODE_ENV === 'test' ? require('@nedb/core') : require('@nedb/multi')(Number(process.env.NEDB_MULTI_PORT));
2
+ const { DataStore } = require('@abtnode/db/lib/base');
3
+
5
4
  const Storage = require('../core/storage');
6
5
  const Permission = require('../core/permission');
7
6
  const Role = require('../core/role');
@@ -33,39 +32,12 @@ class Nedb extends Storage {
33
32
  });
34
33
  }
35
34
  });
36
-
37
- this.asyncDB = new Proxy(this.db, {
38
- get(target, property) {
39
- if (typeof target[property] === 'function') {
40
- return util
41
- .promisify((...args) => {
42
- const cb = args[args.length - 1];
43
- const rest = args.slice(0, args.length - 1);
44
-
45
- target[property](...rest, (err, ...result) => {
46
- if (err) {
47
- return cb(err);
48
- }
49
-
50
- if (result.length === 1) {
51
- return cb(null, result[0]);
52
- }
53
-
54
- return cb(null, result);
55
- });
56
- })
57
- .bind(target);
58
- }
59
-
60
- return target[property];
61
- },
62
- });
63
35
  }
64
36
 
65
37
  async add(item) {
66
38
  const { name } = item;
67
39
 
68
- if (await this.asyncDB.count({ name })) {
40
+ if (await this.db.count({ name })) {
69
41
  throw new Error(`Item ${name} already exists`);
70
42
  }
71
43
 
@@ -85,7 +57,7 @@ class Nedb extends Storage {
85
57
  doc.type = ItemTypes.Permission;
86
58
  }
87
59
 
88
- await this.asyncDB.insert(doc);
60
+ await this.db.insert(doc);
89
61
 
90
62
  return true;
91
63
  }
@@ -93,12 +65,12 @@ class Nedb extends Storage {
93
65
  async remove(item) {
94
66
  const { name } = item;
95
67
 
96
- if (!(await this.asyncDB.count({ name }))) {
68
+ if (!(await this.db.count({ name }))) {
97
69
  throw new Error(`Item ${name} is not presented in storage`);
98
70
  }
99
71
 
100
- await this.asyncDB.remove({ name });
101
- await this.asyncDB.update(
72
+ await this.db.remove({ name });
73
+ await this.db.update(
102
74
  {
103
75
  type: ItemTypes.Role,
104
76
  },
@@ -118,11 +90,11 @@ class Nedb extends Storage {
118
90
  async update(item, { title, description, extra } = {}) {
119
91
  const { name } = item;
120
92
 
121
- if (!(await this.asyncDB.count({ name }))) {
93
+ if (!(await this.db.count({ name }))) {
122
94
  throw new Error(`Item ${name} is not presented in storage`);
123
95
  }
124
96
 
125
- await this.asyncDB.update(
97
+ await this.db.update(
126
98
  { name },
127
99
  {
128
100
  $set: pick({ title, description, extra }, 'title', 'description', 'extra'),
@@ -136,11 +108,11 @@ class Nedb extends Storage {
136
108
  const { name } = role;
137
109
  const { name: childName } = child;
138
110
 
139
- if (!(await this.asyncDB.count({ name }))) {
111
+ if (!(await this.db.count({ name }))) {
140
112
  throw new Error(`Role ${name} is not exist`);
141
113
  }
142
114
 
143
- if (!(await this.asyncDB.count({ name: childName }))) {
115
+ if (!(await this.db.count({ name: childName }))) {
144
116
  throw new Error(`Base ${childName} is not exist`);
145
117
  }
146
118
 
@@ -152,7 +124,7 @@ class Nedb extends Storage {
152
124
  throw new Error(`You can grant yourself ${name}`);
153
125
  }
154
126
 
155
- await this.asyncDB.update(
127
+ await this.db.update(
156
128
  {
157
129
  name,
158
130
  },
@@ -170,11 +142,11 @@ class Nedb extends Storage {
170
142
  const { name } = role;
171
143
  const { name: childName } = child;
172
144
 
173
- if (!(await this.asyncDB.count({ name })) || !(await this.asyncDB.count({ name: childName }))) {
145
+ if (!(await this.db.count({ name })) || !(await this.db.count({ name: childName }))) {
174
146
  throw new Error('Role is not exist');
175
147
  }
176
148
 
177
- await this.asyncDB.update(
149
+ await this.db.update(
178
150
  {
179
151
  name,
180
152
  },
@@ -189,7 +161,7 @@ class Nedb extends Storage {
189
161
  }
190
162
 
191
163
  async get(name) {
192
- const item = await this.asyncDB.findOne({ name });
164
+ const item = await this.db.findOne({ name });
193
165
 
194
166
  if (!item) {
195
167
  return undefined;
@@ -209,7 +181,7 @@ class Nedb extends Storage {
209
181
  async getRoles() {
210
182
  const roles = await new Promise((resolve, reject) => {
211
183
  this.db
212
- .find({ type: ItemTypes.Role })
184
+ .cursor({ type: ItemTypes.Role })
213
185
  .sort({ createdAt: -1 })
214
186
  .exec((err, docs) => {
215
187
  if (err) {
@@ -226,7 +198,7 @@ class Nedb extends Storage {
226
198
  async getPermissions() {
227
199
  const permissions = await new Promise((resolve, reject) => {
228
200
  this.db
229
- .find({ type: ItemTypes.Permission })
201
+ .cursor({ type: ItemTypes.Permission })
230
202
  .sort({ createdAt: -1 })
231
203
  .exec((err, docs) => {
232
204
  if (err) {
@@ -241,7 +213,7 @@ class Nedb extends Storage {
241
213
  }
242
214
 
243
215
  async getGrants(name) {
244
- const item = await this.asyncDB.findOne({ name });
216
+ const item = await this.db.findOne({ name });
245
217
 
246
218
  if (item) {
247
219
  const grants = await Promise.all((item.grants || []).map((x) => this.get(x)));
@@ -255,7 +227,7 @@ class Nedb extends Storage {
255
227
  // custom
256
228
 
257
229
  async updateGrants(roleName, grantNames = []) {
258
- const role = await this.asyncDB.findOne({ name: roleName });
230
+ const role = await this.db.findOne({ name: roleName });
259
231
 
260
232
  if (!role) {
261
233
  throw new Error(`Role ${roleName} is not exist`);
@@ -265,7 +237,7 @@ class Nedb extends Storage {
265
237
  throw new Error(`${roleName} is not a role`);
266
238
  }
267
239
 
268
- const names = await this.asyncDB.find(
240
+ const names = await this.db.find(
269
241
  {},
270
242
  {
271
243
  name: 1,
@@ -287,7 +259,7 @@ class Nedb extends Storage {
287
259
  }
288
260
  });
289
261
 
290
- await this.asyncDB.update(
262
+ await this.db.update(
291
263
  { name: roleName },
292
264
  {
293
265
  $set: {
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "1.15.17",
6
+ "version": "1.16.0-beta-8ee536d7",
7
7
  "description": "Simple lib to manage access controls in ABT Node",
8
8
  "main": "lib/index.js",
9
9
  "files": [
@@ -19,13 +19,12 @@
19
19
  "author": "linchen1987 <linchen.1987@foxmail.com> (http://github.com/linchen1987)",
20
20
  "license": "MIT",
21
21
  "dependencies": {
22
- "@nedb/core": "^1.2.2",
23
- "@nedb/multi": "^1.2.2",
24
- "fs-extra": "^10.0.0",
22
+ "@abtnode/db": "1.16.0-beta-8ee536d7",
23
+ "fs-extra": "^10.1.0",
25
24
  "lodash": "^4.17.21"
26
25
  },
27
26
  "devDependencies": {
28
- "jest": "^27.3.1"
27
+ "jest": "^27.5.1"
29
28
  },
30
- "gitHead": "22715c3ea74d0230f3413162a17f491614b6735a"
29
+ "gitHead": "57d0c45be311a5fbc1c0fffa2814b62c1a3ee34c"
31
30
  }