Developing your App
Primitive: Data Models
Using Data Models

Creating a Model

To get started you simply need to open the models.ts file in your project's datamodels directory and add a new Data Model definition.

datamodels/models.ts
type Key<T extends string | number> = T;
 
export interface UserActivity {
  eventId: Key<string>;
  timestamp: string;
  userId: string;
  activity: string;
}

Data Model conventions

As you can see, the data model is very simple. It's just a list of fields and their types. MooseJS's data modeling is integrated with the data modeling capabilities of your language of choice. For now that's Typescript Interfaces but Python is in the works, leveraging Data classes. We don't currently support all of Typescript schema modeling features, but we're working on it.

Key<T>

The Key<T> type is a MooseJS-specific type that allows you to define a primary key for your data model. This is a required field for all data models. It can be added to multiple columns if you have a composite key.

We are planning to add a moose helper package that will allow you to import the Key type from it. Right now, you have to defined it in your project. It doesn't matter what the type of the key is.

When we are provioning the OLAP storage table, we will use the Key type to define the primary key of the table. What a primary key means in that database will depend on the database you are using. For Clickhouse, it will be the primary key (opens in a new tab) of the table.

Files

Data Models can be defined in any file with a .ts extension in your project's datamodels directory. The name of the file is not important. You may also define multiple models in the same Typescript file.

Supported Types

Here is the status of MooseJS support for various data types in Data Models:

ClickhouseTypescriptMoose
StringString
BooleanBoolean
Int64Number
Int256BigInt
Float64Number
DecimalNumber
DateTimeDate
JsonObject
Bytesbytes
EnumEnum
ArrayArray
nullablenullable

Inspecting Your Model

Once you've created your model, you can inspect it along with the infrastructure that MooseJS automatically generated for you by navigating to the console at http://localhost:3001 (opens in a new tab).

Here you fill find a list of all your Data Models. You can click into any Data Model to see details about it, to get information about the APIs/SDKs for that Data Model, and to directly query the data currently stored in the Data Model (technically, the data stored in the database table associated with that Data Model).

Data Model Console

Test Your Model

When you create a model, MooseJS automatically creates an ingestion point to facilitate immediate data testing. Send a sample payload to this endpoint using:

curl -X POST \
  -H "Content-Type: application/json" \
  -d '{"eventId": "1234567890", "timestamp": "2019-01-01 00:00:01", "userId": "123456", "activityType":"click"}' \
  http://localhost:4000/ingest/UserActivity

Next steps

You're all set to start sending data into your Moose application from upstream sources, and consuming into downstream sources. Check out the next two sections of the docs for more details on Ingesting and Consuming Data. To understand the impact of changes to your Data Model, refer to the Data Change Management section of the docs.