Class: PostgresRepository

arpen/repositories/postgres~ PostgresRepository

Repository base class

new PostgresRepository(app, postgres, cacher, util)

Create repository
Parameters:
Name Type Description
app App The application
postgres Postgres Postgres service
cacher Cacher Cacher service
util Util Util service

Members


<static> provides :string

Service name is 'repositories.postgres'
Type:
  • string

<static> requires :Array.<string>

Dependencies as constructor arguments
Type:
  • Array.<string>

Methods


delete(model [, pg])

Delete a model
Parameters:
Name Type Argument Description
model BaseModel | number Model or ID
pg PostgresClient | string <optional>
Will reuse the Postgres client provided, or if it is a string then will connect to this instance of Postgres.
Returns:
Resolves to number of deleted records
Type
Promise

findAll( [pg])

Find all models
Parameters:
Name Type Argument Description
pg PostgresClient | string <optional>
Will reuse the Postgres client provided, or if it is a string then will connect to this instance of Postgres.
Returns:
Resolves to array of models
Type
Promise

find(id [, pg])

Find a model by ID
Parameters:
Name Type Argument Description
id number ID to search by
pg PostgresClient | string <optional>
Will reuse the Postgres client provided, or if it is a string then will connect to this instance of Postgres.
Returns:
Resolves to array of models
Type
Promise

save(model [, pg])

Save model
Parameters:
Name Type Argument Description
model BaseModel The model
pg PostgresClient | string <optional>
Will reuse the Postgres client provided, or if it is a string then will connect to this instance of Postgres.
Returns:
Resolves to record ID
Type
Promise

Search repository
Parameters:
Name Type Argument Description
options object <optional>
Properties
Name Type Argument Default Description
table string <optional>
Table or view name
fields Array.<string> <optional>
Fields to retrieve
where Array.<string> <optional>
SQL WHERE clause: will be joined with 'AND'
params Array <optional>
Bound parameters of WHERE (referenced as $1, $2, ... in SQL)
sort string <optional>
['id asc'] Used in ORDER BY if provided
pageSize number <optional>
0 Used in LIMIT, 0 = all records
pageNumber number <optional>
1 Used in OFFSET
infoOnly boolean <optional>
false Retrieve data rows or not
pg PostgresClient | string <optional>
Will reuse the Postgres client provided, or if string then will connect to this instance of Postgres.
Returns:
Returns promise resolving to the following:
{
     totalRows: 1, // total rows in result
     totalPages: 1, // total number of pages
     pageSize: 0, // page size
     pageNumber: 1, // returned page number
     sort: [ ... ], // keys used to sort the result
     data: [ ... ], // resulting raw SQL rows as an array of models
}
Type
Promise