Skip to content

Example: Movies Schema

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

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