Skip to content
Merged
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
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,23 @@ const adapter = await MongooseAdapter.newAdapter('mongodb://your_mongodb_uri:270

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

### Custom Collection Name

By default, the adapter uses `casbin_rule` as the collection name. You can customize this by passing a `collectionName` option in `adapterOptions`:

```javascript
const { MongooseAdapter } = require('casbin-mongoose-adapter');

// Use custom collection name
const adapter = await MongooseAdapter.newAdapter(
'mongodb://your_mongodb_uri:27017',
{},
{ collectionName: 'casbinRule' }
);
```

This allows you to follow your own database naming conventions (e.g., camelCase).

## Filtered Adapter

You can create an adapter instance that will load only those rules you need to.
Expand Down
6 changes: 4 additions & 2 deletions src/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export interface MongooseAdapterOptions {
autoAbort?: boolean;
autoCommit?: boolean;
timestamps?: boolean;
collectionName?: string;
}

export interface policyLine {
Expand Down Expand Up @@ -88,10 +89,11 @@ export class MongooseAdapter implements BatchAdapter, FilteredAdapter, Updatable
this.uri = uri;
this.options = options;
this.connection = createConnection(this.uri, this.options);
const customCollectionName = adapterOptions?.collectionName || collectionName;
this.casbinRule = this.connection.model<IModel>(
modelName,
schema(adapterOptions?.timestamps),
collectionName
schema(adapterOptions?.timestamps, customCollectionName),
customCollectionName
);
}

Expand Down
4 changes: 2 additions & 2 deletions src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export interface IModel extends Document {
export const collectionName = 'casbin_rule';
export const modelName = 'CasbinRule';

export const schema = (timestamps = false) => {
export const schema = (timestamps = false, customCollectionName?: string) => {
return new Schema({
ptype: {
type: Schema.Types.String,
Expand Down Expand Up @@ -59,7 +59,7 @@ export const schema = (timestamps = false) => {
index: true
}
}, {
collection: collectionName,
collection: customCollectionName || collectionName,
minimize: false,
timestamps: timestamps
});
Expand Down
27 changes: 27 additions & 0 deletions test/unit/adapter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,31 @@ describe('MongooseAdapter', () => {
assert.isFunction(adapter.removeFilteredPolicy);
assert.isFunction(adapter.getCasbinRule);
});

it('Should use default collection name when not provided', async () => {
const adapter = new MongooseAdapter('mongodb://localhost:27001/casbin');
const casbinRule = adapter.getCasbinRule();

assert.equal(casbinRule.schema.options.collection, 'casbin_rule');

await adapter.close();
});

it('Should use custom collection name when provided', async () => {
const adapter = new MongooseAdapter('mongodb://localhost:27001/casbin', {}, { collectionName: 'casbinRule' });
const casbinRule = adapter.getCasbinRule();

assert.equal(casbinRule.schema.options.collection, 'casbinRule');

await adapter.close();
});

it('Should use custom collection name via newAdapter', async () => {
const adapter = await MongooseAdapter.newAdapter('mongodb://localhost:27001/casbin', {}, { collectionName: 'customCasbinRules' });
const casbinRule = adapter.getCasbinRule();

assert.equal(casbinRule.schema.options.collection, 'customCasbinRules');

await adapter.close();
});
});