wire.data.DataModel

Using a data model is a convenient way to share meta data about the structures in a data services across all queries.

Field Properties

The example below sets up a data model on a data source. Alias names for labels and formatting are set up for fields in the data service. This meta data will automatically be applied to all fields in result sets for any query executed using the data source. As such they can automatically be leveraged by any components or other UI elements displaying the result sets.

const model = new wire.data.DataModel({
    Fields: [
        { Entity: "Metrics", Name: "cost_amt", Alias: "Cost", Format: "C2" },     
        { Entity: "Product", Name: "line_no", Alias: "Line" },
        { Entity: "Time", Name: "week_start_date", Alias: "Week", Format: ((value) => {
                return moment(value).format("MM/DD");
            })
        }
    ]
});

const source = new wire.data.DataSource("table", {
    ...
    Model: model
});

Entity Wildcards

Wildcards (*) can be used with a field's Entity property to apply the field model data to any field in any entity containing the field Name.

The example below will result in any field called Sales in any entity to be formatted as currency and with two decimal places.

const model = new wire.data.DataModel({
    Fields: [
        { Entity: "*", Name: "Sales", Format: "C2" }
    ]
});

Calculated Columns

The example below creates a new calculated column called ProfitMargin on the resulting datatable that is returned from any query that contains the entity Metrics. The calculation uses the existing Sales and Cost columns on the table to return the value for the new calculated column.

const model = new wire.data.DataModel({
    Fields: [
        { Entity: "Metrics", Name: "ProfitMargin", Format: "P0", Calc: (row) => {        
            return ((row.Sales-row.Cost)/row.Sales);
        }}             
    ]
});

const source = new wire.data.DataSource("table", {
    ...
    Model: model
});

const query = wire.data.select("Sales", "Cost").from("Metrics");

const table = await source.execAsync(query);

For more examples, visit DataSource DataModels.

Last updated