kong.dao

Operates over entities of a given type in a database table.

An instance of this class is to be instantiated for each entity, and can interact with the table representing the entity in the database.

Instantiations of this class are managed by the DAO Factory.

This class provides an abstraction for various databases (PostgreSQL, Cassandra) and is responsible for propagating clustering events related to data invalidation, as well as foreign constraints when the underlying database does not support them (as with Cassandra).

Functions

DAO:count (tbl) Count the number of rows.
DAO:delete (tbl) Delete a row.
DAO:find (tbl) Find a row.
DAO:find_all (tbl) Find all rows.
DAO:find_page (tbl, page_offset, page_size) Find a paginated set of rows.
DAO:insert (tbl, options) Insert a row.
DAO:new (db, model_mt, schema, constraints, events_handler) Instantiate a DAO.
DAO:update (tbl, filter_keys, options) Update a row.

Functions


DAO:count

Count the number of rows. Count the number of rows matching the given values.
Parameters:
  • tbl table (optional) A table containing the fields and values to filter for.
Returns:
  • number count The total count of rows matching the given filter, or total count of rows if no filter was given.
  • table err If an error occurred, a table describing the issue.

DAO:delete

Delete a row. Delete a row in table related to this instance. Also deletes all rows with a relashionship to the deleted row (via foreign key relations). For SQL databases such as PostgreSQL, the underlying implementation leverages "FOREIGN KEY" constraints, but for others such as Cassandra, such operations are executed manually.
Parameters:
  • tbl table A table containing the primary key field(s) for this row.
Returns:
  • table row A table representing the deleted row
  • table err If an error occurred, a table describing the issue.

DAO:find

Find a row. Find a row by its given, mandatory primary key. All other fields are ignored.
Parameters:
  • tbl table A table containing the primary key field(s) for this row.
Returns:
  • table row The row, or nil if none could be found.
  • table err If an error occurred, a table describing the issue.

DAO:find_all

Find all rows. Find all rows in the table, eventually matching the values in the given fields.
Parameters:
  • tbl table (optional) A table containing the fields and values to search for.
Returns:
  • rows An array of rows.
  • table err If an error occurred, a table describing the issue.

DAO:find_page

Find a paginated set of rows. Find a pginated set of rows eventually matching the values in the given fields.
Parameters:
  • tbl table (optional) A table containing the fields and values to filter for.
  • page_offset Offset at which to resume pagination.
  • page_size Size of the page to retrieve (number of rows).
Returns:
  • table rows An array of rows.
  • table err If an error occurred, a table describing the issue.

DAO:insert

Insert a row. Insert a given Lua table as a row in the related table.
Parameters:
  • tbl table Table to insert as a row.
  • options table Options to use for this insertion. (ttl: Time-to-live for this row, in seconds)
Returns:
  • table res A table representing the insert row (with fields created during the insertion).
  • table err If an error occurred, a table describing the issue.

DAO:new

Instantiate a DAO. The DAO Factory is responsible for instantiating DAOs for each entity. This method is only documented for clarity.
Parameters:
  • db An instance of the underlying database object (cassandra_db or postgres_db)
  • model_mt The related model metatable. Such metatables contain, among other things, validation methods.
  • schema The schema of the entity for which this DAO is instantiated. The schema contains crucial information about how to interact with the database (fields type, table name, etc...)
  • constraints A table of constraints built by the DAO Factory. Such constraints are mostly useful for databases without support for foreign keys. SQL databases handle those constraints natively.
  • events_handler Instance of the events propagation class, used to propagate data invalidation events through the cluster.
Returns:
  • self

DAO:update

Update a row. Update a row in the related table. Performe a partial update by default (only fields in tbl will) be updated. If asked, can perform a "full" update, replacing the entire entity (assuming it is valid) with the one specified in tbl at once.
Parameters:
  • tbl table A table containing the new values for this row.
  • filter_keys A table containing the values to select the row to be updated.
  • options table Options to use for this update. (full: performs a full update of the entity).
Returns:
  • table res A table representing the updated entity.
  • table err If an error occurred, a table describing the issue.