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 */
export 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 */
export const ActorId = type("ActorId", uuid);
export const Movie = 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,
fieldNum: 1,
},
year: {
type: int,
fieldNum: 2,
},
title: {
type: string,
fieldNum: 3,
},
id: {
type: MovieId,
fieldNum: 4,
initialValue: "uuid",
},
duration: {
type: durationSeconds,
fieldNum: 5,
},
rating: {
type: string,
fieldNum: 6,
},
created: {
type: timestampMilliseconds,
fieldNum: 7,
fromMetadata: "createdAtTime",
},
updated: {
type: timestampMilliseconds,
fieldNum: 8,
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.
export const Character = 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,
fieldNum: 1,
},
name: {
type: string,
fieldNum: 2,
},
role: {
type: string,
fieldNum: 3,
},
movieId: {
type: MovieId,
fieldNum: 4,
},
created: {
type: timestampMilliseconds,
fieldNum: 5,
fromMetadata: "createdAtTime",
},
updated: {
type: timestampMilliseconds,
fieldNum: 8,
fromMetadata: "lastModifiedAtTime",
},
},
});
export const Actor = 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,
fieldNum: 1,
},
id: {
type: ActorId,
fieldNum: 2,
initialValue: "uuid",
},
created: {
type: timestampMilliseconds,
fieldNum: 3,
fromMetadata: "createdAtTime",
},
updated: {
type: timestampMilliseconds,
fieldNum: 4,
fromMetadata: "lastModifiedAtTime",
},
},
});
// Change is similar to an audit log, this would be used to tracks
// changes to movies.
export const Change = itemType("Change", {
keyPath: "/movie-:movieId/change-:id",
ttl: {
source: "fromCreated",
durationSeconds: 60,
},
fields: {
id: {
type: uint,
fieldNum: 1,
initialValue: "sequence",
},
description: {
type: string,
fieldNum: 2,
},
field: {
type: string,
fieldNum: 3,
},
movieId: {
type: MovieId,
fieldNum: 4,
},
},
});