pg-defer
v1.0.0
Published
Create your own entity for postgresql and improve code readability
Downloads
2
Maintainers
Readme
pg-defer
Create your own entity for postgresql and improve code readability
Install
npm install pg-defer
How use
var pgDefer = require('pg-defer');
pgDefer.settings({ db: {"example": "postgres://user:password@localhost/db_name"} });
var Account = pgDefer("example", {
get: "SELECT * FROM #{table} WHERE email='#{email} AND hash='#{hash}'",
add: "INSERT INTO #{table} (email, hash, acc_group) " +
"SELECT '#{email}', '#{hash}', '#{group}' " +
"WHERE NOT EXISTS (SELECT email FROM #{table} WHERE email='#{email}')",
changePassword: "UPDATE #{table} SET hash='#{hash}' WHERE id=#{uid}"
}, {
table: "account"
});
var email = "[email protected]";
var password = "MySimplePWD";
var hash = cryptoMyPassword(password);
Account.get({email: email, hash: hash}, function (err, users) {
if (err) throw err;
if (!users.length) {
Account.add({email: email, hash: hash, group: "user"}, addUserHandler);
return;
}
var newPassword = "anotherPassword123";
var newHash = cryptoMyPassword(password)
Account.changePassword({uid: users[0].id, hash: newHash}, changePasswordHandler);
});
Methods
defer.settings(options)
Set default settings for all project
options
db
type: object
default: {}
description: list of your db
defer(dbName, queryHash, defaultValueHash)
method will make function for each key in queryHash
dbName
type: string
description: it's key for db list
queryHash
type: object
description: Storage your queries with templates for new methods
defaultValueHash
type: object
description: Values insert in query before create method