@anievo-id/rbac-evolution-mongoose 0.7.1 → 0.9.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 +13 -77
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
# @anievo-id/rbac-evolution-mongoose
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
[Mongoose](https://mongoosejs.com) (MongoDB) storage adapter for [rbac-evolution](https://github.com/AniEvo-ID/rbac-evolution) — roles & permissions for JS/TS, inspired by spatie/laravel-permission.
|
|
3
|
+
[Mongoose](https://mongoosejs.com) (MongoDB) storage adapter for
|
|
4
|
+
[RBAC Evolution](https://github.com/AniEvo-ID/rbac-evolution).
|
|
6
5
|
|
|
7
6
|
## Install
|
|
8
7
|
|
|
@@ -10,28 +9,7 @@
|
|
|
10
9
|
npm i @anievo-id/rbac-evolution @anievo-id/rbac-evolution-mongoose mongoose
|
|
11
10
|
```
|
|
12
11
|
|
|
13
|
-
`mongoose` 8
|
|
14
|
-
|
|
15
|
-
## Collections
|
|
16
|
-
|
|
17
|
-
Five collections mirror spatie/laravel-permission's five tables:
|
|
18
|
-
|
|
19
|
-
| Collection | Fields | Indexes |
|
|
20
|
-
| ----------------------- | ------------------------------------------------------------------------- | ---------------------------------------------------- |
|
|
21
|
-
| `permissions` | `_id`, `name`, `guardName`, `createdAt`, `updatedAt` | unique `(name, guardName)` |
|
|
22
|
-
| `roles` | `_id`, `name`, `guardName`, `teamId`, `createdAt`, `updatedAt` | unique `(name, guardName, teamId)` |
|
|
23
|
-
| `role_has_permissions` | `_id`, `roleId`, `permissionId` | unique `(roleId, permissionId)`, `(permissionId)` |
|
|
24
|
-
| `model_has_roles` | `_id`, `roleId`, `subjectType`, `subjectId`, `teamId` | unique `(subjectType, subjectId, teamId, roleId)`, `(roleId, subjectType, teamId)` |
|
|
25
|
-
| `model_has_permissions` | `_id`, `permissionId`, `subjectType`, `subjectId`, `teamId` | unique `(subjectType, subjectId, teamId, permissionId)`, `(permissionId, subjectType, teamId)` |
|
|
26
|
-
|
|
27
|
-
- **Ids are ObjectIds.** Role and permission ids are returned as 24-character hex **strings**
|
|
28
|
-
(`'65f1c0...'`). Ids that aren't valid ObjectId hex strings are ignored (no document could match them).
|
|
29
|
-
- `subjectId` is a string, so subjects may have numeric, UUID or ObjectId ids. Reverse lookups return strings.
|
|
30
|
-
- `teamId` is a string; `''` means "global / no team". `Role.teamId` is `null` for global roles and a **string**
|
|
31
|
-
otherwise (`teamId: 1` → `'1'`).
|
|
32
|
-
- `createdAt` / `updatedAt` are Mongoose timestamps on `permissions` and `roles`.
|
|
33
|
-
- Links live in their own collections (not embedded arrays), so they stay small, indexable and easy to query in both
|
|
34
|
-
directions.
|
|
12
|
+
Requires `mongoose` 8. Mongoose 9 is not tested yet.
|
|
35
13
|
|
|
36
14
|
## Usage
|
|
37
15
|
|
|
@@ -42,64 +20,22 @@ import { mongooseAdapter } from '@anievo-id/rbac-evolution-mongoose'
|
|
|
42
20
|
|
|
43
21
|
await mongoose.connect(process.env.MONGO_URL!)
|
|
44
22
|
|
|
45
|
-
export const rbac = createRbac({
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
Options:
|
|
49
|
-
|
|
50
|
-
```ts
|
|
51
|
-
mongooseAdapter({
|
|
52
|
-
connection: conn, // a mongoose.createConnection() connection; default: mongoose.connection
|
|
53
|
-
prefix: 'rbac_', // rbac_permissions, rbac_roles, ...; default: ''
|
|
23
|
+
export const rbac = createRbac({
|
|
24
|
+
adapter: mongooseAdapter(), // options: { connection, prefix: 'rbac_' }
|
|
54
25
|
})
|
|
55
|
-
```
|
|
56
|
-
|
|
57
|
-
Then:
|
|
58
|
-
|
|
59
|
-
```ts
|
|
60
|
-
await rbac.createPermission('edit articles')
|
|
61
|
-
await rbac.createRole('writer')
|
|
62
|
-
await rbac.role('writer').givePermissionTo('edit articles')
|
|
63
26
|
|
|
64
|
-
|
|
65
|
-
await user.assignRole('writer')
|
|
66
|
-
await user.can('edit articles') // true
|
|
27
|
+
await rbac.for({ type: 'user', id: currentUser.id }).can('edit articles')
|
|
67
28
|
```
|
|
68
29
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
The
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
```ts
|
|
75
|
-
import { createRbacModels, MongooseRbacAdapter } from '@anievo-id/rbac-evolution-mongoose'
|
|
76
|
-
|
|
77
|
-
const models = createRbacModels({ prefix: 'rbac_' }) // registers or reuses the models
|
|
78
|
-
const adapter = new MongooseRbacAdapter(models)
|
|
79
|
-
|
|
80
|
-
await models.Role.find({ teamId: '' }).lean()
|
|
81
|
-
```
|
|
82
|
-
|
|
83
|
-
`rbacSchemas()` returns the unregistered schemas if you want to add plugins first.
|
|
84
|
-
|
|
85
|
-
### Indexes
|
|
86
|
-
|
|
87
|
-
The unique indexes enforce name uniqueness and make `attach*` safe under concurrency. With Mongoose's `autoIndex`
|
|
88
|
-
on (the default) the adapter waits for them before its first operation. If you turn `autoIndex` off in production,
|
|
89
|
-
create them once at deploy time:
|
|
90
|
-
|
|
91
|
-
```ts
|
|
92
|
-
import { createRbacModels, syncRbacIndexes } from '@anievo-id/rbac-evolution-mongoose'
|
|
93
|
-
|
|
94
|
-
await syncRbacIndexes(createRbacModels())
|
|
95
|
-
```
|
|
30
|
+
- Role and permission ids are ObjectIds, returned as 24-character hex strings.
|
|
31
|
+
- The models are registered as `rbac:<collection>` (for example `rbac:roles`), so they don't clash with your own.
|
|
32
|
+
- The unique indexes make `attach*` safe under concurrency. If you turn Mongoose's `autoIndex` off in production,
|
|
33
|
+
run `await syncRbacIndexes(createRbacModels())` once at deploy time.
|
|
34
|
+
- Deletes don't use a transaction, so no replica set is needed.
|
|
96
35
|
|
|
97
|
-
##
|
|
36
|
+
## Documentation
|
|
98
37
|
|
|
99
|
-
-
|
|
100
|
-
error from a concurrent insert of the same link is ignored.
|
|
101
|
-
- `deleteRole` / `deletePermission` delete the link documents first, then the role / permission. They don't use a
|
|
102
|
-
transaction (which would require a replica set).
|
|
38
|
+
https://creation.anievo.id/rbac-evolution/adapters/mongoose
|
|
103
39
|
|
|
104
40
|
## License
|
|
105
41
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@anievo-id/rbac-evolution-mongoose",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.9.0",
|
|
4
4
|
"description": "Mongoose (MongoDB) storage adapter for rbac-evolution.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "AniEvo-ID (https://github.com/AniEvo-ID)",
|
|
@@ -46,10 +46,10 @@
|
|
|
46
46
|
"access": "public"
|
|
47
47
|
},
|
|
48
48
|
"dependencies": {
|
|
49
|
-
"@anievo-id/rbac-evolution": "^0.
|
|
49
|
+
"@anievo-id/rbac-evolution": "^0.9.0"
|
|
50
50
|
},
|
|
51
51
|
"peerDependencies": {
|
|
52
|
-
"mongoose": "
|
|
52
|
+
"mongoose": "^8.0.0"
|
|
53
53
|
},
|
|
54
54
|
"devDependencies": {
|
|
55
55
|
"mongodb-memory-server": "^10.1.0",
|