Created
April 11, 2020 10:52
-
-
Save Phonbopit/6289f2895ea57148f2f2751cc9f1fd16 to your computer and use it in GitHub Desktop.
ตัวอย่าง Code ตอนที่ 10 - Mongoose
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require('express'); | |
const mongoose = require('mongoose'); | |
const options = { | |
useNewUrlParser: true, | |
useUnifiedTopology: true | |
}; | |
mongoose.connect('mongodb://localhost:27017/hello-mongoose', options); | |
const CatModel = mongoose.model( | |
'cat', | |
new mongoose.Schema( | |
{ name: String, age: Number }, | |
{ versionKey: false, timestamps: true } | |
) | |
); | |
// const kitty = new CatModel({ | |
// name: 'Kitty2222', | |
// address: 'sadfasdf', | |
// lastname: 'sadfasdf' | |
// }); | |
// kitty.save().then(() => console.log('meow')); | |
const app = express(); | |
app.use(express.json()); | |
app.get('/', (req, res) => { | |
res.json({ message: 'ok' }); | |
}); | |
// Query | |
app.get('/cats', (req, res) => { | |
CatModel.find({}) | |
.then((cats) => res.json(cats)) | |
.catch((error) => | |
res.status(400).json({ message: 'something went wrong!' }) | |
); | |
}); | |
app.get('/cats/:id', (req, res) => { | |
const { id } = req.params; | |
CatModel.findById(id) | |
.then((data) => res.json(data || {})) | |
.catch((error) => res.status(400).json(error)); | |
}); | |
// Update | |
app.put('/cats/:id', async (req, res) => { | |
const { id } = req.params; | |
const { name, age } = req.body; | |
const updated = { | |
$set: { | |
name, | |
age | |
} | |
}; | |
const cat = await CatModel.findByIdAndUpdate(id, updated, { new: true }); | |
res.json(cat); | |
}); | |
app.delete('/cats/:id', async (req, res) => { | |
const { id } = req.params; | |
await CatModel.findByIdAndDelete(id, {}); | |
res.json({ message: `${id} already removed` }); | |
}); | |
// Create | |
app.post('/cats', async (req, res) => { | |
const { name, age } = req.body; | |
const cat = new CatModel({ | |
name, | |
age | |
}); | |
await cat.save(); | |
res.json({ message: 'saved!' }); | |
}); | |
app.listen(9999, () => console.log('ok')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment