@@ -75,29 +75,23 @@ export class PrismaAdapter implements Adapter {
75
75
async savePolicy ( model : Model ) : Promise < boolean > {
76
76
await this . #prisma. $executeRaw `DELETE FROM casbin_rule;` ;
77
77
78
- let astMap = model . model . get ( 'p' ) ! ;
79
78
const processes : Array < Promise < CasbinRule > > = [ ] ;
80
79
81
- for ( const [ ptype , ast ] of astMap ) {
82
- for ( const rule of ast . policy ) {
83
- const line = this . #savePolicyLine( ptype , rule ) ;
84
- const p = this . #prisma. casbinRule . create ( {
85
- data : line ,
86
- } ) ;
87
- processes . push ( p ) ;
80
+ const savePolicyType = ( ptype : string ) : void => {
81
+ const astMap = model . model . get ( ptype ) ;
82
+ if ( astMap ) {
83
+ for ( const [ ptype , ast ] of astMap ) {
84
+ for ( const rule of ast . policy ) {
85
+ const line = this . #savePolicyLine( ptype , rule ) ;
86
+ const p = this . #prisma. casbinRule . create ( { data : line } ) ;
87
+ processes . push ( p ) ;
88
+ }
89
+ }
88
90
}
89
- }
91
+ } ;
90
92
91
- astMap = model . model . get ( 'g' ) ! ;
92
- for ( const [ ptype , ast ] of astMap ) {
93
- for ( const rule of ast . policy ) {
94
- const line = this . #savePolicyLine( ptype , rule ) ;
95
- const p = this . #prisma. casbinRule . create ( {
96
- data : line ,
97
- } ) ;
98
- processes . push ( p ) ;
99
- }
100
- }
93
+ savePolicyType ( 'p' ) ;
94
+ savePolicyType ( 'g' ) ;
101
95
102
96
// https://github.com/prisma/prisma-client-js/issues/332
103
97
await Promise . all ( processes ) ;
0 commit comments