postgres-repo

1.2.0 • Public • Published

postgres-repo

Build Status NPM version

A light, promise-based alternative to a full ORM that simply wraps a repository pattern around basic PostgreSQL calls.

Installation

$ npm install postgres-repo

Usage

Create a new repo by providing the database connection URL and a table name. This will use the built-in connection pools setup on the global pg object.

var PostgresRepo = require('postgres-repo');

var users = new PostgresRepo(DATABASE_URL, 'user');

You can also specify a different primary key (default is simply id);

var users = new PostgresRepo(client, 'users', 'user_id');

Get a row from the table by its primary key

users.get(id).then(function(user) { ... });

Get an array of all of the items in the table

users.getAll().then(function(users) { ... });

Add a new row to the table. The returned representation will be what is stored, including any default values set during the INSERT command.

var user = { email: 'billy@awesome.net', name: 'Billy' };
users.add().then(function(user) { ... });

Remove a row that correlates to some object representation. This checks the identity primary key (e.g, id) to execute the DELETE command.

users.remove(user).then(function() { ... });

Update a row that correlates to some object representation. This checks the identity primary key and executes an UPDATE command.

user.name = 'Bob';
users.update(user).then(function(user) { ... });

Execute a query with parametric values (automatically escaped appropriately by the underlying pg driver)

users.query('select * from user where points > @points and group = @group',
  { points: 100, group: 'ballers' })
    .then(function(users) { ... });

Testing

$ npm test

License

MIT

Package Sidebar

Install

npm i postgres-repo

Weekly Downloads

0

Version

1.2.0

License

MIT

Last publish

Collaborators

  • bvalosek