Skip to main content
There are several methods that allow fetching data
  • 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 entity

Find 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 table

Find 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.
Type: Specifies the context from which the value will be retrieved. Value: Name of the property that contains the value to be used. The next filter allows to filter in a related table (Many to One)
Notice the use of $ to specify the property belongs to a different table

Nested 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:
Parameters
  • required: true → Only records WITH the relation (INNER JOIN)
  • required: false → Include records WITHOUT the relation (LEFT JOIN)
Important Rule ⚠️ If you filter by $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 limitandlimit and 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 100
If you pass limit=null explicitly then we will ignore the limit option completely. It will return all data from the database (Not recommended). This is causing some edge case issues when using nested column filter notation to details. https://github.com/sequelize/sequelize/issues/10962 https://github.com/sequelize/sequelize/pull/11077 https://github.com/sequelize/sequelize/issues/6400#issuecomment-950544812

Ordering ($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 the order 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:
OptionDefault Value
requiredWhen true all joins will be handle as “INNER JOINfalse
includeOptionsAllows 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.
The next configuration produces a different result.

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

  1. See operator reference below.