DbQuery
DbQuery TypeScript Class Reference

Overview

The DbQuery class provides methods to execute data queries against the local (on-device) database. This is the preferred approach to work with local relational data. It is used to DbQuery to select, insert, update and delete business objects.
Properties
Methods
Constructor
App Types
There are no properties
Don't instantiate directly instances of the DbQuery class. DbQuery instances are created through a call to the query() method of the BusinessObjectBase assessor. See also BusinessObjectBase.query() method
1
import {DbQuery} from '@dms';
2
import { Customer } from '@dms-bo';
3
...
4
const query = Customer.query();
5
Copied!
📳
Mobile
🌐
Backend

fields(fields: Array<string>): DbQuery<T>

Description
Parameters
Returns
Example
fields method sets the list of the fields to be returned by the executeSelect method of DbQuery. It provides functionality similar to the SELECT clause in a sql statement.
    fields: Array<string> array containing the name of the business object members to be returned by executeSelect method.
DbQuery instance, allowing methods chaining.
1
import {DbQuery} from '@dms'
2
import {Customer} from '@dms-bo'
3
4
const query = Customer.query();
5
6
//define only 3 fields to be returned
7
query.fields(['Id','Name','City']);
8
9
//method chaining - e.g. calling several method on a single line
10
query.fields(['Id','Name','City']).filter("Id='123'");
Copied!

filter(filterExpression: string): DbQuery<T>

Description
Parameters
Returns
Example
Allows the developer to define a filter to be applied, when using executeSelect() method. The filter expects the same syntax as WHERE clause in sql statement.
    filterExpression: string the filter to be applied during a call to executeSelect method
DbQuery instance, allowing method chaining.
1
import {DbQuery} from '@dms'
2
import {Customer} from '@dms-bo'
3
4
const query = Customer.query();
5
6
//equal
7
query.filter("Id=1123'");
8
9
//greater than
10
query.filter("Revenue > 6000");
11
12
//more complex filter with parentess
13
query.filter("((Revenue > 6000 or Revenue=0) and (City='Sofia'))");
14
15
const result = await query.executeSelect();
16
17
//method chaining - e.g. calling several method on a single line
18
const result2 = await query.fields(['Id','Name','City','Revenue']).filter("City='Dubai'and Revenue>1234").executeSelect();
Copied!

limit(maxRecords: number): DbQuery<T>

Description
Parameters
Returns
Example
Allows the developer to limit the number of the records returned by the query.
    max - maximum number of records to be returned by the executeSelect() method
DbQuery instance, allowing method chaining.
1
import {DbQuery} from '@dms'
2
import {Customer} from '@dms-bo'
3
4
const query = Customer.query();
5
6
//limit to 100 records
7
query.max(100);
8
const result = await query.executeSelect();
9
Copied!

page(index: number): DbQuery<T>

Description
Parameters
Returns
Example
Allows the developer to define the page number to be returned in page indexing scenarios. This make sense only if the limit() method was used.
    index - the page index number to be returned by the executeSelect() method
DbQuery instance, allowing method chaining.
1
import {DbQuery} from '@dms'
2
import {Customer} from '@dms-bo'
3
4
const query = Customer.query();
5
6
//limit to 100 records
7
query.max(100);
8
//select second page , e.g. records from 100th to 199th
9
query.page(2);
10
11
const result = await query.executeSelect();
Copied!

group(groupExpression: string): DbQuery<T>

Description
Parameters
Returns
Example
Allows the developer to define a query group expression to be used. This is the same as in the GROUP BY clause in Sql
    groupExpression - the actual group expression to be applied during the execution of the executeSelect() method
DbQuery instance, allowing method chaining.
1
import {DbQuery} from '@dms'
2
import {Customer} from '@dms-bo'
3
4
const query = Customer.query();
5
6
query.group('City, Country');
7
const result = await query.executeSelect();
8
9
//method chaining - e.g. calling several method on a single line
10
const result2 = await query.group('City, Country').executeSelect();
Copied!

aggregate(aggregateExpression: string): DbQuery<T>

Description
Parameters
Returns
Example
Allows the developer to define a aggregate expression to be used. This is the same as in aggregates(max, count, min , avg, ...) in Sql Note that the aggregate() method can NOT be used together with fields() method - e.g. the developer must either fields() or aggregate() methods
    aggregateExpression - the actual aggregate expression to be applied during the execution of the executeSelect() method
DbQuery instance, allowing method chaining.
1
import {DbQuery} from '@dms'
2
import {Customer} from '@dms-bo'
3
4
const query = Customer.query();
5
6
query.aggregate('max(Revenue) as revenue, count(*) as cnt');
7
const result = await query.executeSelect();
8
9
//method chaining - e.g. calling several method on a single line
10
const result2 = await query.group('City, Country').aggregate('max(Revenue) as revenue, count(*) as cnt').executeSelect();
Copied!

sort(sortExpression: string): DbQuery<T>

Description
Parameters
Returns
Example
Allows the developer to define a sort expression to be used. This is the same as in ORDER BY clause in Sql
    sortExpression - the actual sorting expression to be applied during the execution of the executeSelect() method
DbQuery instance, allowing method chaining.
1
import {DbQuery} from '@dms'
2
import {Customer} from '@dms-bo'
3
4
const query = Customer.query();
5
6
query.sort('Revenue asc');
7
const result = await query.executeSelect();
8
9
//method chaining - e.g. calling several method on a single line
10
const result2 = await query.fields(['City','Country']).sort('Revenue asc').executeSelect();
Copied!

executeSelect(): Promise<BusinessObjectBase:T>

Description
Parameters
Returns
Example
executeSelect method performs the actual query execution and returns array of objects, which extends class BusinessObjectBase.
the array of objects, where each object extends class BusinessObjectBase.
1
import {DbQuery} from '@dms'
2
import {Customer} from '@dms-bo'
3
4
const query = Customer.query();
5
6
//fetch all records from the local on-device database
7
const result = await query.executeSelect();
8
9
//iterating over the result set and call the update method for each businessobjectbase instance
10
//which will execute update operation against the local on-device database
11
result.forEach(function(record){
12
const customer = (record as Customer);
13
console.log(`Name:${customer.name},Revenue:${customer.Revenue}`);
14
customer.Revenue += 1000;
15
await customer.update();
16
});
Copied!
Last modified 1yr ago