-
Notifications
You must be signed in to change notification settings - Fork 60
Feature/update to current #11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
7e8a792
eeb73c4
94fd40b
7864c16
be538fb
e323663
1edc020
f1af90f
75f711a
709021d
88791e9
a71fa2f
11966d8
40d459c
c22230e
2bcc8e7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
PORT=3000 | ||
DATABASE_URL=postgres://postgres:ppp@localhost:5432/todos | ||
PGSSLMODE=disable |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,4 @@ | |
blog.md | ||
node_modules/ | ||
npm-debug.log | ||
package-lock.json |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
web: npm start | ||
release: npm run db-migrate -- up |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"name": "node-postgres-todo", | ||
"description": "", | ||
"scripts": {}, | ||
"env": {}, | ||
"formation": { | ||
"web": { | ||
"quantity": 1 | ||
} | ||
}, | ||
"addons": [ | ||
"heroku-postgresql" | ||
], | ||
"buildpacks": [ | ||
{ | ||
"url": "heroku/nodejs" | ||
} | ||
] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
'use strict'; | ||
|
||
var dbm; | ||
var type; | ||
var seed; | ||
var fs = require('fs'); | ||
var path = require('path'); | ||
var Promise; | ||
|
||
/** | ||
* We receive the dbmigrate dependency from dbmigrate initially. | ||
* This enables us to not have to rely on NODE_PATH. | ||
*/ | ||
exports.setup = function(options, seedLink) { | ||
dbm = options.dbmigrate; | ||
type = dbm.dataType; | ||
seed = seedLink; | ||
Promise = options.Promise; | ||
}; | ||
|
||
exports.up = function(db) { | ||
var filePath = path.join(__dirname, 'sqls', '20180610171545-bootstrap-db-up.sql'); | ||
return new Promise( function( resolve, reject ) { | ||
fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data){ | ||
if (err) return reject(err); | ||
console.log('received data: ' + data); | ||
|
||
resolve(data); | ||
}); | ||
}) | ||
.then(function(data) { | ||
return db.runSql(data); | ||
}); | ||
}; | ||
|
||
exports.down = function(db) { | ||
var filePath = path.join(__dirname, 'sqls', '20180610171545-bootstrap-db-down.sql'); | ||
return new Promise( function( resolve, reject ) { | ||
fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data){ | ||
if (err) return reject(err); | ||
console.log('received data: ' + data); | ||
|
||
resolve(data); | ||
}); | ||
}) | ||
.then(function(data) { | ||
return db.runSql(data); | ||
}); | ||
}; | ||
|
||
exports._meta = { | ||
"version": 1 | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
CREATE TABLE items( | ||
id SERIAL PRIMARY KEY, | ||
text VARCHAR NOT NULL, | ||
complete BOOLEAN | ||
); |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,124 +1,38 @@ | ||
const express = require('express'); | ||
const router = express.Router(); | ||
const pg = require('pg'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
const { Client } = require('pg'); | ||
const connectionString = process.env.DATABASE_URL; | ||
const pg = new Client({ connectionString }); | ||
pg.connect(); | ||
const path = require('path'); | ||
const connectionString = process.env.DATABASE_URL || 'postgres://localhost:5432/todo'; | ||
|
||
router.get('/', (req, res, next) => { | ||
res.sendFile(path.join( | ||
__dirname, '..', '..', 'client', 'views', 'index.html')); | ||
}); | ||
|
||
router.get('/api/v1/todos', (req, res, next) => { | ||
const results = []; | ||
// Get a Postgres client from the connection pool | ||
pg.connect(connectionString, (err, client, done) => { | ||
// Handle connection errors | ||
if(err) { | ||
done(); | ||
console.log(err); | ||
return res.status(500).json({success: false, data: err}); | ||
} | ||
// SQL Query > Select Data | ||
const query = client.query('SELECT * FROM items ORDER BY id ASC;'); | ||
// Stream results back one row at a time | ||
query.on('row', (row) => { | ||
results.push(row); | ||
}); | ||
// After all data is returned, close connection and return results | ||
query.on('end', () => { | ||
done(); | ||
return res.json(results); | ||
}); | ||
}); | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Surprised by how much boilerplate went away! |
||
router.get('/api/v1/todos', (req, res, next) => pg.query('SELECT * FROM items ORDER BY id ASC;') | ||
.then(({ rows }) => res.json(rows)) | ||
.catch(e => console.error(e) || res.status(500).json({success: false, data: e}))); | ||
|
||
router.post('/api/v1/todos', (req, res, next) => { | ||
const results = []; | ||
// Grab data from http request | ||
const data = {text: req.body.text, complete: false}; | ||
// Get a Postgres client from the connection pool | ||
pg.connect(connectionString, (err, client, done) => { | ||
// Handle connection errors | ||
if(err) { | ||
done(); | ||
console.log(err); | ||
return res.status(500).json({success: false, data: err}); | ||
} | ||
// SQL Query > Insert Data | ||
client.query('INSERT INTO items(text, complete) values($1, $2)', | ||
[data.text, data.complete]); | ||
// SQL Query > Select Data | ||
const query = client.query('SELECT * FROM items ORDER BY id ASC'); | ||
// Stream results back one row at a time | ||
query.on('row', (row) => { | ||
results.push(row); | ||
}); | ||
// After all data is returned, close connection and return results | ||
query.on('end', () => { | ||
done(); | ||
return res.json(results); | ||
}); | ||
}); | ||
}); | ||
router.post('/api/v1/todos', (req, res, next) => pg.query('INSERT INTO items(text, complete) values($1, $2)', [req.body.text, false]) | ||
.then(() => pg.query('SELECT * FROM items ORDER BY id ASC')) | ||
.then(({ rows }) => res.json(rows)) | ||
.catch(e => console.error(e) || res.status(500).json({success: false, data: e}))); | ||
|
||
router.put('/api/v1/todos/:todo_id', (req, res, next) => { | ||
const results = []; | ||
// Grab data from the URL parameters | ||
const id = req.params.todo_id; | ||
// Grab data from http request | ||
const data = {text: req.body.text, complete: req.body.complete}; | ||
// Get a Postgres client from the connection pool | ||
pg.connect(connectionString, (err, client, done) => { | ||
// Handle connection errors | ||
if(err) { | ||
done(); | ||
console.log(err); | ||
return res.status(500).json({success: false, data: err}); | ||
} | ||
// SQL Query > Update Data | ||
client.query('UPDATE items SET text=($1), complete=($2) WHERE id=($3)', | ||
[data.text, data.complete, id]); | ||
// SQL Query > Select Data | ||
const query = client.query("SELECT * FROM items ORDER BY id ASC"); | ||
// Stream results back one row at a time | ||
query.on('row', (row) => { | ||
results.push(row); | ||
}); | ||
// After all data is returned, close connection and return results | ||
query.on('end', function() { | ||
done(); | ||
return res.json(results); | ||
}); | ||
}); | ||
}); | ||
const { text, complete } = req.body; | ||
const { todo_id: id } = req.params; | ||
|
||
router.delete('/api/v1/todos/:todo_id', (req, res, next) => { | ||
const results = []; | ||
// Grab data from the URL parameters | ||
const id = req.params.todo_id; | ||
// Get a Postgres client from the connection pool | ||
pg.connect(connectionString, (err, client, done) => { | ||
// Handle connection errors | ||
if(err) { | ||
done(); | ||
console.log(err); | ||
return res.status(500).json({success: false, data: err}); | ||
} | ||
// SQL Query > Delete Data | ||
client.query('DELETE FROM items WHERE id=($1)', [id]); | ||
// SQL Query > Select Data | ||
var query = client.query('SELECT * FROM items ORDER BY id ASC'); | ||
// Stream results back one row at a time | ||
query.on('row', (row) => { | ||
results.push(row); | ||
}); | ||
// After all data is returned, close connection and return results | ||
query.on('end', () => { | ||
done(); | ||
return res.json(results); | ||
}); | ||
return pg.query('UPDATE items SET text=($1), complete=($2) WHERE id=($3)', [text, complete, id]) | ||
.then(() => pg.query('SELECT * FROM items ORDER BY id ASC')) | ||
.then(({ rows }) => res.json(rows)) | ||
.catch(e => console.log(e) || res.status(500).json({success: false, data: e})); | ||
}); | ||
}); | ||
|
||
router.delete('/api/v1/todos/:todo_id', (req, res, next) => pg.query('DELETE FROM items WHERE id=($1)', [req.params.todo_id]) | ||
.then(() => pg.query('SELECT * FROM items ORDER BY id ASC')) | ||
.then(({ rows }) => res.json(rows)) | ||
.catch(e => console.log(e) || res.status(500).json({success: false, data: e}))); | ||
|
||
module.exports = router; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a file generated by db-migrate