jungles-data-postgres
v3.0.0
Published
Data for Jungles using Postgresql
Downloads
9
Readme
Jungles Data Postgresql
API
Setup
var data = require('jungles-data-postgres')({
user: 'james',
password: '1234',
database: 'jungles',
host: '127.0.0.1'
});
data.setup(); // Creates table called instances
Find
var result = data.find(<query object>);
result.many(function (response) {
// Always called
// response: Array (can be empty)
});
result.one(function (response) {
// called when there is atleast one result
// { id, name, path, arrange, sort, ..data.. }
});
result.empty(function () {
// called when result is empty
});
result.error(function (error) {
// error
});
Remove
var result = data.remove(<query object>);
result.success(function () { });
Save (create, update)
// Create
var result = data.save({ type: 'product', parent: 0, name: 'snowboard', arrange: '0' });
result.success(function () {});
These fields are required to create. Extra fields get json stringified and added to the data column.
// Update
var result = data.save({ id: 1, name: 'skateboard' });
result.success(function () {});
Because there is an id it knows it's an update.
Tree
var result = data.tree(<query object>);
result.success(function (tree) { });
Structure
{
id: 1,
name: 'snowboard',
path: '/snowboard',
type: 'product',
sort: '/0',
arrange: 0,
children: [
{
id: 2,
name: 'tags',
path: '/snowboard/tags',
type: 'tags',
sort: '/0/0',
arrange: 0,
children: [
{
id: 3,
name: 'red',
path: '/snowboard/tags/red',
type: 'tag',
sort: '/0/0/0',
arrange: 0,
}
]
}
]
}
Query object examples
{ id: 5}
// SELECT * FROM instances where id = 5;
{ path: /.*/ }
// SELECT * FROM instance WHERE path ~ .*;
{ path: /.*/, id: 5 }
// SELECT * FROM instances WHERE path ~ .* and id = 5;
Test
mocha.js and should.js is needed. Tests are run against a real database so you need to have postgresql. Check specs/db for database settings.
make test