Creating DataModel Instances
Basic Instance Creation
To create a DataModel instance, use the following constructor:
const dataModelObject = new DataModel(formattedData);
The constructor expects formatted data from DataModel.loadData()
as its input parameter.
Complete Example
Here's a step-by-step walkthrough of creating and accessing a DataModel instance:
const DataModel = muze.DataModel;
// Define the schema
const schema = [
{
name: "Name",
type: "dimension",
},
{
name: "Maker",
type: "dimension",
},
{
name: "Horsepower",
type: "measure",
defAggFn: "avg",
},
{
name: "Origin",
type: "dimension",
},
];
// Prepare the raw data
const rawData = [
{
Name: "chevrolet chevelle malibu",
Maker: "chevrolet",
Horsepower: 130,
Origin: "USA",
},
{
Name: "buick skylark 320",
Maker: "buick",
Horsepower: 165,
Origin: "USA",
},
{
Name: "datsun pl510",
Maker: "datsun",
Horsepower: 88,
Origin: "Japan",
},
];
// Format the data
const formattedData = await DataModel.loadData(rawData, schema);
// Create the DataModel instance
const dataModelObject = new DataModel(formattedData);
// Access the formatted data and schema
const { data, schema } = dataModelObject.getData();
// Log the internal data structure
console.log(data);
Data Structure
The getData()
method returns an object with two properties:
-
data: A two-dimensional array in column-major order where:
- Each array represents a column of values
- Column order matches the schema field order
- This format optimizes DataModel's internal operations
-
schema: A reference to the schema used to create the instance
- Maintains field definitions and metadata
- Used for data validation and operations
Working with DataModel Instances
Once created, a DataModel instance provides access to:
- The underlying data through
getData()
- Various transformation operations that create new DataModel instances
- The schema information used to interpret the data
Each operation performed on a DataModel instance creates a new instance, maintaining the immutable architecture discussed in the introduction.