Skip to content

Commit 7db67fa

Browse files
committed
feat: add configurable collection name via adapterOptions
1 parent 4782daf commit 7db67fa

File tree

4 files changed

+50
-4
lines changed

4 files changed

+50
-4
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,23 @@ const adapter = await MongooseAdapter.newAdapter('mongodb://your_mongodb_uri:270
5656

5757
Additional information regard to options you can pass in you can find in [mongoose documentation](https://mongoosejs.com/docs/connections.html#options)
5858

59+
### Custom Collection Name
60+
61+
By default, the adapter uses `casbin_rule` as the collection name. You can customize this by passing a `collectionName` option in `adapterOptions`:
62+
63+
```javascript
64+
const { MongooseAdapter } = require('casbin-mongoose-adapter');
65+
66+
// Use custom collection name
67+
const adapter = await MongooseAdapter.newAdapter(
68+
'mongodb://your_mongodb_uri:27017',
69+
{},
70+
{ collectionName: 'casbinRule' }
71+
);
72+
```
73+
74+
This allows you to follow your own database naming conventions (e.g., camelCase).
75+
5976
## Filtered Adapter
6077

6178
You can create an adapter instance that will load only those rules you need to.

src/adapter.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export interface MongooseAdapterOptions {
3030
autoAbort?: boolean;
3131
autoCommit?: boolean;
3232
timestamps?: boolean;
33+
collectionName?: string;
3334
}
3435

3536
export interface policyLine {
@@ -88,10 +89,11 @@ export class MongooseAdapter implements BatchAdapter, FilteredAdapter, Updatable
8889
this.uri = uri;
8990
this.options = options;
9091
this.connection = createConnection(this.uri, this.options);
92+
const customCollectionName = adapterOptions?.collectionName || collectionName;
9193
this.casbinRule = this.connection.model<IModel>(
9294
modelName,
93-
schema(adapterOptions?.timestamps),
94-
collectionName
95+
schema(adapterOptions?.timestamps, customCollectionName),
96+
customCollectionName
9597
);
9698
}
9799

src/model.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export interface IModel extends Document {
2727
export const collectionName = 'casbin_rule';
2828
export const modelName = 'CasbinRule';
2929

30-
export const schema = (timestamps = false) => {
30+
export const schema = (timestamps = false, customCollectionName?: string) => {
3131
return new Schema({
3232
ptype: {
3333
type: Schema.Types.String,
@@ -59,7 +59,7 @@ export const schema = (timestamps = false) => {
5959
index: true
6060
}
6161
}, {
62-
collection: collectionName,
62+
collection: customCollectionName || collectionName,
6363
minimize: false,
6464
timestamps: timestamps
6565
});

test/unit/adapter.test.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,31 @@ describe('MongooseAdapter', () => {
7474
assert.isFunction(adapter.removeFilteredPolicy);
7575
assert.isFunction(adapter.getCasbinRule);
7676
});
77+
78+
it('Should use default collection name when not provided', async () => {
79+
const adapter = new MongooseAdapter('mongodb://localhost:27001/casbin');
80+
const casbinRule = adapter.getCasbinRule();
81+
82+
assert.equal(casbinRule.schema.options.collection, 'casbin_rule');
83+
84+
await adapter.close();
85+
});
86+
87+
it('Should use custom collection name when provided', async () => {
88+
const adapter = new MongooseAdapter('mongodb://localhost:27001/casbin', {}, { collectionName: 'casbinRule' });
89+
const casbinRule = adapter.getCasbinRule();
90+
91+
assert.equal(casbinRule.schema.options.collection, 'casbinRule');
92+
93+
await adapter.close();
94+
});
95+
96+
it('Should use custom collection name via newAdapter', async () => {
97+
const adapter = await MongooseAdapter.newAdapter('mongodb://localhost:27001/casbin', {}, { collectionName: 'customCasbinRules' });
98+
const casbinRule = adapter.getCasbinRule();
99+
100+
assert.equal(casbinRule.schema.options.collection, 'customCasbinRules');
101+
102+
await adapter.close();
103+
});
77104
});

0 commit comments

Comments
 (0)