Skip to content

Commit c9c4aa2

Browse files
author
Royer Reyes León
committed
Sequelize ORM #2 CRUD/API básicos
1 parent 601f2fc commit c9c4aa2

File tree

4 files changed

+133
-16
lines changed

4 files changed

+133
-16
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

app.js

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,25 @@
11
const express = require('express')
22
const app = express();
33
const sequelize = require('./database/db');
4-
const User = require('./database/models/Users');
4+
// const User = require('./database/models/Users');
55

66
// CONFIGURACION
7-
const port = process.env.PORT || 3006
7+
const port = process.env.PORT || 3006;
8+
9+
10+
// MIDDLEWARE
11+
app.use(express.json()); // PARA PODER RECIBIR DATOS EN FORMATO JSON.
12+
app.use(express.urlencoded({extended:false})); // PARA PODER PASAR DATOS LIVIANOS.
813

914

1015

1116
// RUTAS
12-
app.get('/insertar' , (req , res)=>{
13-
User.create({
14-
name:"Royer",
15-
birthday: new Date(1997, 11, 16)
16-
}).then(user => {
17-
res.json(user);
18-
});
19-
})
20-
21-
app.get('/listar' , (req , res)=>{
22-
User.findAll().then(users => {
23-
res.json(users);
24-
});
25-
})
17+
app.get('/' , (req , res)=>{
18+
res.json("Hola mundo");
19+
});
20+
21+
app.use('/api/posts', require('./routes/posts'));
22+
2623

2724

2825

database/models/Post.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const { Model, DataTypes } = require('sequelize');
2+
const sequelize = require('../db');
3+
4+
// CREAMOS EL MODELO Y CADA UNO DE SUS ATRIBUTOS CON SUS TIPOS DE DATOS
5+
class Post extends Model {}
6+
Post.init({
7+
// ATRIBUTOS
8+
title: DataTypes.STRING,
9+
body: DataTypes.TEXT,
10+
}, {
11+
sequelize,
12+
modelName: 'post' // TABLA DE BD.
13+
});
14+
15+
module.exports = Post;

routes/posts.js

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
const { Router } = require("express");
2+
const { destroy } = require("./../database/models/Post");
3+
const router = Router();
4+
// REQUERIR EL MODELO
5+
// const Post = require('./../database/models/Post');
6+
const Post = require('./../database/models/Post');
7+
8+
9+
10+
// RETORNAR TODOS
11+
/*
12+
http://localhost:3006/api/posts
13+
*/
14+
router.get('/' , (req , res)=>{
15+
16+
Post.findAll().then(posts => {
17+
res.json(posts);
18+
})
19+
20+
});
21+
22+
23+
24+
// RETORNAR SOLO UNO POR EL ID
25+
/*
26+
http://localhost:3006/api/posts/1
27+
*/
28+
router.get('/:id' , (req , res)=>{
29+
Post.findByPk(req.params.id).then(post => {
30+
res.json(post);
31+
})
32+
});
33+
34+
35+
// CREAR
36+
/*
37+
http://localhost:3006/api/posts
38+
Content-Type application/json
39+
{
40+
"title" : "Nuevo 2",
41+
"body": "Nuevo desde post 2"
42+
}
43+
*/
44+
router.post('/' , (req , res)=>{
45+
46+
Post.create({
47+
title: req.body.title,
48+
body: req.body.body
49+
}).then(post => {
50+
res.json(post);
51+
}).catch(error => {
52+
console.log("Ocurrio error al guardar: ", error);
53+
res.json({status: error})
54+
})
55+
56+
});
57+
58+
59+
60+
// ACTUALIZAR
61+
/*
62+
http://localhost:3006/api/posts/1
63+
Content-Type application/json
64+
{
65+
"title" : "Nuevo 2",
66+
"body": "Nuevo desde post 2"
67+
}
68+
*/
69+
router.patch('/:id' , (req , res)=>{
70+
71+
Post.update({
72+
title: req.body.title,
73+
body: req.body.body
74+
},{
75+
where: {
76+
id: req.params.id
77+
}
78+
}).then(result => {
79+
res.json(result);
80+
});
81+
82+
});
83+
84+
85+
86+
// ELIMINAR
87+
/*
88+
http://localhost:3006/api/posts/1
89+
*/
90+
router.delete('/:id' , (req , res)=>{
91+
92+
Post.destroy({
93+
where:{
94+
id : req.params.id
95+
}
96+
}).then(result => {
97+
res.json(result);
98+
})
99+
100+
});
101+
102+
103+
104+
module.exports = router;

0 commit comments

Comments
 (0)