forked from GoogleCloudPlatform/pgadapter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsample.ts
More file actions
271 lines (248 loc) · 8.5 KB
/
sample.ts
File metadata and controls
271 lines (248 loc) · 8.5 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import {Prisma, PrismaClient} from "@prisma/client";
import {
randomAlbumTitle,
randomFirstName,
randomId,
randomLastName,
randomReleaseDate, randomTrackTitle,
times
} from "./random_names";
import {randomBytes, randomInt} from "crypto";
import {promisify} from "util";
import {exec} from "child_process";
let prisma: PrismaClient;
let staleReadClient: PrismaClient;
export function createPrismaClient() {
// This is the 'normal' Prisma client that can be used for strong reads and read/write transactions.
prisma = new PrismaClient();
// This client is read-only and will use a staleness of up to 10 seconds when reading data.
// These reads can have a slightly lower latency, but could return data that is up to 10 seconds
// stale.
staleReadClient = new PrismaClient({
datasources: {
db: {
url: `${process.env.DATABASE_URL}%20-c%20spanner.read_only_staleness='MAX_STALENESS 10s'`,
},
},
});
}
const execAsync = promisify(exec);
export async function deployMigrations() {
console.log(`Running npx prisma migrate deploy on ${process.env.DATABASE_URL}`);
await execAsync("npx prisma migrate deploy", {
env: process.env,
});
}
export async function deleteExistingData() {
console.log("Deleting all existing ticket sales");
let result = await prisma.ticketSale.deleteMany({});
console.log(`Deleted ${result.count} ticket sales`);
console.log("Deleting all existing concerts");
result = await prisma.concert.deleteMany({});
console.log(`Deleted ${result.count} concerts`);
console.log("Deleting all existing albums and related tracks");
result = await prisma.album.deleteMany({});
console.log(`Deleted ${result.count} albums`);
console.log("Deleting all existing singers");
result = await prisma.singer.deleteMany({});
console.log(`Deleted ${result.count} singers`);
console.log("Deleting all existing venues");
result = await prisma.venue.deleteMany({});
console.log(`Deleted ${result.count} venues`);
console.log();
}
export async function createRandomSingersAndAlbumsAndTracks(numSingers?: number | undefined, numAlbums?: number | undefined, numTracks?: number | undefined) {
console.log("Creating random singers and albums");
const singers: Prisma.SingerCreateManyInput[] = [];
times(numSingers ?? randomInt(5, 15), () => singers.push({
id: randomId(),
firstName: randomFirstName(),
lastName: randomLastName(),
active: true,
}));
const createdSingers = await prisma.singer.createMany({data: singers});
console.log(`Created ${createdSingers.count} singers`);
let numCreatedAlbums = 0;
const allAlbums: Prisma.AlbumCreateManyInput[] = [];
for (const singer of singers) {
const albums: Prisma.AlbumCreateManyInput[] = [];
times(numAlbums ?? randomInt(5, 15), () => albums.push({
id: randomId(),
title: randomAlbumTitle(),
releaseDate: randomReleaseDate(),
marketingBudget: Math.random() * 1000000,
coverPicture: randomBytes(randomInt(2048) + 32),
singerId: singer.id,
}));
const createdAlbums = await prisma.album.createMany({data: albums});
console.log(`Created ${createdAlbums.count} albums for ${singer.firstName} ${singer.lastName}`);
numCreatedAlbums += createdAlbums.count;
allAlbums.push(...albums);
}
let numCreatedTracks = 0;
for (const album of allAlbums) {
const tracks: Prisma.TrackCreateManyInput[] = [];
let n = 0;
times(numTracks ?? randomInt(5, 15), () => tracks.push({
id: album.id,
trackNumber: ++n,
title: randomTrackTitle(),
sampleRate: Math.random(),
}));
const createdTracks = await prisma.track.createMany({data: tracks});
console.log(`Created ${createdTracks.count} tracks for ${album.title}`);
numCreatedTracks += createdTracks.count;
}
console.log(`Created ${singers.length} singers, ${numCreatedAlbums} albums, and ${numCreatedTracks} tracks.`);
console.log();
}
export async function printSingersAndAlbums() {
console.log("Printing all singers and albums");
const singersAndAlbums = await prisma.singer.findMany({
select: {
fullName: true,
albums: {
select: {
title: true,
releaseDate: true,
},
orderBy: {
title: 'asc',
},
skip: 0,
take: 5,
},
},
orderBy: {
lastName: 'asc',
},
skip: 0,
take: 5,
});
for (const singer of singersAndAlbums) {
console.log(`Singer ${singer.fullName} has ${singer.albums.length} albums:`);
for (const album of singer.albums) {
console.log(` ${album.title} was released on ${album.releaseDate}`);
}
}
console.log();
}
export async function createVenueAndConcertInTransaction() {
console.log("Creating a Venue and a Concert in a transaction");
const result = await prisma.$transaction(async (tx) => {
// Get a random singer. Note that we read using the transaction.
const singer = await tx.singer.findFirst({});
// Create and save a Venue and a Concert for this singer.
const venue = await tx.venue.create({
data: {
id: randomId(),
name: "Avenue Park",
description: {Capacity: 5000, Location: "New York", Country: "US"},
}
});
const concert = await tx.concert.create({
data: {
id: randomId(),
name: "Avenue Park Open",
venueId: venue.id,
singerId: singer.id,
startTime: "2023-02-01T20:00:00-05:00",
endTime: "2023-02-02T02:00:00-05:00",
}
});
return {concert: concert, singer: singer, venue: venue};
})
console.log(`Created a concert for ${result.singer.fullName}`);
console.log(`The concert is named ${result.concert.name} and start at ${result.concert.startTime} at ${result.venue.name}`);
console.log();
}
export async function createTicketSale() {
console.log("Creating a ticket sale. Ticket sale uses an auto-generated primary key");
const concert = await prisma.concert.findFirst({});
const ticketSale = await prisma.ticketSale.create({
data: {
concertId: concert.id,
customerName: "Alice Neeson",
price: 99.50,
seats: ["A11", "A12", "A13"],
}
});
console.log(`Created a ticket sale with id ${ticketSale.id}`);
console.log();
}
export async function printAlbumsReleasedBefore1900() {
console.log("Searching for albums released before 1900");
const albums = await prisma.album.findMany({
select: {
title: true,
releaseDate: true,
singer: {
select: {
fullName: true,
}
},
},
where: {
releaseDate: {
lt: new Date('1900-01-01'),
}
},
orderBy: {
title: 'asc',
},
});
for (const album of albums) {
console.log(`Singer ${album.singer.fullName} released "${album.title}" on ${album.releaseDate}`);
}
console.log();
}
export async function updateVenueDescription() {
console.log("Updating the description of venue 'Avenue Park");
const updated = await prisma.$transaction(async (tx) => {
const venue = await tx.venue.findFirst({
where: {
name: "Avenue Park",
}
});
return tx.venue.update({
data: {
description: {Capacity: 10000, Location: "New York", Country: "US", Type: "Park"},
},
where: {
id: venue.id,
},
select: {
description: true
}
});
})
console.log("Updated description of venue 'Avenue Park'");
console.log(`New description: ${JSON.stringify(updated.description)}`);
console.log();
}
/** Executes a stale read on the Venue table. */
export async function staleRead() {
console.log("Executing a stale read");
// Read using the client that has been configured to use a 10-second max staleness.
// This could mean that we are not seeing the most recent update to the table.
const venue = await staleReadClient.venue.findFirst({
where: {
name: "Avenue Park",
}
});
console.log(`Stale read returned a venue with description: ${JSON.stringify(venue.description)}`);
console.log();
}