Skip to content

Instantly share code, notes, and snippets.

@JitendraZaa
Last active August 29, 2015 14:09
Show Gist options
  • Select an option

  • Save JitendraZaa/c0d5f17d024e29ec6b8e to your computer and use it in GitHub Desktop.

Select an option

Save JitendraZaa/c0d5f17d024e29ec6b8e to your computer and use it in GitHub Desktop.
package.json file of node-postgres repository
{
"name": "BasicCRUDOperation",
"version": "1.0.0",
"description": "CRUD Operations in Node.js using PostgreSQL",
"main": "server.js",
"dependencies": {
"http": "~0.0.0",
"request": "~2.36.0",
"express": "~4.4.5",
"logfmt": "~1.1.2",
"body-parser": "~1.6.2",
"serve-favicon": "*",
"cookie-parser": "~1.0.1",
"jade": "*"
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node App.js"
},
"repository": {
"type": "git",
"url": "https://github.com/JitendraZaa/node-postgres"
},
"engines": {
"node": "0.10.x"
},
"keywords": [
"Salesforce",
"Tooling",
"API"
],
"author": "Jitendra Zaa",
"license": "MIT"
}
module.exports = {
getRecords: function(req, res) {
var pg = require('pg');
//You can run command "heroku config" to see what is Database URL from Heroku belt
var conString = process.env.DATABASE_URL || "postgres://postgres:Welcome123@localhost:5432/postgres";
var client = new pg.Client(conString);
client.connect();
var query = client.query("select * from employee");
query.on("row", function (row, result) {
result.addRow(row);
});
query.on("end", function (result) {
client.end();
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write(JSON.stringify(result.rows, null, " ") + "\n");
res.end();
});
},
addRecord : function(req, res){
var pg = require('pg');
var conString = process.env.DATABASE_URL || "postgres://postgres:Welcome123@localhost:5432/postgres";
var client = new pg.Client(conString);
client.connect();
var query = client.query("insert into employee (firstName,lastName,email,mobile) "+
"values ('"+req.query.fName+"','"+req.query.lName+"','"+
req.query.email+"','"+req.query.mbl+"')");
query.on("end", function (result) {
client.end();
res.write('Success');
res.end();
});
},
delRecord : function(req, res){
var pg = require('pg');
var conString = process.env.DATABASE_URL || "postgres://postgres:Welcome123@localhost:5432/postgres";
var client = new pg.Client(conString);
client.connect();
var query = client.query( "Delete from employee Where id ="+req.query.id);
query.on("end", function (result) {
client.end();
res.write('Success');
res.end();
});
},
createTable : function(req, res){
var pg = require('pg');
var conString = process.env.DATABASE_URL || "postgres://postgres:Welcome123@localhost:5432/postgres";
var client = new pg.Client(conString);
client.connect();
var query = client.query( "CREATE TABLE employee"+
"("+
"firstname character varying(50),"+
"lastname character varying(20),"+
"email character varying(30),"+
"mobile character varying(12),"+
"id serial NOT NULL"+
")");
query.on("end", function (result) {
client.end();
res.write('Table Schema Created');
res.end();
});
},
dropTable : function(req, res){
var pg = require('pg');
var conString = process.env.DATABASE_URL || "postgres://postgres:Welcome123@localhost:5432/postgres";
var client = new pg.Client(conString);
client.connect();
var query = client.query( "Drop TABLE employee");
query.on("end", function (result) {
client.end();
res.write('Table Schema Deleted');
res.end();
});
}
};
var express = require('express'),
http = require('http'),
request = require('request'),
bodyParser = require('body-parser'),
app = express();
var dbOperations = require("./dbOperations.js");
var logFmt = require("logfmt");
app.set('views', __dirname + '/views') ;
app.get('/' , function(req,res) {
res.sendfile('views/index.html');
} );
app.get('/db/readRecords', function(req,res){
dbOperations.getRecords(req,res);
});
app.get('/db/addRecord', function(req,res){
dbOperations.addRecord(req,res);
});
app.get('/db/delRecord', function(req,res){
dbOperations.delRecord(req,res);
});
app.get('/db/createTable', function(req,res){
dbOperations.createTable(req,res);
});
app.get('/db/dropTable', function(req,res){
dbOperations.dropTable(req,res);
});
app.set('port', process.env.PORT || 3001);
app.use(express.static(__dirname + '/client'));
app.listen(app.get('port'), function () {
console.log('Express server listening on port ' + app.get('port'));
});
<html>
<head>
<link href="css/bootstrap.min.css" rel="stylesheet">
<link rel="icon" type="image/icon" href="Node.ico" />
</head>
<body ng-app="postgreSQL">
<div class="container" style="margin-top:20px;" ng-controller="postgreSQLCtrl">
<div class="row clearfix">
<div class="col-md-12 column">
<div class="jumbotron">
<h1>
PostgreSQL in Nodejs
</h1>
<p>
This application demonstrates database operations using PostgreSQL as a database, Heroku as platform, Node.js as language and Angularjs as javascript based MVC.
<br /> <br />
<a class="pull-right" href="http://JitendraZaa.com"> Jitendra Zaa </a>
</p>
</div>
</div>
</div>
<div class="row clearfix" style="margin-bottom:10px;">
<div class="controls form-inline form-group">
<div class="col-md-2">
<input id="fName" name="fName" placeholder="First Name" ng-model="fName"
type="text" class="form-control input-small">
</div>
<div class="col-md-2">
<input id="lName" name="lName" placeholder="Last Name" ng-model="lName"
type="text" class="form-control input-small">
</div>
<div class="col-md-2">
<input id="Email" name="Email" placeholder="Email" ng-model="email"
type="text" class="form-control input-small">
</div>
<div class="col-md-2">
<input id="mobile" name="mobile" placeholder="Mobile" ng-model="mbl"
type="text" class="form-control input-small">
</div>
<div class="col-md-2">
<button ng-click="addRecord()" id="btnAdd" name="btnAdd" class="btn btn-danger">Add</button>
</div>
</div>
</div>
<div class="row clearfix">
<table class="table table-bordered table-hover table-condensed">
<thead>
<tr>
<th> Id </th>
<th> First Name </th>
<th> Last Name </th>
<th> Email </th>
<th> Mobile </th>
<th> </th>
</tr>
</thead>
<tbody>
<tr ng-repeat="item in dataset">
<td> {{item.id}} </td>
<td> {{item.firstname}} </td>
<td> {{item.lastname}} </td>
<td> {{item.email}} </td>
<td> {{item.mobile}} </td>
<td> <span title="delete this record" class="glyphicon glyphicon-trash text-danger" style="cursor:pointer" ng-click="delRecord(item.id)"></span> </td>
</tr>
</tbody>
</table>
</div>
</div>
<script src="lib/angular.min.js"></script>
<script>
var myApp = angular.module('postgreSQL',[ ]);
myApp.controller('postgreSQLCtrl', ['$scope' ,'$http', function($scope,$http) {
$scope.val1 = 'This is temp Val' ;
$scope.fName = '';
$scope.lName = '';
$scope.email = '';
$scope.mbl = '' ;
$scope.getAllRec = function(){
$http({method: 'GET', url: '/db/readRecords'}).
success(function(data, status) {
$scope.dataset = data;
}).
error(function(data, status) {
$scope.dataset = data || "Request failed ";
});
}
$scope.addRecord = function(){
$http({method: 'GET', url: '/db/addRecord?fName='+$scope.fName+'&lName='+
$scope.lName+'&email='+$scope.email+'&mbl='+$scope.mbl}).
success(function(data, status) {
alert('Record Added');
$scope.getAllRec();
});
}
$scope.delRecord = function(recId){
console.log(recId);
if(confirm('Are you sure you want to delete this record ? '))
{
$http({method: 'GET', url: '/db/delRecord?id='+recId}).
success(function(data, status) {
$scope.getAllRec();
});
}
}
$scope.getAllRec();
}]);
</script>
</body>
</html>
{
"name": "PostgreSQL in Nodejs",
"description": "CRUD operations using PostgreSQL as a database, Heroku as platform, Node.js as language and Angularjs as javascript based MVC",
"repository": "https://github.com/JitendraZaa/node-postgres.git",
"logo": "https://node-postgres.herokuapp.com/Node.ico",
"keywords": [ "Salesforce", "AngularJs", "Node.js", "PostgreSQL", "Jitendra"]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment