A node framework to create, update and use postgres
We assume that you are using pg
to communicate with the postgres db.
Therefore, when you initialise the manager, you need to pass us the pool or client you want us to query with.
To configure your manager, you will need to build a config object. This will look like
{
table1:{...stuff},
table2:{...stuff},
}
Each table will have a number of columns:
var a = table1:{
columns:{
key:{
dataType:'uuid',
isKey:true
},
name:{
dataType:'varchar',
maxLength:30,
mandatory:true,
unique:false
}
}
}
We can passthrough the dataType value so can in theory support anything in the postgres documentation. However, we only really support a smaller subset of types where we apply validation rules.
So far we support:
- bigint
- boolean
- date
- int
- json
- serial
- smallint
- text
- timestamp (not Z!)
- uuid
- varchar
we also support arrays of all of the above types.
note that for varchars, you pass an extra parameter
{dataType:'varchar', maxLength:30}
;
additionally, for arrays:
{dataType:'array', arrayContent:'int'}
//OR
{
dataType:'array',
arrayContent: {
dataType: 'varchar',
maxLength: 30
}
};
Inserts a row (or rows) into the db
db.insert('people', [{id:1, name:'A'}], cb)
Supports an options argument. Currently the only supported options are:
ignoreConflicts
- if true, will ignore conflicts and return the conflicting resource (a bit like getOrInsert)
db.insertWithOpts('people', [{id:1, name:'A'}], {ignoreConflicts:true}, cb)
Supports a prep
function to apply to the data before it is returned
db.insertAndPrep('people', [{id:1, name:'A'}], data => data[0], cb)
This runs a bulk update, but only ever on a single table. If you want to update an object
and its children, then use updateById
.
db.updateBulk('mytable',{_filter:{id:123}, data:{newCol:'newVal'}}, cb)
Options:
data
- an object with the data you want to update on the nodes_filter
- an object defining which nodes you want to updatecolumns
- which columns you want to update (only required in strict mode)strict
- is the update in strict mode?
Note - updates run in parallel, so don't return a response object
db.updateById('mytable',{newCol:'newVal', id:123}, cb);
Ignores any subtables
db.updateByIdShallow('mytable',{newCol:'newVal', id:123}, cb);
db.get('people', { _filter: { id: 234 } }, cb);
Options:
_filter
- an object defining which nodes you want to retrieveshallow
- if true, it won't get any 'child' records - i.e. it will only hit one tableindex
- if true, it will return the objects as a dictionary using the IDs, rather than as an array.
db.getById('people',{id: 123}, cb);
Options:
id
- the id of the recordshallow
- if true, it won't get any 'child' records - i.e. it will only hit one table
Supports a prep
function to apply to the data before it is returned
db.getAndPrep('people', { _filter: { id: 234 } }, data => data[0], cb);
db.delete('people', { _filter: { id: 123 } }, cb);
_filter
- an object defining which nodes you want to deletehard
- should the row be saved in a 'deleted' table somewhere? (defaults to false)shallow
- should child rows also be deleted
db.deleteById('people', { id: seedId }, cb);
id
- a single id to deleteids
- an array of ids to deletehard
- should the row be saved in a 'deleted' table somewhere? (defaults to false)shallow
- should child rows also be deleted
Issues your query 'blindly'
Creates all the tables specified in your config
Removes all the data from your tables
Drops all the tables from your database
Stores (as an array) the strings exactly as they are sent to the database
Clears the query log