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: enable mongoose timestamps for casbin rule model via adapter options #71

Merged
merged 3 commits into from
Apr 21, 2023
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
26 changes: 20 additions & 6 deletions src/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,23 @@
// limitations under the License.

import {BatchAdapter, FilteredAdapter, Helper, logPrint, Model, UpdatableAdapter} from "casbin";
import {ClientSession, Connection, ConnectOptions, createConnection, FilterQuery, Model as MongooseModel} from "mongoose";
import {
ClientSession,
Connection,
ConnectOptions,
createConnection,
FilterQuery,
Model as MongooseModel
} from "mongoose";
import {modelName, IModel, schema, collectionName} from './model'
import {AdapterError, InvalidAdapterTypeError} from "./errors";

export interface MongooseAdapterOptions {
filtered?: boolean,
synced?: boolean,
autoAbort?: boolean,
autoCommit?: boolean
autoCommit?: boolean,
timestamps?: boolean
}

export interface policyLine {
Expand Down Expand Up @@ -63,11 +71,12 @@ export class MongooseAdapter implements BatchAdapter, FilteredAdapter, Updatable
* @constructor
* @param {String} uri Mongo URI where casbin rules must be persisted
* @param {Object} [options={}] Additional options to pass on to mongoose client
* @param {Object} [adapterOptions={}] adapterOptions additional adapter options
* @example
* 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, adapterOptions?: MongooseAdapterOptions) {
if (!uri) {
throw new AdapterError('You must provide Mongo URI to connect to!');
}
Expand All @@ -79,7 +88,7 @@ export class MongooseAdapter implements BatchAdapter, FilteredAdapter, Updatable
this.uri = uri;
this.options = options;
this.connection = createConnection(this.uri, this.options);
this.casbinRule = this.connection.model(modelName, schema, collectionName);
this.casbinRule = this.connection.model(modelName, schema(adapterOptions?.timestamps), collectionName);
}

/**
Expand All @@ -96,8 +105,13 @@ export class MongooseAdapter implements BatchAdapter, FilteredAdapter, Updatable
* const adapter = await MongooseAdapter.newAdapter('MONGO_URI', { mongoose_options: 'here' });
*/
static async newAdapter(uri: string, options: ConnectOptions = {}, adapterOptions: MongooseAdapterOptions = {}) {
const adapter = new MongooseAdapter(uri, options);
const {filtered = false, synced = false, autoAbort = false, autoCommit = false} = adapterOptions;
const adapter = new MongooseAdapter(uri, options, adapterOptions);
const {
filtered = false,
synced = false,
autoAbort = false,
autoCommit = false,
} = adapterOptions;
adapter.setFiltered(filtered);
adapter.setSynced(synced);
adapter.setAutoAbort(autoAbort);
Expand Down
72 changes: 37 additions & 35 deletions src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,38 +27,40 @@ export interface IModel extends Document {
export const collectionName = 'casbin_rule';
export const modelName = 'CasbinRule';

export const schema = new Schema({
ptype: {
type: Schema.Types.String,
required: true,
index: true
},
v0: {
type: Schema.Types.String,
index: true
},
v1: {
type: Schema.Types.String,
index: true
},
v2: {
type: Schema.Types.String,
index: true
},
v3: {
type: Schema.Types.String,
index: true
},
v4: {
type: Schema.Types.String,
index: true
},
v5: {
type: Schema.Types.String,
index: true
}
}, {
collection: collectionName,
minimize: false,
timestamps: false
});
export const schema = (timestamps = false) => {
return new Schema({
ptype: {
type: Schema.Types.String,
required: true,
index: true
},
v0: {
type: Schema.Types.String,
index: true
},
v1: {
type: Schema.Types.String,
index: true
},
v2: {
type: Schema.Types.String,
index: true
},
v3: {
type: Schema.Types.String,
index: true
},
v4: {
type: Schema.Types.String,
index: true
},
v5: {
type: Schema.Types.String,
index: true
}
}, {
collection: collectionName,
minimize: false,
timestamps: timestamps
});
}