import { LeanDocumentOrArray , model , Schema } from "mongoose" ; import { ITrafficDocument } from "../../typings" ; import { ITrafficModel } from "../../typings/db/ITrafficDocument" ; export const schema = new Schema ( { ip : { type : String , index : true } , lastRequest : { type : Date , expires : 3600 } } , { collection : "traffic" } ) ; schema . statics . findForIp = function ( this , ip ) : Promise < LeanDocumentOrArray < ITrafficDocument | null > > { return this . findOne ( { ip : ip } ) . lean ( ) . exec ( ) ; } ; schema . statics . updateRequestTime = function ( this , ip , time = new Date ( ) ) : Promise < any > { return this . updateOne ( { ip : ip } , { lastRequest : time } , { upsert : true } ) . exec ( ) ; } ; export const Traffic = model < ITrafficDocument , ITrafficModel > ( "Traffic" , schema ) ;