mirror of
https://github.com/versia-pub/server.git
synced 2026-03-13 05:49:16 +01:00
refactor: 🏷️ Move all types that represent ORM abstractions to ORM class static properties
This commit is contained in:
parent
ca31830fb3
commit
02c3c9d0bf
17 changed files with 143 additions and 136 deletions
|
|
@ -2,22 +2,9 @@ import { mentionValidator } from "@/api";
|
|||
import { sanitizeHtml, sanitizeHtmlInline } from "@/sanitization";
|
||||
import markdownItTaskLists from "@hackmd/markdown-it-task-lists";
|
||||
import type { ContentFormat } from "@versia/federation/types";
|
||||
import { User, db } from "@versia/kit/db";
|
||||
import {
|
||||
type Attachments,
|
||||
Instances,
|
||||
type Notes,
|
||||
Users,
|
||||
} from "@versia/kit/tables";
|
||||
import {
|
||||
type InferSelectModel,
|
||||
and,
|
||||
eq,
|
||||
inArray,
|
||||
isNull,
|
||||
or,
|
||||
sql,
|
||||
} from "drizzle-orm";
|
||||
import { type Note, User, db } from "@versia/kit/db";
|
||||
import { Instances, Users } from "@versia/kit/tables";
|
||||
import { and, eq, inArray, isNull, or, sql } from "drizzle-orm";
|
||||
import linkifyHtml from "linkify-html";
|
||||
import {
|
||||
anyOf,
|
||||
|
|
@ -31,42 +18,13 @@ import {
|
|||
import MarkdownIt from "markdown-it";
|
||||
import markdownItContainer from "markdown-it-container";
|
||||
import markdownItTocDoneRight from "markdown-it-toc-done-right";
|
||||
import type { ApplicationType } from "~/classes/database/application.ts";
|
||||
import type { EmojiWithInstance } from "~/classes/database/emoji.ts";
|
||||
import { config } from "~/packages/config-manager/index.ts";
|
||||
import {
|
||||
type UserWithInstance,
|
||||
type UserWithRelations,
|
||||
transformOutputToUserWithRelations,
|
||||
userExtrasTemplate,
|
||||
userRelations,
|
||||
} from "./user.ts";
|
||||
|
||||
export type Status = InferSelectModel<typeof Notes>;
|
||||
|
||||
export type StatusWithRelations = Status & {
|
||||
author: UserWithRelations;
|
||||
mentions: UserWithInstance[];
|
||||
attachments: InferSelectModel<typeof Attachments>[];
|
||||
reblog: StatusWithoutRecursiveRelations | null;
|
||||
emojis: EmojiWithInstance[];
|
||||
reply: Status | null;
|
||||
quote: Status | null;
|
||||
application: ApplicationType | null;
|
||||
reblogCount: number;
|
||||
likeCount: number;
|
||||
replyCount: number;
|
||||
pinned: boolean;
|
||||
reblogged: boolean;
|
||||
muted: boolean;
|
||||
liked: boolean;
|
||||
};
|
||||
|
||||
export type StatusWithoutRecursiveRelations = Omit<
|
||||
StatusWithRelations,
|
||||
"reply" | "quote" | "reblog"
|
||||
>;
|
||||
|
||||
/**
|
||||
* Wrapper against the Status object to make it easier to work with
|
||||
* @param query
|
||||
|
|
@ -75,7 +33,7 @@ export type StatusWithoutRecursiveRelations = Omit<
|
|||
export const findManyNotes = async (
|
||||
query: Parameters<typeof db.query.Notes.findMany>[0],
|
||||
userId?: string,
|
||||
): Promise<StatusWithRelations[]> => {
|
||||
): Promise<(typeof Note.$type)[]> => {
|
||||
const output = await db.query.Notes.findMany({
|
||||
...query,
|
||||
with: {
|
||||
|
|
|
|||
|
|
@ -3,25 +3,17 @@ import type {
|
|||
FollowAccept,
|
||||
FollowReject,
|
||||
} from "@versia/federation/types";
|
||||
import { type Application, type Token, type User, db } from "@versia/kit/db";
|
||||
import type { Instances, Roles, Users } from "@versia/kit/tables";
|
||||
import {
|
||||
type Application,
|
||||
type Emoji,
|
||||
type Instance,
|
||||
type Role,
|
||||
type Token,
|
||||
type User,
|
||||
db,
|
||||
} from "@versia/kit/db";
|
||||
import type { Users } from "@versia/kit/tables";
|
||||
import { type InferSelectModel, type SQL, sql } from "drizzle-orm";
|
||||
import type { EmojiWithInstance } from "~/classes/database/emoji.ts";
|
||||
|
||||
export type UserType = InferSelectModel<typeof Users>;
|
||||
|
||||
export type UserWithInstance = UserType & {
|
||||
instance: InferSelectModel<typeof Instances> | null;
|
||||
};
|
||||
|
||||
export type UserWithRelations = UserType & {
|
||||
instance: InferSelectModel<typeof Instances> | null;
|
||||
emojis: EmojiWithInstance[];
|
||||
followerCount: number;
|
||||
followingCount: number;
|
||||
statusCount: number;
|
||||
roles: InferSelectModel<typeof Roles>[];
|
||||
};
|
||||
|
||||
export const userRelations = {
|
||||
instance: true,
|
||||
|
|
@ -84,24 +76,24 @@ export interface AuthData {
|
|||
}
|
||||
|
||||
export const transformOutputToUserWithRelations = (
|
||||
user: Omit<UserType, "endpoints"> & {
|
||||
user: Omit<InferSelectModel<typeof Users>, "endpoints"> & {
|
||||
followerCount: unknown;
|
||||
followingCount: unknown;
|
||||
statusCount: unknown;
|
||||
emojis: {
|
||||
userId: string;
|
||||
emojiId: string;
|
||||
emoji?: EmojiWithInstance;
|
||||
emoji?: typeof Emoji.$type;
|
||||
}[];
|
||||
instance: InferSelectModel<typeof Instances> | null;
|
||||
instance: typeof Instance.$type | null;
|
||||
roles: {
|
||||
userId: string;
|
||||
roleId: string;
|
||||
role?: InferSelectModel<typeof Roles>;
|
||||
role?: typeof Role.$type;
|
||||
}[];
|
||||
endpoints: unknown;
|
||||
},
|
||||
): UserWithRelations => {
|
||||
): typeof User.$type => {
|
||||
return {
|
||||
...user,
|
||||
followerCount: Number(user.followerCount),
|
||||
|
|
@ -121,17 +113,17 @@ export const transformOutputToUserWithRelations = (
|
|||
emojis: user.emojis.map(
|
||||
(emoji) =>
|
||||
(emoji as unknown as Record<string, object>)
|
||||
.emoji as EmojiWithInstance,
|
||||
.emoji as typeof Emoji.$type,
|
||||
),
|
||||
roles: user.roles
|
||||
.map((role) => role.role)
|
||||
.filter(Boolean) as InferSelectModel<typeof Roles>[],
|
||||
.filter(Boolean) as (typeof Role.$type)[],
|
||||
};
|
||||
};
|
||||
|
||||
export const findManyUsers = async (
|
||||
query: Parameters<typeof db.query.Users.findMany>[0],
|
||||
): Promise<UserWithRelations[]> => {
|
||||
): Promise<(typeof User.$type)[]> => {
|
||||
const output = await db.query.Users.findMany({
|
||||
...query,
|
||||
with: {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue