Skip to content

Commit

Permalink
Replace mongodb database to mysql
Browse files Browse the repository at this point in the history
  • Loading branch information
Yaroslav committed Feb 4, 2018
1 parent ebaddff commit 847b5ae
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 43 deletions.
11 changes: 7 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
const express = require('express');
const MongoClient = require('mongodb').MongoClient;
const mysql = require("mysql");
const bodyParser = require('body-parser');
const db = require('./config/db');
const app = express();
const port = 8000;

app.use(bodyParser.urlencoded({ extended: true }));
MongoClient.connect(db.url, (err, database) => {

const con = mysql.createConnection(db);

con.connect(err => {
if (err) {
return console.log(err);
console.log(err);
}
require('./server')(app, database);
require('./server')(app, con);
app.listen(port, () => console.log('We are live on ' + port));
});
35 changes: 25 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@
"url": "git+https://github.com/yaroslav-perec/keep-notes.git"
},
"author": "Yaroslav Perec",
"license": "MIT",
"license": "MIT License",
"bugs": {
"url": "https://github.com/yaroslav-perec/keep-notes/issues"
},
"homepage": "https://github.com/yaroslav-perec/keep-notes#readme",
"dependencies": {
"body-parser": "^1.18.2",
"express": "^4.16.2",
"mongodb": "^3.0.1"
"mysql": "^2.15.0"
},
"devDependencies": {
"nodemon": "^1.14.6"
Expand Down
47 changes: 20 additions & 27 deletions server/note_routes.js
Original file line number Diff line number Diff line change
@@ -1,67 +1,60 @@
var ObjectID = require('mongodb').ObjectID;

module.exports = function(app, database) {
const DB = database.db('test_database');

app.get('/notes', (req, res) => {
DB.collection('notes').find().toArray((err, notes) => {
const sql = "SELECT * FROM notes ORDER BY id";

database.query(sql, (err, result) => {
if (err) {
res.send({'error':'An error has occurred'});
} else {
res.send(notes);
res.send({ 'error': 'An error has occurred' });
}
res.send(result);
});
});

app.get('/notes/:id', (req, res) => {
const ID = req.params.id;
const DETAILS = { '_id': new ObjectID(ID) };
const sql = "SELECT * FROM notes WHERE id = ?";

DB.collection('notes').findOne(DETAILS, (err, item) => {
database.query(sql, [ID], (err, result) => {
if (err) {
res.send({'error':'An error has occurred'});
} else {
res.send(item);
res.send({ 'error': 'An error has occurred' });
}
res.send(result);
});
});

app.put('/notes/:id', (req, res) => {
const ID = req.params.id;
const NOTE = { $set: { text: req.body.body, title: req.body.title } };
const DETAILS = { '_id': new ObjectID(ID) };
const sql = `UPDATE notes SET note = ?, date = NOW() WHERE id = ?`;

DB.collection('notes').updateOne(DETAILS, NOTE, err => {
database.query(sql, [req.body.note, ID], (err, result) => {
if (err) {
res.send({'error':'An error has occurred'});
} else {
res.send(NOTE);
res.send({ 'error': 'An error has occurred' });
}
res.send(result);
});
});

app.delete('/notes/:id', (req, res) => {
const ID = req.params.id;
const DETAILS = { '_id': new ObjectID(ID) };
const sql = "DELETE FROM notes WHERE id = ?";

DB.collection('notes').removeOne(DETAILS, err => {
database.query(sql, [ID], (err, result) => {
if (err) {
res.send({'error': 'An error has occurred'});
} else {
res.send(`Note ${ID} deleted!`);
res.send({ 'error': 'An error has occurred' });
}
res.send(`Notes deleted: ${result.affectedRows}`);
});
});

app.post('/notes', (req, res) => {
const NOTE = { text: req.body.body, title: req.body.title };
const sql = "INSERT INTO notes (note, date) VALUES (?, NOW())";

DB.collection('notes').insertOne(NOTE, (err, result) => {
database.query(sql, [req.body.note], (err, result) => {
if (err) {
res.send({ 'error': 'An error has occurred' });
} else {
res.send(result.ops[0]);
}
res.send(`1 record inserted, ID: ${result.insertId}`);
});
});
};

0 comments on commit 847b5ae

Please sign in to comment.