Writing Data
The process of persisting data in the data service using a dataset is similar to the one used for the same operation using a data source.
To turn on write-back functionality for the dataset, set the Write attribute on the dataset's configuration to true.
To check whether a data service supports write-back, check the allow.write attribute on the data source. Use the write method to send the data to be persisted.
1
const accountService = new wire.data.DataSource("sql", {
2
Provider: {
3
Server: "query-server"
4
}
5
});
6
7
const query = wire.data.select().from("Users");
8
9
const dataset = new wire.data.DataSet({
10
Source: accountService,
11
Query: query,
12
Write: true
13
});
14
15
const allowWrite = accountService.allow().write;
16
17
if(allowWrite) {
18
19
const users = [
20
{ UserName: "mchermela", Active: true }
21
];
22
23
dataset.write(users);
24
25
}
Copied!
The Write attribute can also be set to allow optional configuration. These options are available for service providers that can support them. See the service provider documentation for details on write-back options support.

Entity

By default, the write call to the data service will use the entity specified in the select statement's from clause. Depending on the service provider, this could translate into the name of the method called on the data service, the name of a table in a relational database, or another structure that represents the update target.
To specify a different entity, use the Entity attribute. In the example below, the entity used in the call will be ActiveUsers instead of Users.
1
...
2
3
const query = wire.data.select().from("Users");
4
5
const dataset = new wire.data.DataSet({
6
Source: accountService,
7
Query: query,
8
Write: { Entity: ["ActiveUsers"] }
9
});
Copied!

Keys

The Keys attribute can be set for data services that need to know the fields that make the data to be updated unique.
1
...
2
3
const dataset = new wire.data.DataSet({
4
Source: accountService,
5
Query: query,
6
Write: { Keys: ["UserName"] }
7
});
Copied!

Data

The Data attribute is a transformation hook to change the data that is sent to the server to be persisted.
In the example below, the data is trimmed to only include the UserName and Active fields using a data table. See Working with DataTables for more on data tables.
1
...
2
3
const dataset = new wire.data.DataSet({
4
Source: accountService,
5
Query: query,
6
Write: {
7
Data: (rows) => {
8
const table = new wire.data.DataTable(rows);
9
return table.select("UserName", "Active").rows();
10
}
11
}
12
});
13
Copied!

Refresh

By default, the dataset will not refresh its data after a write operation. Use the Refresh option to force a refresh of the data from the data service after the write operation. Note, this will update any of the dataset's data promises sending out notifications to all consumers waiting for changes.
1
...
2
3
const dataset = new wire.data.DataSet({
4
Source: accountService,
5
Query: query,
6
Write: { Refresh: true }
7
});
Copied!

Provider

Service provider specific options can be set up using the Provider attribute. These options are used by data source service providers to allow consumers to control persistence behavior for write operations by a data service.
In the example below, the WireBootstrap Query Service is being told not to add any new records using the service provider's UpdateOnly option. Only records that currently have a UserName value will be updated.
1
const accountService = new wire.data.DataSource("table", {
2
Provider: {
3
Server: "query-server"
4
}
5
});
6
7
...
8
9
const dataset = new wire.data.DataSet({
10
Source: accountService,
11
Query: query,
12
Write: {
13
Keys: ["UserName"],
14
Provider: { UpdateOnly: true }
15
}
16
});
Copied!
For more on persisting data using datasets, visit the DataSet reference page.