node-crudapi-ts
node-crudapi-ts copied to clipboard
CRUD boilerplate for create Node Restful API's with Express Framework and Sequelize ORM written in Typescript.
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
- Installing pm2
- Environments
-
2. Database
- Install database driver
- Database Config
- Migrations and Seeders
- Creating Migration
- Running Migration
- Creating Seed
- Running Seed
-
3. Schedules
- Installing
- Configuring
- 4. Running
-
5. Building
- DEV
- PROD
- 6. Docs
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/ordatabase/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