Project Name | Stars | Downloads | Repos Using This | Packages Using This | Most Recent Commit | Total Releases | Latest Release | Open Issues | License | Language |
---|---|---|---|---|---|---|---|---|---|---|
Gocmdpev | 768 | 3 years ago | 2 | mit | Go | |||||
A command-line GO Postgres query visualizer, heavily inspired by pev (https://github.com/AlexTatiyants/pev) | ||||||||||
Vipsql | 161 | 4 years ago | Vim script | |||||||
A vim-frontend for interacting with psql | ||||||||||
Postgresql Ocaml | 137 | 19 days ago | 7 | other | OCaml | |||||
OCaml-bindings for the PostgreSQL database | ||||||||||
Pgocaml | 133 | 2 years ago | 41 | other | OCaml | |||||
PG'OCaml provides an interface to PostgreSQL databases for OCaml applications. It uses Camlp4 to extend the OCaml syntax, enabling one to directly embed SQL statements inside the OCaml code. | ||||||||||
Pgsql Postal | 110 | 2 years ago | mit | C | ||||||
PostgreSQL binding for libpostal | ||||||||||
Dpq2 | 59 | 2 | a month ago | 138 | May 17, 2022 | 13 | bsl-1.0 | D | ||
This is yet another attempt to create a good interface to PostgreSQL for the D programming language. | ||||||||||
Node Postgres Pure | 55 | 164 | 78 | 8 years ago | 30 | December 13, 2014 | 3 | JavaScript | ||
node-postgres without any of the C/C++ stuff | ||||||||||
Pg Bricks | 50 | 5 | 4 | a year ago | 12 | August 07, 2017 | bsd-2-clause | JavaScript | ||
Higher level PostgreSQL client for Node.js | ||||||||||
Ext Pq | 34 | a month ago | 1 | bsd-2-clause | C | |||||
PostgreSQL client library (libpq) binding | ||||||||||
Hapi Node Postgres | 32 | 10 | 2 | 5 years ago | 14 | January 18, 2017 | 3 | mit | JavaScript | |
:package: Wrap hapi requests with a pg connection |
This extremely small, zero-dependency tool helps you create PostgreSQL queries easier by using objects and automated objects to values bindings. Forget the dollar sign $1
- use pg-bind.
Jump to:
Install dependency:
npm install --save pg-bind
Require library in your code:
const bind = require('pg-bind');
const query = bind('SELECT * FROM table WHERE age = :age', {age: 1});
With pg-bind
you can do that:
const bind = require('pg-bind');
const obj = {a: 1, b: 2, c: 3};
const query = bind('SELECT :a, :b, :c', obj); // Result is gonna be the same
// Query: {text: 'SELECT $1, $2, $3', values: [1, 2, 3]}
And if query gets even more complicated (and it usually is):
// How would you count binding here? Aha!
const bind = require('pg-bind');
const query = `
WITH prepared_statement AS (
SELECT :blah, col1
FROM some_table
WHERE col2 = :kek
)
SELECT :colOne
FROM prepared_statement
`;
const prepared = bind(query, {
blah: 'blah',
kek: 'lol',
colOne: 'one'
});
As you can see binding of queries is easy and queries themselves are more readable than with $ (dollar sign) bindings.
If you ever wanted to insert multiple rows with one INSERT statement and concatenated strings (or joined an array of strings) for that - I'm deeply sorry.
With pg-bind
you can do bind.insert(query, arrayOfObjects)
:
// We've got an insert list somewhere
const employees = [
{name: 'lol', age: 228, role: 'loler'},
{name: 'kek', age: 1488, role: 'keker'}
];
const insertQuery = bind.insert(`
INSERT INTO some_table (group_id, name, age, role, whatever)
VALUES (1, :name, :age, :role, 'whatever')
`, employees);
// insertQuery will be:
// {
// text: `
// INSERT INTO some_table (group_id, name, age, role, whatever)
// VALUES (1, $1, $2, $3, 'whatever'),
// (1, $4, $5, $6, 'whatever')`,
// values: ['lol', 228, 'loler', 'kek', 1488, 'keker']
// }
Is it hard? Nope. Is it hard to do it on your own? Probably.
bind(query: String, values: Object) -> {text: String, values: Array}
Create query object supported by node-postgres with text and values properties.
:param
).null
is put instead.bind()
, an Error is thrown.Examples:
const bind = require('pg-bind');
bind(`SELECT * FROM table WHERE col = :colVal`, {colVal: 'sammy'}]);
// Result: {
// text: `SELECT * FROM table WHERE col = $1`
// values: ['sammy']
// }
bind('UPDATE TABLE somethings SET thing = :thing WHERE some = :some', {some: 'kek', thing: 'lol'});
// Result: {
// text: 'UPDATE TABLE somethings SET thing = $1 WHERE some = $2',
// values: ['lol', 'kek']
// }
bind.insert(query: String, values: Object[]) -> {text: String, values: Array}
Alias: bind.bindInsert
Create multiple/single insert query from pattern.
VALUES (...)
block, as it is used to create statements.values
is not an Array but an Object -> bind()
is used. So it's pretty safe on single inserts.Examples:
const bind = require('pg-bind');
bind.insert(`INSERT INTO user_roles (user_id, role) VALUES (:userId, 'default')`, [{userId: 1}, {userId: 2}]);
// Result: {
// text: `INSERT INTO user_roles (user_id, role) VALUES ($1, 'default'), ($2, 'default')`
// values: [1, 2]
// }
To generate documentation (and open it) run:
npm run docs # just generate docs
npm run docs && open docs/index.html # generate and open docs in default application for HTML ext
To generate coverage report (and open it) run:
npm run coverage # generate report
npm run coverare && open coverage/index.html # generate and open in default application for HTML ext
To run tests type:
npm test