SqlQueryService

SqlQueryService TypeScript Class Reference

Overview

SERVICE This class is a "service" and it is automatically injected if declared as a member in the constructor of the consuming class. - See the constructor section below for details - See the Automatic Instance Injection page for details on the topic

The SqlQueryService class provides methods to execute raw sql queries against the local (on-device) database.

Properties
Methods
Constructor
App Types
Properties

No properties

Methods
Constructor

Don't instantiate directly instances of the SqlQueryServiceclass.

SqlQueryService instances are automatically injected by Dynamics Mobile framework.

import { View, SqlQueryService } from '$dms';
import { Customer } from '$dms-bo';
@View()
export class MyView {
constructor(
//we need to only declare the member
private SqlQueryService: SqlQueryService
){
}
async load(){
//we can now use the injected instance
let value = await this.SqlQueryService.execute(`select * from ${Customer.boTableName}}`);
}
}
App Types

📳 Mobile

Avoid using SqlQueryService directly as it uses raw (low-level) sql query and does not return business entities. Use DbQuery class for more cases. See also BusinessObjectBase class

execute(sql: string): Promise<Array<any>>

Description
Parameters
Returns
Example
Description

execute method performs query execution of the sql statement provided as argument against the local on-device database and returns array of flat typescript objects.

Parameters

Returns
  • array of "flat" javascript objects, where each javascript object represent the affected records , when the sql parameter represents "select" sql clause. If the sql parameter represents sql clause other than "select", the method returns undefined.

Example

mport { SqlQueryService } from '@dms';
import { Customer } from '@dms-bo';
...
var records = await this.SqlQueryService.execute(`select * from ${Customer.boTableName}`);
records.forEach((record)=>{
console.log(`Customer name is ${records.name}`);
});

How to use

import { View, SqlQueryService } from '@dms';
import { Customer } from '@dms-bo';
// it is supposed that there is defined business object Customer in the Business Object folder
@View()
export class MyView {
constructor(private SqlQueryService: SqlQueryService){
}
async load(){
var records = await this.SqlQueryService.execute(`select * from ${Customer.boTableName}`);
records.forEach(function(records){
console.log(`Customer name is ${record.name}`);
});
}
}