- Find a record by Id
- Find a list of records paginated
- Find a list of records paginated and get the total of items in all pages
Methods
Find by Id
This method allows you to get an item by ID, You will find this method always with the same name of the entityFind a list of records
This method allows you to get a list of items, You will find this method always with the same name of the database tableFind a list of items and count
This is similar to the previous, but this allows us to get a “total” of items in the list. The name of this method is also the same as the previous one but with the “AndCount” suffix.
Parameters
Filtering ($where)
Find a list and find a list and count accept a where parameter. This is a JSON parameter that allows it to be built dynamically.
The next example combines the and, like and the or operators.
Specifies the condition where 1the value is greater than or less than X.
Name: The variable’s name that holds the value to be used. (Should include the type of operator to be applied at the end.)- gt: Greater than.
- gte: Greater than or equal to.
- lt: Less than.
- lte: Less than or equal to.
Notice the use of
$ to specify the property belongs to a different tableNested Relation Queries
When to use this?
When you need to filter data using fields from related tables. For example: search samples by client name.Real Example
Search samples by client name:- required: true → Only records WITH the relation (INNER JOIN)
- required: false → Include records WITHOUT the relation (LEFT JOIN)
$Relation.Field$, you MUST include that relation in queryOptions
Include Filter
The next filter allows to filter in a related table (One to Many)Pagination ($limit, $offset)
All endpoints that return a list of items allow pagination by default. You only need to pass the offset variables in the data source.
If you are configuring a data source for a remote drop-down or data grid component, you only need to pass the variables as follows. The component will handle the right values for these variables.
If you don’t define the
limit variable, the default value will be 100Ordering ($order)
You can define a list of fields that you want to use for ordering
Ordering by Child Properties (Attributes)
To order a query result by a child property (attribute) of an associated model, you can use theorder option in Sequelize. This option takes an array of items, each representing a column to order by and its direction (ascending or descending). Here’s how you can do it:
Query Options ($queryOptions)
By default, all join clauses in Codenull are “LEFT OUTER JOIN”. However you can overwrite this behavior. This parameter is optional.
You can customize the following options in the queryOptions parameter:
| Option | Default Value | |
|---|---|---|
required | When true all joins will be handle as “INNER JOIN” | false |
includeOptions | Allows to set Inner or left join at table level. This is an array with model and required props like this { model: "Role", required: true} | null |
This will return different sets of data depending on how you configure, be aware of the configuration you are defining.
Query Options with multiple relations with the same table
If I have a table with several ties with the same table I should define the model name in the query options with the same name as the auto-generated name by codenull. For instance:Footnotes
- See operator reference below. ↩