Skip to main content

Angular code samples

Using IDataService

Retrieving data using the IDataService

Inject the IDataService in your components constructor:

constructor(
@Inject('IDataService')
private _ds: wy.IDataService) {
}

Fetching data from the API

async ngOnInit() {
await this.loadUsers();
}

async loadUsers() {
// query users
const users = await this._ds.fetch<HelloWorld.NgLayer.User>('Default', '/Users', {
select: 'Name,Email', // optional field selection, by default all fields are fetched
filter: 'Active eq true', // optional filter, by default no filter is applied
orderby: 'Name', // optional, by default no sorting is applied
top: 100, // optional, by default all entities are requested
expand: '', // optional, expand lookup entities/navigation properties
}).toPromise(); // ⚠️ NOTE: don't forget the toPromise() for async/await

console.log(users);

// fetch one user by primary key
const user = await this._ds.fetchOne<HelloWorld.NgLayer.User>('Default', '/Users', 1).toPromise();
console.log(user);

// fetch first user
const firstUser = await this._ds.fetchFirst<HelloWorld.NgLayer.User>('Default', '/Users').toPromise();
console.log(firstUser);
}

Creating/updating data using the API

private async crudExamples() {
// create new user
const newUser: Partial<HelloWorld.NgLayer.User> = {
Name: 'testuser@test.com',
Active: true,
Email: 'my@email.com',
};

await this._ds.save('Default', '/Users', newUser as wy.BaseEntity, true)
.toPromise();

// update existing user
const existingUser: Partial<HelloWorld.NgLayer.User> = {
Id: 1,
Name: 'testuser@test.com',
Active: true,
Email: 'my@email.com',
};

await this._ds.save('Default', '/Users', existingUser as wy.BaseEntity, false)
.toPromise();

// remove user
await this._ds.remove('Default', '/Users', {
Id: 1
}).toPromise();

// restoring a user from the recycle bin (soft delete mode only)
await this._ds.restore('Default', '/Users', {
Id: 1
}).toPromise();
}

Executing API operations

private async operationExamples() {
// trigger operation (global)
await this._ds.triggerOperation('Default', null, 'ResetAllUsers', null, {
arg1: 123, // provide optional operation arguments
arg2: "test"
}).toPromise();

// trigger operation (on a specific entity)
await this._ds.triggerOperation('Default', '/Users', 'ResetUser', 1, {
arg1: 123, // provide optional operation arguments
arg2: "test"
}).toPromise();
}

Using other services

Check also the following services that are available using DI:

  • IAuthenticationService
  • IBlobService
  • IConstantService
  • IUtilService
  • ILinkService
  • IMessageService
  • ISocialService
  • IInsightsService
  • IQueryListService
  • IWorkflowService
  • IStorageService
  • IUserSettingsService
  • IFeatureService
  • IAnonymousService