diff --git a/.eslintignore b/.eslintignore index 4ebc8ae..bd045a4 100644 --- a/.eslintignore +++ b/.eslintignore @@ -1 +1,2 @@ coverage +lib/ diff --git a/API.md b/API.md deleted file mode 100644 index 1106901..0000000 --- a/API.md +++ /dev/null @@ -1,347 +0,0 @@ - - -## MongooseAdapter -Implements a policy adapter for casbin with MongoDB support. - -**Kind**: global class - -* [MongooseAdapter](#MongooseAdapter) - * [new MongooseAdapter(uri, [options])](#new_MongooseAdapter_new) - * _instance_ - * [._open()](#MongooseAdapter+_open) ⇒ Promise.<void> - * [.setFiltered([enable])](#MongooseAdapter+setFiltered) - * [.isFiltered()](#MongooseAdapter+isFiltered) ⇒ boolean - * [.setSynced([synced])](#MongooseAdapter+setSynced) - * [.setAutoAbort([abort])](#MongooseAdapter+setAutoAbort) - * [.setAutoCommit([commit])](#MongooseAdapter+setAutoCommit) - * [.getSession()](#MongooseAdapter+getSession) ⇒ Promise.<Session> - * [.setSession()](#MongooseAdapter+setSession) - * [.getTransaction()](#MongooseAdapter+getTransaction) ⇒ Promise.<Session> - * [.commitTransaction()](#MongooseAdapter+commitTransaction) ⇒ Promise.<void> - * [.abortTransaction()](#MongooseAdapter+abortTransaction) ⇒ Promise.<void> - * [.loadPolicyLine(line, model)](#MongooseAdapter+loadPolicyLine) - * [.loadPolicy(model)](#MongooseAdapter+loadPolicy) ⇒ Promise.<void> - * [.loadFilteredPolicy(model, [filter])](#MongooseAdapter+loadFilteredPolicy) - * [.savePolicyLine(ptype, rule)](#MongooseAdapter+savePolicyLine) ⇒ Object - * [.savePolicy(model)](#MongooseAdapter+savePolicy) ⇒ Promise.<Boolean> - * [.addPolicy(sec, ptype, rule)](#MongooseAdapter+addPolicy) ⇒ Promise.<void> - * [.addPolicies(sec, ptype, rule)](#MongooseAdapter+addPolicies) ⇒ Promise.<void> - * [.removePolicy(sec, ptype, rule)](#MongooseAdapter+removePolicy) ⇒ Promise.<void> - * [.removePolicies(sec, ptype, rules)](#MongooseAdapter+removePolicies) ⇒ Promise.<void> - * [.removeFilteredPolicy(sec, ptype, fieldIndex, ...fieldValues)](#MongooseAdapter+removeFilteredPolicy) ⇒ Promise.<void> - * _static_ - * [.newAdapter(uri, [options], [adapterOptions])](#MongooseAdapter.newAdapter) - * [.newFilteredAdapter(uri, [options])](#MongooseAdapter.newFilteredAdapter) - * [.newSyncedAdapter(uri, [options], autoAbort)](#MongooseAdapter.newSyncedAdapter) - - - -### new MongooseAdapter(uri, [options]) -Creates a new instance of mongoose adapter for casbin. -It does not wait for successfull connection to MongoDB. -So, if you want to have a possibility to wait until connection successful, use newAdapter instead. - - -| Param | Type | Default | Description | -| --- | --- | --- | --- | -| uri | String | | Mongo URI where casbin rules must be persisted | -| [options] | Object | {} | Additional options to pass on to mongoose client | - -**Example** -```js -const adapter = new MongooseAdapter('MONGO_URI'); -const adapter = new MongooseAdapter('MONGO_URI', { mongoose_options: 'here' }) -``` - - -### mongooseAdapter.\_open() ⇒ Promise.<void> -Opens a connection to mongoDB - -**Kind**: instance method of [MongooseAdapter](#MongooseAdapter) - - -### mongooseAdapter.setFiltered([enable]) -Switch adapter to (non)filtered state. -Casbin uses this flag to determine if it should load the whole policy from DB or not. - -**Kind**: instance method of [MongooseAdapter](#MongooseAdapter) - -| Param | Type | Default | Description | -| --- | --- | --- | --- | -| [enable] | Boolean | true | Flag that represents the current state of adapter (filtered or not) | - - - -### mongooseAdapter.isFiltered() ⇒ boolean -isFiltered determines whether the filtered model is enabled for the adapter. - -**Kind**: instance method of [MongooseAdapter](#MongooseAdapter) - - -### mongooseAdapter.setSynced([synced]) -SyncedAdapter: Switch adapter to (non)synced state. -This enables mongoDB transactions when loading and saving policies to DB. - -**Kind**: instance method of [MongooseAdapter](#MongooseAdapter) - -| Param | Type | Default | Description | -| --- | --- | --- | --- | -| [synced] | Boolean | true | Flag that represents the current state of adapter (filtered or not) | - - - -### mongooseAdapter.setAutoAbort([abort]) -SyncedAdapter: Automatically abort on Error. -When enabled, functions will automatically abort on error - -**Kind**: instance method of [MongooseAdapter](#MongooseAdapter) - -| Param | Type | Default | Description | -| --- | --- | --- | --- | -| [abort] | Boolean | true | Flag that represents if automatic abort should be enabled or not | - - - -### mongooseAdapter.setAutoCommit([commit]) -SyncedAdapter: Automatically commit after each addition. -When enabled, functions will automatically commit after function has finished - -**Kind**: instance method of [MongooseAdapter](#MongooseAdapter) - -| Param | Type | Default | Description | -| --- | --- | --- | --- | -| [commit] | Boolean | true | Flag that represents if automatic commit should be enabled or not | - - - -### mongooseAdapter.getSession() ⇒ Promise.<Session> -SyncedAdapter: Gets active session or starts a new one. Sessions are used to handle transactions. - -**Kind**: instance method of [MongooseAdapter](#MongooseAdapter) - - -### mongooseAdapter.setSession() -SyncedAdapter: Sets current session to specific one. Do not use this unless you know what you are doing. - -**Kind**: instance method of [MongooseAdapter](#MongooseAdapter) - - -### mongooseAdapter.getTransaction() ⇒ Promise.<Session> -SyncedAdapter: Gets active transaction or starts a new one. Transaction must be closed before changes are done -to the database. See: commitTransaction, abortTransaction - -**Kind**: instance method of [MongooseAdapter](#MongooseAdapter) -**Returns**: Promise.<Session> - Returns a session with active transaction - - -### mongooseAdapter.commitTransaction() ⇒ Promise.<void> -SyncedAdapter: Commits active transaction. Documents are not saved before this function is used. -Transaction closes after the use of this function. - -**Kind**: instance method of [MongooseAdapter](#MongooseAdapter) - - -### mongooseAdapter.abortTransaction() ⇒ Promise.<void> -SyncedAdapter: Aborts active transaction. All Document changes within this transaction are reverted. -Transaction closes after the use of this function. - -**Kind**: instance method of [MongooseAdapter](#MongooseAdapter) - - -### mongooseAdapter.loadPolicyLine(line, model) -Loads one policy rule into casbin model. -This method is used by casbin and should not be called by user. - -**Kind**: instance method of [MongooseAdapter](#MongooseAdapter) - -| Param | Type | Description | -| --- | --- | --- | -| line | Object | Record with one policy rule from MongoDB | -| model | Object | Casbin model to which policy rule must be loaded | - - - -### mongooseAdapter.loadPolicy(model) ⇒ Promise.<void> -Implements the process of loading policy from database into enforcer. -This method is used by casbin and should not be called by user. - -**Kind**: instance method of [MongooseAdapter](#MongooseAdapter) - -| Param | Type | Description | -| --- | --- | --- | -| model | Model | Model instance from enforcer | - - - -### mongooseAdapter.loadFilteredPolicy(model, [filter]) -Loads partial policy based on filter criteria. -This method is used by casbin and should not be called by user. - -**Kind**: instance method of [MongooseAdapter](#MongooseAdapter) - -| Param | Type | Description | -| --- | --- | --- | -| model | Model | Enforcer model | -| [filter] | Object | MongoDB filter to query | - - - -### mongooseAdapter.savePolicyLine(ptype, rule) ⇒ Object -Generates one policy rule ready to be saved into MongoDB. -This method is used by casbin to generate Mongoose Model Object for single policy -and should not be called by user. - -**Kind**: instance method of [MongooseAdapter](#MongooseAdapter) -**Returns**: Object - Returns a created CasbinRule record for MongoDB - -| Param | Type | Description | -| --- | --- | --- | -| ptype | String | Policy type to save into MongoDB | -| rule | Array.<String> | An array which consists of policy rule elements to store | - - - -### mongooseAdapter.savePolicy(model) ⇒ Promise.<Boolean> -Implements the process of saving policy from enforcer into database. -If you are using replica sets with mongo, this function will use mongo -transaction, so every line in the policy needs tosucceed for this to -take effect. -This method is used by casbin and should not be called by user. - -**Kind**: instance method of [MongooseAdapter](#MongooseAdapter) - -| Param | Type | Description | -| --- | --- | --- | -| model | Model | Model instance from enforcer | - - - -### mongooseAdapter.addPolicy(sec, ptype, rule) ⇒ Promise.<void> -Implements the process of adding policy rule. -This method is used by casbin and should not be called by user. - -**Kind**: instance method of [MongooseAdapter](#MongooseAdapter) - -| Param | Type | Description | -| --- | --- | --- | -| sec | String | Section of the policy | -| ptype | String | Type of the policy (e.g. "p" or "g") | -| rule | Array.<String> | Policy rule to add into enforcer | - - - -### mongooseAdapter.addPolicies(sec, ptype, rule) ⇒ Promise.<void> -Implements the process of adding a list of policy rules. -This method is used by casbin and should not be called by user. - -**Kind**: instance method of [MongooseAdapter](#MongooseAdapter) - -| Param | Type | Description | -| --- | --- | --- | -| sec | String | Section of the policy | -| ptype | String | Type of the policy (e.g. "p" or "g") | -| rule | Array.<String> | Policy rule to add into enforcer | - - - -### mongooseAdapter.removePolicy(sec, ptype, rule) ⇒ Promise.<void> -Implements the process of removing a list of policy rules. -This method is used by casbin and should not be called by user. - -**Kind**: instance method of [MongooseAdapter](#MongooseAdapter) - -| Param | Type | Description | -| --- | --- | --- | -| sec | String | Section of the policy | -| ptype | String | Type of the policy (e.g. "p" or "g") | -| rule | Array.<String> | Policy rule to remove from enforcer | - - - -### mongooseAdapter.removePolicies(sec, ptype, rules) ⇒ Promise.<void> -Implements the process of removing a policyList rules. -This method is used by casbin and should not be called by user. - -**Kind**: instance method of [MongooseAdapter](#MongooseAdapter) - -| Param | Type | Description | -| --- | --- | --- | -| sec | String | Section of the policy | -| ptype | String | Type of the policy (e.g. "p" or "g") | -| rules | Array.<String> | Policy rule to remove from enforcer | - - - -### mongooseAdapter.removeFilteredPolicy(sec, ptype, fieldIndex, ...fieldValues) ⇒ Promise.<void> -Implements the process of removing policy rules. -This method is used by casbin and should not be called by user. - -**Kind**: instance method of [MongooseAdapter](#MongooseAdapter) - -| Param | Type | Description | -| --- | --- | --- | -| sec | String | Section of the policy | -| ptype | String | Type of the policy (e.g. "p" or "g") | -| fieldIndex | Number | Index of the field to start filtering from | -| ...fieldValues | String | Policy rule to match when removing (starting from fieldIndex) | - - - -### MongooseAdapter.newAdapter(uri, [options], [adapterOptions]) -Creates a new instance of mongoose adapter for casbin. -Instead of constructor, it does wait for successfull connection to MongoDB. -Preferable way to construct an adapter instance, is to use this static method. - -**Kind**: static method of [MongooseAdapter](#MongooseAdapter) - -| Param | Type | Default | Description | -| --- | --- | --- | --- | -| uri | String | | Mongo URI where casbin rules must be persisted | -| [options] | Object | {} | Additional options to pass on to mongoose client | -| [adapterOptions] | Object | {} | Additional options to pass on to adapter | - -**Example** -```js -const adapter = await MongooseAdapter.newAdapter('MONGO_URI'); -const adapter = await MongooseAdapter.newAdapter('MONGO_URI', { mongoose_options: 'here' }); -``` - - -### MongooseAdapter.newFilteredAdapter(uri, [options]) -Creates a new instance of mongoose adapter for casbin. -It does the same as newAdapter, but it also sets a flag that this adapter is in filtered state. -That way, casbin will not call loadPolicy() automatically. - -**Kind**: static method of [MongooseAdapter](#MongooseAdapter) - -| Param | Type | Default | Description | -| --- | --- | --- | --- | -| uri | String | | Mongo URI where casbin rules must be persisted | -| [options] | Object | {} | Additional options to pass on to mongoose client | - -**Example** -```js -const adapter = await MongooseAdapter.newFilteredAdapter('MONGO_URI'); -const adapter = await MongooseAdapter.newFilteredAdapter('MONGO_URI', { mongoose_options: 'here' }); -``` - - -### MongooseAdapter.newSyncedAdapter(uri, [options], autoAbort) -Creates a new instance of mongoose adapter for casbin. -It does the same as newAdapter, but it checks wether database is a replica set. If it is, it enables -transactions for the adapter. -Transactions are never commited automatically. You have to use commitTransaction to add pending changes. - -**Kind**: static method of [MongooseAdapter](#MongooseAdapter) - -| Param | Type | Default | Description | -| --- | --- | --- | --- | -| uri | String | | Mongo URI where casbin rules must be persisted | -| [options] | Object | {} | Additional options to pass on to mongoose client | -| autoAbort | Boolean | true | Whether to abort transactions on Error automatically | - -**Example** -```js -const adapter = await MongooseAdapter.newFilteredAdapter('MONGO_URI'); -const adapter = await MongooseAdapter.newFilteredAdapter('MONGO_URI', { mongoose_options: 'here' }); -``` diff --git a/README.md b/README.md index 5375385..1f2149c 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,8 @@ Mongoose Adapter is the [Mongoose](https://github.com/Automattic/mongoose/) adap Based on [Officially Supported Databases](https://mongoosejs.com/docs/), The current supported database is MongoDB. +Mongoose Adapter has been rewritten to TypeScript since v3.x. + ## Getting Started Install the package as dependency in your project: diff --git a/package.json b/package.json index f6b91ed..ffe178f 100644 --- a/package.json +++ b/package.json @@ -46,9 +46,8 @@ "registry": "https://registry.npmjs.org" }, "scripts": { - "docs": "jsdoc2md \"src/**/*.ts\" > ./API.md", "lint": "eslint .", - "prepublishOnly": "npm run lint && npm run test && npm run docs", + "prepublishOnly": "npm run lint", "test:integration": "nyc mocha -- --config test/integration/.mocharc.js", "test:unit": "nyc mocha -- --config test/unit/.mocharc.js", "tests": "run-s test:*",