Skip to content

Commit b20a059

Browse files
Copilotnomeguy
andcommitted
Rewrite CI workflow and README to match typeorm-adapter pattern
Co-authored-by: nomeguy <85475922+nomeguy@users.noreply.github.com>
1 parent ea168ef commit b20a059

File tree

2 files changed

+122
-76
lines changed

2 files changed

+122
-76
lines changed

.github/workflows/ci.yml

Lines changed: 68 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,95 @@
1-
name: ci
1+
name: CI
22

33
on: [push, pull_request]
44

55
jobs:
6-
build:
6+
lint:
77
runs-on: ubuntu-latest
8+
steps:
9+
- uses: actions/checkout@v2
810

9-
services:
10-
mysql:
11-
image: mysql:latest
12-
ports:
13-
- 3306:3306
14-
env:
15-
MYSQL_ALLOW_EMPTY_PASSWORD: yes
16-
options: --health-cmd="mysqladmin ping" --health-interval=5s --health-timeout=2s --health-retries=3
11+
- name: Set up Node.js
12+
uses: actions/setup-node@v2.1.5
13+
14+
- name: Install Dependency
15+
run: yarn install
16+
17+
- name: Run Lint
18+
run: yarn run lint
19+
20+
- name: Run Format Check
21+
run: yarn run format:check
1722

23+
coverage:
24+
needs: [lint]
25+
runs-on: ubuntu-latest
1826
steps:
1927
- uses: actions/checkout@v2
20-
- uses: actions/setup-node@v2
28+
29+
- name: Setup MySQL
30+
uses: mirromutth/mysql-action@v1.1
31+
with:
32+
mysql database: casbin
33+
mysql root password: password
34+
35+
- name: Set up Node.js
36+
uses: actions/setup-node@v2.1.5
2137
with:
22-
node-version: 12
23-
- run: yarn install --frozen-lockfile
24-
- run: mysql --host 127.0.0.1 --port 3306 -uroot -p -e "CREATE DATABASE casbin"
25-
- run: yarn format:check
26-
- run: yarn lint
27-
- run: yarn run jest --coverage --forceExit
28-
- name: Coveralls Parallel
38+
node-version: ^20
39+
40+
- name: Install Dependency
41+
run: yarn install
42+
43+
- name: Run Coverage
44+
run: yarn run jest --coverage --forceExit
45+
46+
- name: Coveralls
2947
uses: coverallsapp/github-action@master
3048
with:
31-
github-token: ${{ secrets.github_token }}
49+
github-token: ${{ secrets.GITHUB_TOKEN }}
50+
parallel: true
3251

33-
finish:
34-
needs: build
52+
test:
53+
needs: [lint]
3554
runs-on: ubuntu-latest
55+
strategy:
56+
matrix:
57+
node: [^18, ^20]
3658
steps:
37-
- name: Coveralls Finished
38-
uses: coverallsapp/github-action@master
59+
- uses: actions/checkout@v2
60+
61+
- name: Setup MySQL
62+
uses: mirromutth/mysql-action@v1.1
63+
with:
64+
mysql database: casbin
65+
mysql root password: password
66+
67+
- name: Set up Node.js
68+
uses: actions/setup-node@v3
3969
with:
40-
github-token: ${{ secrets.github_token }}
41-
parallel-finished: true
70+
node-version: ${{ matrix.node }}
71+
72+
- name: Install Dependency
73+
run: yarn install
74+
75+
- name: Run Unit test
76+
run: yarn run test
4277

