Skip to content

Commit

Permalink
mongodb
Browse files Browse the repository at this point in the history
  • Loading branch information
sveltemaster committed Dec 27, 2020
1 parent aa9c99c commit 75e37cb
Show file tree
Hide file tree
Showing 4 changed files with 332 additions and 22 deletions.
45 changes: 30 additions & 15 deletions api/crud.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,33 @@
let people = [{name: 'Patrick'}, {name: 'Sandy'}, {name: 'Bob'}]

module.exports = function (req, res) {
if (req.method === 'GET') {
res.status(200).json(people);
} else if (req.method === 'POST') {
people.push(req.body)
res.status(200).json(people)
} else if (req.method === 'PUT') {
const {person, index} = req.body;
people[index] = person;
res.status(200).json(people)
} else if (req.method === 'DELETE') {
const {index} = req.body;
people = people.filter((_, i) => i !== index);
res.status(200).json(people)
}
};
if (req.method === 'GET') {
res.status(200).json(people);
} else if (req.method === 'POST') {
people.push(req.body)
res.status(200).json(people)
} else if (req.method === 'PUT') {
const {person, index} = req.body;
people[index] = person;
res.status(200).json(people)
} else if (req.method === 'DELETE') {
const {index} = req.body;
people = people.filter(function(_, i) { return i !== index });
res.status(200).json(people)
}
};

/* To integrate with MongoDB..
1. On MongoDB site, Create..
- Account
- Cluster
- Database Access User
- Network Access
2. Copy URI from MongoDB site and then..
- Add as Env variable
3. Setup API!
- npm install mongoose
- create schema
- connect to database with URI
- write CRUD functions
*/
50 changes: 50 additions & 0 deletions api/newcrud.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const mongoose = require('mongoose');

const uri = process.env.DB_URI

const personSchema = new mongoose.Schema({
name: String,
age: Number
})

const Person = mongoose.model(
'Person',
personSchema,
'People'
)

mongoose.connect(
uri,
{useNewUrlParser: true, useUnifiedTopology: true, useFindAndModify: false},
function(error) { if (error) console.log("Error!" + error); }
);

module.exports = function (req, res) {
if (req.method === 'GET') {
Person.find()
.then(people => {
res.status(200).json(people);
})
.catch((error) => res.status(500).json(error.message))
} else if (req.method === 'POST') {
const newPerson = new Person(req.body);
newPerson.save()
.then(newPerson => {
res.status(200).json(newPerson)
})
.catch((error) => res.status(500).json(error.message))
} else if (req.method === 'PUT') {
const {person, _id} = req.body;
Person.findByIdAndUpdate(_id, person, {returnOriginal: false})
.then(updatedPerson => {
res.status(200).json(updatedPerson)
})
.catch((error) => res.status(500).json(error.message))
} else if (req.method === 'DELETE') {
const {_id} = req.body;
Person.findByIdAndDelete(_id)
.then(deletedPerson => {
res.status(200).json(deletedPerson)
}).catch((error) => res.status(500).json(error.message))
}
};
Loading

0 comments on commit 75e37cb

Please sign in to comment.