Typescript Repository Code from basic until expert
Learn init ts using typescript
Learn how to use axios with typescript
and many more to create a restfull api using typescript and nodejs with express
npm init -y (if you don't have package.json)
pnpm add typescript -D
npx tsc --init
- nodemon - pnpm add nodemon -D
"scripts": {
"tsc" : "rm -rf build/ && tsc",
"ts" : "rm -rf build/ && tsc -w",
"dev" : "nodemon ./build"
}
"allowJs": true,
"outDir": "./build",
"forceConsistentCasingInFileNames": false,
ctrl + shift + b -> tsc:build - tsconfig.json
pnpm dev
-
body-parser - pnpm add body-parser (parse incoming request bodies in a middleware before your handlers, available under the req.body property.)
-
morgan - pnpm add morgan (HTTP request logger middleware for node.js) / winston - pnpm add winston (A logger for just about everything.)
-
express - pnpm add express (nodejs framework)
-
helmet - pnpm add helmet (secure Express apps by setting HTTP response headers.)
-
cors - pnpm add cors (a Connect/Express middleware)
-
compression - pnpm add compression (compress response)
-
dotenv - pnpm add dotenv (save credentials from environment)
-
sequelize - pnpm add sequelize (Promise-based Node.js ORM for Postgres, MySQL, MariaDB, SQLite and Microsoft SQL Server.)
-
sequelize-cli - pnpm add sequelize-cli (The Sequelize Command Line Interface (CLI))
-
mysql2 - pnpm add mysql2 (MySQL client for Node.js with focus on performance. Supports prepared statements, non-utf8 encodings, binary log protocol, compression, ssl)
-
bcrypt - pnpm add bcrypt (A library to help you hash passwords.)
-
express-validator - pnpm add express-validator (An express.js middleware for validator)
-
jsonwebtoken - pnpm add jsonwebtoken (An implementation of JSON Web Tokens)
- Add Dependencies
pnpm add mysql2 sequelize sequelize-cli
- Add .sequelizerc in project
// .sequelizerc
const path = require("path")
require("dotenv").config()
if (process.env.NODE_ENV == "development") {
module.exports = {
config: path.resolve("src/config", "database.js"),
"models-path": path.resolve("src/db", "models"),
"seeders-path": path.resolve("src/db", "seeders"),
"migrations-path": path.resolve("src/db", "migrations"),
}
} else {
module.exports = {
config: path.resolve("build/config", "database.js"),
"models-path": path.resolve("build/db", "models"),
"seeders-path": path.resolve("build/db", "seeders"),
"migrations-path": path.resolve("build/db", "migrations"),
}
}
- Init sequelize-cli
npx sequelize-cli init
- Create model dan migrate
npx sequelize-cli model:generate --name user --attributes username:string,password:string --underscored
- Migrate database to MySql
npx sequelize-cli db:migrate
# untuk membalikan migrasi
npx sequelize-cli db:undo
- Add Development Dependencies
pnpm add @types/validator -D
pnpm add @types/bluebird -D