Typeorm Adapter

TypeORM adapter for Casbin
Alternatives To Typeorm Adapter
Project NameStarsDownloadsRepos Using ThisPackages Using ThisMost Recent CommitTotal ReleasesLatest ReleaseOpen IssuesLicenseLanguage
Gorm Adapter613715 days ago53July 03, 2022apache-2.0Go
GORM adapter for Casbin, see extended version of GORM Adapter Ex at: https://github.com/casbin/gorm-adapter-ex
Xorm Adapter37111a month ago11January 20, 2022apache-2.0Go
Xorm adapter for Casbin
Casbin Server276243 days ago6December 21, 20221apache-2.0Go
Casbin as a Service (CaaS)
Mongodb Adapter24252 months ago12September 16, 2022apache-2.0Go
MongoDB adapter for Casbin
Protobuf Adapter186
6 years agoMay 24, 2021apache-2.0Go
Google Protocol Buffers adapter for Casbin
Redis Adapter182
8 months ago2January 20, 2022apache-2.0Go
Redis adapter for Casbin
Rethinkdb Adapter148
a year agoMay 22, 20211mitGo
RethinkDB adapter for Casbin https://github.com/casbin/casbin
Dynacasbin143
2 years agoGo
DynamoDB adapter for Casbin
Typeorm Adapter632517 days ago14August 04, 20221apache-2.0TypeScript
TypeORM adapter for Casbin
Sqlalchemy Adapter63
4 months ago12September 09, 2021apache-2.0Python
SQLAlchemy Adapter for PyCasbin
Alternatives To Typeorm Adapter
Select To Compare


Alternative Project Comparisons
Readme

TypeORM Adapter

NPM version NPM download codebeat badge CI Coverage Status Discord

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() {
    // 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();
}

Simple Filter Example

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();
}

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() {
    // 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();
}

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() {
    // 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();
}

Getting Help

License

This project is under Apache 2.0 License. See the LICENSE file for the full license text.

Popular Casbin Projects
Popular Adapter Projects
Popular Security Categories
Related Searches

Get A Weekly Email With Trending Projects For These Categories
No Spam. Unsubscribe easily at any time.
Javascript
Js
Typescript
Ts
Node
Databases
Adapter
Orm
Authorization
Mariadb
Access Control
Casbin
Websql