https://github.com/swlkr/acidjs
A simple postgres library
https://github.com/swlkr/acidjs
Last synced: 7 months ago
JSON representation
A simple postgres library
- Host: GitHub
- URL: https://github.com/swlkr/acidjs
- Owner: swlkr
- License: mit
- Created: 2014-09-14T01:52:08.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2015-10-01T04:15:53.000Z (over 10 years ago)
- Last Synced: 2025-06-05T07:01:46.616Z (7 months ago)
- Language: JavaScript
- Homepage:
- Size: 335 KB
- Stars: 2
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# acid.js
_A simple postgres library_
Built on top of [pg](https://github.com/brianc/node-postgres) and [psqljs](https://github.com/swlkr/psqljs)
[](https://travis-ci.org/swlkr/acid)
## Install
```bash
$ npm install --save acidjs
```
## Examples
```js
// Require the module
var acid = require("acidjs")("postgres://postgres:@localhost:5432/database");
```
## Create a table
```bash
$ psql -c "create table users (id bigserial primary key, email text not null, created_at timestamp without time zone default (now() at time zone "utc"));"
```
## Insert a record
```js
var tables = {
users: "users"
};
acid
.insert(tables.users, {email: "test@example.com"});
.then(function(rows) {
/*
rows === [
{
id: "1",
email: "test@example.com",
created_at: ...
}
]
*/
});
```
## Find a record with a where clause
```js
acid
.where(tables.users, "id = $1", "1")
.then(function(rows) {
/*
rows === [
{
id: "1",
email: "test@example.com",
created_at: ...
}
]
*/
});
```
## Update a record
```js
acid
.update(tables.users, {name: "Ryan Dahl"}, "id = $1", "1")
.then(function(rows) {
/*
rows === [
{
id: "1",
name: "Ryan Dahl",
created_at: ...
}
]
*/
})
```
## Delete a record
```js
acid
.delete(tables.users, "id = $1", "1")
.then(function(rows) {
// rows == []
});
```
## Run arbitrary sql
```js
acid
.sql("select * from user_defined_function($1)", ["value"])
.then(function(rows) {
/*
rows === [
{
... whatever data
}
]
*/
})
```
## Tests
```bash
# Set up the database
$ psql -c "create user postgres createdb;"
$ psql -c "create database postgres;"
$ psql -c "create database acidjs;" -U postgres
$ npm test
```