Skip to content
This repository was archived by the owner on Oct 17, 2025. It is now read-only.

Commit dcd39f7

Browse files
committed
削除の N+1
1 parent b8819cc commit dcd39f7

File tree

11 files changed

+50
-31
lines changed

11 files changed

+50
-31
lines changed

workspaces/schema/src/models/episode.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export const episode = sqliteTable(
1818
chapter: integer('chapter').notNull(),
1919

2020
// relations
21+
authorId: text('author_id').notNull(),
2122
bookId: text('book_id').notNull(),
2223
imageId: text('image_id').notNull(),
2324

workspaces/schema/src/models/episodePage.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ export const episodePage = sqliteTable(
1616

1717
// relations
1818
episodeId: text('episode_id').notNull(),
19+
bookId: text('book_id').notNull(),
20+
authorId: text('author_id').notNull(),
1921
imageId: text('image_id').notNull(),
2022

2123
// metadata

workspaces/schema/src/models/feature.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export const feature = sqliteTable(
1313

1414
// relations
1515
bookId: text('book_id').notNull(),
16+
authorId: text('author_id').notNull(),
1617

1718
// metadata
1819
createdAt: text('created_at')

workspaces/schema/src/models/ranking.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export const ranking = sqliteTable(
1616

1717
// relations
1818
bookId: text('book_id').notNull(),
19+
authorId: text('author_id').notNull(),
1920

2021
// metadata
2122
createdAt: text('created_at')

workspaces/schema/src/models/relations/episodePageRelations.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
11
import { relations } from 'drizzle-orm';
22

3+
import { author } from '../author';
4+
import { book } from '../book';
35
import { episode } from '../episode';
46
import { episodePage } from '../episodePage';
57
import { image } from '../image';
68

79
export const episodePageRelations = relations(episodePage, ({ one }) => ({
10+
author: one(author, {
11+
fields: [episodePage.authorId],
12+
references: [author.id],
13+
}),
14+
book: one(book, {
15+
fields: [episodePage.bookId],
16+
references: [book.id],
17+
}),
818
episode: one(episode, {
919
fields: [episodePage.episodeId],
1020
references: [episode.id],

workspaces/schema/src/models/relations/episodeRelations.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
import { relations } from 'drizzle-orm';
22

3+
import { author } from '../author';
34
import { book } from '../book';
45
import { episode } from '../episode';
56
import { episodePage } from '../episodePage';
67
import { image } from '../image';
78

89
export const episodeRelations = relations(episode, ({ many, one }) => ({
10+
author: one(author, {
11+
fields: [episode.authorId],
12+
references: [author.id],
13+
}),
914
book: one(book, {
1015
fields: [episode.bookId],
1116
references: [book.id],

workspaces/schema/src/models/relations/featureRelations.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
import { relations } from 'drizzle-orm';
22

3+
import { author } from '../author';
34
import { book } from '../book';
45
import { feature } from '../feature';
56

67
export const featureRelations = relations(feature, ({ one }) => ({
8+
author: one(author, {
9+
fields: [feature.authorId],
10+
references: [author.id],
11+
}),
712
book: one(book, {
813
fields: [feature.bookId],
914
references: [book.id],

workspaces/schema/src/models/relations/rankingRelations.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
import { relations } from 'drizzle-orm';
22

3+
import { author } from '../author';
34
import { book } from '../book';
45
import { ranking } from '../ranking';
56

67
export const rankingRelations = relations(ranking, ({ one }) => ({
8+
author: one(author, {
9+
fields: [ranking.authorId],
10+
references: [author.id],
11+
}),
712
book: one(book, {
813
fields: [ranking.bookId],
914
references: [book.id],

workspaces/server/src/database/drizzle.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,19 @@ export function initializeDatabase() {
1919
sqlite = new Database(DATABASE_PATH, {
2020
readonly: false,
2121
});
22+
sqlite.exec('ALTER TABLE episode ADD COLUMN author_id TEXT NOT NULL DEFAULT "0";');
23+
sqlite.exec('UPDATE episode SET author_id = (SELECT author_id FROM book WHERE book_id = episode.book_id);');
24+
sqlite.exec('ALTER TABLE episode_page ADD COLUMN author_id TEXT NOT NULL DEFAULT "0";');
25+
sqlite.exec('ALTER TABLE episode_page ADD COLUMN book_id TEXT NOT NULL DEFAULT "0";');
26+
sqlite.exec('UPDATE episode_page SET author_id = (SELECT author_id FROM book WHERE book_id = episode_page.book_id);');
27+
sqlite.exec(
28+
'UPDATE episode_page SET book_id = (SELECT book_id FROM episode WHERE episode_id = episode_page.episode_id);',
29+
);
30+
sqlite.exec('ALTER TABLE feature ADD COLUMN author_id TEXT NOT NULL DEFAULT "0";');
31+
sqlite.exec('UPDATE feature SET author_id = (SELECT author_id FROM book WHERE book_id = feature.book_id);');
32+
sqlite.exec('ALTER TABLE ranking ADD COLUMN author_id TEXT NOT NULL DEFAULT "0";');
33+
sqlite.exec('UPDATE ranking SET author_id = (SELECT author_id FROM book WHERE book_id = ranking.book_id);');
34+
2235
database = drizzle(sqlite, { schema });
2336
}
2437

workspaces/server/src/repositories/author.ts

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -205,27 +205,11 @@ class AuthorRepository implements AuthorRepositoryInterface {
205205
try {
206206
await getDatabase().transaction(async (tx) => {
207207
await tx.delete(author).where(eq(author.id, options.params.authorId)).execute();
208-
const deleteBookRes = await tx
209-
.delete(book)
210-
.where(eq(book.authorId, options.params.authorId))
211-
.returning({
212-
bookId: book.id,
213-
})
214-
.execute();
215-
for (const book of deleteBookRes) {
216-
await tx.delete(feature).where(eq(feature.bookId, book.bookId)).execute();
217-
await tx.delete(ranking).where(eq(ranking.bookId, book.bookId)).execute();
218-
const deleteEpisodeRes = await tx
219-
.delete(episode)
220-
.where(eq(episode.bookId, book.bookId))
221-
.returning({
222-
episodeId: episode.id,
223-
})
224-
.execute();
225-
for (const episode of deleteEpisodeRes) {
226-
await tx.delete(episodePage).where(eq(episodePage.episodeId, episode.episodeId)).execute();
227-
}
228-
}
208+
await tx.delete(book).where(eq(book.authorId, options.params.authorId)).execute();
209+
await tx.delete(feature).where(eq(feature.authorId, options.params.authorId)).execute();
210+
await tx.delete(ranking).where(eq(ranking.authorId, options.params.authorId)).execute();
211+
await tx.delete(episode).where(eq(episode.authorId, options.params.authorId)).execute();
212+
await tx.delete(episodePage).where(eq(episodePage.authorId, options.params.authorId)).execute();
229213
});
230214

231215
return ok({});

0 commit comments

Comments
 (0)