Skip to content

Commit

Permalink
Add get_person to Drizzle (#73)
Browse files Browse the repository at this point in the history
  • Loading branch information
fantix authored Oct 6, 2024
1 parent fa31953 commit 88e5eb6
Show file tree
Hide file tree
Showing 3 changed files with 182 additions and 8 deletions.
5 changes: 5 additions & 0 deletions _drizzle/db/mysql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ export const persons = mysqlTable("persons", {
bio: text("bio"),
});

export const personActsRelations = relations(persons, ({ many }) => ({
actedIn: many(actors),
directed: many(directors),
}));

export const directors = mysqlTable(
"directors",
{
Expand Down
5 changes: 5 additions & 0 deletions _drizzle/db/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ export const persons = pgTable("persons", {
bio: text("bio"),
});

export const personActsRelations = relations(persons, ({ many }) => ({
actedIn: many(actors),
directed: many(directors),
}));

export const directors = pgTable(
"directors",
{
Expand Down
180 changes: 172 additions & 8 deletions _drizzle/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,18 @@ abstract class BaseApp {

abstract setup(query: string): Promise<void>;
abstract movieDetails(id: number): Promise<string>;
abstract userDetails(id: number): Promise<any>;
abstract userDetails(id: number): Promise<string | undefined>;
abstract insertMovie(val: {
prefix: string;
people: number[];
}): Promise<string>;
abstract personDetails(id: number): Promise<string>;

async benchQuery(query: string, val: any) {
if (query == "get_user") {
return await this.userDetails(val as number);
} else if (query == "get_person") {
// return await this.personDetails(id);
return await this.personDetails(val as number);
} else if (query == "get_movie") {
return await this.movieDetails(val as number);
} else if (query == "update_movie") {
Expand Down Expand Up @@ -76,6 +77,7 @@ abstract class BaseApp {
export class App extends BaseApp {
private client;
private db;
private fullName;
private preparedAvgRating;
private preparedMovieDetails;
private preparedUserDetails;
Expand Down Expand Up @@ -114,7 +116,7 @@ export class App extends BaseApp {
.groupBy(schema.reviews.movieId)
.where(eq(schema.reviews.movieId, sql`any(${ids})`))
.prepare("avgRating");
const fullName = sql<string>`
this.fullName = sql<string>`
CASE WHEN ${schema.persons.middleName} != '' THEN
${schema.persons.firstName} || ' ' || ${schema.persons.middleName} || ' ' || ${schema.persons.lastName}
ELSE
Expand Down Expand Up @@ -142,7 +144,7 @@ export class App extends BaseApp {
image: true,
},
extras: {
full_name: fullName.as("full_name"),
full_name: this.fullName.as("full_name"),
},
},
},
Expand All @@ -161,7 +163,7 @@ export class App extends BaseApp {
image: true,
},
extras: {
full_name: fullName.as("full_name"),
full_name: this.fullName.as("full_name"),
},
},
},
Expand Down Expand Up @@ -237,7 +239,7 @@ export class App extends BaseApp {
image: true,
},
extras: {
full_name: fullName.as("full_name"),
full_name: this.fullName.as("full_name"),
},
where: eq(schema.users.id, sql`any(${ids})`),
})
Expand Down Expand Up @@ -335,7 +337,7 @@ export class App extends BaseApp {
return JSON.stringify(result);
}

async userDetails(id: number): Promise<any> {
async userDetails(id: number): Promise<string | undefined> {
const rv = await this.preparedUserDetails.execute({ id });
if (rv === undefined) {
return;
Expand Down Expand Up @@ -387,6 +389,87 @@ export class App extends BaseApp {
);
return JSON.stringify({ ...movie, directors, cast });
}

async personDetails(id: number): Promise<string> {
let person = await this.db.transaction(
async (tx) => {
let person = await tx.query.persons.findFirst({
columns: {
id: true,
image: true,
bio: true,
},
with: {
actedIn: {
columns: {},
with: {
movie: {
columns: {
id: true,
},
},
},
},
directed: {
columns: {},
with: {
movie: {
columns: {
id: true,
},
},
},
},
},
extras: {
full_name: this.fullName.as("full_name"),
},
where: eq(schema.persons.id, id),
});
let actedIn =
person!.actedIn.length > 0
? await tx.query.movies.findMany({
columns: {
id: true,
image: true,
title: true,
year: true,
},
where: inArray(
schema.movies.id,
person!.actedIn.map((r) => r.movie.id),
),
orderBy: [asc(schema.movies.year), asc(schema.movies.title)],
})
: [];
let directed =
person!.directed.length > 0
? await tx.query.movies.findMany({
columns: {
id: true,
image: true,
title: true,
year: true,
},
where: inArray(
schema.movies.id,
person!.directed.map((r) => r.movie.id),
),
orderBy: [asc(schema.movies.year), asc(schema.movies.title)],
})
: [];
return {
...person,
actedIn,
directed,
};
},
{
isolationLevel: "repeatable read",
},
);
return JSON.stringify(person);
}
}

export class MySQLApp extends BaseApp {
Expand Down Expand Up @@ -632,7 +715,7 @@ export class MySQLApp extends BaseApp {
return JSON.stringify(result);
}

async userDetails(id: number): Promise<any> {
async userDetails(id: number): Promise<string | undefined> {
const rv = await this.preparedUserDetails.execute({ id });
if (rv === undefined) {
return;
Expand Down Expand Up @@ -710,4 +793,85 @@ export class MySQLApp extends BaseApp {
);
return JSON.stringify({ ...movie, directors, cast });
}

async personDetails(id: number): Promise<string> {
let person = await this.db.transaction(
async (tx) => {
let person = await tx.query.persons.findFirst({
columns: {
id: true,
image: true,
bio: true,
},
with: {
actedIn: {
columns: {},
with: {
movie: {
columns: {
id: true,
},
},
},
},
directed: {
columns: {},
with: {
movie: {
columns: {
id: true,
},
},
},
},
},
extras: {
full_name: this.fullName.as("full_name"),
},
where: eq(mysql.persons.id, id),
});
let actedIn =
person!.actedIn.length > 0
? await tx.query.movies.findMany({
columns: {
id: true,
image: true,
title: true,
year: true,
},
where: inArray(
mysql.movies.id,
person!.actedIn.map((r) => r.movie.id),
),
orderBy: [asc(mysql.movies.year), asc(mysql.movies.title)],
})
: [];
let directed =
person!.directed.length > 0
? await tx.query.movies.findMany({
columns: {
id: true,
image: true,
title: true,
year: true,
},
where: inArray(
mysql.movies.id,
person!.directed.map((r) => r.movie.id),
),
orderBy: [asc(mysql.movies.year), asc(mysql.movies.title)],
})
: [];
return {
...person,
actedIn,
directed,
};
},
{
isolationLevel: "repeatable read",
},
);
return JSON.stringify(person);
}
}

0 comments on commit 88e5eb6

Please sign in to comment.