-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
working get and post communications with postgres db using node pg
- Loading branch information
1 parent
4eb9ab6
commit 869b44a
Showing
7 changed files
with
201 additions
and
74 deletions.
There are no files selected for viewing
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
Submodule EmbraceYourself
deleted from
d80fd7
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,46 @@ | ||
var express = require('express'); | ||
var bodyParser = require('body-parser') | ||
var bodyParser = require('body-parser'); | ||
// database dependencies | ||
var pg = require('pg'); | ||
var connectionString = 'postgres://localhost:5432/test'; | ||
|
||
// database users | ||
var userTableSure = require('./server/models/users/userModel.js').userTableSure; | ||
var userController = require('./server/models/users/userController.js'); | ||
// database trips | ||
var tripTableSure = require('./server/models/trips/tripModel.js').tripTableSure; | ||
|
||
var app = express(); | ||
var port = process.env.PORT || 8000; | ||
|
||
app.use( bodyParser.json() ); // to support JSON-encoded bodies | ||
app.use(bodyParser.urlencoded({ // to support URL-encoded bodies | ||
extended: true | ||
})); | ||
})); | ||
app.use(express.static(__dirname + '/client')); | ||
app.use('/scripts', express.static(__dirname + '/bower_components')); | ||
//app.use('/scripts', express.static(__dirname + '/bower_components')); | ||
|
||
app.get('/', function(req, res){ | ||
res.send("Rideshare server up and running!"); | ||
}); | ||
|
||
app.get('/data', function (req, res) { | ||
console.log("Get Received!"); | ||
var client = new pg.Client(connectionString); | ||
userController.getUsers(req, res, client); | ||
|
||
}); | ||
|
||
app.post('/data', function (req, res) { | ||
console.log("Post received!"); | ||
if (req.body) { | ||
var client = new pg.Client(connectionString); | ||
userController.newUser(req.body.firstname, req.body.lastname, req, res, client); | ||
} | ||
}); | ||
|
||
app.listen(port, function() { | ||
console.log('App up and running on http://localhost: ', port); | ||
}); | ||
userTableSure(); | ||
tripTableSure(); | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// PostgresQL database Setup | ||
// Official Documentation: https://github.com/brianc/node-postgres/wiki/pg | ||
// Guide: http://mherman.org/blog/2015/02/12/postgresql-and-nodejs/ | ||
|
||
var pg = require('pg'); | ||
// var conString = "postgres://username:password@localhost/database"; | ||
var connectionString = process.env.DATABASE_URL || 'postgres://localhost:5432/test'; | ||
|
||
var tripTableSure = function () { | ||
|
||
var client = new pg.Client(connectionString); | ||
|
||
client.connect(function(err) { | ||
if(err) { | ||
return console.error('could not connect to postgres', err); | ||
} | ||
var query1 = client.query('CREATE TABLE IF NOT EXISTS trips(id SERIAL PRIMARY KEY, event VARCHAR(255))'); | ||
|
||
query1.on('end', function() { client.end(); }); | ||
}); | ||
|
||
} | ||
|
||
module.exports = { | ||
tripTableSure: tripTableSure | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,134 @@ | ||
var User = require('./userModel.js'), | ||
function tableSure (client) { | ||
|
||
/*exmaple of how to make table relatonsips: | ||
var User = sequelize.define('user', { name: Sequelize.STRING }) | ||
, Task = sequelize.define('task', { name: Sequelize.STRING }) | ||
, Tool = sequelize.define('tool', { name: Sequelize.STRING }) | ||
client.connect(function(err) { | ||
if(err) { | ||
return console.error('could not connect to postgres', err); | ||
} | ||
var query1 = client.query('CREATE TABLE IF NOT EXISTS users(id SERIAL PRIMARY KEY, firstname VARCHAR(255), lastname VARCHAR(255))'); | ||
|
||
Task.belongsTo(User) | ||
User.hasMany(Task) | ||
User.hasMany(Tool, { as: 'Instruments' }) | ||
var query2 = client.query('CREATE TABLE IF NOT EXISTS cars(id SERIAL PRIMARY KEY, text VARCHAR(255))'); | ||
|
||
// var query3 = client.query("INSERT INTO users (firstname, lastname) VALUES ('Bob', 'Barley')"); | ||
|
||
sequelize.sync().then(function() { | ||
// this is where we continue ... | ||
}) | ||
*/ | ||
|
||
|
||
module.exports = { | ||
|
||
|
||
findAllUsers: function(req, res, next) { | ||
|
||
User.findAll({ }).then(function(users) { | ||
console.log(JSON.stringify(tasks)); | ||
query2.on('end', function() { client.end(); }); | ||
}); | ||
}, | ||
|
||
|
||
signup: function(req, res, next) { | ||
|
||
User | ||
.findOrCreate({where: {username: 'sdepold'}, defaults: {job: 'Technical Lead JavaScript'}}) | ||
.spread(function(user, created) { | ||
console.log(user.get({ | ||
plain: true | ||
})) | ||
console.log(created) | ||
} | ||
|
||
/*EXAMPLE USER GOES HERE: | ||
{ | ||
username: 'sdepold', | ||
job: 'Technical Lead JavaScript', | ||
id: 1, | ||
createdAt: Fri Mar 22 2013 21: 28: 34 GMT + 0100(CET), | ||
updatedAt: Fri Mar 22 2013 21: 28: 34 GMT + 0100(CET) | ||
} | ||
created: true | ||
*/ | ||
}); | ||
}, | ||
function getUsers (req, res, client) { | ||
var results = []; | ||
client.connect(function(err) { | ||
if(err) { | ||
console.error('get failed!'); | ||
return res.status(500).json({ success: false, data: err}); | ||
} | ||
|
||
var query = client.query('SELECT * FROM users'); | ||
|
||
signin: function(req, res, next) { | ||
query.on('row', function(row) { | ||
results.push(row); | ||
}); | ||
|
||
//code goes here | ||
query.on('end', function() { | ||
client.end(); | ||
return res.json(results); | ||
}); | ||
}); // end client.connect | ||
} | ||
|
||
}, | ||
function newUser (firstname, lastname, req, res, client) { | ||
|
||
client.connect(function(err) { | ||
if(err) { | ||
console.error('post failed!'); | ||
return res.status(500).json({ success: false, data: err}); | ||
} | ||
|
||
checkAuth: function(req, res, next) { | ||
var query = client.query("INSERT INTO users(firstname, lastname) values($1, $2)", [firstname, lastname]); | ||
|
||
//code goes here | ||
|
||
} | ||
query.on('end', function() { | ||
client.end(); | ||
return res.status(302); | ||
}); | ||
|
||
}); // end client.connect | ||
} | ||
|
||
}; | ||
module.exports = { | ||
tableSure: tableSure, | ||
getUsers: getUsers, | ||
newUser: newUser | ||
}; | ||
|
||
|
||
// var User = require('./userModel.js'), | ||
// | ||
// /*exmaple of how to make table relatonsips: | ||
// var User = sequelize.define('user', { name: Sequelize.STRING }) | ||
// , Task = sequelize.define('task', { name: Sequelize.STRING }) | ||
// , Tool = sequelize.define('tool', { name: Sequelize.STRING }) | ||
// | ||
// Task.belongsTo(User) | ||
// User.hasMany(Task) | ||
// User.hasMany(Tool, { as: 'Instruments' }) | ||
// | ||
// | ||
// sequelize.sync().then(function() { | ||
// // this is where we continue ... | ||
// }) | ||
// */ | ||
// | ||
// | ||
// module.exports = { | ||
// | ||
// | ||
// findAllUsers: function(req, res, next) { | ||
// | ||
// User.findAll({ }).then(function(users) { | ||
// console.log(JSON.stringify(tasks)); | ||
// }); | ||
// }, | ||
// | ||
// | ||
// signup: function(req, res, next) { | ||
// | ||
// User | ||
// .findOrCreate({where: {username: 'sdepold'}, defaults: {job: 'Technical Lead JavaScript'}}) | ||
// .spread(function(user, created) { | ||
// console.log(user.get({ | ||
// plain: true | ||
// })) | ||
// console.log(created) | ||
// | ||
// /*EXAMPLE USER GOES HERE: | ||
// { | ||
// username: 'sdepold', | ||
// job: 'Technical Lead JavaScript', | ||
// id: 1, | ||
// createdAt: Fri Mar 22 2013 21: 28: 34 GMT + 0100(CET), | ||
// updatedAt: Fri Mar 22 2013 21: 28: 34 GMT + 0100(CET) | ||
// } | ||
// created: true | ||
// */ | ||
// }); | ||
// }, | ||
// | ||
// | ||
// signin: function(req, res, next) { | ||
// | ||
// //code goes here | ||
// | ||
// }, | ||
// | ||
// | ||
// checkAuth: function(req, res, next) { | ||
// | ||
// //code goes here | ||
// | ||
// } | ||
// | ||
// | ||
// }; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,29 @@ | ||
var User = sequelize.define('user', { | ||
firstName: { | ||
type: Sequelize.STRING, | ||
field: 'first_name' // Will result in an attribute that is firstName when user facing but first_name in the database | ||
}, | ||
lastName: { | ||
type: Sequelize.STRING | ||
} | ||
}, { | ||
freezeTableName: true // Model tableName will be the same as the model name | ||
}); | ||
|
||
User.sync({force: true}).then(function () { | ||
// Table created | ||
return User.create({ | ||
firstName: 'John', | ||
lastName: 'Hancock' | ||
// PostgresQL database Setup | ||
// Official Documentation: https://github.com/brianc/node-postgres/wiki/pg | ||
// Guide: http://mherman.org/blog/2015/02/12/postgresql-and-nodejs/ | ||
|
||
var pg = require('pg'); | ||
// var conString = "postgres://username:password@localhost/database"; | ||
var connectionString = process.env.DATABASE_URL || 'postgres://localhost:5432/test'; | ||
|
||
var userTableSure = function () { | ||
|
||
var client = new pg.Client(connectionString); | ||
|
||
client.connect(function(err) { | ||
if(err) { | ||
return console.error('could not connect to postgres', err); | ||
} | ||
var query1 = client.query('CREATE TABLE IF NOT EXISTS users(id SERIAL PRIMARY KEY, firstname VARCHAR(255), lastname VARCHAR(255))'); | ||
|
||
query1.on('end', function() { client.end(); }); | ||
}); | ||
}); | ||
|
||
} | ||
|
||
module.exports = { | ||
userTableSure: userTableSure | ||
}; | ||
|
||
// var query2 = client.query('CREATE TABLE IF NOT EXISTS cars(id SERIAL PRIMARY KEY, text VARCHAR(255))'); | ||
// query2.on('end', function() { client.end(); }); |