Giter VIP home page Giter VIP logo

node-crudapi-ts's Introduction

Node CrudAPI Boilerplate | TypeScript

Node boilerplate for create CRUDs and restful API's with Express Framework and Sequelize ORM written in Typescript.

1. Installing

Install dependencies

$ npm i

Installing pm2

$ npm i -g pm2

Environments

Copy .env.example to .env and set environments

2. Database

Install database driver

  • PostgresSQL
$ npm install --save pg pg-hstore
  • MySQL
$ npm install --save mysql2
  • MariaDB
$ npm install --save mariadb
  • SQLite
$ npm install --save sqlite3
  • SQL Server
$ npm install --save tedious

Database Config

Setting config database connection on config file: config/database.js *if you will not use migrations or seeds this config can be created with ".ts" extension more config details

Migrations and Seeders

  • For implements migrations or seeds install sequelize-cli module:
$ npm i -g sequelize-cli
  • Create database/seeders and/or database/migrations folder OBS: You can create seeds or migrations folders individually

  • Create a .sequeilizerc and paste follow code:

module.exports = {
  "config": "./config/database.js", //database config file reference
  "seeders-path": "./database/seeders", //remove if you don't use seeders
  "migrations-path": "./database/migrations" //remove if you don't use migrations
};

Creating Migration

$ sequelize migration:create --name MIGRATION_NAME

Running Migration

$ sequelize db:migrate

Creating Seed

$ sequelize seed:create --name SEED_NAME

Running Seed

$ sequelize db:seed:all

3. Schedules

Installing

  • For implements Schedules install node-schedule module:
$ npm i node-schedule --save
$ npm i @types/node-schedule --save-dev

Configuring

  • Create src/jobs folder and view example of job file.

  • Create src/schedule.ts file ande paste follow code or copy from example

/* src/schedule.ts */
import { resolve } from "path";
import { scheduleJob } from "node-schedule";

import * as fs from "fs";
import { Job } from "./common/interfaces/job";

const env = process.env.NODE_ENV || "development";
const prefix = env == "development" ? "" : "build/";

export default class Schedule {
  public jobs: Job[] = [];

  public constructor() {
    this.getJobs();
  }

  public getJobs = (): void => {
    const path = resolve(`${prefix}src/jobs`);
    let files = fs.readdirSync(path);
    this.jobs = files.map((file): Job => require(`./jobs/${file}`).default);
  };

  public startJobs = (): void => {
    this.jobs.map(
      (job: Job): void => {
        if (job.enabled)
          scheduleJob(job.date, async (): Promise<void> => await job.execute());
      }
    );
  };
}
  • Import src/schedule.ts on app and call startJobs() method on App constructor

4. Running

$ npm run dev

5. Building

DEV

$ npm run build:dev

PROD

$ npm run build

6. Docs

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.