pg-model
Takes a relational database structure and returns model objects for noSQL-like abilities.
Install
$ npm install pg-model --save
Usage
const pg = const pgInfo = // Make a new Postgres clientconst client = 'postgres://postgres:postgres@localhost:5432/my_test_db'client //Now get the structure of the 'space' database schema
API
Each model offers the following methods.
jsonData
, options
)
create (Inserts the supplied JSON documents into relational tables.
Resolves to the document's id properties.
Example
modelshrpeople
id
)
findById (Finds one 'document' by ID - all nested docs will be assembled too.
Example
modelshrpeople
options
)
find (Find zero-or-more docs - can be filtered, ordered, paginated etc. Resolves to the found document array.
Example
modelshrpeople
options
)
findOne (Like find
but resolves to a single doc.
Example
modelshrpeople
doc
, options
)
update (Updates a single 'document'. The top-level primary key is inferred from the data - automatically inserts/updates/deletes nested docs.
Example
modelshrpeople
doc
, options
)
patch (Same as update
, but any omitted properties will be retained (i.e. they won't be turned into null
values like update
will).
Example
modelshrpeople
doc
, options
)
upsert (A combination of create
and update
. If a document already exists then upsert
will update it, else it'll create it.
Example
modelshrpeople
id
)
destroyById (Deletes one 'document' by ID - all nested docs will be cascade-deleted too.
Example
modelshrpeople
doc
, options
)
parseDoc (Takes a single doc and parses it into a form that's usable by several of the other methods.
Example
const parsedDoc = modelshrpeople
Testing
Before running these tests, you'll need a test PostgreSQL database available and set a PG_CONNECTION_STRING
environment variable to point to it, for example:
PG_CONNECTION_STRING=postgres://postgres:postgres@localhost:5432/my_test_db
$ npm test