4378
semantic-release:
44-
needs: [finish, build]
79+
needs: [lint, test, coverage]
4580
runs-on: ubuntu-latest
46-
services:
47-
mysql:
48-
image: mysql:latest
49-
ports:
50-
- 3306:3306
51-
env:
52-
MYSQL_ALLOW_EMPTY_PASSWORD: yes
53-
options: --health-cmd="mysqladmin ping" --health-interval=5s --health-timeout=2s --health-retries=3
5481
steps:
5582
- uses: actions/checkout@v2
83+
84+
- name: Set up Node.js
85+
uses: actions/setup-node@v3
86+
with:
87+
node-version: ^20
88+
5689
- name: Run semantic-release
5790
if: github.repository == 'node-casbin/sequelize-adapter' && github.event_name == 'push'
5891
run: |
59-
yarn install --frozen-lockfile
60-
mysql --host 127.0.0.1 --port 3306 -uroot -p -e "CREATE DATABASE casbin"
92+
yarn install
6193
yarn run prepack
6294
yarn run release
6395
env:

README.md

Lines changed: 54 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,94 @@
1-
# Sequelize Adapter
2-
1+
Sequelize Adapter
2+
====
3+
[![CI](https://github.com/node-casbin/sequelize-adapter/actions/workflows/ci.yml/badge.svg)](https://github.com/node-casbin/sequelize-adapter/actions/workflows/ci.yml)
4+
[![Coverage Status](https://coveralls.io/repos/github/node-casbin/sequelize-adapter/badge.svg?branch=master)](https://coveralls.io/github/node-casbin/sequelize-adapter?branch=master)
35
[![NPM version][npm-image]][npm-url]
46
[![NPM download][download-image]][download-url]
5-
[![codebeat badge](https://codebeat.co/badges/c17c9ee1-da42-4db3-8047-9574ad2b23b1)](https://codebeat.co/projects/github-com-node-casbin-sequelize-adapter-master)
6-
[![ci](https://github.com/node-casbin/sequelize-adapter/actions/workflows/ci.yml/badge.svg)](https://github.com/node-casbin/sequelize-adapter/actions/workflows/ci.yml)
7-
[![Coverage Status](https://coveralls.io/repos/github/node-casbin/sequelize-adapter/badge.svg?branch=master)](https://coveralls.io/github/node-casbin/sequelize-adapter?branch=master)
87
[![Discord](https://img.shields.io/discord/1022748306096537660?logo=discord&label=discord&color=5865F2)](https://discord.gg/S5UjpzGZjN)
98

109
[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
1211
[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
1413

1514
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.
1615

1716
Based on [Officially Supported Databases](http://docs.sequelizejs.com/), the current supported databases are:
1817

19-
- PostgreSQL
2018
- MySQL
19+
- PostgreSQL
2120
- SQLite
2221
- MSSQL
2322

2423
You may find other 3rd-party supported DBs in Sequelize website or other places.
2524

2625
## Installation
2726

28-
NPM Install
27+
npm install casbin-sequelize-adapter
2928

30-
```bash
31-
npm install casbin-sequelize-adapter --save
32-
```
29+
## Simple Example
3330

34-
Yarn Install
31+
```typescript
32+
import { newEnforcer } from 'casbin';
33+
import { SequelizeAdapter } from 'casbin-sequelize-adapter';
3534

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+
});
3946

40-
## Testing Locally
47+
const e = await newEnforcer('examples/rbac_model.conf', a);
4148

42-
Start mysql for tests:
49+
// Load the policy from DB.
50+
await e.loadPolicy();
4351

44-
```bash
45-
docker compose up -d
46-
```
52+
// Check the permission.
53+
await e.enforce('alice', 'data1', 'read');
4754

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+
}
5062
```
5163

52-
## Simple Example
64+
## Simple Filter Example
5365

5466
```typescript
55-
import casbin from 'casbin';
67+
import { newEnforcer } from 'casbin';
5668
import { SequelizeAdapter } from 'casbin-sequelize-adapter';
5769

5870
async function myFunction() {
5971
// Initialize a Sequelize adapter and use it in a Node-Casbin enforcer:
6072
// 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+
});
7589

7690
// Check the permission.
77-
e.enforce('alice', 'data1', 'read');
91+
await e.enforce('alice', 'data1', 'read');
7892

7993
// Modify the policy.
8094
// await e.addPolicy(...);

0 commit comments

Comments
 (0)