
Security News
Node.js TSC Votes to Stop Distributing Corepack
Corepack will be phased out from future Node.js releases following a TSC vote.
typeorm-adapter
Advanced tools
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 create 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 create 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 create 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 create 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,
});
const a = 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.
FAQs
TypeORM adapter for Casbin
The npm package typeorm-adapter receives a total of 5,713 weekly downloads. As such, typeorm-adapter popularity was classified as popular.
We found that typeorm-adapter demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Corepack will be phased out from future Node.js releases following a TSC vote.
Research
Security News
Research uncovers Black Basta's plans to exploit package registries for ransomware delivery alongside evidence of similar attacks already targeting open source ecosystems.
Security News
Oxlint's beta release introduces 500+ built-in linting rules while delivering twice the speed of previous versions, with future support planned for custom plugins and improved IDE integration.