Initialize a Streaming Function
Viewing typescript
switch to python
Using the CLI
Initialize your Streaming Function with the moose function init
CLI command:
npx moose-cli function init --source <SOURCE_DATA_MODEL> --destination <DESTINATION_DATA_MODEL>
moose-cli function init --source <SOURCE_DATA_MODEL> --destination <DESTINATION_DATA_MODEL>
This command generates a new Streaming Function file in the /functions
directory of your Moose app according to the file structure described previously.
- SourceDataModel__DestinationDataModel.py
SOURCE_DATA_MODEL
and DESTINATION_DATA_MODEL
must be valid data models
that exist in your /datamodels
directory.
You can find a list of your existing data models by running moose-cli ls
.
Manual Initialization
You can alternatively create a new Streaming Function file manually in the /functions
directory of your Moose app. The file name should be in the format SourceDataModel__DestinationDataModel.ts
SourceDataModel__DestinationDataModel.py
.
Inside the file, define the function as follows:
import { SourceDataModel } from "../datamodels/path/to/SourceDataModel";
import { DestinationDataModel } from "../datamodels/path/to/DestinationDataModel";
export default function functionName(
source: SourceDataModel,
): DestinationDataModel[] | DestinationDataModel | null {
// Transformation logic
}
from app.datamodels.path.to.SourceDataModel import SourceDataModel
from app.datamodels.path.to.DestinationDataModel import DestinationDataModel
from moose_lib import StreamingFunction
def functionName(source: SourceDataModel) -> DestinationDataModel | list[DestinationDataModel] | None:
# Transformation logic
my_streaming_function = StreamingFunction(run=functionName)
- Ensure you import the source and destination Data Models from the correct paths within your application.
- Replace
functionName
with a descriptive name that clearly reflects the function's purpose. - Substitute
SourceDataModel
andDestinationDataModel
with the actual names of your Data Models.
- The default export is crucial because Moose uses it to identify the entry point of the Streaming Function.
- Assign your function to the
run
parameter in theStreamingFunction
class to designate it as the entry point.
Next Steps
With your Streaming Function initialized, you can now start defining the transformation logic to process data from the source Data Model to the destination Data Model. We'll cover this in more detail in the next section.