Tangos
https://appening.github.io/Tangos/
Broilerplate code Node MongoDB server written in TypeScript
First things first
npm i tangos --save
Get all the CRUD operations along with end points just be extending our controller.
-
/find
- request:
{query(optional):YourQuery}
- sample:
{query:{name:"tej"}}
- request:
-
/findOne
- request:
{query(optional):YourQuery}
- sample:
{query:{_id:"yourmongodbobjecit"}}
- request:
-
/create
- request:
{data(must):YourData}
- sample:
{data:{name:"tej",email:"SaiTejD@gmail.com"}}
- request:
-
/update
-
request:
{query(optional):YourQuery, data(must):YourData}
Note: if you dont provide query, all the records within the collection will be updated
-
sample:
{query:{name:"tej"}, data:{name:"tej2"}}
-
-
/deleteData
-
request:
{query(optional):YourQuery}
Note: if you dont provide query, all the records within the collection will be deleted
-
Architecture
MVP design pattern is used here.
Strictly typed.
Controller -> Presenter (Model).
Ever Controller/Router is linked with Presenter. Every Presenter is linked with Model.
Model has all the data about the collection . i.e Name of the collection, attributes...etc
Presenter does all the business logic i.e, Writing to database and returning the data as promise for every endpoint of that respective controller.
Running the Sample Application
- Define your mongo creds in .env file
-
npm run dev
(For running sample application)
Usage
1.Create your model by extending BaseModel
class Book extends BaseModel {
collectionName: string = "books";
schema: any = Book.getMongoSchema({name: String});
}
export default Book;
2.Create your presenter by extending the BasePresenter
class BookPresenter extends BasePresenter {
constructor(baseModel: BaseModel) {
super(baseModel);
}
public test():Promise<BaseResponse> {
return new Promise<BaseResponse>((resolve => {
resolve(BaseResponse.getSuccessResponse(null));
}));
}
}
export default BookPresenter;
3.Create your controller by extending the BaseController
class BookController extends BaseController<BookPresenter> {
constructor(endPoint: string) {
super(endPoint);
}
attachPresenter(): BookPresenter {
return new BookPresenter(new Book());
}
attachCustomRoutes(): CustomRoute[] {
let routes = [new CustomRoute("test", this.test.bind(this))];
return routes;
}
test(web_req: express.Request, web_res: express.Response, next: express.NextFunction) {
this.getPresenter().test();
web_res.json({reached: 1});
}
}
export default BookController;
4.Now add your controllers to server file
let dbConfig = new DbConfig(MONGO_USER, MONGO_PASSWORD, MONGO_PATH, {});
let config = new Config(false, []);
let authConfig = new AuthConfig("somerandom");
const app = new BaseApp(
config,
authConfig,
dbConfig,
[
new BookController("/books"),
]
);
app.listen();
5.HOLAAAAAA ! Here we are in the END GAME
Middlewares 'N Layers
Middlewares mentioned below are default loaded in top to bottom order
-
Logger Middleware
-
Servertimeout Middleware (default is 5s)
-
MongoDB Connection check Middleware
-
Auth Middleware (JWT tokens are used and Configurable)
-
Input Validation middlewares (Checks for proper JSON....etc)
-
Base Controllers for common functionalities (User management, SMS/Email through AWS, Invoice Controller...etc Many more to come)
-
$Your Controllers$, You can add any number of Validation middlewares before your controllers.
-
Page not found (Route not found) handler Middleware.
-
Exception handler Middleware (All the exceptions thrown in previous layers are caught here and handled).
-
Analytics Middleware (Coming soon.)
_Dependency Graphs_
Base Controller and Base Presenter
If you like my project, Star this and if you want to contribute mail me at saitejdandge1@gmail.com .
Thanks for stopping by ! :D !!
Regards
SaiTej Dandge