{"id":15295163,"url":"https://github.com/touchifyapp/casbin-pg-adapter","last_synced_at":"2025-08-20T07:23:30.926Z","repository":{"id":40773968,"uuid":"256756740","full_name":"touchifyapp/casbin-pg-adapter","owner":"touchifyapp","description":"PostgreSQL native adapter for Node-Casbin with advanced filter capability and improved performance.","archived":false,"fork":false,"pushed_at":"2023-01-06T03:42:35.000Z","size":658,"stargazers_count":16,"open_issues_count":2,"forks_count":6,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-06-16T18:55:37.957Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/touchifyapp.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}},"created_at":"2020-04-18T13:10:08.000Z","updated_at":"2025-04-20T23:47:16.000Z","dependencies_parsed_at":"2023-02-05T06:02:09.539Z","dependency_job_id":null,"html_url":"https://github.com/touchifyapp/casbin-pg-adapter","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"purl":"pkg:github/touchifyapp/casbin-pg-adapter","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/touchifyapp%2Fcasbin-pg-adapter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/touchifyapp%2Fcasbin-pg-adapter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/touchifyapp%2Fcasbin-pg-adapter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/touchifyapp%2Fcasbin-pg-adapter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/touchifyapp","download_url":"https://codeload.github.com/touchifyapp/casbin-pg-adapter/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/touchifyapp%2Fcasbin-pg-adapter/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":261759290,"owners_count":23205527,"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":[],"created_at":"2024-09-30T17:08:52.571Z","updated_at":"2025-06-24T21:32:18.922Z","avatar_url":"https://github.com/touchifyapp.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# PostgreSQL Casbin Adapter\n\n[![NPM version](https://img.shields.io/npm/v/casbin-pg-adapter.svg?style=flat-square)](https://npmjs.org/package/casbin-pg-adapter)\n[![NPM download](https://img.shields.io/npm/dm/casbin-pg-adapter.svg?style=flat-square)](https://npmjs.org/package/casbin-pg-adapter)\n![Run Tests](https://github.com/touchifyapp/casbin-pg-adapter/workflows/Run%20Tests/badge.svg?branch=master\u0026event=push)\n[![Coverage Status](https://coveralls.io/repos/github/touchifyapp/casbin-pg-adapter/badge.svg?branch=master)](https://coveralls.io/github/touchifyapp/casbin-pg-adapter?branch=master)\n\n[PostgreSQL](https://www.postgresql.org/) native adapter for [Node-Casbin](https://github.com/casbin/node-casbin). With this library, Node-Casbin can load policy from PosgreSQL database or save policy to it. It supports loading filtered policies and is built for improving performances in PostgreSQL. It uses [node-postgres](https://node-postgres.com/) to connect to PostgreSQL.\n\n`casbin-pg-adapter` also adds advanced filtering capability. You can filter using `LIKE` or `regexp` expressions when using `loadFilteredPolicy`.\n\n## Installation\n\n```bash\nnpm install casbin-pg-adapter\n```\n\n## Simple example\n\n```typescript\nimport { newEnforcer } from \"casbin\";\nimport PostgresAdapter from \"casbin-pg-adapter\";\n\nasync function myFunction() {\n    // Initialize a Postgres adapter and use it in a Node-Casbin enforcer:\n    // The adapter can not automatically create database.\n    // But the adapter will automatically and use the table named \"casbin\".\n    // I think ORM should not automatically create databases.  \n    const a = await PostgresAdapter.newAdapter({\n        connectionString: \"postgresql://casbin:casbin@localhost:5432/casbin\"\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    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## Filtering example\n\n```typescript\nimport { newEnforcer } from \"casbin\";\nimport PostgresAdapter from \"casbin-pg-adapter\";\n\nasync function myFunction() {\n    const a = await PostgresAdapter.newAdapter({\n        connectionString: \"postgresql://casbin:casbin@localhost:5432/casbin\"\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        p: [\"alice\"],\n        g: [\"\", \"role:admin\"]\n    });\n\n    // Check the permission.\n    e.enforce(\"alice\", \"data1\", \"read\");\n}\n```\n\n## Advanced filtering example\n\n```typescript\nimport { newEnforcer } from \"casbin\";\nimport PostgresAdapter from \"casbin-pg-adapter\";\n\nasync function myFunction() {\n    const a = await PostgresAdapter.newAdapter({\n        connectionString: \"postgresql://casbin:casbin@localhost:5432/casbin\"\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        p: [\"regex:(role:.*)|(alice)\"],\n        g: [\"\", \"like:role:%\"]\n    });\n\n    // Check the permission.\n    e.enforce(\"alice\", \"data1\", \"read\");\n}\n```\n\n## Configuration\n\nYou can pass any [node-postgres](https://node-postgres.com/) options to the Adapter.\nSee `node-postgres` documentation: [Connecting to PostgreSQL](https://node-postgres.com/features/connecting#Programmatic).\n\n## Additional configurations\n\n#### Avoid database migration\n\nAdditionnally, you can pass the following option to the Adapter:\n * `migrate` (*Boolean*): If set to `false`, the Adapter will not apply migration when starting.\n\n**Note:** If you use this parameter, you should apply migration manually:\n\n```typescript\nasync function startup(): Promise\u003cvoid\u003e {\n    PostgresAdapter.migrate({\n        connectionString: \"postgresql://casbin:casbin@localhost:5432/casbin\"\n    });\n}\n\nasync function createEnforcer(): Promise\u003cEnforcer\u003e {\n    const a = await PostgresAdapter.newAdapter({\n        connectionString: \"postgresql://casbin:casbin@localhost:5432/casbin\",\n        migrate: false\n    });\n\n    return newEnforcer(\"examples/rbac_model.conf\", a);\n}\n```\n\n#### Disabling filtered behavior\n\nIf you want to use the `savePolicy` feature from `node-casbin`, you have to disable the filtered behavior of `PostgresAdapter`.\nYou can do it by calling `enableFiltered` on the adapter:\n\n```typescript\na.enableFiltered(false);\n```\n\n## Getting Help\n\n- [Node-Casbin](https://github.com/casbin/node-casbin)\n- [Casbin-PG-Adapter](https://github.com/touchifyapp/casbin-pg-adapter)\n\n## License\n\nThis project is under MIT License. See the [LICENSE](LICENSE) file for the full license text.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftouchifyapp%2Fcasbin-pg-adapter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftouchifyapp%2Fcasbin-pg-adapter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftouchifyapp%2Fcasbin-pg-adapter/lists"}