4 // Projections
You've read about the concept of Event Sourcing on your second step of the journey. Because the data is stored as a sequence of events, accessing this type of data is not as straightforward as a traditional database. Reading the event stream directly can be very complicated and slow. This is where the Projections service comes in.
The projection service is responsible for providing a view (projection) of the data in your event stream that is optimized for reading. By defining a simple GraphQl-like schema, you can define how events generate the current state of your data. The Projection Service will analyze the events and create APIs for the projection you described using that GraphQl schema.
Example
Let's say you have a simple event stream that contains events like CreateUser
, UpdateUser
, and RemoveUser
.
The CreateUser
event contains the user's id
, name
, and email
.
The UpdateUser
event contains the id
and the fields that were updated.
The RemoveUser
event contains the id
of the user that was removed.
You can define a projection that generates a list of users from these events:
type User
@upsertOn(
on: { topic: "UserTopic", eventTypes: ["CreateUser", "UpdateUser"] }
identifyBy: { payload: ["id"] }
)
@removeOn(
on: { topic: "UserTopic", eventTypes: ["RemoveUser"] }
identifyBy: { payload: ["id"] }
) {
id: ID!
name: String!
email: String!
}
The details of the schema structure and features are described in the Projections Schema Definition documentation.
Using such a schema, the Projections service will then generate the GraphQL APIs for you. These APIs can be used to query data.
Did you know?
While being event driven, Freym uses an SQL database (PostgreSQL) to store the data internally. This brings the best of both worlds: the power of event sourcing and the read performance of SQL databases.
Further Reading Beyond The Freym Journey
Eager to learn more more about schema definitions with GraphQL? Take a sneak peek at the documentation page.
You'll leave The Freym Journey for now if you decide to dive into the service documentation. Of course, you can always come back to continue your journey.