A base for the development of webservice with Typescript, Restify and Mongoose 📦
Started this project in order to help people create webservices with typescript easily. Let's give it a try!
In order to install this base you must have this requirements on your machine:
npm ^= 6.13.4
node ^= 12.14.0
tsc-cli
To install this typescript base you must run the following command under your bash:
npm install
- Clone this repo to your local machine using
https://github.com/adevr/typescript-webservice.git
- Create New Model
import * as mongoose from "mongoose";
import { validateZipCode } from "../validators";
export interface Lead extends mongoose.Document{
name: string,
nif: bigint,
phone: bigint,
// ...
}
const leadSchema = new mongoose.Schema({
name: {
type: String,
required: true,
minlength: 2
},
// ...
});
export const Lead = mongoose.model<Lead>("Lead", leadSchema);
- Create new routes for the model
import * as restify from "restify"
import { ModelRoutes } from "../middlewares/model.routes"
import { Lead } from "../http/models/lead.model"
class LeadsRoutes extends ModelRoutes<Lead>
{
constructor(){
super( Lead )
}
applyRoutes( application: restify.Server ){
application.get("/leads", this.findAll );
}
}
export const leadsRouter = new LeadsRoutes();
- Register the route on server
import { clientsRouter } from "./routes/client.routes"
import { usersRouter } from "./routes/user.routes"
import { leadsRouter } from "./routes/lead.routes"
const routes = [
clientsRouter,
usersRouter,
leadsRouter
];
In order to run the base you must do the following commands:
tsc -w
nodemon dist/index.js
At the moment, this base supports actions for the following:
- findAll (GET)
- findById (GET)
- store (POST)
- findIdAndUpdate (PUT & PATCH)
- findByIdAndDelete (DELETE)
- Add Sequelize support
- Divide ModelRoutes file under a controller structure
To get started you must follow the following steps
-
Option 1
- 🍴 Fork this repo!
-
Option 2
- 👯 Clone this repo to your local machine using
https://github.com/adevr/typescript-webservice.git
- 👯 Clone this repo to your local machine using
- HACK AWAY! 🔨🔨🔨
- 🔃 Create a new pull request using
https://github.com/adevr/typescript-webservice/compare/
.
If you find an error, don't hesitate and create an issue! Additionally you can contribute to this project by fixing that or other issues that will be placed on the issues tab.
- MIT license
- Copyright 2020 © Alexandre Reis.