Skip to content

Data

The data module provides a simple interface to the underlying Mongo database. It is meant for basic tasks. See the Mongo module if you require more advanced usage.

Client-Side Data

Most data handling can be handled directly on the client-side, without the need to create a server-side api. See the client-side the Data module.

core.data

To perform any of the object methods, you need a reference to the data module.

core.data(source_name[, db_name])

Parameters

Name Description Type Required
source_name The source name to use for the objects. String Y
db_name Set a specific database. Default: "_app" String N

Returns

A source object, or nil and an error.

Example

local users = core.data("users")

get

Get a data object from the Mongo database.

<source>:get(idOrQuery)

Parameters

Name Description Type Required
idOrQuery A string id or query table. String or Table Y

Example

local obj, err = users:get("id1234")

Tip

To retrieve multiple data objects, see the getPage method.

save

Create a new, or update an existing, data object and save it in the Mongo database.

<source>:save(obj)

Parameters

Name Description Type Required
obj A data object to save. Table Y

Example

local id, err = users:save(obj)

delete

Delete a data object from the Mongo database.

<source>:delete(idOrObj)

Parameters

Name Description Type Required
idOrObj An object id string or data object with _id field. String or Table Y

Example

local success, err = users:delete(obj)

Pagination

getPage

Get multiple data objects from the Mongo database based on a specific criteria.

<source>:getPage(page, perPage, sort, query)

Parameters

Name Description Default Type Required
page The page number to return. none Number Y
perPage The number of objects per page. 20 Number N
sort Sort constant or sorting table. core.ASC Const or Table N
query Specialized query table. { } (all records) Table N

Sort Constants

Enum Description
core.ASC Sort in an ascending order.
core.DESC Sort in a descending order.

Example

local objects = users:getPage({}, 1, 10)