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.

const accountService = new wire.data.DataSource("sql", {
    Provider: {
        Server: "query-server"
    }
});

const query = wire.data.select().from("Users");
    
const dataset = new wire.data.DataSet({
    Source: accountService,
    Query: query,
    Write: true
});

const allowWrite = accountService.allow().write;

if(allowWrite) {

    const users = [
        { UserName: "mchermela", Active: true }
    ];

    dataset.write(users);
    
}

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.

...

const query = wire.data.select().from("Users");

const dataset = new wire.data.DataSet({
    Source: accountService,
    Query: query,
    Write: { Entity: ["ActiveUsers"] }
});

Keys

The Keys attribute can be set for data services that need to know the fields that make the data to be updated unique.

...

const dataset = new wire.data.DataSet({
    Source: accountService,
    Query: query,
    Write: { Keys: ["UserName"] }
});

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.

...

const dataset = new wire.data.DataSet({
    Source: accountService,
    Query: query,
    Write: { 
        Data: (rows) => {
            const table = new wire.data.DataTable(rows);
            return table.select("UserName", "Active").rows();
        }
    }
});

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.

...

const dataset = new wire.data.DataSet({
    Source: accountService,
    Query: query,
    Write: { Refresh: true }
});

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.

const accountService = new wire.data.DataSource("sql", {
    Provider: {
        Server: "query-server"
    }
});

...

const dataset = new wire.data.DataSet({
    Source: accountService,
    Query: query,
    Write: { 
        Keys: ["UserName"], 
        Provider: { UpdateOnly: true } 
    }
});

For more on persisting data using datasets, visit the DataSet reference page.

Last updated