Add Data to Your Application
A front-end application relies on backend data to function. Once you create your package, define the data model and add some seed data so you can build UI to allow end-users to interact with that data. This topic explains how to create a Persistable Type and add data using JSON and CSV files.
Create a Persistable Type
For an overview of the C3 AI Type system, see the Type System Overview.
To create an entity Type, add a {package}/src/**/{TypeName}.c3typ file in the src directory of your package. The following example defines the WindTurbine Type:
/* examplePackage/src/WindTurbine.c3typ */
entity type WindTurbine {
/**
* The manufacturer of the wind turbine.
*/
manufacturer: string
/**
* The location of the wind turbine.
*/
location: string
}This entity stores instances of WindTurbine in the database.
Add data
Store data files in the data directory of the package. Create a subdirectory that matches the entity name to associate the data with the correct Type. Use JSON or CSV files to provide initial data.
For example, create the following subdirectory WindTurbine in the data dir since the Type is WindTurbine:
examplePackage/data/WindTurbineAdd data using JSON
Use JSON files to create one or multiple instances of an entity.
Single instance
Create a file where the filename becomes the instance ID. The following example creates WindTurbine1:
/* examplePackage/data/WindTurbine/WindTurbine1.json */
{
"type": "WindTurbine",
"location": "Boston",
"manufacturer": "GE"
}This file defines a single turbine with the id of WindTurbine1.
Multiple instances
Define multiple instances in a single JSON file. The following example adds two wind turbines:
/* examplePackage/data/WindTurbine/WindTurbine.json */
[
{
"id": "WindTurbine1",
"location": "Boston",
"manufacturer": "GE"
},
{
"id": "WindTurbine2",
"location": "San Francisco",
"manufacturer": "Vestas"
}
]Both entries will be available in the application. Store this file in examplePackage/data/WindTurbine/WindTurbine.json.
Add data using CSV
Use can also use CSV files to store multiple instances of an entity. The following example creates the same data as the JSON file above:
id,location,manufacturer
WindTurbine1,Boston,GE
WindTurbine2,San Francisco,VestasStore this file in examplePackage/data/WindTurbine/WindTurbine.csv.
Both JSON and CSV formats provide flexibility when defining application data.