Facility

Facility is a predefined configuration distributed in a single facility class. Facility like a configuration combo with some predefined configuration like Validators, Middlewares etc.

Facility solve the case when an external library required to change some configuration in the KambojaApplication. It prevents user to do manual configuration.

To use Facility you need to register it globally in KambojaApplication using apply

import { KambojaApplication } from "kamboja-express"
import { MongooseFacility } from "kamboja-mongoose"

let app = new KambojaApplication(__dirname)
    .apply(new MongooseFacility())
    .init()
    
app.listen(5000)

Create Facility

To create facility simply implements Facility interface from Core namespace.

import { Core } from "kamboja"
import { ObjectIdValidator } from "./validator/objectid-validator"
import { ShortIdValidator } from "./validator/shortid-validator"

export class MongooseFacility implements Core.Facility {
    apply(app: Core.Application) {
        let validators = <any[]>app.get("validators")
        validators.push(new ObjectIdValidator())
        validators.push(new ShortIdValidator())
        app.set("validators", validators)
    }
}

Example above showing the source code of MongooseFacility which does registration to ObjectIdValidator and ShortIdValidator. By using this facility user can use @val.objectid() and @val.shortid() to validate Mongoose objectid or shortid passed in action parameter.