-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathschema.ts
More file actions
82 lines (72 loc) · 1.79 KB
/
schema.ts
File metadata and controls
82 lines (72 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// These data structures define your client-side schema.
// They must be equal to or a subset of the server-side schema.
// Note the "relationships" field, which defines first-class
// relationships between tables.
// See https://github.com/rocicorp/mono/blob/main/apps/zbugs/shared/schema.ts
// for more complex examples, including many-to-many.
import {
createBuilder,
createSchema,
Row,
table,
string,
boolean,
number,
relationships,
json,
} from "@rocicorp/zero";
const message = table("message")
.columns({
id: string(),
senderID: string().from("sender_id"),
mediumID: string().from("medium_id"),
body: string(),
labels: json<string[]>(),
timestamp: number(),
})
.primaryKey("id");
const user = table("user")
.columns({
id: string(),
name: string(),
partner: boolean(),
})
.primaryKey("id");
const medium = table("medium")
.columns({
id: string(),
name: string(),
})
.primaryKey("id");
const messageRelationships = relationships(message, ({ one }) => ({
sender: one({
sourceField: ["senderID"],
destField: ["id"],
destSchema: user,
}),
medium: one({
sourceField: ["mediumID"],
destField: ["id"],
destSchema: medium,
}),
}));
export const schema = createSchema({
tables: [user, medium, message],
relationships: [messageRelationships],
enableLegacyQueries: false,
enableLegacyMutators: false,
});
export const zql = createBuilder(schema);
export type Schema = typeof schema;
export type Message = Row<typeof schema.tables.message>;
export type Medium = Row<typeof schema.tables.medium>;
export type User = Row<typeof schema.tables.user>;
export type AuthData = {
userID: string | null;
};
declare module "@rocicorp/zero" {
interface DefaultTypes {
schema: Schema;
context: AuthData;
}
}