Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: upgrade to Mongoose 6.x and drop Node 10 support #52

Merged
merged 1 commit into from
Jan 29, 2022
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
4 changes: 2 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ jobs:
strategy:
fail-fast: false
matrix:
node-version: [^10, ^12, ^14]
node-version: [^12, ^14]
steps:
- name: Checkout
uses: actions/checkout@v2
Expand All @@ -33,7 +33,7 @@ jobs:
strategy:
fail-fast: false
matrix:
node-version: [ ^10, ^12, ^14 ]
node-version: [ ^12, ^14 ]
steps:
- name: Checkout
uses: actions/checkout@v2
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
},
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^4.21.0",
"casbin": "^5.11.1",
"casbin": "^5.11.5",
"chai": "^4.3.0",
"coveralls": "^3.0.9",
"cz-conventional-changelog": "^3.3.0",
Expand All @@ -79,7 +79,7 @@
"typescript": "^4.2.3"
},
"dependencies": {
"mongoose": "^5.12.3"
"mongoose": "^6.0.12"
},
"peerDependencies": {
"casbin": "^5.0.7"
Expand Down
4 changes: 2 additions & 2 deletions src/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export class MongooseAdapter implements BatchAdapter, FilteredAdapter, Updatable
private filtered: boolean;
private isSynced: boolean;
private uri: string;
private options: ConnectOptions;
private options?: ConnectOptions;
private mongooseInstance: Mongoose;
private autoAbort: boolean;
private autoCommit: boolean;
Expand All @@ -66,7 +66,7 @@ export class MongooseAdapter implements BatchAdapter, FilteredAdapter, Updatable
* const adapter = new MongooseAdapter('MONGO_URI');
* const adapter = new MongooseAdapter('MONGO_URI', { mongoose_options: 'here' })
*/
constructor(uri: string, options: ConnectOptions) {
constructor(uri: string, options?: ConnectOptions) {
if (!uri) {
throw new AdapterError('You must provide Mongo URI to connect to!');
}
Expand Down
10 changes: 4 additions & 6 deletions test/helpers/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,27 @@ const rbacPolicy = path.resolve(__dirname, '../fixtures/rbac_policy.csv');
const rbacDenyDomainModel = path.resolve(__dirname, '../fixtures/rbac_with_domains_with_deny_model.conf');
const rbacDenyDomainPolicy = path.resolve(__dirname, '../fixtures/rbac_with_domains_with_deny_policy.csv');

const MONGOOSE_OPTIONS = { useNewUrlParser: true, useCreateIndex: true, useUnifiedTopology: true };

async function createEnforcer () {
const adapter = await MongooseAdapter.newAdapter('mongodb://localhost:27001,localhost:27002/casbin?replicaSet=rs0', MONGOOSE_OPTIONS);
const adapter = await MongooseAdapter.newAdapter('mongodb://localhost:27001,localhost:27002/casbin?replicaSet=rs0');

return newEnforcer(basicModel, adapter);
}

async function createAdapter (useTransaction = false) {
return MongooseAdapter.newAdapter('mongodb://localhost:27001,localhost:27002/casbin?replicaSet=rs0', MONGOOSE_OPTIONS, {
return MongooseAdapter.newAdapter('mongodb://localhost:27001,localhost:27002/casbin?replicaSet=rs0', {
filtered: false,
synced: useTransaction
});
}

async function createSyncedAdapter () {
const adapter = MongooseAdapter.newSyncedAdapter('mongodb://localhost:27001,localhost:27002/casbin?replicaSet=rs0', MONGOOSE_OPTIONS);
const adapter = MongooseAdapter.newSyncedAdapter('mongodb://localhost:27001,localhost:27002/casbin?replicaSet=rs0');
await new Promise(resolve => setTimeout(resolve, 1000));
return adapter;
}

async function createDisconnectedAdapter () {
return new MongooseAdapter('mongodb://localhost:27001,localhost:27002/casbin?replicaSet=rs0', MONGOOSE_OPTIONS);
return new MongooseAdapter('mongodb://localhost:27001,localhost:27002/casbin?replicaSet=rs0');
}

module.exports = {
Expand Down
10 changes: 4 additions & 6 deletions test/unit/adapter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,13 @@
const { assert } = require('chai');
const { MongooseAdapter } = require('../../lib/cjs');

const MONGOOSE_OPTIONS = { useNewUrlParser: true, useCreateIndex: true, useUnifiedTopology: true };

describe('MongooseAdapter', () => {
it('Should properly throw error if Mongo URI is not provided', async () => {
assert.throws(() => new MongooseAdapter(), 'You must provide Mongo URI to connect to!');
});

it('Should properly instantiate adapter', async () => {
const adapter = new MongooseAdapter('mongodb://localhost:27001/casbin', MONGOOSE_OPTIONS);
const adapter = new MongooseAdapter('mongodb://localhost:27001/casbin');

assert.instanceOf(adapter, MongooseAdapter);
assert.isFalse(adapter.isFiltered());
Expand All @@ -32,7 +30,7 @@ describe('MongooseAdapter', () => {
});

it('Should properly create new instance via static newAdapter', async () => {
const adapter = await MongooseAdapter.newAdapter('mongodb://localhost:27001,localhost:27002/casbin?replicaSet=rs0', MONGOOSE_OPTIONS);
const adapter = await MongooseAdapter.newAdapter('mongodb://localhost:27001,localhost:27002/casbin?replicaSet=rs0');

assert.instanceOf(adapter, MongooseAdapter);
assert.isFalse(adapter.isFiltered());
Expand All @@ -41,7 +39,7 @@ describe('MongooseAdapter', () => {
});

it('Should properly create filtered instance via static newFilteredAdapter', async () => {
const adapter = await MongooseAdapter.newFilteredAdapter('mongodb://localhost:27001,localhost:27002/casbin?replicaSet=rs0', MONGOOSE_OPTIONS);
const adapter = await MongooseAdapter.newFilteredAdapter('mongodb://localhost:27001,localhost:27002/casbin?replicaSet=rs0');

assert.instanceOf(adapter, MongooseAdapter);
assert.isTrue(adapter.isFiltered());
Expand All @@ -50,7 +48,7 @@ describe('MongooseAdapter', () => {
});

it('Should have implemented interface for casbin', async () => {
const adapter = new MongooseAdapter('mongodb://localhost:27001,localhost:27002/casbin?replicaSet=rs0', MONGOOSE_OPTIONS);
const adapter = new MongooseAdapter('mongodb://localhost:27001,localhost:27002/casbin?replicaSet=rs0');

assert.isFunction(MongooseAdapter.newAdapter);
assert.isFunction(MongooseAdapter.newFilteredAdapter);
Expand Down
Loading