tiny-controllers
v1.0.11
Published
Controller with base methods for your REST server. Tiny-controllers now only supports for Mongoose. # Installation `$ npm install --save tiny-controllers` # Usage - Create your *.d.ts in root folder and add reference to `tiny-controllers` type. ```javascr
Downloads
3
Readme
tiny-controllers
Controller with base methods for your REST server. Tiny-controllers now only supports for Mongoose.
Installation
$ npm install --save tiny-controllers
Usage
- Create your *.d.ts in root folder and add reference to
tiny-controllers
type.
// app.d.ts
/// <reference types="tiny-controllers" />
- Create your own Controller then extends TinyController.
// IUser is model interface and
// IUserDoc is model interface which extends Document (Mongoose)
class UsersController extends TinyController<IUser, IUserDoc> {
constructor() {
super(User);
}
newMethod(req: Request, res: Response) {
...
.then(data => this.sendSuccess(res, data))
.catch(error => this.sendFailure(res, error));
}
}
Properties
Model
Use for child class to override or create new methods.
protected _model: Model<DocType>;
// Example usage
import { UserModel } from './UserModel';
class UserController extends TinyController<IUser, IUserDoc> {
constructor() {
super(User);
}
// override
doGet(req: Request, res: Response) {
// some code here...
this._model.findById(...)
.then(...)
.catch(...);
// some code here...
}
newMethod(req: Request, res: Response) {
// some code here...
this._model.find({...})
.then(...)
.catch(...);
// some code here...
}
...
}
...
Methods
constructor
constructor(model: Model<DocType>);
doFind
doFind(req: Request, res: Response): void;
doGet
doGet(req: Request, res: Response): void;
doPost
doPost(req: Request, res: Response, next: NextFunction): void;
doPut
doPut(req: Request, res: Response): void;
doDelete
doDelete(req: Request, res: Response): void;
sendSuccess
protected sendSuccess(res: Response, data: any): void;
sendFailure
protected sendFailure(next: NextFunction, error: any): void;