Skip to content

Commit 3dd8862

Browse files
shubhanshusinghSingh Kushwah, Shubhanshu
andauthored
feat: enable mongoose timestamps for casbin rule model via adapter options (#71)
* chore: enable mongoose timestamps for casbin rule model via adapter options * chore: enable mongoose timestamps for casbin rule model via adapter options * chore: enable mongoose timestamps for casbin rule model via adapter options --------- Co-authored-by: Singh Kushwah, Shubhanshu <[email protected]>
1 parent 96075e2 commit 3dd8862

File tree

2 files changed

+57
-41
lines changed

2 files changed

+57
-41
lines changed

src/adapter.ts

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,23 @@
1313
// limitations under the License.
1414

1515
import {BatchAdapter, FilteredAdapter, Helper, logPrint, Model, UpdatableAdapter} from "casbin";
16-
import {ClientSession, Connection, ConnectOptions, createConnection, FilterQuery, Model as MongooseModel} from "mongoose";
16+
import {
17+
ClientSession,
18+
Connection,
19+
ConnectOptions,
20+
createConnection,
21+
FilterQuery,
22+
Model as MongooseModel
23+
} from "mongoose";
1724
import {modelName, IModel, schema, collectionName} from './model'
1825
import {AdapterError, InvalidAdapterTypeError} from "./errors";
1926

2027
export interface MongooseAdapterOptions {
2128
filtered?: boolean,
2229
synced?: boolean,
2330
autoAbort?: boolean,
24-
autoCommit?: boolean
31+
autoCommit?: boolean,
32+
timestamps?: boolean
2533
}
2634

2735
export interface policyLine {
@@ -63,11 +71,12 @@ export class MongooseAdapter implements BatchAdapter, FilteredAdapter, Updatable
6371
* @constructor
6472
* @param {String} uri Mongo URI where casbin rules must be persisted
6573
* @param {Object} [options={}] Additional options to pass on to mongoose client
74+
* @param {Object} [adapterOptions={}] adapterOptions additional adapter options
6675
* @example
6776
* const adapter = new MongooseAdapter('MONGO_URI');
6877
* const adapter = new MongooseAdapter('MONGO_URI', { mongoose_options: 'here' })
6978
*/
70-
constructor(uri: string, options?: ConnectOptions) {
79+
constructor(uri: string, options?: ConnectOptions, adapterOptions?: MongooseAdapterOptions) {
7180
if (!uri) {
7281
throw new AdapterError('You must provide Mongo URI to connect to!');
7382
}
@@ -79,7 +88,7 @@ export class MongooseAdapter implements BatchAdapter, FilteredAdapter, Updatable
7988
this.uri = uri;
8089
this.options = options;
8190
this.connection = createConnection(this.uri, this.options);
82-
this.casbinRule = this.connection.model(modelName, schema, collectionName);
91+
this.casbinRule = this.connection.model(modelName, schema(adapterOptions?.timestamps), collectionName);
8392
}
8493

8594
/**
@@ -96,8 +105,13 @@ export class MongooseAdapter implements BatchAdapter, FilteredAdapter, Updatable
96105
* const adapter = await MongooseAdapter.newAdapter('MONGO_URI', { mongoose_options: 'here' });
97106
*/
98107
static async newAdapter(uri: string, options: ConnectOptions = {}, adapterOptions: MongooseAdapterOptions = {}) {
99-
const adapter = new MongooseAdapter(uri, options);
100-
const {filtered = false, synced = false, autoAbort = false, autoCommit = false} = adapterOptions;
108+
const adapter = new MongooseAdapter(uri, options, adapterOptions);
109+
const {
110+
filtered = false,
111+
synced = false,
112+
autoAbort = false,
113+
autoCommit = false,
114+
} = adapterOptions;
101115
adapter.setFiltered(filtered);
102116
adapter.setSynced(synced);
103117
adapter.setAutoAbort(autoAbort);

src/model.ts

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

30-
export const schema = new Schema({
31-
ptype: {
32-
type: Schema.Types.String,
33-
required: true,
34-
index: true
35-
},
36-
v0: {
37-
type: Schema.Types.String,
38-
index: true
39-
},
40-
v1: {
41-
type: Schema.Types.String,
42-
index: true
43-
},
44-
v2: {
45-
type: Schema.Types.String,
46-
index: true
47-
},
48-
v3: {
49-
type: Schema.Types.String,
50-
index: true
51-
},
52-
v4: {
53-
type: Schema.Types.String,
54-
index: true
55-
},
56-
v5: {
57-
type: Schema.Types.String,
58-
index: true
59-
}
60-
}, {
61-
collection: collectionName,
62-
minimize: false,
63-
timestamps: false
64-
});
30+
export const schema = (timestamps = false) => {
31+
return new Schema({
32+
ptype: {
33+
type: Schema.Types.String,
34+
required: true,
35+
index: true
36+
},
37+
v0: {
38+
type: Schema.Types.String,
39+
index: true
40+
},
41+
v1: {
42+
type: Schema.Types.String,
43+
index: true
44+
},
45+
v2: {
46+
type: Schema.Types.String,
47+
index: true
48+
},
49+
v3: {
50+
type: Schema.Types.String,
51+
index: true
52+
},
53+
v4: {
54+
type: Schema.Types.String,
55+
index: true
56+
},
57+
v5: {
58+
type: Schema.Types.String,
59+
index: true
60+
}
61+
}, {
62+
collection: collectionName,
63+
minimize: false,
64+
timestamps: timestamps
65+
});
66+
}

0 commit comments

Comments
 (0)