Project Name | Stars | Downloads | Repos Using This | Packages Using This | Most Recent Commit | Total Releases | Latest Release | Open Issues | License | Language |
---|---|---|---|---|---|---|---|---|---|---|
Gorm Adapter | 613 | 71 | 5 days ago | 53 | July 03, 2022 | apache-2.0 | Go | |||
GORM adapter for Casbin, see extended version of GORM Adapter Ex at: https://github.com/casbin/gorm-adapter-ex | ||||||||||
Xorm Adapter | 371 | 11 | a month ago | 11 | January 20, 2022 | apache-2.0 | Go | |||
Xorm adapter for Casbin | ||||||||||
Casbin Server | 276 | 2 | 4 | 3 days ago | 6 | December 21, 2022 | 1 | apache-2.0 | Go | |
Casbin as a Service (CaaS) | ||||||||||
Mongodb Adapter | 242 | 5 | 2 months ago | 12 | September 16, 2022 | apache-2.0 | Go | |||
MongoDB adapter for Casbin | ||||||||||
Protobuf Adapter | 186 | 6 years ago | May 24, 2021 | apache-2.0 | Go | |||||
Google Protocol Buffers adapter for Casbin | ||||||||||
Redis Adapter | 182 | 8 months ago | 2 | January 20, 2022 | apache-2.0 | Go | ||||
Redis adapter for Casbin | ||||||||||
Rethinkdb Adapter | 148 | a year ago | May 22, 2021 | 1 | mit | Go | ||||
RethinkDB adapter for Casbin https://github.com/casbin/casbin | ||||||||||
Dynacasbin | 143 | 2 years ago | Go | |||||||
DynamoDB adapter for Casbin | ||||||||||
Typeorm Adapter | 63 | 2 | 5 | 17 days ago | 14 | August 04, 2022 | 1 | apache-2.0 | TypeScript | |
TypeORM adapter for Casbin | ||||||||||
Sqlalchemy Adapter | 63 | 4 months ago | 12 | September 09, 2021 | apache-2.0 | Python | ||||
SQLAlchemy Adapter for PyCasbin |
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:
You may find other 3rd-party supported DBs in TypeORM website or other places.
npm install typeorm-adapter
import { newEnforcer } from 'casbin';
import TypeORMAdapter from 'typeorm-adapter';
async function myFunction() {
// Initialize a TypeORM 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".
// I think ORM should not automatically create databases.
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);
// Load the policy from DB.
await e.loadPolicy();
// Check the permission.
await e.enforce('alice', 'data1', 'read');
// Modify the policy.
// await e.addPolicy(...);
// await e.removePolicy(...);
// Save the policy back to DB.
await e.savePolicy();
}
import { newEnforcer } from 'casbin';
import TypeORMAdapter from 'typeorm-adapter';
async function myFunction() {
// Initialize a TypeORM 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".
// I think ORM should not automatically create databases.
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);
// Load the filtered policy from DB.
await e.loadFilteredPolicy({
'ptype': 'p',
'v0': 'alice'
});
// Check the permission.
await e.enforce('alice', 'data1', 'read');
// Modify the policy.
// await e.addPolicy(...);
// await e.removePolicy(...);
// Save the policy back to DB.
await e.savePolicy();
}
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() {
// Initialize a TypeORM 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".
// I think ORM should not automatically create databases.
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);
// Load the filtered policy from DB.
await e.loadFilteredPolicy({
'ptype': 'p',
'v0': 'alice'
});
// Check the permission.
await e.enforce('alice', 'data1', 'read');
// Modify the policy.
// await e.addPolicy(...);
// await e.removePolicy(...);
// Save the policy back to DB.
await e.savePolicy();
}
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() {
// Initialize a TypeORM 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".
// I think ORM should not automatically create databases.
const datasource = new DataSource({
type: 'mysql',
host: 'localhost',
port: 3306,
username: 'root',
password: '',
database: 'casbin',
entities: [CustomCasbinRule],
synchronize: true,
});
await TypeORMAdapter.newAdapter(
{ connection: datasource },
{
customCasbinRuleEntity: CustomCasbinRule,
},
);
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.
await e.enforce('alice', 'data1', 'read');
// Modify the policy.
// await e.addPolicy(...);
// await e.removePolicy(...);
// Save the policy back to DB.
await e.savePolicy();
}
This project is under Apache 2.0 License. See the LICENSE file for the full license text.