Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitf834e4c

Browse files
committed
first commit
0 parents  commitf834e4c

File tree

15 files changed

+639
-0
lines changed

15 files changed

+639
-0
lines changed

‎.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
build
2+
node_modules
3+
package-lock.json

‎README.md

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#Typescript ORM with Postgres example
2+
Sequelize ORM using Typescript with Postgres database - CRUD operations example with Express REST API.
3+
4+
For more detail, please visit:
5+
>[TypeScript ORM with Postgres example](https://www.bezkoder.com/typescript-orm-postgres/)
6+
7+
>[Express Typescript example](https://www.bezkoder.com/express-typescript-example/)
8+
9+
>[TypeScript ORM with MySQL example](https://www.bezkoder.com/typescript-orm-mysql/)
10+
11+
##Project setup
12+
```
13+
npm install
14+
```
15+
16+
###Run
17+
```
18+
npm run start
19+
```
20+
21+
Front-end that works well with this Back-end
22+
>[Axios Client](https://www.bezkoder.com/axios-request/)
23+
24+
>[Angular 8](https://www.bezkoder.com/angular-crud-app/) /[Angular 10](https://www.bezkoder.com/angular-10-crud-app/) /[Angular 11](https://www.bezkoder.com/angular-11-crud-app/) /[Angular 12](https://www.bezkoder.com/angular-12-crud-app/) /[Angular 13](https://www.bezkoder.com/angular-13-crud-example/) /[Angular 14](https://www.bezkoder.com/angular-14-crud-example/) /[Angular 15](https://www.bezkoder.com/angular-15-crud-example/) /[Angular 16 Client](https://www.bezkoder.com/angular-16-crud-example/)
25+
26+
>[Vue 2 Client](https://www.bezkoder.com/vue-typescript-crud/) /[Vue 3 Client](https://www.bezkoder.com/vue-3-typescript-axios/)
27+
28+
>[React Client](https://www.bezkoder.com/react-typescript-api-call/)
29+
30+
##More Practice
31+
>[Node.js Express Pagination with PostgreSQL example](https://bezkoder.com/node-js-pagination-postgresql/)
32+
33+
>[Node.js Express File Upload Rest API example](https://bezkoder.com/node-js-express-file-upload/)
34+
35+
>[Node.js Express File Upload with Google Cloud Storage example](https://bezkoder.com/google-cloud-storage-nodejs-upload-file/)
36+
37+
Security:
38+
>[Node.js JWT Authentication & Authorization with PostgreSQL example](https://bezkoder.com/node-js-jwt-authentication-postgresql/)
39+
40+
Associations:
41+
>[Sequelize Associations: One-to-Many Relationship example](https://bezkoder.com/sequelize-associate-one-to-many/)
42+
43+
>[Sequelize Associations: Many-to-Many Relationship example](https://bezkoder.com/sequelize-associate-many-to-many/)
44+
45+
Fullstack:
46+
>[Vue + Node.js + Express + PostgreSQL example](https://www.bezkoder.com/vue-node-express-postgresql/)
47+
48+
>[React + Node.js + Express + PostgreSQL example](https://www.bezkoder.com/react-node-express-postgresql/)
49+
50+
>[Angular 8 + Node.js + Express + PostgreSQL example](https://www.bezkoder.com/angular-node-express-postgresql/)
51+
52+
>[Angular 10 + Node.js + Express + PostgreSQL example](https://www.bezkoder.com/angular-10-node-express-postgresql/)
53+
54+
>[Angular 11 + Node.js + Express + PostgreSQL example](https://www.bezkoder.com/angular-11-node-js-express-postgresql/)
55+
56+
>[Angular 12 + Node.js + Express + PostgreSQL example](https://www.bezkoder.com/angular-12-node-js-express-postgresql/)
57+
58+
>[Angular 13 + Node.js + Express + PostgreSQL example](https://www.bezkoder.com/angular-13-node-js-express-postgresql/)
59+
60+
>[Angular 14 + Node.js + Express + PostgreSQL example](https://www.bezkoder.com/angular-14-node-js-express-postgresql/)
61+
62+
>[Angular 15 + Node.js + Express + PostgreSQL example](https://www.bezkoder.com/angular-15-node-js-express-postgresql/)
63+
64+
>[Angular 16 + Node.js + Express + PostgreSQL example](https://www.bezkoder.com/angular-16-node-js-express-postgresql/)
65+
66+
Integration (run back-end & front-end on same server/port)
67+
>[Integrate React with Node.js Restful Services](https://bezkoder.com/integrate-react-express-same-server-port/)
68+
69+
>[Integrate Angular with Node.js Restful Services](https://bezkoder.com/integrate-angular-10-node-js/)
70+
71+
>[Integrate Vue with Node.js Restful Services](https://bezkoder.com/serve-vue-app-express/)

‎package.json

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
"name":"typescript-orm-postgres",
3+
"version":"1.0.0",
4+
"description":"Rest API using Node.js, TypeScript ORM Sequelize, Express, Postgres",
5+
"main":"server.ts",
6+
"scripts": {
7+
"test":"echo\"Error: no test specified\" && exit 1",
8+
"build":"tsc",
9+
"dev":"node ./build/server.js",
10+
"start":"tsc && npm run dev"
11+
},
12+
"keywords": [
13+
"nodejs",
14+
"typescript",
15+
"orm",
16+
"sequelize",
17+
"express",
18+
"postgres",
19+
"postgresql",
20+
"restapi",
21+
"rest",
22+
"api",
23+
"crud"
24+
],
25+
"author":"bezkoder",
26+
"license":"ISC",
27+
"devDependencies": {
28+
"@types/cors":"^2.8.13",
29+
"@types/express":"^4.17.17",
30+
"@types/node":"^20.4.2",
31+
"ts-node":"^10.9.1",
32+
"typescript":"^5.1.6"
33+
},
34+
"dependencies": {
35+
"cors":"^2.8.5",
36+
"express":"^4.18.2",
37+
"pg":"^8.11.1",
38+
"sequelize":"^6.32.1",
39+
"sequelize-typescript":"^2.1.5"
40+
}
41+
}

‎server.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
importexpress,{Application}from"express";
2+
importServerfrom"./src/index";
3+
4+
constapp:Application=express();
5+
constserver:Server=newServer(app);
6+
constPORT:number=process.env.PORT ?parseInt(process.env.PORT,10) :8080;
7+
8+
app
9+
.listen(PORT,"localhost",function(){
10+
console.log(`Server is running on port${PORT}.`);
11+
})
12+
.on("error",(err:any)=>{
13+
if(err.code==="EADDRINUSE"){
14+
console.log("Error: address already in use");
15+
}else{
16+
console.log(err);
17+
}
18+
});

‎src/config/db.config.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
exportconstconfig={
2+
HOST:"localhost",
3+
USER:"postgres",
4+
PASSWORD:"123",
5+
DB:"testdb",
6+
pool:{
7+
max:5,
8+
min:0,
9+
acquire:30000,
10+
idle:10000
11+
}
12+
};
13+
14+
exportconstdialect="postgres";

‎src/controllers/home.controller.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import{Request,Response}from"express";
2+
3+
exportfunctionwelcome(req:Request,res:Response):Response{
4+
returnres.json({message:"Welcome to bezkoder application."});
5+
}
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
import{Request,Response}from"express";
2+
importTutorialfrom"../models/tutorial.model";
3+
importtutorialRepositoryfrom"../repositories/tutorial.repository";
4+
5+
exportdefaultclassTutorialController{
6+
asynccreate(req:Request,res:Response){
7+
if(!req.body.title){
8+
res.status(400).send({
9+
message:"Content can not be empty!"
10+
});
11+
return;
12+
}
13+
14+
try{
15+
consttutorial:Tutorial=req.body;
16+
if(!tutorial.published)tutorial.published=false;
17+
18+
constsavedTutorial=awaittutorialRepository.save(tutorial);
19+
20+
res.status(201).send(savedTutorial);
21+
}catch(err){
22+
res.status(500).send({
23+
message:"Some error occurred while retrieving tutorials."
24+
});
25+
}
26+
}
27+
28+
asyncfindAll(req:Request,res:Response){
29+
consttitle=typeofreq.query.title==="string" ?req.query.title :"";
30+
31+
try{
32+
consttutorials=awaittutorialRepository.retrieveAll({ title});
33+
34+
res.status(200).send(tutorials);
35+
}catch(err){
36+
res.status(500).send({
37+
message:"Some error occurred while retrieving tutorials."
38+
});
39+
}
40+
}
41+
42+
asyncfindOne(req:Request,res:Response){
43+
constid:number=parseInt(req.params.id);
44+
45+
try{
46+
consttutorial=awaittutorialRepository.retrieveById(id);
47+
48+
if(tutorial)res.status(200).send(tutorial);
49+
else
50+
res.status(404).send({
51+
message:`Cannot find Tutorial with id=${id}.`
52+
});
53+
}catch(err){
54+
res.status(500).send({
55+
message:`Error retrieving Tutorial with id=${id}.`
56+
});
57+
}
58+
}
59+
60+
asyncupdate(req:Request,res:Response){
61+
lettutorial:Tutorial=req.body;
62+
tutorial.id=parseInt(req.params.id);
63+
64+
try{
65+
constnum=awaittutorialRepository.update(tutorial);
66+
67+
if(num==1){
68+
res.send({
69+
message:"Tutorial was updated successfully."
70+
});
71+
}else{
72+
res.send({
73+
message:`Cannot update Tutorial with id=${tutorial.id}. Maybe Tutorial was not found or req.body is empty!`
74+
});
75+
}
76+
}catch(err){
77+
res.status(500).send({
78+
message:`Error updating Tutorial with id=${tutorial.id}.`
79+
});
80+
}
81+
}
82+
83+
asyncdelete(req:Request,res:Response){
84+
constid:number=parseInt(req.params.id);
85+
86+
try{
87+
constnum=awaittutorialRepository.delete(id);
88+
89+
if(num==1){
90+
res.send({
91+
message:"Tutorial was deleted successfully!"
92+
});
93+
}else{
94+
res.send({
95+
message:`Cannot delete Tutorial with id=${id}. Maybe Tutorial was not found!`,
96+
});
97+
}
98+
}catch(err){
99+
res.status(500).send({
100+
message:`Could not delete Tutorial with id==${id}.`
101+
});
102+
}
103+
}
104+
105+
asyncdeleteAll(req:Request,res:Response){
106+
try{
107+
constnum=awaittutorialRepository.deleteAll();
108+
109+
res.send({message:`${num} Tutorials were deleted successfully!`});
110+
}catch(err){
111+
res.status(500).send({
112+
message:"Some error occurred while removing all tutorials."
113+
});
114+
}
115+
}
116+
117+
asyncfindAllPublished(req:Request,res:Response){
118+
try{
119+
consttutorials=awaittutorialRepository.retrieveAll({published:true});
120+
121+
res.status(200).send(tutorials);
122+
}catch(err){
123+
res.status(500).send({
124+
message:"Some error occurred while retrieving tutorials."
125+
});
126+
}
127+
}
128+
}

‎src/db/index.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import{Sequelize}from"sequelize-typescript";
2+
import{config,dialect}from"../config/db.config";
3+
importTutorialfrom"../models/tutorial.model";
4+
5+
classDatabase{
6+
publicsequelize:Sequelize|undefined;
7+
8+
constructor(){
9+
this.connectToDatabase();
10+
}
11+
12+
privateasyncconnectToDatabase(){
13+
this.sequelize=newSequelize({
14+
database:config.DB,
15+
username:config.USER,
16+
password:config.PASSWORD,
17+
host:config.HOST,
18+
dialect:dialect,
19+
pool:{
20+
max:config.pool.max,
21+
min:config.pool.min,
22+
acquire:config.pool.acquire,
23+
idle:config.pool.idle
24+
},
25+
models:[Tutorial]
26+
});
27+
28+
awaitthis.sequelize
29+
.authenticate()
30+
.then(()=>{
31+
console.log("Connection has been established successfully.");
32+
})
33+
.catch((err)=>{
34+
console.error("Unable to connect to the Database:",err);
35+
});
36+
}
37+
}
38+
39+
exportdefaultDatabase;

‎src/index.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
importexpress,{Application}from"express";
2+
importcors,{CorsOptions}from"cors";
3+
importRoutesfrom"./routes";
4+
importDatabasefrom"./db";
5+
6+
exportdefaultclassServer{
7+
constructor(app:Application){
8+
this.config(app);
9+
this.syncDatabase();
10+
newRoutes(app);
11+
}
12+
13+
privateconfig(app:Application):void{
14+
constcorsOptions:CorsOptions={
15+
origin:"http://localhost:8081"
16+
};
17+
18+
app.use(cors(corsOptions));
19+
app.use(express.json());
20+
app.use(express.urlencoded({extended:true}));
21+
}
22+
23+
privatesyncDatabase():void{
24+
constdb=newDatabase();
25+
db.sequelize?.sync();
26+
}
27+
}

‎src/models/tutorial.model.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import{Model,Table,Column,DataType}from"sequelize-typescript";
2+
3+
@Table({
4+
tableName:"tutorials",
5+
})
6+
exportdefaultclassTutorialextendsModel{
7+
@Column({
8+
type:DataType.INTEGER,
9+
primaryKey:true,
10+
autoIncrement:true,
11+
field:"id"
12+
})
13+
id?:number;
14+
15+
@Column({
16+
type:DataType.STRING(255),
17+
field:"title"
18+
})
19+
title?:string;
20+
21+
@Column({
22+
type:DataType.STRING(255),
23+
field:"description"
24+
})
25+
description?:string;
26+
27+
@Column({
28+
type:DataType.BOOLEAN,
29+
field:"published"
30+
})
31+
published?:boolean;
32+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp