PostGreSQL
DESCRIPTION
General
Installation
npm install eden-postgres
Usage
var postgres = require('eden-postgres');
Methods
collection
eden/postgres/collection collection(collection[1].user_email);
Returns collection
Parameters
- collection[1].user_email - function
Returns
eden/postgres/collection
Example
Code
database.collection('eden_users')
.add({})
.add({})
.setUserName('Bobby2')
.setUserEmail('bobby2@gmail.com')
.setUserFacebook(123)
postgres().collection[1].user_name;
postgres().collection[1].user_email;
postgres().collection[1].user_facebook;
Outputs
'Bobby2'
'bobby2@gmail.com'
123
connect
this connect('eden_users');
Connects to the database
Parameters
- 'eden_users' - array - the connection options
Returns
this
Example
Code
postgres().connect('eden_users');
Outputs
'edenjs_test'
getConnection
connection resource getConnection(edenjs_test);
Returns the connection object if no connection has been made it will attempt to make it
Parameters
- edenjs_test - array - connection options (database name)
Returns
connection resource
Example
Code
postgres().getConnection('edenjs_test');
Outputs
'edenjs_users'
getColumns
this getColumns(eden_users, user_id, SELECT);
Returns the columns and attributes given the table name
Parameters
-
eden_users - the name of the table
-
user_id - name of schema
-
SELECT - function - callback
Returns
this
Example
Code
postgres().getColumns('SELECT user_id FROM eden_users');
postgres().getColumns('SELECT user_name FROM eden_users');
postgres().getColumns('SELECT user_email FROM eden_users');
postgres().getColumns('SELECT user_facebook FROM eden_users');
Outputs
'user_id'
'user_name'
'user_email'
'user_facebook'
getRow
this getRow(eden_users, user_email, 'bob@gmail.com', select);
Returns a 1 row result given the column name and the value
Parameters
-
eden_users - string - table name
-
user_email - string - name of schema (column name)
-
'bob@gmail.com' - string - row value in column user_email
-
select - function - callback
Returns
this
Example
Code
database.getRow('eden_users', 'user_email', 'bob@gmail.com');
database.getRow('eden_users', 'user_email', 'dayle@gmail.com');
Outputs
'bobby'
null
insert
eden/postgres/insert insert('blog_users');
Returns the insert query builder
Parameters
- 'blog_users' - string - table name to add columns and rows
Returns
eden/postgres/insert
Example
Code
var query = database.insert('blog_users')
.set('user_name', 'chris')
.set('user_age', 21)
.getQuery();
query = database.insert('blog_users')
.set({user_name: 'chris', user_age: 21})
.getQuery();
query = database.insert('blog_users')
.set('user_name', 'chris', 0)
.set('user_age', 21, 0)
.set('user_name', '?', 1)
.set('user_age', 22, 1)
.getQuery();
query = database.insert('blog_users')
.set({user_name: 'chris', user_age: 21})
.set({user_name: 'dan', user_age: 22}, 1)
.getQuery();
Outputs
'INSERT INTO "blog_users" ("user_name", "user_age") VALUES (\'chris\', 21);'
'INSERT INTO "blog_users" ("user_name", "user_age") VALUES (\'chris\', 21);'
'INSERT INTO "blog_users" ("user_name", "user_age") VALUES (\'chris\', 21), (?, 22);'
'INSERT INTO "blog_users" ("user_name", "user_age") VALUES (\'chris\', 21), (\'dan\', 22);'
insertRow
this insertRow('eden_users', user_name, Array|bool|null, Function);
Inserts data into a table and returns the ID
Parameters
-
'eden_users' - string - table
-
user_name - object - setting
-
array|bool|null
-
function - callback
Returns
this
Example
Code
database.insertRow('eden_users', {
user_name: 'bob',
user_email: 'bob@gmail.com',
user_facebook: 123
}, function(error, rows, meta));
postgres().insertRow(meta.oid > 1);
Outputs
true
insertRows
this insertRows(eden_users, user_name, Array|bool|null, Function);
Inserts multiple rows into a table
Parameters
-
eden_users - string - table
-
user_name - object - settings
-
array|bool|null
-
function - callback
Returns
this
Example
Code
database.insertRows('eden_users', [{
user_name: 'bob',
user_email: 'bob@gmail.com',
user_facebook: 123
}, {
user_name: 'chris',
user_email: 'bob@gmail.com',
user_facebook: 312
}], function(error, rows, meta));
postgres().insertRows(meta.rowCount);
Outputs
2
model
eden/postgres/model model(eden_users);
Returns model
Parameters
- eden_users - string (table name to set row in column)
Returns
eden/postgres/model
Example
Code
database.model('eden_users')
.setUserName('Bobby')
.setUserEmail('bobby@gmail.com')
.setUserFacebook(123)
.save(function(error, model, meta));
postgres().model(null);
model.setUserName('Billy').save(function(error, model, meta));
postgres().model(error);
Outputs
error
null
query
this query(eden_users, all/*, select);
Queries the database
Parameters
-
eden_users - string - query
-
all/* - array - binded value
-
select - function - callback
Returns
this
Example
Code
database.query('SELECT * FROM eden_users', function(error, rows, meta));
postgres().query(meta.rows.length > 0);
Outputs
true
remove
eden/postgres/delete remove('user_id = 1');
Returns the delete query builder
Parameters
- 'user_id = 1' - string (column and row value to be remove)
Returns
eden/postgres/delete
Example
Code
var query = database
.remove('blog_users')
.where('user_id = ?')
.getQuery();
query = database
.remove('blog_users')
.where(['user_id = ?', 'user_name = ?'])
.getQuery();
query = database
.remove('blog_users')
.where('user_id = ?')
.where('user_name = ?')
.getQuery();
Outputs
'DELETE FROM "blog_users" WHERE user_id = ?;'
'DELETE FROM "blog_users" WHERE user_id = ? AND user_name = ?;'
'DELETE FROM "blog_users" WHERE user_id = ? AND user_name = ?;'
removeRows
this removeRows(eden_users, 'user_email = ?', Function);
Removes rows that match a filter
Parameters
-
eden_users - string - table name
-
'user_email = ?' - array - filter
-
function - callback
Returns
this
Example
Code
database.removeRows('eden_users',
[['user_email = ?', 'bob@gmail.com']],
function(error, rows, meta));
postgres().removeRows(typeof meta === 'object');
Outputs
true
select
eden/postgres/select select('*'/all);
Returns the select query builder
Parameters
- '*' / all - string (to get the respective column)
Returns
eden/postgres/select
Example
Code
var query = database.select('*')
.from('blog_users')
.innerJoin('blog_post', 'post_user=user_id', false)
.where('user_name = ?')
.sortBy('user_name')
.groupBy('user_id')
.limit(1, 2)
.getQuery();
query = database.select('*')
.from('blog_users')
.leftJoin('blog_post', 'post_user=user_id', false)
.where('user_name = ?')
.sortBy('user_name', 'DESC')
.groupBy('user_id')
.limit(1, 2)
.getQuery();
query = database.select('*')
.from('blog_users')
.rightJoin('blog_post', 'post_user=user_id', false)
.where('user_name = ?')
.sortBy('user_name')
.getQuery();
query = database.select('*')
.from('blog_users')
.outerJoin('blog_post', 'post_user')
.where('user_name = ?')
.groupBy('user_id')
.limit(1, 2)
.getQuery();
Outputs
'SELECT * FROM blog_users INNER JOIN blog_post ON (post_user=user_id) '
+ 'WHERE user_name = ? GROUP BY "user_id" ORDER BY "user_name" ASC OFFSET 1 LIMIT 2;'
'SELECT * FROM blog_users LEFT JOIN blog_post ON (post_user=user_id) '
+ 'WHERE user_name = ? GROUP BY "user_id" ORDER BY "user_name" DESC OFFSET 1 LIMIT 2;'
'SELECT * FROM blog_users RIGHT JOIN blog_post ON (post_user=user_id) '
+ 'WHERE user_name = ? ORDER BY "user_name" ASC;'
'SELECT * FROM blog_users OUTER JOIN blog_post USING (post_user) '
+ 'WHERE user_name = ? GROUP BY "user_id" OFFSET 1 LIMIT 2;'
search
eden/postgres/search search('user_name = ?');
Returns search
Parameters
- 'user_name = ?' - function (to get the respective row data)
Returns
eden/postgres/search
Example
Code
database.search('eden_users').getRows(function(error, rows));
postgres().search(typeof rows.length);
database.search('eden_users')
.addFilter('user_name = ?', 'Christian Blanquera')
.getRow(function(error, row, meta));
postgres().search(row.user_email);
database.search('eden_post')
.innerJoinOn('eden_users', 'post_user=user_id')
.getRows(function(error, rows));
postgres().search(typeof rows.length);
database.search('eden_post')
.innerJoinOn('eden_users', 'post_user=user_id')
.filterByUserName('Christian Blanquera')
.getRow(function(error, row, meta));
postgres().search(row.user_email);
database.search('eden_post')
.innerJoinOn('eden_users', 'post_user=user_id')
.filterByUserName('Christian Blanquera')
.sortByUserId('DESC')
.getRows(function(error, rows, meta));
postgres().search(typeof rows.length);
database.search('eden_post')
.innerJoinOn('eden_users', 'post_user=user_id')
.filterByUserName('Christian Blanquera')
.sortByUserId('DESC')
.getCollection(function(error, collection, meta));
postgres().search(collection[0].getUserName());
database.search('eden_post')
.innerJoinOn('eden_users', 'post_user=user_id')
.filterByUserName('Christian Blanquera')
.sortByUserId('DESC')
.getCollection(function(error, collection, meta));
collection.setUserFacebook(321).save(function(error, collection, meta));
postgres().search(collection[0].getUserFacebook());
database.search('eden_post')
.innerJoinOn('eden_users', 'post_user=user_id')
.filterByUserName('Christian Blanquera')
.sortByUserId('DESC')
.getModel(function(error, model, meta));
postgres().search(model.getUserName());
database.search('eden_post')
.innerJoinOn('eden_users', 'post_user=user_id')
.filterByUserName('Christian Blanquera')
.sortByUserId('DESC')
.getModel(function(error, model, meta));
model.setUserFacebook(10204676425696496).save(function(error, collection, meta));
postgres().search(model.getUserFacebook());
Outputs
'number'
'cblanquera@gmail.com'
'number'
'cblanquera@gmail.com'
'number'
'Christian Blanquera'
321
'Christian Blanquera'
10204676425696496
setRow
this setRow(String, String, String, Object, Function);
Sets only 1 row given the column name and the value
Parameters
-
string - table
-
string - name
-
string - value
-
object - setting
-
function - callback
Returns
this
Example
Code
postgres().setRow();
Outputs
RESULTS
update
eden/postgres/update update(String);
Returns the update query builder
Parameters
- string
Returns
eden/postgres/update
Example
Code
var query = database.update('blog_users')
.set('user_name', 'chris')
.set('user_age', 21)
.where('user_id = ?')
.where('user_name = ?')
.getQuery();
postgres().update(query);
query = database.update('blog_users')
.set({user_name: 'chris', user_age: 21})
.where(['user_id = ?', 'user_name = ?'])
.getQuery();
postgres().update(query);
Outputs
'UPDATE "blog_users" SET "user_name" = \'chris\', "user_age" = 21 WHERE user_id = ? AND user_name = ?;'
'UPDATE "blog_users" SET "user_name" = \'chris\', "user_age" = 21 WHERE user_id = ? AND user_name = ?;'
updateRows
this updateRows('eden_users', Object, Array, Array|bool|null, Function);
Updates rows that match a filter given the update settings
Parameters
-
'eden_users' - string - table
-
object - setting
-
array - filter
-
array|bool|null
-
function - callback
Returns
this
Example
Code
database.updateRows('eden_users', {
user_name: 'bobby'
}, [['user_email = ?', 'bob@gmail.com']], function(error, rows, meta));
postgres().updateRows(meta.rowCount > 1);
Outputs
true