Skip to content

Example: Movies Schema

This schema is used in the API Reference examples - it also shows a selection of interesting schema features:

import {
durationSeconds,
int,
itemType,
string,
timestampMilliseconds,
type,
uint,
uuid,
} from "@stately-cloud/schema";
// You can define custom types like this to make your schema more
// readable as well as ensure consistent types. Here we assign a UUID
// type to the MovieId type to ensure that all MovieId fields are UUIDs.
/** The unique ID of a movie */
const MovieId = type("MovieId", uuid);
// Here we assign a UUID type to the ActorId type to ensure that all
// ActorId fields are UUIDs.
/** The unique ID of an actor */
const ActorId = type("ActorId", uuid);
itemType("Movie", {
keyPath: [
// This is the primary key path for the Movie item type, it's
// globally unique to your Stately store since :id is configured
// with an initial value.
"/movie-:id",
// This key path allows for querying for a movie based on its title
"/name-:title/movie-:id",
// This key path allows you to query for a movie based on a
// genre/year
"/genres-:genre/years-:year/movie-:id",
],
ttl: {
source: "fromCreated",
durationSeconds: 60,
},
fields: {
genre: { type: string },
year: { type: int },
title: { type: string },
id: { type: MovieId, initialValue: "uuid" },
duration: { type: durationSeconds },
rating: { type: string },
created: {
type: timestampMilliseconds,
fromMetadata: "createdAtTime",
},
updated: {
type: timestampMilliseconds,
fromMetadata: "lastModifiedAtTime",
},
},
});
/**
* A character is a role played by an actor in a movie. In this example
* we model that actors can play multiple characters in a movie.
*/
itemType("Character", {
keyPath: [
// This key path enables queries like: "What characters did actor X
// play?" or further specify "What characters did actor X play in
// movie Y?"
"/actor-:actorId/movie-:movieId/name-:name",
// This key path enables queries like: "Who played role X in movie
// Y?" Even more generally: "What characters were in movie Y?"
"/movie-:movieId/role-:role/name-:name",
],
ttl: {
source: "fromCreated",
durationSeconds: 60,
},
fields: {
actorId: { type: ActorId },
name: { type: string },
role: { type: string },
movieId: { type: MovieId },
created: {
type: timestampMilliseconds,
fromMetadata: "createdAtTime",
},
updated: {
type: timestampMilliseconds,
fromMetadata: "lastModifiedAtTime",
},
},
});
itemType("Actor", {
keyPath: [
// This is the primary key path for the Actor item type, it's
// globally unique in stately since it's using an initial value
"/actor-:id",
// This key path allows you to query for all actors with a given
// name
"/name-:name/actor-:id",
],
ttl: {
source: "fromCreated",
durationSeconds: 60,
},
fields: {
name: { type: string },
id: { type: ActorId, initialValue: "uuid" },
created: {
type: timestampMilliseconds,
fromMetadata: "createdAtTime",
},
updated: {
type: timestampMilliseconds,
fromMetadata: "lastModifiedAtTime",
},
},
});
/**
* Change is similar to an audit log, this would be used to tracks
* changes to movies.
*/
itemType("Change", {
keyPath: "/movie-:movieId/change-:id",
ttl: {
source: "fromCreated",
durationSeconds: 60,
},
fields: {
id: { type: uint, initialValue: "sequence" },
description: { type: string },
field: { type: string },
movieId: { type: MovieId },
},
});