Skip to content

Commit 1b3748e

Browse files
authored
Merge pull request #7 from nabadeep25/dev
Documentation and refactor
2 parents 25b8781 + 75b0858 commit 1b3748e

File tree

5 files changed

+72
-55
lines changed

5 files changed

+72
-55
lines changed

.env.example

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ DB_HOST=localhost
22
DB_NAME=name
33
DB_PASSWORD=password
44
DB_PORT=3306
5-
DB_TYPE=mysql
5+
DB_TYPE=postgres
66
DB_USER=username
77
PORT=5000
88

README.md

Lines changed: 47 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ A boilerplate/starter project for quickly building RESTful APIs using Node.js,Ty
2929

3030
create boillerplate with single command
3131

32-
```
32+
```sh
3333
npx @nabadeep25/create-ts-node-app myapp
3434

3535
```
@@ -42,7 +42,7 @@ steps:
4242

4343
Clone the repo:
4444

45-
```
45+
```sh
4646
git clone --depth 1 https://github.com/nabadeep25/typescript-node-sequelize-boilerplate.git foldername
4747

4848
cd folder name
@@ -51,19 +51,19 @@ npx rimraf ./.git
5151

5252
Install the dependencies:
5353

54-
```
54+
```sh
5555
npm install
5656
```
5757

5858
Set the environment variables:
5959

60-
```
60+
```sh
6161
cp .env.example .env
6262

6363
```
6464
## Getting started
6565

66-
```
66+
```sh
6767
npm install
6868

6969
npm run build-ts
@@ -74,20 +74,20 @@ npm start
7474

7575
## For development
7676

77-
```
77+
```sh
7878
npm install
7979

80-
npm run watch
80+
npm run dev
8181

8282
```
8383

8484
## Sample .ENV
85-
```
85+
```sh
8686
DB_HOST=localhost
8787
DB_NAME=name
8888
DB_PASSWORD=password
8989
DB_PORT=3306
90-
DB_TYPE=mysql
90+
DB_TYPE=postgres
9191
DB_USER=username
9292
PORT=5000
9393

@@ -114,7 +114,7 @@ OTP_SECRET=shgdbnbgw
114114

115115
```bash
116116
# run in development
117-
npm run watch
117+
npm run dev
118118

119119
# run in production
120120
npm run start
@@ -147,7 +147,31 @@ src\
147147
|--app.ts\ # Express app
148148
|--server.ts\ # App entry point
149149
```
150-
150+
## Changing Database
151+
152+
### step 1
153+
154+
```sh
155+
# Change the value of DB_TYPE in .env file to one of the follwing
156+
DB_TYPE=postgres
157+
DB_TYPE=mysql
158+
DB_TYPE=sqlite
159+
DB_TYPE=mariadb
160+
DB_TYPE=mssql
161+
DB_TYPE=db2
162+
DB_TYPE=oracle
163+
```
164+
### step 2
165+
```sh
166+
# Install one of the related packge:
167+
npm install --save pg pg-hstore # for Postgres
168+
npm install --save mysql2 # for Mysql
169+
npm install --save mariadb # for Mariadb
170+
npm install --save sqlite3 # for Sqlite
171+
npm install --save tedious # for Microsoft SQL Server (mssql)
172+
npm install --save oracledb # for Oracle
173+
```
174+
for more details please refer [Sequelize](https://sequelize.org/docs/v6/getting-started/)
151175
## API Documentation
152176

153177
To view the list of available APIs and their specifications, run the server and go to `http://localhost:5000/api/v1/docs` in your browser. This documentation page is automatically generated using the [swagger](https://swagger.io/) definitions written as comments in the route files.
@@ -156,16 +180,20 @@ To view the list of available APIs and their specifications, run the server and
156180

157181
List of available routes:
158182

159-
**Auth routes**:
160-
`POST api/v1/auth/register` - register
161-
`POST api/v1/auth/login` - login
162-
`POST api/v1/auth/forgot-password` - send reset password email
163-
`POST api/v1/auth/reset-password` - reset password
183+
**General routes**: <br>
184+
`GET api/` - get server status <br>
185+
`PATCH api/sync` - Sync model with database <br>
186+
187+
**Auth routes**: <br>
188+
`POST api/v1/auth/register` - register <br>
189+
`POST api/v1/auth/login` - login <br>
190+
`POST api/v1/auth/forgot-password` - send reset password email <br>
191+
`POST api/v1/auth/reset-password` - reset password <br>
164192

165193

166-
**User routes**:
167-
`GET api/v1/user` - get user info
168-
`PATCH api/v1/user` - update user
194+
**User routes**: <br>
195+
`GET api/v1/user` - get user info <br>
196+
`PATCH api/v1/user` - update user <br>
169197

170198

171199

src/app.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import express from "express";
22
import logger from "morgan";
3-
import dbInit from "./db/init";
3+
import {dbSync} from "./db/connection";
44
import cors from "cors";
55
import { customRequest } from "./types/customDefinition";
66
import { deserializeUser } from "./middleware";
@@ -38,7 +38,7 @@ app.get("/api/", (req: customRequest, res) => {
3838
*/
3939
app.patch("/api/sync", async (req, res) => {
4040
try {
41-
const sync = await dbInit();
41+
const sync = await dbSync();
4242
res.status(200).json({ ...sync, error: false });
4343
} catch (err) {
4444
console.log("ERR", err);

src/db/connection.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { Sequelize } from "sequelize";
2-
32
import { dbConfig } from "../config/config";
3+
import logger from "../util/logger";
4+
5+
const isDev = process.env.NODE_ENV === "development";
46

57
const sequelizeConnection = new Sequelize(
68
dbConfig.database,
@@ -10,7 +12,26 @@ const sequelizeConnection = new Sequelize(
1012
host: dbConfig.host,
1113
port: dbConfig.port,
1214
dialect: dbConfig.dialect,
15+
logging: msg => logger.debug(msg),
1316
}
1417
);
1518

19+
const dbSync = async () => {
20+
try {
21+
await sequelizeConnection.sync({ alter: isDev });
22+
return { success: true };
23+
} catch (error) {
24+
throw error;
25+
}
26+
};
27+
dbSync()
28+
.then(res => {
29+
logger.info(`DB sync with status: ${res.success}`);
30+
})
31+
.catch(err => {
32+
logger.error("Failed to sync DB", err);
33+
});
34+
35+
export { dbSync };
36+
1637
export default sequelizeConnection;

src/db/init.ts

Lines changed: 0 additions & 32 deletions
This file was deleted.

0 commit comments

Comments
 (0)