|
1 | | -# Sequelize Adapter |
2 | | - |
| 1 | +Sequelize Adapter |
| 2 | +==== |
| 3 | +[](https://github.com/node-casbin/sequelize-adapter/actions/workflows/ci.yml) |
| 4 | +[](https://coveralls.io/github/node-casbin/sequelize-adapter?branch=master) |
3 | 5 | [![NPM version][npm-image]][npm-url] |
4 | 6 | [![NPM download][download-image]][download-url] |
5 | | -[](https://codebeat.co/projects/github-com-node-casbin-sequelize-adapter-master) |
6 | | -[](https://github.com/node-casbin/sequelize-adapter/actions/workflows/ci.yml) |
7 | | -[](https://coveralls.io/github/node-casbin/sequelize-adapter?branch=master) |
8 | 7 | [](https://discord.gg/S5UjpzGZjN) |
9 | 8 |
|
10 | 9 | [npm-image]: https://img.shields.io/npm/v/casbin-sequelize-adapter.svg?style=flat-square |
11 | | -[npm-url]: https://npmjs.org/package/casbin-sequelize-adapter |
| 10 | +[npm-url]: https://npmjs.com/package/casbin-sequelize-adapter |
12 | 11 | [download-image]: https://img.shields.io/npm/dm/casbin-sequelize-adapter.svg?style=flat-square |
13 | | -[download-url]: https://npmjs.org/package/casbin-sequelize-adapter |
| 12 | +[download-url]: https://npmjs.com/package/casbin-sequelize-adapter |
14 | 13 |
|
15 | 14 | Sequelize Adapter is the [Sequelize](https://github.com/sequelize/sequelize) adapter for [Node-Casbin](https://github.com/casbin/node-casbin). With this library, Node-Casbin can load policy from Sequelize supported database or save policy to it. |
16 | 15 |
|
17 | 16 | Based on [Officially Supported Databases](http://docs.sequelizejs.com/), the current supported databases are: |
18 | 17 |
|
19 | | -- PostgreSQL |
20 | 18 | - MySQL |
| 19 | +- PostgreSQL |
21 | 20 | - SQLite |
22 | 21 | - MSSQL |
23 | 22 |
|
24 | 23 | You may find other 3rd-party supported DBs in Sequelize website or other places. |
25 | 24 |
|
26 | 25 | ## Installation |
27 | 26 |
|
28 | | -NPM Install |
| 27 | + npm install casbin-sequelize-adapter |
29 | 28 |
|
30 | | -```bash |
31 | | -npm install casbin-sequelize-adapter --save |
32 | | -``` |
| 29 | +## Simple Example |
33 | 30 |
|
34 | | -Yarn Install |
| 31 | +```typescript |
| 32 | +import { newEnforcer } from 'casbin'; |
| 33 | +import { SequelizeAdapter } from 'casbin-sequelize-adapter'; |
35 | 34 |
|
36 | | -```bash |
37 | | -yarn add casbin-sequelize-adapter |
38 | | -``` |
| 35 | +async function myFunction() { |
| 36 | + // Initialize a Sequelize adapter and use it in a Node-Casbin enforcer: |
| 37 | + // The adapter can not automatically create database. |
| 38 | + // But the adapter will automatically create and use the table named "casbin_rule". |
| 39 | + // I think ORM should not automatically create databases. |
| 40 | + const a = await SequelizeAdapter.newAdapter({ |
| 41 | + username: 'root', |
| 42 | + password: '', |
| 43 | + database: 'casbin', |
| 44 | + dialect: 'mysql', |
| 45 | + }); |
39 | 46 |
|
40 | | -## Testing Locally |
| 47 | + const e = await newEnforcer('examples/rbac_model.conf', a); |
41 | 48 |
|
42 | | -Start mysql for tests: |
| 49 | + // Load the policy from DB. |
| 50 | + await e.loadPolicy(); |
43 | 51 |
|
44 | | -```bash |
45 | | -docker compose up -d |
46 | | -``` |
| 52 | + // Check the permission. |
| 53 | + await e.enforce('alice', 'data1', 'read'); |
47 | 54 |
|
48 | | -```bash |
49 | | -yarn test |
| 55 | + // Modify the policy. |
| 56 | + // await e.addPolicy(...); |
| 57 | + // await e.removePolicy(...); |
| 58 | + |
| 59 | + // Save the policy back to DB. |
| 60 | + await e.savePolicy(); |
| 61 | +} |
50 | 62 | ``` |
51 | 63 |
|
52 | | -## Simple Example |
| 64 | +## Simple Filter Example |
53 | 65 |
|
54 | 66 | ```typescript |
55 | | -import casbin from 'casbin'; |
| 67 | +import { newEnforcer } from 'casbin'; |
56 | 68 | import { SequelizeAdapter } from 'casbin-sequelize-adapter'; |
57 | 69 |
|
58 | 70 | async function myFunction() { |
59 | 71 | // Initialize a Sequelize adapter and use it in a Node-Casbin enforcer: |
60 | 72 | // The adapter can not automatically create database. |
61 | | - // But the adapter will automatically and use the table named "casbin_rule". |
62 | | - // The second boolean argument: autoCreateTable determines whether the adapter will automatically create the "casbin_rule" table. |
63 | | - // ORM should not create databases automatically. |
64 | | - const a = await SequelizeAdapter.newAdapter( |
65 | | - { |
66 | | - username: 'root', |
67 | | - password: '', |
68 | | - database: 'casbin', |
69 | | - dialect: 'mysql', |
70 | | - }, |
71 | | - true, |
72 | | - ); |
73 | | - |
74 | | - const e = await casbin.newEnforcer('examples/rbac_model.conf', a); |
| 73 | + // But the adapter will automatically create and use the table named "casbin_rule". |
| 74 | + // I think ORM should not automatically create databases. |
| 75 | + const a = await SequelizeAdapter.newAdapter({ |
| 76 | + username: 'root', |
| 77 | + password: '', |
| 78 | + database: 'casbin', |
| 79 | + dialect: 'mysql', |
| 80 | + }); |
| 81 | + |
| 82 | + const e = await newEnforcer('examples/rbac_model.conf', a); |
| 83 | + |
| 84 | + // Load the filtered policy from DB. |
| 85 | + await e.loadFilteredPolicy({ |
| 86 | + 'ptype': 'p', |
| 87 | + 'v0': 'alice' |
| 88 | + }); |
75 | 89 |
|
76 | 90 | // Check the permission. |
77 | | - e.enforce('alice', 'data1', 'read'); |
| 91 | + await e.enforce('alice', 'data1', 'read'); |
78 | 92 |
|
79 | 93 | // Modify the policy. |
80 | 94 | // await e.addPolicy(...); |
|
0 commit comments