Welcome to docs|Projections|Creating projection definitions

Creating projection definitions

This page describes how to create projection definitions in Serialized.

Projections are created by configuring and saving a Projection Definition. You can create as many projection definitions as you want. Each projection definition must have a unique name so that no two projection definitions will write to the same data keys.

Creating a projection definition

Creating a projection definition is done either via our client SDKs or via the API directly. The SDKs help you define the projection definition and its handlers in a type-safe way, but if you prefer to use the API directly you can do that as well.

Creating a projection definition is a strict consistent operation. If the create operation is successful, and you try to create a projection definition with the same name again, you will get an error.

// Create the projection definitionvar definition = singleProjection("orders-by-id")        .feed("order")        .addHandler(newHandler("OrderPlaced")            .addFunction(Functions.merge().build())            .build())        .build()projectionClient.createDefinition(definition); // Create the projection definitionprojectionClient.createDefinition(definition); // Will fail the second time
const definition = {  feedName: 'order',  projectionName: 'orders-by-id',  handlers: [    {      eventType: 'OrderPlaced',      functions: [ { function: 'merge', } ],    }  ]}await projectionsClient.create(definition) // Create the projection definitionawait projectionsClient.create(definition) // Will fail the second time

After your projection definition has been created, you can view it in the Console. You can also view the projection definition in the API or getting it using the SDK.

In the example above, a projection definition called orders-by-id was created from the order feed. When there are orders placed, projections will be created with the aggregate ID as the key. Since the merge function was used, all data from the event will be merged into the projection.

Further reading