TypeORM Adapter

TypeORM Adapter is the TypeORM adapter for Node-Casbin. With this library, Node-Casbin can load policy from TypeORM supported database or save policy to it.
Based on Officially Supported Databases, the current supported databases are:
- MySQL
- PostgreSQL
- MariaDB
- SQLite
- MS SQL Server
- Oracle
- WebSQL
- MongoDB
You may find other 3rd-party supported DBs in TypeORM website or other places.
Installation
npm install typeorm-adapter
Simple Example
import { newEnforcer } from 'casbin';
import TypeORMAdapter from 'typeorm-adapter';
async function myFunction() {
const a = await TypeORMAdapter.newAdapter({
type: 'mysql',
host: 'localhost',
port: 3306,
username: 'root',
password: '',
database: 'casbin',
});
const e = await newEnforcer('examples/rbac_model.conf', a);
await e.loadPolicy();
await e.enforce('alice', 'data1', 'read');
await e.savePolicy();
}
Simple Filter Example
import { newEnforcer } from 'casbin';
import TypeORMAdapter from 'typeorm-adapter';
async function myFunction() {
const a = await TypeORMAdapter.newAdapter({
type: 'mysql',
host: 'localhost',
port: 3306,
username: 'root',
password: '',
database: 'casbin',
});
const e = await newEnforcer('examples/rbac_model.conf', a);
await e.loadFilteredPolicy({
'ptype': 'p',
'v0': 'alice'
});
await e.enforce('alice', 'data1', 'read');
await e.savePolicy();
}
Custom Entity Example
Use a custom entity that matches the CasbinRule or MongoCasbinRule in order to add additional fields or metadata to the entity.
import { newEnforcer } from 'casbin';
import {
CreateDateColumn,
UpdateDateColumn,
} from 'typeorm';
import TypeORMAdapter from 'typeorm-adapter';
@Entity('custom_rule')
class CustomCasbinRule extends CasbinRule {
@CreateDateColumn()
createdDate: Date;
@UpdateDateColumn()
updatedDate: Date;
}
async function myFunction() {
const a = await TypeORMAdapter.newAdapter({
type: 'mysql',
host: 'localhost',
port: 3306,
username: 'root',
password: '',
database: 'casbin',
},
{
customCasbinRuleEntity: CustomCasbinRule,
},
);
const e = await newEnforcer('examples/rbac_model.conf', a);
await e.loadFilteredPolicy({
'ptype': 'p',
'v0': 'alice'
});
await e.enforce('alice', 'data1', 'read');
await e.savePolicy();
}
Custom Database Table Name Example
If you want to use a custom table name for the casbin rules, you need to:
Create a custom entity class that inherits from CasbinRule and uses the @Entity decorator with your table name.
Pass the custom entity class to the entities array of the data source constructor.
Pass the custom entity class to the customCasbinRuleEntity option of the typeorm-adapter constructor.
import { newEnforcer } from 'casbin';
import {
CreateDateColumn,
UpdateDateColumn,
} from 'typeorm';
import TypeORMAdapter from 'typeorm-adapter';
@Entity('custom_rule')
class CustomCasbinRule extends CasbinRule {
@CreateDateColumn()
createdDate: Date;
@UpdateDateColumn()
updatedDate: Date;
}
async function myFunction() {
const datasource = new DataSource({
type: 'mysql',
host: 'localhost',
port: 3306,
username: 'root',
password: '',
database: 'casbin',
entities: [CustomCasbinRule],
synchronize: true,
});
const a = await TypeORMAdapter.newAdapter(
{ connection: datasource },
{
customCasbinRuleEntity: CustomCasbinRule,
},
);
const e = await newEnforcer('examples/rbac_model.conf', a);
await e.loadFilteredPolicy({
'ptype': 'p',
'v0': 'alice'
});
await e.enforce('alice', 'data1', 'read');
await e.savePolicy();
}
Getting Help
License
This project is under Apache 2.0 License. See the LICENSE file for the full license text.