import { Connection , connection as defaultConnection , Document , Model } from 'mongoose' ; import { RestRegistry } from './rest-registry' ; import { RestgooseModel } from './restgoose-model' ; import { Constructor } from './types' ; param connection param model export function getModel < T extends RestgooseModel > ( model , connection ? ) { if ( ! connection ) { connection = defaultConnection ; } const modelEntry = RestRegistry . getModel ( model ) ; const schemaOptions = modelEntry && modelEntry . restConfig ? modelEntry . restConfig . schemaOptions : undefined ; if ( ! connection . models [ model . name ] ) { const schema = model . prototype . buildSchema ( schemaOptions ) ; const newModel = connection . model ( model . name , schema ) ; newModel . init ( ) ; newModel . ensureIndexes ( ) ; return newModel ; } return connection . models [ model . name ] ; }