Skip to content

Commit

Permalink
feat(tmongodbplugin): allow to register type from code
Browse files Browse the repository at this point in the history
  • Loading branch information
Itee committed Aug 12, 2019
1 parent 5b24e7b commit 479898c
Showing 1 changed file with 50 additions and 3 deletions.
53 changes: 50 additions & 3 deletions sources/mongodb/TMongoDBPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,32 @@ import {
} from 'itee-validators'
import path from 'path'
import { TAbstractDatabasePlugin } from '../core/plugins/TAbstractDatabasePlugin'
import { TMongooseController } from './TMongooseController'

class TMongoDBPlugin extends TAbstractDatabasePlugin {

get schemas () {
return this._schemas
}

set schemas ( value ) {
this._schemas = value
}

get types () {
return this._types
}

set types ( value ) {
this._types = value
}

addType ( value ) {

this._types.push( value )
return this

}

static _registerTypesTo ( Mongoose, dirname ) {

const typesBasePath = path.join( dirname, 'types' )
Expand Down Expand Up @@ -104,18 +126,43 @@ class TMongoDBPlugin extends TAbstractDatabasePlugin {

}

constructor () {
super( { controllers: new Map( [ [ 'TMongooseController', TMongooseController ] ] ) } )
constructor ( parameters = {} ) {

const _parameters = {
...{
types: [],
schemas: []
},
...parameters
}

super( _parameters )

this.types = _parameters.types
this.schemas = _parameters.schemas

}

beforeRegisterRoutes ( Mongoose ) {
super.beforeRegisterRoutes( Mongoose )

this._registerTypes( Mongoose )
TMongoDBPlugin._registerTypesTo( Mongoose, this.__dirname )
TMongoDBPlugin._registerSchemasTo( Mongoose, this.__dirname )

}

_registerTypes ( Mongoose ) {

for ( let typeWrapper of this._types ) {

console.log( `Register type: ${typeWrapper.name}` )
typeWrapper( Mongoose )

}

}

}

export { TMongoDBPlugin }

0 comments on commit 479898c

Please sign in to comment.