Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 71 additions & 36 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,63 +1,98 @@
name: ci
name: CI

on: [push, pull_request]

jobs:
build:
lint:
runs-on: ubuntu-latest
permissions:
contents: read
steps:
- uses: actions/checkout@v2

services:
mysql:
image: mysql:latest
ports:
- 3306:3306
env:
MYSQL_ALLOW_EMPTY_PASSWORD: yes
options: --health-cmd="mysqladmin ping" --health-interval=5s --health-timeout=2s --health-retries=3
- name: Set up Node.js
uses: actions/setup-node@v2.1.5

- name: Install Dependency
run: yarn install

- name: Run Lint
run: yarn run lint

- name: Run Format Check
run: yarn run format:check

coverage:
needs: [lint]
runs-on: ubuntu-latest
permissions:
contents: read
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2

- name: Setup MySQL
uses: mirromutth/mysql-action@v1.1
with:
mysql database: casbin

- name: Set up Node.js
uses: actions/setup-node@v2.1.5
with:
node-version: 12
- run: yarn install --frozen-lockfile
- run: mysql --host 127.0.0.1 --port 3306 -uroot -p -e "CREATE DATABASE casbin"
- run: yarn format:check
- run: yarn lint
- run: yarn run jest --coverage --forceExit
- name: Coveralls Parallel
node-version: 20

- name: Install Dependency
run: yarn install

- name: Run Coverage
run: yarn run jest --coverage --forceExit

- name: Coveralls
uses: coverallsapp/github-action@master
with:
github-token: ${{ secrets.github_token }}
github-token: ${{ secrets.GITHUB_TOKEN }}
parallel: true

finish:
needs: build
test:
needs: [lint]
runs-on: ubuntu-latest
permissions:
contents: read
steps:
- name: Coveralls Finished
uses: coverallsapp/github-action@master
- uses: actions/checkout@v2

- name: Setup MySQL
uses: mirromutth/mysql-action@v1.1
with:
mysql database: casbin

- name: Set up Node.js
uses: actions/setup-node@v3
with:
github-token: ${{ secrets.github_token }}
parallel-finished: true
node-version: 20

- name: Install Dependency
run: yarn install

- name: Run Unit test
run: yarn run test

semantic-release:
needs: [finish, build]
needs: [lint, test, coverage]
runs-on: ubuntu-latest
services:
mysql:
image: mysql:latest
ports:
- 3306:3306
env:
MYSQL_ALLOW_EMPTY_PASSWORD: yes
options: --health-cmd="mysqladmin ping" --health-interval=5s --health-timeout=2s --health-retries=3
permissions:
contents: write
steps:
- uses: actions/checkout@v2

- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: 20

- name: Run semantic-release
if: github.repository == 'node-casbin/sequelize-adapter' && github.event_name == 'push'
run: |
yarn install --frozen-lockfile
mysql --host 127.0.0.1 --port 3306 -uroot -p -e "CREATE DATABASE casbin"
yarn install
yarn run prepack
yarn run release
env:
Expand Down
94 changes: 54 additions & 40 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,80 +1,94 @@
# Sequelize Adapter

Sequelize Adapter
====
[![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)
[![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)
[![NPM version][npm-image]][npm-url]
[![NPM download][download-image]][download-url]
[![codebeat badge](https://codebeat.co/badges/c17c9ee1-da42-4db3-8047-9574ad2b23b1)](https://codebeat.co/projects/github-com-node-casbin-sequelize-adapter-master)
[![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)
[![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)
[![Discord](https://img.shields.io/discord/1022748306096537660?logo=discord&label=discord&color=5865F2)](https://discord.gg/S5UjpzGZjN)

[npm-image]: https://img.shields.io/npm/v/casbin-sequelize-adapter.svg?style=flat-square
[npm-url]: https://npmjs.org/package/casbin-sequelize-adapter
[npm-url]: https://npmjs.com/package/casbin-sequelize-adapter
[download-image]: https://img.shields.io/npm/dm/casbin-sequelize-adapter.svg?style=flat-square
[download-url]: https://npmjs.org/package/casbin-sequelize-adapter
[download-url]: https://npmjs.com/package/casbin-sequelize-adapter

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.

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

- PostgreSQL
- MySQL
- PostgreSQL
- SQLite
- MSSQL

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

## Installation

NPM Install
npm install casbin-sequelize-adapter

```bash
npm install casbin-sequelize-adapter --save
```
## Simple Example

Yarn Install
```typescript
import { newEnforcer } from 'casbin';
import { SequelizeAdapter } from 'casbin-sequelize-adapter';

```bash
yarn add casbin-sequelize-adapter
```
async function myFunction() {
// Initialize a Sequelize adapter and use it in a Node-Casbin enforcer:
// The adapter can not automatically create database.
// But the adapter will automatically create and use the table named "casbin_rule".
// I think ORM should not automatically create databases.
const a = await SequelizeAdapter.newAdapter({
username: 'root',
password: '',
database: 'casbin',
dialect: 'mysql',
});

## Testing Locally
const e = await newEnforcer('examples/rbac_model.conf', a);

Start mysql for tests:
// Load the policy from DB.
await e.loadPolicy();

```bash
docker compose up -d
```
// Check the permission.
await e.enforce('alice', 'data1', 'read');

```bash
yarn test
// Modify the policy.
// await e.addPolicy(...);
// await e.removePolicy(...);

// Save the policy back to DB.
await e.savePolicy();
}
```

## Simple Example
## Simple Filter Example

```typescript
import casbin from 'casbin';
import { newEnforcer } from 'casbin';
import { SequelizeAdapter } from 'casbin-sequelize-adapter';

async function myFunction() {
// Initialize a Sequelize adapter and use it in a Node-Casbin enforcer:
// The adapter can not automatically create database.
// But the adapter will automatically and use the table named "casbin_rule".
// The second boolean argument: autoCreateTable determines whether the adapter will automatically create the "casbin_rule" table.
// ORM should not create databases automatically.
const a = await SequelizeAdapter.newAdapter(
{
username: 'root',
password: '',
database: 'casbin',
dialect: 'mysql',
},
true,
);

const e = await casbin.newEnforcer('examples/rbac_model.conf', a);
// But the adapter will automatically create and use the table named "casbin_rule".
// I think ORM should not automatically create databases.
const a = await SequelizeAdapter.newAdapter({
username: 'root',
password: '',
database: 'casbin',
dialect: 'mysql',
});

const e = await newEnforcer('examples/rbac_model.conf', a);

// Load the filtered policy from DB.
await e.loadFilteredPolicy({
ptype: 'p',
v0: 'alice'
});

// Check the permission.
e.enforce('alice', 'data1', 'read');
await e.enforce('alice', 'data1', 'read');

// Modify the policy.
// await e.addPolicy(...);
Expand Down