@@ -136,8 +136,7 @@ async function myFunction() {
136136 // The adapter can not automatically create database.
137137 // But the adapter will automatically and use the table named "casbin_rule".
138138 // I think ORM should not automatically create databases.
139- const a = await TypeORMAdapter .newAdapter (
140- {
139+ const a = await TypeORMAdapter .newAdapter ({
141140 type: ' mysql' ,
142141 host: ' localhost' ,
143142 port: 3306 ,
@@ -169,6 +168,73 @@ async function myFunction() {
169168 await e .savePolicy ();
170169}
171170```
171+ ## Custom Database Table Name Example
172+ If you want to use a custom table name for the casbin rules, you need to:
173+ Create a custom entity class that inherits from CasbinRule and uses the @Entity decorator with your table name.
174+ Pass the custom entity class to the entities array of the data source constructor.
175+ Pass the custom entity class to the customCasbinRuleEntity option of the typeorm-adapter constructor.
176+
177+ ``` typescript
178+ import { newEnforcer } from ' casbin' ;
179+ import {
180+ CreateDateColumn ,
181+ UpdateDateColumn ,
182+ } from ' typeorm' ;
183+ import TypeORMAdapter from ' typeorm-adapter' ;
184+
185+ @Entity (' custom_rule' )
186+ class CustomCasbinRule extends CasbinRule {
187+ @CreateDateColumn ()
188+ createdDate: Date ;
189+
190+ @UpdateDateColumn ()
191+ updatedDate: Date ;
192+ }
193+
194+ async function myFunction() {
195+ // Initialize a TypeORM adapter and use it in a Node-Casbin enforcer:
196+ // The adapter can not automatically create database.
197+ // But the adapter will automatically and use the table named "casbin_rule".
198+ // I think ORM should not automatically create databases.
199+
200+ const datasource = new DataSource ({
201+ type: ' mysql' ,
202+ host: ' localhost' ,
203+ port: 3306 ,
204+ username: ' root' ,
205+ password: ' ' ,
206+ database: ' casbin' ,
207+ entities: [CustomCasbinRule ],
208+ synchronize: true ,
209+ });
210+
211+ await TypeORMAdapter .newAdapter (
212+ { connection: datasource },
213+ {
214+ customCasbinRuleEntity: CustomCasbinRule ,
215+ },
216+ );
217+
218+ const e = await newEnforcer (' examples/rbac_model.conf' , a );
219+
220+ // Load the filtered policy from DB.
221+ await e .loadFilteredPolicy ({
222+ ' ptype' : ' p' ,
223+ ' v0' : ' alice'
224+ });
225+
226+ // Check the permission.
227+ await e .enforce (' alice' , ' data1' , ' read' );
228+
229+ // Modify the policy.
230+ // await e.addPolicy(...);
231+ // await e.removePolicy(...);
232+
233+ // Save the policy back to DB.
234+ await e .savePolicy ();
235+ }
236+ ```
237+
172238## Getting Help
173239
174240- [ Node-Casbin] ( https://github.com/casbin/node-casbin )
0 commit comments