Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,18 @@
"husky": "^1.1.2",
"jest": "^28.1.3",
"lint-staged": "^7.3.0",
"mysql2": "^2.1.0",
"better-sqlite3": "^12.0.0",
"mongodb": "^7.0.0",
"mysql2": "^3.15.3",
"pg": "^8.4.2",
"rimraf": "^2.6.2",
"sqlite3": "^5.1.7",
"ts-jest": "28.0.7",
"typescript": "^5.2.2"
},
"dependencies": {
"casbin": "^5.27.0",
"reflect-metadata": "^0.1.13",
"typeorm": "^0.3.17"
"reflect-metadata": "^0.2.2",
"typeorm": "^1.0.0"
},
"files": [
"lib",
Expand Down
29 changes: 11 additions & 18 deletions src/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ export default class TypeORMAdapter

const defaults = {
synchronize: true,
name: 'node-casbin-official',
};
if ((option as ExistentConnection).connection) {
a = new TypeORMAdapter(option, adapterConfig);
Expand Down Expand Up @@ -265,20 +264,10 @@ export default class TypeORMAdapter
oldRule: string[],
newRule: string[],
): Promise<void> {
const { v0, v1, v2, v3, v4, v5, v6 } = this.savePolicyLine(ptype, oldRule);
const newLine = this.savePolicyLine(ptype, newRule);

const foundLine = await this.getRepository().findOneOrFail({
where: {
ptype,
v0,
v1,
v2,
v3,
v4,
v5,
v6,
},
where: this.toWhere(this.savePolicyLine(ptype, oldRule)),
});

await this.getRepository().save(Object.assign(foundLine, newLine));
Expand All @@ -289,9 +278,7 @@ export default class TypeORMAdapter
*/
public async removePolicy(sec: string, ptype: string, rule: string[]) {
const line = this.savePolicyLine(ptype, rule);
await this.getRepository().delete({
...line,
});
await this.getRepository().delete(this.toWhere(line));
}

/**
Expand Down Expand Up @@ -372,9 +359,7 @@ export default class TypeORMAdapter
}
}

await this.getRepository().delete({
...line,
});
await this.getRepository().delete(this.toWhere(line));
}

private getCasbinRuleConstructor(): CasbinRuleConstructor {
Expand Down Expand Up @@ -407,4 +392,12 @@ export default class TypeORMAdapter
private getRepository(): Repository<GenericCasbinRule> {
return this.typeorm.getRepository(this.getCasbinRuleConstructor());
}

private toWhere(
line: GenericCasbinRule,
): FindOptionsWhere<GenericCasbinRule> {
return Object.fromEntries(
Object.entries(line).filter(([, v]) => v !== undefined),
) as FindOptionsWhere<GenericCasbinRule>;
}
}
3 changes: 2 additions & 1 deletion src/casbinMongoRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import { BaseEntity, Column, Entity, ObjectId, ObjectIdColumn } from 'typeorm';
import { ObjectId } from 'mongodb';
import { BaseEntity, Column, Entity, ObjectIdColumn } from 'typeorm';

@Entity()
export class CasbinMongoRule extends BaseEntity {
Expand Down
2 changes: 1 addition & 1 deletion test/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const connectionConfig: DataSourceOptions = SHOULD_USE_MYSQL
dropSchema: true,
}
: {
type: 'sqlite',
type: 'better-sqlite3',
database: ':memory:',
dropSchema: true,
};
Loading