1313// limitations under the License.
1414
1515import { 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" ;
1724import { modelName , IModel , schema , collectionName } from './model'
1825import { AdapterError , InvalidAdapterTypeError } from "./errors" ;
1926
2027export interface MongooseAdapterOptions {
2128 filtered ?: boolean ,
2229 synced ?: boolean ,
2330 autoAbort ?: boolean ,
24- autoCommit ?: boolean
31+ autoCommit ?: boolean ,
32+ timestamps ?: boolean
2533}
2634
2735export 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 ) ;
0 commit comments