mirror of
https://github.com/versia-pub/server.git
synced 2026-03-13 05:49:16 +01:00
refactor(database): ♻️ Move Note <-> Media relations to a many-to-many model instead of one-to-many
This commit is contained in:
parent
9c30dacda7
commit
3216fc339a
7 changed files with 2428 additions and 31 deletions
|
|
@ -17,7 +17,7 @@ import type {
|
|||
import { Instance, db } from "@versia/kit/db";
|
||||
import {
|
||||
EmojiToNote,
|
||||
Medias,
|
||||
MediasToNotes,
|
||||
NoteToMentions,
|
||||
Notes,
|
||||
Users,
|
||||
|
|
@ -630,22 +630,15 @@ export class Note extends BaseInterface<typeof Notes, NoteTypeWithRelations> {
|
|||
|
||||
// Remove old attachments
|
||||
await db
|
||||
.update(Medias)
|
||||
.set({
|
||||
noteId: null,
|
||||
})
|
||||
.where(eq(Medias.noteId, this.data.id));
|
||||
await db
|
||||
.update(Medias)
|
||||
.set({
|
||||
.delete(MediasToNotes)
|
||||
.where(eq(MediasToNotes.noteId, this.data.id));
|
||||
|
||||
await db.insert(MediasToNotes).values(
|
||||
mediaAttachments.map((media) => ({
|
||||
noteId: this.data.id,
|
||||
})
|
||||
.where(
|
||||
inArray(
|
||||
Medias.id,
|
||||
mediaAttachments.map((i) => i.id),
|
||||
),
|
||||
);
|
||||
mediaId: media.id,
|
||||
})),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -38,7 +38,11 @@ export const findManyNotes = async (
|
|||
...query,
|
||||
with: {
|
||||
...query?.with,
|
||||
attachments: true,
|
||||
attachments: {
|
||||
with: {
|
||||
media: true,
|
||||
},
|
||||
},
|
||||
emojis: {
|
||||
with: {
|
||||
emoji: {
|
||||
|
|
@ -65,7 +69,11 @@ export const findManyNotes = async (
|
|||
},
|
||||
reblog: {
|
||||
with: {
|
||||
attachments: true,
|
||||
attachments: {
|
||||
with: {
|
||||
media: true,
|
||||
},
|
||||
},
|
||||
emojis: {
|
||||
with: {
|
||||
emoji: {
|
||||
|
|
@ -176,6 +184,7 @@ export const findManyNotes = async (
|
|||
...mention.user,
|
||||
endpoints: mention.user.endpoints,
|
||||
})),
|
||||
attachments: post.attachments.map((attachment) => attachment.media),
|
||||
emojis: (post.emojis ?? []).map((emoji) => emoji.emoji),
|
||||
reblog: post.reblog && {
|
||||
...post.reblog,
|
||||
|
|
@ -184,6 +193,9 @@ export const findManyNotes = async (
|
|||
...mention.user,
|
||||
endpoints: mention.user.endpoints,
|
||||
})),
|
||||
attachments: post.reblog.attachments.map(
|
||||
(attachment) => attachment.media,
|
||||
),
|
||||
emojis: (post.reblog.emojis ?? []).map((emoji) => emoji.emoji),
|
||||
reblogCount: Number(post.reblog.reblogCount),
|
||||
likeCount: Number(post.reblog.likeCount),
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue