Skip to content

Commit bfeab65

Browse files
Dr-GregDr-Greg
and
Dr-Greg
authored
feat: fix astMap undefined issue (#54)
Co-authored-by: Dr-Greg <grigor.mouraydan@outlook.fr>
1 parent 8967c52 commit bfeab65

File tree

1 file changed

+13
-19
lines changed

1 file changed

+13
-19
lines changed

src/adapter.ts

+13-19
Original file line numberDiff line numberDiff line change
@@ -75,29 +75,23 @@ export class PrismaAdapter implements Adapter {
7575
async savePolicy(model: Model): Promise<boolean> {
7676
await this.#prisma.$executeRaw`DELETE FROM casbin_rule;`;
7777

78-
let astMap = model.model.get('p')!;
7978
const processes: Array<Promise<CasbinRule>> = [];
8079

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+
}
8890
}
89-
}
91+
};
9092

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');
10195

10296
// https://github.com/prisma/prisma-client-js/issues/332
10397
await Promise.all(processes);

0 commit comments

Comments
 (0)