/
models.ts
79 lines (66 loc) · 1.63 KB
/
models.ts
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
import { Table } from "../../main/dynamo/Table"
export const table = new Table({
name: "TestTable",
partitionKeyName: "pk",
sortKeyName: "sk",
})
export enum ModelType {
Musician = "musician",
Song = "song",
}
export interface Musician {
model: ModelType.Musician
id: string
name: string
details: {
description?: string
}
}
export interface Song {
model: ModelType.Song
musicianId: string
id: string
title: string
mp3: Buffer
}
export const MusicianModel = table
.model<Musician>(ModelType.Musician)
.partitionKey(ModelType.Musician, "id")
.sortKey(ModelType.Musician, "id")
export const SongModel = table
.model<Song>(ModelType.Song)
.partitionKey(ModelType.Musician, "musicianId")
.sortKey(ModelType.Song, "id")
export const MusicianPartition = table.partition([MusicianModel, SongModel])
export const byModelAndIdGSI = table
.gsi("byModelAndId")
.models([MusicianModel, SongModel])
.partitionKey("model")
.sortKey("id")
export const invertedIndexGSI = table
.gsi("invertedIndex")
.models([MusicianModel, SongModel])
.partitionKey("sk")
.sortKey("pk")
export function aMusicianWithTwoSongs(): [Musician, Song, Song] {
const musician = MusicianModel.create({
id: "1",
name: "Bob Marley",
details: {
description: "rasta man",
},
})
const song1 = SongModel.create({
musicianId: "1",
id: "2",
title: "Buffalo Soldier",
mp3: Buffer.from("fake-data", "utf8"),
})
const song2 = SongModel.create({
musicianId: "1",
id: "3",
title: "No Woman, No Cry",
mp3: Buffer.from("fake-data", "utf8"),
})
return [musician, song1, song2]
}