This schema is used in the API Reference examples - it also shows a selection of interesting schema features:
1import {2 durationSeconds,3 int,4 itemType,5 string,6 timestampMilliseconds,7 type,8 uint,9 uuid,10} from "@stately-cloud/schema";11 12// You can define custom types like this to make your schema more13// readable as well as ensure consistent types. Here we assign a UUID14// type to the MovieId type to ensure that all MovieId fields are UUIDs.15/** The unique ID of a movie */16export const MovieId = type("MovieId", uuid);17// Here we assign a UUID type to the ActorId type to ensure that all18// ActorId fields are UUIDs.19/** The unique ID of an actor */20export const ActorId = type("ActorId", uuid);21 22export const Movie = itemType("Movie", {23 keyPath: [24 // This is the primary key path for the Movie item type, it's25 // globally unique to your Stately store since :id is configured26 // with an initial value.27 "/movie-:id",28 // This key path allows for querying for a movie based on its title29 "/name-:title/movie-:id",30 // This key path allows you to query for a movie based on a31 // genre/year32 "/genres-:genre/years-:year/movie-:id",33 ],34 ttl: {35 source: "fromCreated",36 durationSeconds: 60,37 },38 fields: {39 genre: {40 type: string,41 fieldNum: 1,42 },43 year: {44 type: int,45 fieldNum: 2,46 },47 title: {48 type: string,49 fieldNum: 3,50 },51 id: {52 type: MovieId,53 fieldNum: 4,54 initialValue: "uuid",55 },56 duration: {57 type: durationSeconds,58 fieldNum: 5,59 },60 rating: {61 type: string,62 fieldNum: 6,63 },64 created: {65 type: timestampMilliseconds,66 fieldNum: 7,67 fromMetadata: "createdAtTime",68 },69 updated: {70 type: timestampMilliseconds,71 fieldNum: 8,72 fromMetadata: "lastModifiedAtTime",73 },74 },75});76 77// A character is a role played by an actor in a movie. In this example78// we model that actors can play multiple characters in a movie.79export const Character = itemType("Character", {80 keyPath: [81 // This key path enables queries like: "What characters did actor X82 // play?" or further specify "What characters did actor X play in83 // movie Y?"84 "/actor-:actorId/movie-:movieId/name-:name",85 // This key path enables queries like: "Who played role X in movie86 // Y?" Even more generally: "What characters were in movie Y?"87 "/movie-:movieId/role-:role/name-:name",88 ],89 ttl: {90 source: "fromCreated",91 durationSeconds: 60,92 },93 fields: {94 actorId: {95 type: ActorId,96 fieldNum: 1,97 },98 name: {99 type: string,100 fieldNum: 2,101 },102 role: {103 type: string,104 fieldNum: 3,105 },106 movieId: {107 type: MovieId,108 fieldNum: 4,109 },110 created: {111 type: timestampMilliseconds,112 fieldNum: 5,113 fromMetadata: "createdAtTime",114 },115 updated: {116 type: timestampMilliseconds,117 fieldNum: 8,118 fromMetadata: "lastModifiedAtTime",119 },120 },121});122 123export const Actor = itemType("Actor", {124 keyPath: [125 // This is the primary key path for the Actor item type, it's126 // globally unique in stately since it's using an initial value127 "/actor-:id",128 // This key path allows you to query for all actors with a given129 // name130 "/name-:name/actor-:id",131 ],132 ttl: {133 source: "fromCreated",134 durationSeconds: 60,135 },136 fields: {137 name: {138 type: string,139 fieldNum: 1,140 },141 id: {142 type: ActorId,143 fieldNum: 2,144 initialValue: "uuid",145 },146 created: {147 type: timestampMilliseconds,148 fieldNum: 3,149 fromMetadata: "createdAtTime",150 },151 updated: {152 type: timestampMilliseconds,153 fieldNum: 4,154 fromMetadata: "lastModifiedAtTime",155 },156 },157});158 159// Change is similar to an audit log, this would be used to tracks160// changes to movies.161export const Change = itemType("Change", {162 keyPath: "/movie-:movieId/change-:id",163 ttl: {164 source: "fromCreated",165 durationSeconds: 60,166 },167 fields: {168 id: {169 type: uint,170 fieldNum: 1,171 initialValue: "sequence",172 },173 description: {174 type: string,175 fieldNum: 2,176 },177 field: {178 type: string,179 fieldNum: 3,180 },181 movieId: {182 type: MovieId,183 fieldNum: 4,184 },185 },186});