{"id":13683333,"url":"https://github.com/node-casbin/typeorm-adapter","last_synced_at":"2025-04-06T06:11:49.744Z","repository":{"id":33144668,"uuid":"153125098","full_name":"node-casbin/typeorm-adapter","owner":"node-casbin","description":"TypeORM adapter for Casbin","archived":false,"fork":false,"pushed_at":"2024-03-27T04:31:51.000Z","size":419,"stargazers_count":72,"open_issues_count":0,"forks_count":36,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-30T05:05:43.450Z","etag":null,"topics":["access-control","adapter","authorization","casbin","javascript","js","node-casbin","orm","ts","typeorm","typescript"],"latest_commit_sha":null,"homepage":"https://github.com/casbin/node-casbin","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/node-casbin.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-10-15T14:11:07.000Z","updated_at":"2025-03-23T08:53:10.000Z","dependencies_parsed_at":"2023-10-15T14:19:38.257Z","dependency_job_id":"fa9ec827-38f6-43db-9c7d-2cec66306c03","html_url":"https://github.com/node-casbin/typeorm-adapter","commit_stats":{"total_commits":66,"total_committers":22,"mean_commits":3.0,"dds":0.7878787878787878,"last_synced_commit":"1501f30dacfc844f772eedc3709f013eebc4f87b"},"previous_names":[],"tags_count":19,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/node-casbin%2Ftypeorm-adapter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/node-casbin%2Ftypeorm-adapter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/node-casbin%2Ftypeorm-adapter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/node-casbin%2Ftypeorm-adapter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/node-casbin","download_url":"https://codeload.github.com/node-casbin/typeorm-adapter/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247441056,"owners_count":20939239,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["access-control","adapter","authorization","casbin","javascript","js","node-casbin","orm","ts","typeorm","typescript"],"created_at":"2024-08-02T13:02:07.812Z","updated_at":"2025-04-06T06:11:49.723Z","avatar_url":"https://github.com/node-casbin.png","language":"TypeScript","readme":"TypeORM Adapter\n====\n[![NPM version][npm-image]][npm-url]\n[![NPM download][download-image]][download-url]\n[![codebeat badge](https://codebeat.co/badges/7b938f17-ac89-4ee9-b3cc-787b5e94720d)](https://codebeat.co/projects/github-com-node-casbin-typeorm-adapter-master)\n[![CI](https://github.com/node-casbin/typeorm-adapter/actions/workflows/ci.yml/badge.svg)](https://github.com/node-casbin/typeorm-adapter/actions/workflows/ci.yml)\n[![Coverage Status](https://coveralls.io/repos/github/node-casbin/typeorm-adapter/badge.svg?branch=master)](https://coveralls.io/github/node-casbin/typeorm-adapter?branch=master)\n[![Discord](https://img.shields.io/discord/1022748306096537660?logo=discord\u0026label=discord\u0026color=5865F2)](https://discord.gg/S5UjpzGZjN)\n\n[npm-image]: https://img.shields.io/npm/v/typeorm-adapter.svg?style=flat-square\n[npm-url]: https://npmjs.com/package/typeorm-adapter\n[download-image]: https://img.shields.io/npm/dm/typeorm-adapter.svg?style=flat-square\n[download-url]: https://npmjs.com/package/typeorm-adapter\n\nTypeORM Adapter is the [TypeORM](https://github.com/typeorm/typeorm) adapter for [Node-Casbin](https://github.com/casbin/node-casbin). With this library, Node-Casbin can load policy from TypeORM supported database or save policy to it.\n\nBased on [Officially Supported Databases](http://typeorm.io), the current supported databases are:\n\n- MySQL\n- PostgreSQL\n- MariaDB\n- SQLite\n- MS SQL Server\n- Oracle\n- WebSQL\n- MongoDB \n\n\nYou may find other 3rd-party supported DBs in TypeORM website or other places.\n\n## Installation\n\n    npm install typeorm-adapter\n\n## Simple Example\n\n```typescript\nimport { newEnforcer } from 'casbin';\nimport TypeORMAdapter from 'typeorm-adapter';\n\nasync function myFunction() {\n    // Initialize a TypeORM adapter and use it in a Node-Casbin enforcer:\n    // The adapter can not automatically create database.\n    // But the adapter will automatically create and use the table named \"casbin_rule\".\n    // I think ORM should not automatically create databases.  \n    const a = await TypeORMAdapter.newAdapter({\n        type: 'mysql',\n        host: 'localhost',\n        port: 3306,\n        username: 'root',\n        password: '',\n        database: 'casbin',\n    });\n\n\n    const e = await newEnforcer('examples/rbac_model.conf', a);\n\n    // Load the policy from DB.\n    await e.loadPolicy();\n\n    // Check the permission.\n    await e.enforce('alice', 'data1', 'read');\n\n    // Modify the policy.\n    // await e.addPolicy(...);\n    // await e.removePolicy(...);\n\n    // Save the policy back to DB.\n    await e.savePolicy();\n}\n```\n\n## Simple Filter Example\n\n```typescript\nimport { newEnforcer } from 'casbin';\nimport TypeORMAdapter from 'typeorm-adapter';\n\nasync function myFunction() {\n    // Initialize a TypeORM adapter and use it in a Node-Casbin enforcer:\n    // The adapter can not automatically create database.\n    // But the adapter will automatically create and use the table named \"casbin_rule\".\n    // I think ORM should not automatically create databases.  \n    const a = await TypeORMAdapter.newAdapter({\n        type: 'mysql',\n        host: 'localhost',\n        port: 3306,\n        username: 'root',\n        password: '',\n        database: 'casbin',\n    });\n\n\n    const e = await newEnforcer('examples/rbac_model.conf', a);\n\n    // Load the filtered policy from DB.\n    await e.loadFilteredPolicy({\n        'ptype': 'p',\n        'v0': 'alice'\n    });\n\n    // Check the permission.\n    await e.enforce('alice', 'data1', 'read');\n\n    // Modify the policy.\n    // await e.addPolicy(...);\n    // await e.removePolicy(...);\n\n    // Save the policy back to DB.\n    await e.savePolicy();\n}\n```\n\n## Custom Entity Example\nUse a custom entity that matches the CasbinRule or MongoCasbinRule in order to add additional fields or metadata to the entity.\n\n```typescript\nimport { newEnforcer } from 'casbin';\nimport {\n  CreateDateColumn,\n  UpdateDateColumn,\n} from 'typeorm';\nimport TypeORMAdapter from 'typeorm-adapter';\n\n@Entity('custom_rule')\nclass CustomCasbinRule extends CasbinRule {\n  @CreateDateColumn()\n  createdDate: Date;\n\n  @UpdateDateColumn()\n  updatedDate: Date;\n}\n\nasync function myFunction() {\n    // Initialize a TypeORM adapter and use it in a Node-Casbin enforcer:\n    // The adapter can not automatically create database.\n    // But the adapter will automatically create and use the table named \"casbin_rule\".\n    // I think ORM should not automatically create databases.  \n    const a = await TypeORMAdapter.newAdapter({\n        type: 'mysql',\n        host: 'localhost',\n        port: 3306,\n        username: 'root',\n        password: '',\n        database: 'casbin',\n      },\n      {\n        customCasbinRuleEntity: CustomCasbinRule,\n      },\n    );\n\n    const e = await newEnforcer('examples/rbac_model.conf', a);\n\n    // Load the filtered policy from DB.\n    await e.loadFilteredPolicy({\n        'ptype': 'p',\n        'v0': 'alice'\n    });\n\n    // Check the permission.\n    await e.enforce('alice', 'data1', 'read');\n\n    // Modify the policy.\n    // await e.addPolicy(...);\n    // await e.removePolicy(...);\n\n    // Save the policy back to DB.\n    await e.savePolicy();\n}\n```\n## Custom Database Table Name Example\nIf you want to use a custom table name for the casbin rules, you need to:\nCreate a custom entity class that inherits from CasbinRule and uses the @Entity decorator with your table name.\nPass the custom entity class to the entities array of the data source constructor.\nPass the custom entity class to the customCasbinRuleEntity option of the typeorm-adapter constructor.\n\n```typescript\nimport { newEnforcer } from 'casbin';\nimport {\n  CreateDateColumn,\n  UpdateDateColumn,\n} from 'typeorm';\nimport TypeORMAdapter from 'typeorm-adapter';\n\n@Entity('custom_rule')\nclass CustomCasbinRule extends CasbinRule {\n  @CreateDateColumn()\n  createdDate: Date;\n\n  @UpdateDateColumn()\n  updatedDate: Date;\n}\n\nasync function myFunction() {\n    // Initialize a TypeORM adapter and use it in a Node-Casbin enforcer:\n    // The adapter can not automatically create database.\n    // But the adapter will automatically create and use the table named \"casbin_rule\".\n    // I think ORM should not automatically create databases.  \n\n    const datasource = new DataSource({\n        type: 'mysql',\n        host: 'localhost',\n        port: 3306,\n        username: 'root',\n        password: '',\n        database: 'casbin',\n        entities: [CustomCasbinRule],\n        synchronize: true,\n    });\n\n    const a = await TypeORMAdapter.newAdapter(\n      { connection: datasource },\n      {\n        customCasbinRuleEntity: CustomCasbinRule,\n      },\n    );\n\n    const e = await newEnforcer('examples/rbac_model.conf', a);\n\n    // Load the filtered policy from DB.\n    await e.loadFilteredPolicy({\n        'ptype': 'p',\n        'v0': 'alice'\n    });\n\n    // Check the permission.\n    await e.enforce('alice', 'data1', 'read');\n\n    // Modify the policy.\n    // await e.addPolicy(...);\n    // await e.removePolicy(...);\n\n    // Save the policy back to DB.\n    await e.savePolicy();\n}\n```\n\n## Getting Help\n\n- [Node-Casbin](https://github.com/casbin/node-casbin)\n\n## License\n\nThis project is under Apache 2.0 License. See the [LICENSE](LICENSE) file for the full license text.\n","funding_links":[],"categories":["TypeScript"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnode-casbin%2Ftypeorm-adapter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnode-casbin%2Ftypeorm-adapter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnode-casbin%2Ftypeorm-adapter/lists"}