Skip to content

Commit

Permalink
refactor(db): enhance index definitions, add relations comments
Browse files Browse the repository at this point in the history
  • Loading branch information
rocktimsaikia committed Jan 14, 2024
1 parent faafbd5 commit 939e49b
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions server/db/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ export const anime = mysqlTable(
},
(table) => {
return {
nameIdx: index('name_index'),
// Indexes
nameIdx: index('name_index').on(table.name),
};
}
);
Expand All @@ -31,13 +32,15 @@ export const character = mysqlTable(
},
(table) => {
return {
nameIdx: index('name_index'),
// Indexs
nameIdx: index('name_index').on(table.name),
};
}
);

// Relations
export const quoteRelations = relations(quote, ({ one }) => ({
// quote has one-to-one relation with anime and character
anime: one(anime, {
fields: [quote.animeId],
references: [anime.id],
Expand All @@ -49,18 +52,23 @@ export const quoteRelations = relations(quote, ({ one }) => ({
}));

export const animeRelations = relations(anime, ({ many }) => ({
// anime has one-to-many relation with quote and character
quotes: many(quote),
characters: many(character),
}));

export const characterRelations = relations(character, ({ many, one }) => ({
// character has:
// 1. one-to-many relation with quotes
// 2. one-to-one relation with anime
quotes: many(quote),
anime: one(anime, {
fields: [character.animeId],
references: [anime.id],
}),
}));

// quote view for ease of access
export const quoteView = mysqlView('quote_view', {
anime: text('anime'),
character: text('character'),
Expand Down

0 comments on commit 939e49b

Please sign in to comment.