Initializing a Data Model
Viewing typescript
switch to python
Using the CLI
To create a new Data Model in your Moose application, use the data-model init
command. This will generate a file in the /datamodels
directory of your Moose application and define your Data Model within it.
Terminal
npx moose-cli data-model init --sample <PATH_TO_SAMPLE_DATA> <DATA_MODEL_NAME>
Terminal
moose-cli data-model init --sample <PATH_TO_SAMPLE_DATA> <DATA_MODEL_NAME>
<PATH_TO_SAMPLE_DATA>
: The path to the sample data file.<DATA_MODEL_NAME>
: The name of the Data Model.
Automatically Infer Data Model
If you have a .json
or .csv
file with sample data representing the structure of the data you want to ingest, you can use the --sample
flag to specify the path to that file. The CLI will then infer the Data Model schema from the provided file and create the Data Model for you.
Creating a new Data Model Manually
There are two additional ways to initialize a new Data Model in your Moose project:
- By creating a new
.ts
.py
file in the/datamodels
directory of your Moose application and defining a new Data Model inside this file. - By appending a new Data Model to an existing
.ts
.py
file in your/datamodels
directory.
datamodels/myDataModel.ts
import { Key } from "moose-lib";
export interface MyNewDataModel {
primaryKey: Key<string>;
someString: string;
someNumber: number;
someBoolean: boolean;
someDate: Date;
someArray: string[];
...
}
datamodels/new_data_model.py
from moose_lib import Key, moose_data_model
from dataclasses import dataclass
@moose_data_model
@dataclass
class NewDataModel:
primary_key: Key[str]
some_string: str
some_number: int
some_boolean: bool
some_list: list[str]