Business object behaviors add business logic to your object. Beyond simple cases like setting defaults or last modified, the logic usually needs to reference and update other information. This page shows you how to do that.
Reading Information
Before we get into all the details, here is an example of reading information:
let filter = {
where: {
supplierId: supplier.id
},
include: {
contacts: {}
}
};
let locations = await Location.find(filter, options);
Here are the highlights:
- filter.where narrows the locations to the ones for a specific supplier
- filter.include pulls the contacts for those locations in as well
- await says to wait for the Location.find operation to complete and then put the results in locations
- options passes the current context (see Using Options in Business Object Behaviors for more details)
Read Methods
Each business object have 5 methods to read information:
Method | Inputs | Outputs | Description |
find | filter | instances | Find a list of instances that match a where filter |
findOne | filter | instance or undefined | Find the first instance that matches a where filter |
findById | id, filter | instance or undefined | Find an instance using its id |
count | where filter | number | Count the number of instances that match a where filter |
exists | where filter | boolean | See if any instances match a where filter |
Remember that all of these methods are available in autocomplete while editing so you don't have to memorize this stuff :).
Filter
The filter is a powerful way to define what information should be read. It is an object that can contain the following keys:
Filter Key | Type | Description |
where | object | Defines the items that should be included (see Where Filter below for more details). If there is no where filter key, all instances will be returned. |
fields | object or array of strings |
Defines the fields to be returned. It can be one of:
If there is no fields filter key, all properties will be returned. |
limit | number | Defines the maximum number of instances to return. If there is no limit, all matching instances are returned. |
offset | number | Defines the number of matching instances to skip (to support pagination). |
include | object | Defines what related instances should be returned along with the matches (see Include Filter below for more details). |
Where Filter
The where filter object is an implicit "and" of conditions (this example finds suppliers with name "Dave" and code "47"):
{ name: 'Dave', code: 47 }
You can specify "or" conditions like this:
{
or: [
{name: 'Dave'},
{name: 'Steve'}
]
}
The conditions in the examples above are implicitly equivalence. You can also use operators like this:
{
weight: {
gt: 1500
}
}
Here is the full list of where filter operators:
Operator Key | Example | Description |
gt |
{ weight: { gt: 1500 }} |
Greater than |
lt |
{ weight: { lt: 1500 }} |
Less than |
gte |
{ weight: { gte: 1500 }} |
Greater than or equal to |
lte |
{ weight: { lte: 1500 }} |
Less than or equal to |
between |
{ weight: { between: [750,1500] }} |
Between |
neq |
{ status: { neq: 'Draft' }} |
Not equal to |
like |
{ name: { like:'%Dave%' }} |
Like |
nlike |
{ name: { nlike:'%Dave%' }} |
Not like |
ilike |
{ name: { ilike:'%dave%' }} |
Case insensitive like |
nilike |
{ name: { nilike:'%dave%' }} |
Case insensitive not like |
inq |
{ status: { inq: ['Draft', 'Published']}} |
In |
ninq |
{ status: { ninq: ['Draft', 'Published']}} |
Not in |
Include Filter
The include filter lets you define related items that should be included in the results. This example reads a location and includes the supplier (a belongs to relationship) and the contacts (a has many relationship):
let filter = {
include: {
supplier:{},
contacts:{}
}
}
let location = await Location.findById(47, filter);
debug('location.supplier.name',location.supplier.name);
debug('location.contacts[0].firstName',location.contacts[0].firstName);
An include is actually a nested filter where you can apply all the filter keys. This example reduces the fields returned for supplier and filters and orders the contacts:
let filter = {
include: {
supplier: {
fields: {code: false}
},
contacts: {
where: {
status: 'Active'
},
order: 'lastName, firstName'
}
}
}
Create Information
Here is a simple example of creating an object:
let supplier = await Supplier.create({ name: 'Dave', code: 47 });
Here are the three different methods to create objects:
Method | Inputs | Outputs | Description |
create | object | instance | Create a single instance |
createAll | array of data | array of instances | Create a set of instances |
createWIthChildren | array of data | array of instances | Create a set of instances with related data |
The last one is super cool because you can create a whole tree of information:
let data = [
{
name: 'WATD',
code: 47,
locations: [
{
name: 'Madison',
contacts: [
{
firstName: 'Dave',
lastName: 'Knapp'
}
]
}
]
}, {
name: 'Nick Co',
code: 74,
locations: [
{
name: 'Detroit',
contacts: [
{
firstName: 'Nick',
lastName: 'Laughton'
}
]
}
]
}
]
Update Information
Updating information is super easy too:
let updatedSupplier = await Supplier.updateById(47, {status: 'Active'}, options);
There are two methods for performing updates:
Method | Inputs | Output | Description |
updateById | id, data | updated instance | Update a single instance |
updateAll | where filter, data | Array of updated instances | Update instances that match the where filter |
Delete Information
Delete is also super easy:
let result = await Supplier.deleteById(47, options);
There are two methods for deleting information:
Method | Inputs | Outputs | Description |
deleteById | id | none | Delete an instance |
deleteAll | where filter | none | Delete instances that match the where filter |