Skip to content

Commit

Permalink
feat: fix astMap undefined issue (#54)
Browse files Browse the repository at this point in the history
Co-authored-by: Dr-Greg <[email protected]>
  • Loading branch information
Dr-Greg and Dr-Greg committed Jan 5, 2024
1 parent 8967c52 commit bfeab65
Showing 1 changed file with 13 additions and 19 deletions.
32 changes: 13 additions & 19 deletions src/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,29 +75,23 @@ export class PrismaAdapter implements Adapter {
async savePolicy(model: Model): Promise<boolean> {
await this.#prisma.$executeRaw`DELETE FROM casbin_rule;`;

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

for (const [ptype, ast] of astMap) {
for (const rule of ast.policy) {
const line = this.#savePolicyLine(ptype, rule);
const p = this.#prisma.casbinRule.create({
data: line,
});
processes.push(p);
const savePolicyType = (ptype: string): void => {
const astMap = model.model.get(ptype);
if (astMap) {
for (const [ptype, ast] of astMap) {
for (const rule of ast.policy) {
const line = this.#savePolicyLine(ptype, rule);
const p = this.#prisma.casbinRule.create({ data: line });
processes.push(p);
}
}
}
}
};

astMap = model.model.get('g')!;
for (const [ptype, ast] of astMap) {
for (const rule of ast.policy) {
const line = this.#savePolicyLine(ptype, rule);
const p = this.#prisma.casbinRule.create({
data: line,
});
processes.push(p);
}
}
savePolicyType('p');
savePolicyType('g');

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

0 comments on commit bfeab65

Please sign in to comment.