1414
1515import { Adapter , Helper , Model } from 'casbin' ;
1616import { CasbinRule } from './casbinRule' ;
17- import { Connection , ConnectionOptions , createConnection } from 'typeorm' ;
17+ import { Connection , ConnectionOptions , createConnection , getRepository } from 'typeorm' ;
1818
1919/**
2020 * TypeORMAdapter represents the TypeORM adapter for policy storage.
2121 */
2222export default class TypeORMAdapter implements Adapter {
2323 private option : ConnectionOptions ;
2424 private typeorm : Connection ;
25-
2625 private constructor ( option : ConnectionOptions ) {
2726 this . option = option ;
2827 }
@@ -32,12 +31,16 @@ export default class TypeORMAdapter implements Adapter {
3231 * @param option typeorm connection option
3332 */
3433 public static async newAdapter ( option : ConnectionOptions ) {
35- const a = new TypeORMAdapter (
36- Object . assign ( option ,
37- {
38- entities : [ CasbinRule ] ,
39- synchronize : true
40- } ) ) ;
34+ const officialOption = {
35+ entities : [ CasbinRule ] ,
36+ synchronize : true ,
37+ name : 'node-casbin-official'
38+ } ;
39+ if ( option . name ) {
40+ officialOption . name = option . name ;
41+ }
42+
43+ const a = new TypeORMAdapter ( Object . assign ( option , officialOption ) ) ;
4144 await a . open ( ) ;
4245 return a ;
4346 }
@@ -56,7 +59,7 @@ export default class TypeORMAdapter implements Adapter {
5659 }
5760
5861 private async clearTable ( ) {
59- await this . typeorm . getRepository ( CasbinRule ) . clear ( ) ;
62+ await getRepository ( CasbinRule , this . option . name ) . clear ( ) ;
6063 }
6164
6265 private loadPolicyLine ( line : CasbinRule , model : Model ) {
@@ -68,7 +71,8 @@ export default class TypeORMAdapter implements Adapter {
6871 * loadPolicy loads all policy rules from the storage.
6972 */
7073 public async loadPolicy ( model : Model ) {
71- const lines = await CasbinRule . find ( ) ;
74+ const lines = await getRepository ( CasbinRule , this . option . name ) . find ( ) ;
75+
7276 for ( const line of lines ) {
7377 this . loadPolicyLine ( line , model ) ;
7478 }
@@ -124,7 +128,7 @@ export default class TypeORMAdapter implements Adapter {
124128 lines . push ( line ) ;
125129 }
126130 }
127- await this . typeorm . getRepository ( CasbinRule ) . save ( lines ) ;
131+ await getRepository ( CasbinRule , this . option . name ) . save ( lines ) ;
128132
129133 return true ;
130134 }
@@ -134,15 +138,15 @@ export default class TypeORMAdapter implements Adapter {
134138 */
135139 public async addPolicy ( sec : string , ptype : string , rule : string [ ] ) {
136140 const line = this . savePolicyLine ( ptype , rule ) ;
137- await line . save ( ) ;
141+ await getRepository ( CasbinRule , this . option . name ) . save ( line ) ;
138142 }
139143
140144 /**
141145 * removePolicy removes a policy rule from the storage.
142146 */
143147 public async removePolicy ( sec : string , ptype : string , rule : string [ ] ) {
144148 const line = this . savePolicyLine ( ptype , rule ) ;
145- await CasbinRule . delete ( line ) ;
149+ await getRepository ( CasbinRule , this . option . name ) . delete ( line ) ;
146150 }
147151
148152 /**
@@ -171,7 +175,6 @@ export default class TypeORMAdapter implements Adapter {
171175 if ( fieldIndex <= 5 && 5 < fieldIndex + fieldValues . length ) {
172176 line . v5 = fieldValues [ 5 - fieldIndex ] ;
173177 }
174-
175- await CasbinRule . delete ( line ) ;
178+ await getRepository ( CasbinRule , this . option . name ) . delete ( line ) ;
176179 }
177180}
0 commit comments