/typescript-webservice

A base for the development of webservice with Typescript, Restify and Mongoose 📦

Primary LanguageTypeScriptMIT LicenseMIT

Typescript Webservice

A base for the development of webservice with Typescript, Restify and Mongoose 📦

Example Request

License License


Motivation

Started this project in order to help people create webservices with typescript easily. Let's give it a try!


Installation

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

  • Clone this repo to your local machine using https://github.com/adevr/typescript-webservice.git

Examples

  • 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
];

Run the project

In order to run the base you must do the following commands:

  • tsc -w
  • nodemon dist/index.js

Current Features

At the moment, this base supports actions for the following:
  • findAll (GET)
  • findById (GET)
  • store (POST)
  • findIdAndUpdate (PUT & PATCH)
  • findByIdAndDelete (DELETE)

Todo List

  • Add Sequelize support
  • Divide ModelRoutes file under a controller structure

Contributing

To get started you must follow the following steps

Step 1

  • Option 1

    • 🍴 Fork this repo!
  • Option 2

    • 👯 Clone this repo to your local machine using https://github.com/adevr/typescript-webservice.git

Step 2

  • HACK AWAY! 🔨🔨🔨

Step 3


Support

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.


License

License