refactor: 🏷️ Move all types that represent ORM abstractions to ORM class static properties

This commit is contained in:
Jesse Wierzbinski 2024-11-04 14:58:17 +01:00
parent ca31830fb3
commit 02c3c9d0bf
No known key found for this signature in database
17 changed files with 143 additions and 136 deletions

View file

@ -12,7 +12,7 @@ import {
import { z } from "zod";
import { BaseInterface } from "./base.ts";
export type ApplicationType = InferSelectModel<typeof Applications>;
type ApplicationType = InferSelectModel<typeof Applications>;
export class Application extends BaseInterface<typeof Applications> {
public static schema: z.ZodType<APIApplication> = z.object({
@ -23,6 +23,8 @@ export class Application extends BaseInterface<typeof Applications> {
scopes: z.string().optional(),
});
public static $type: ApplicationType;
public async reload(): Promise<void> {
const reloaded = await Application.fromId(this.data.id);

View file

@ -16,7 +16,7 @@ import { MediaBackendType } from "~/packages/config-manager/config.type";
import { config } from "~/packages/config-manager/index.ts";
import { BaseInterface } from "./base.ts";
export type AttachmentType = InferSelectModel<typeof Attachments>;
type AttachmentType = InferSelectModel<typeof Attachments>;
export class Attachment extends BaseInterface<typeof Attachments> {
public static schema: z.ZodType<ApiAttachment> = z.object({
@ -47,6 +47,8 @@ export class Attachment extends BaseInterface<typeof Attachments> {
blurhash: z.string().nullable(),
});
public static $type: AttachmentType;
public async reload(): Promise<void> {
const reloaded = await Attachment.fromId(this.data.id);

View file

@ -17,7 +17,7 @@ import { z } from "zod";
import { BaseInterface } from "./base.ts";
import { Instance } from "./instance.ts";
export type EmojiWithInstance = InferSelectModel<typeof Emojis> & {
type EmojiWithInstance = InferSelectModel<typeof Emojis> & {
instance: InferSelectModel<typeof Instances> | null;
};
@ -30,6 +30,8 @@ export class Emoji extends BaseInterface<typeof Emojis, EmojiWithInstance> {
static_url: z.string(),
});
public static $type: EmojiWithInstance;
public async reload(): Promise<void> {
const reloaded = await Emoji.fromId(this.data.id);

View file

@ -20,9 +20,11 @@ import { config } from "~/packages/config-manager/index.ts";
import { BaseInterface } from "./base.ts";
import { User } from "./user.ts";
export type InstanceType = InferSelectModel<typeof Instances>;
type InstanceType = InferSelectModel<typeof Instances>;
export class Instance extends BaseInterface<typeof Instances> {
public static $type: InstanceType;
public async reload(): Promise<void> {
const reloaded = await Instance.fromId(this.data.id);

View file

@ -1,7 +1,12 @@
import { RolePermission } from "@versia/client/types";
import type { Delete, LikeExtension } from "@versia/federation/types";
import { db } from "@versia/kit/db";
import { Likes, Notifications } from "@versia/kit/tables";
import {
Likes,
type Notes,
Notifications,
type Users,
} from "@versia/kit/tables";
import {
type InferInsertModel,
type InferSelectModel,
@ -13,15 +18,13 @@ import {
} from "drizzle-orm";
import { z } from "zod";
import { config } from "~/packages/config-manager/index.ts";
import type { Status } from "../functions/status.ts";
import type { UserType } from "../functions/user.ts";
import { BaseInterface } from "./base.ts";
import { Note } from "./note.ts";
import { User } from "./user.ts";
export type LikeType = InferSelectModel<typeof Likes> & {
liker: UserType;
liked: Status;
type LikeType = InferSelectModel<typeof Likes> & {
liker: InferSelectModel<typeof Users>;
liked: InferSelectModel<typeof Notes>;
};
export class Like extends BaseInterface<typeof Likes, LikeType> {
@ -35,6 +38,8 @@ export class Like extends BaseInterface<typeof Likes, LikeType> {
icon: z.string().nullable(),
});
public static $type: LikeType;
public async reload(): Promise<void> {
const reloaded = await Like.fromId(this.data.id);

View file

@ -14,7 +14,7 @@ import type {
Delete as VersiaDelete,
Note as VersiaNote,
} from "@versia/federation/types";
import { Notification, db } from "@versia/kit/db";
import { type Instance, Notification, db } from "@versia/kit/db";
import {
Attachments,
EmojiToNote,
@ -24,6 +24,7 @@ import {
} from "@versia/kit/tables";
import {
type InferInsertModel,
type InferSelectModel,
type SQL,
and,
desc,
@ -36,7 +37,6 @@ import { htmlToText } from "html-to-text";
import { createRegExp, exactly, global } from "magic-regexp";
import { z } from "zod";
import {
type StatusWithRelations,
contentToHtml,
findManyNotes,
parseTextMentions,
@ -48,10 +48,37 @@ import { BaseInterface } from "./base.ts";
import { Emoji } from "./emoji.ts";
import { User } from "./user.ts";
type NoteType = InferSelectModel<typeof Notes>;
type NoteTypeWithRelations = NoteType & {
author: typeof User.$type;
mentions: (InferSelectModel<typeof Users> & {
instance: typeof Instance.$type | null;
})[];
attachments: (typeof Attachment.$type)[];
reblog: NoteTypeWithoutRecursiveRelations | null;
emojis: (typeof Emoji.$type)[];
reply: NoteType | null;
quote: NoteType | null;
application: typeof Application.$type | null;
reblogCount: number;
likeCount: number;
replyCount: number;
pinned: boolean;
reblogged: boolean;
muted: boolean;
liked: boolean;
};
export type NoteTypeWithoutRecursiveRelations = Omit<
NoteTypeWithRelations,
"reply" | "quote" | "reblog"
>;
/**
* Gives helpers to fetch notes from database in a nice format
*/
export class Note extends BaseInterface<typeof Notes, StatusWithRelations> {
export class Note extends BaseInterface<typeof Notes, NoteTypeWithRelations> {
public static schema: z.ZodType<ApiStatus> = z.object({
id: z.string().uuid(),
uri: z.string().url(),
@ -142,7 +169,9 @@ export class Note extends BaseInterface<typeof Notes, StatusWithRelations> {
bookmarked: z.boolean(),
});
public save(): Promise<StatusWithRelations> {
public static $type: NoteTypeWithRelations;
public save(): Promise<NoteTypeWithRelations> {
return this.update(this.data);
}
@ -814,8 +843,8 @@ export class Note extends BaseInterface<typeof Notes, StatusWithRelations> {
}
public async update(
newStatus: Partial<StatusWithRelations>,
): Promise<StatusWithRelations> {
newStatus: Partial<NoteTypeWithRelations>,
): Promise<NoteTypeWithRelations> {
await db.update(Notes).set(newStatus).where(eq(Notes.id, this.data.id));
const updated = await Note.fromId(this.data.id);
@ -932,7 +961,7 @@ export class Note extends BaseInterface<typeof Notes, StatusWithRelations> {
// TODO: Add polls
poll: null,
reblog: data.reblog
? await new Note(data.reblog as StatusWithRelations).toApi(
? await new Note(data.reblog as NoteTypeWithRelations).toApi(
userFetching,
)
: null,

View file

@ -12,9 +12,7 @@ import {
import { z } from "zod";
import { MediaBackendType } from "~/packages/config-manager/config.type";
import { config } from "~/packages/config-manager/index.ts";
import type { StatusWithRelations } from "../functions/status.ts";
import {
type UserWithRelations,
transformOutputToUserWithRelations,
userExtrasTemplate,
userRelations,
@ -22,8 +20,8 @@ import {
import { BaseInterface } from "./base.ts";
export type NotificationType = InferSelectModel<typeof Notifications> & {
status: StatusWithRelations | null;
account: UserWithRelations;
status: typeof Note.$type | null;
account: typeof User.$type;
};
export class Notification extends BaseInterface<

View file

@ -14,9 +14,9 @@ import { z } from "zod";
import { BaseInterface } from "./base.ts";
import type { User } from "./user.ts";
export type RelationshipType = InferSelectModel<typeof Relationships>;
type RelationshipType = InferSelectModel<typeof Relationships>;
export type RelationshipWithOpposite = RelationshipType & {
type RelationshipWithOpposite = RelationshipType & {
followedBy: boolean;
blockedBy: boolean;
requestedBy: boolean;
@ -43,6 +43,8 @@ export class Relationship extends BaseInterface<
showing_reblogs: z.boolean(),
});
public static $type: RelationshipWithOpposite;
public async reload(): Promise<void> {
const reloaded = await Relationship.fromId(this.data.id);

View file

@ -18,7 +18,7 @@ import { z } from "zod";
import { config } from "~/packages/config-manager/index.ts";
import { BaseInterface } from "./base.ts";
export type RoleType = InferSelectModel<typeof Roles>;
type RoleType = InferSelectModel<typeof Roles>;
export class Role extends BaseInterface<typeof Roles> {
public static schema = z.object({
@ -31,6 +31,8 @@ export class Role extends BaseInterface<typeof Roles> {
icon: z.string().nullable(),
});
public static $type: RoleType;
public async reload(): Promise<void> {
const reloaded = await Role.fromId(this.data.id);

View file

@ -1,6 +1,6 @@
import type { Token as ApiToken } from "@versia/client/types";
import { User, db } from "@versia/kit/db";
import { type Applications, Tokens } from "@versia/kit/tables";
import { type Application, User, db } from "@versia/kit/db";
import { Tokens } from "@versia/kit/tables";
import {
type InferInsertModel,
type InferSelectModel,
@ -12,8 +12,8 @@ import {
import { z } from "zod";
import { BaseInterface } from "./base.ts";
export type TokenType = InferSelectModel<typeof Tokens> & {
application: InferSelectModel<typeof Applications> | null;
type TokenType = InferSelectModel<typeof Tokens> & {
application: typeof Application.$type | null;
};
export class Token extends BaseInterface<typeof Tokens, TokenType> {
@ -24,6 +24,8 @@ export class Token extends BaseInterface<typeof Tokens, TokenType> {
created_at: z.number(),
});
public static $type: TokenType;
public async reload(): Promise<void> {
const reloaded = await Token.fromId(this.data.id);

View file

@ -33,6 +33,7 @@ import {
import chalk from "chalk";
import {
type InferInsertModel,
type InferSelectModel,
type SQL,
and,
countDistinct,
@ -47,7 +48,6 @@ import {
import { htmlToText } from "html-to-text";
import { z } from "zod";
import {
type UserWithRelations,
findManyUsers,
followAcceptToVersia,
followRejectToVersia,
@ -64,6 +64,18 @@ import type { Note } from "./note.ts";
import { Relationship } from "./relationship.ts";
import { Role } from "./role.ts";
type UserWithInstance = InferSelectModel<typeof Users> & {
instance: typeof Instance.$type | null;
};
type UserWithRelations = UserWithInstance & {
emojis: (typeof Emoji.$type)[];
followerCount: number;
followingCount: number;
statusCount: number;
roles: (typeof Role.$type)[];
};
/**
* Gives helpers to fetch users from database in a nice format
*/
@ -125,6 +137,8 @@ export class User extends BaseInterface<typeof Users, UserWithRelations> {
mute_expires_at: z.string().optional(),
});
public static $type: UserWithRelations;
public async reload(): Promise<void> {
const reloaded = await User.fromId(this.data.id);