Quick Reference

Quick Reference

This is a quick guide on using MooseJS with all the basic code in one place. For more details you can visit individual pages on each section.


Creating a Model

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

datamodels/models.ts
import { Key } from "@514labs/moose-lib";
 
export interface UserActivity {
  eventId: Key<string>;
  timestamp: string;
  userId: string;
  activity: string;
}

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
ℹ️

Disclaimer: All Typescript number types are mapped to Float64

Configuring Data Models

To set the configuration for a data model, just export a configuration object from the model file with the data model name as a prefix and config as the suffix, like so:

UserActivity.ts
import { Key, DataModelConfig, IngestionFormat } from "@514labs/moose-lib";
 
// <modelName>Config
export const UserActivityConfig: DataModelConfig<UserActivity> = {
  ingestion: {
    format: "json",
  },
  storage: {
    enabled: true,
    order_by_fields: ["timestamp"],
  },
};
 
export interface UserActivity {
  eventId: Key<string>;
  timestamp: string;
  userId: string;
  activity: string;
}

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

Ingesting via API Endpoints

MooseJS will automatically generate ingestion points for you to send data to.The default port is 4000. The standard endpoint for each data model is:

http://localhost:4000/ingest/<DataModelName>

Following is some example code for testing the API endpoints for a Data Model called UserActivity. In the Moose developer console, when you view your Data Model, go to the Setup tab, and you'll find sample code for utilizing the API Endpoints for your specific models.

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

You can also ingest data using the MooseJS SDK. To learn more about the different ways to ingest data refer to the Ingesting Data page.


For more information refer to the Data Models Usage page.