Skip to content

Commit 29a647d

Browse files
feat: Add a argument to determine whether to automatically create a table (#73)
* feat: Add a argument to determine whether to automatically create a table Signed-off-by: terry-xuan-gao <[email protected]> * feat: Add a argument to determine whether to automatically create a table Signed-off-by: terry-xuan-gao <[email protected]> * Update README.md --------- Signed-off-by: terry-xuan-gao <[email protected]> Co-authored-by: hsluoyz <[email protected]>
1 parent a32cf76 commit 29a647d

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

README.md

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,17 @@ async function myFunction() {
5959
// Initialize a Sequelize adapter and use it in a Node-Casbin enforcer:
6060
// The adapter can not automatically create database.
6161
// But the adapter will automatically and use the table named "casbin_rule".
62+
// The second boolean argument: autoCreateTable determines whether the adapter will automatically create the "casbin_rule" table.
6263
// ORM should not create databases automatically.
63-
const a = await SequelizeAdapter.newAdapter({
64-
username: 'root',
65-
password: '',
66-
database: 'casbin',
67-
dialect: 'mysql',
68-
});
64+
const a = await SequelizeAdapter.newAdapter(
65+
{
66+
username: 'root',
67+
password: '',
68+
database: 'casbin',
69+
dialect: 'mysql',
70+
},
71+
true,
72+
);
6973

7074
const e = await casbin.newEnforcer('examples/rbac_model.conf', a);
7175

src/adapter.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,11 @@ export class SequelizeAdapter implements Adapter {
2929
private readonly option: SequelizeAdapterOptions;
3030
private sequelize: Sequelize;
3131
private filtered = false;
32+
private autoCreateTable = true;
3233

33-
constructor(option: SequelizeAdapterOptions) {
34+
constructor(option: SequelizeAdapterOptions, autoCreateTable = true) {
3435
this.option = option;
36+
this.autoCreateTable = autoCreateTable;
3537
}
3638

3739
public isFiltered(): boolean {
@@ -47,9 +49,10 @@ export class SequelizeAdapter implements Adapter {
4749
* @param option sequelize connection option
4850
*/
4951
public static async newAdapter(
50-
option: SequelizeAdapterOptions
52+
option: SequelizeAdapterOptions,
53+
autoCreateTable?: boolean
5154
): Promise<SequelizeAdapter> {
52-
const a = new SequelizeAdapter(option);
55+
const a = new SequelizeAdapter(option, autoCreateTable);
5356
await a.open();
5457

5558
return a;
@@ -60,7 +63,9 @@ export class SequelizeAdapter implements Adapter {
6063
updateCasbinRule(this.option.tableName);
6164
await this.sequelize.authenticate();
6265
this.sequelize.addModels([CasbinRule]);
63-
await this.createTable();
66+
if (this.autoCreateTable) {
67+
await this.createTable();
68+
}
6469
}
6570

6671
public async close(): Promise<void> {

0 commit comments

Comments
 (0)