refactor(database): 🚚 Rename Application to Client everywhere

This commit is contained in:
Jesse Wierzbinski 2025-08-21 01:21:32 +02:00
parent c55be8a862
commit 87da7796be
25 changed files with 2549 additions and 90 deletions

View file

@ -14,7 +14,7 @@ import { type ZodAny, ZodError, z } from "zod/v4";
import { fromZodError } from "zod-validation-error";
import type { AuthData, HonoEnv } from "~/types/api";
import { ApiError } from "./api-error.ts";
import { Application } from "./db/application.ts";
import { Client } from "./db/application.ts";
import { Emoji } from "./db/emoji.ts";
import { Note } from "./db/note.ts";
import { Token } from "./db/token.ts";
@ -170,7 +170,7 @@ export const auth = <AuthRequired extends boolean>(options: {
const auth: AuthData = {
token,
application: token?.data.client
? new Application(token?.data.client)
? new Client(token?.data.client)
: null,
user: (await token?.getUser()) ?? null,
};

View file

@ -16,37 +16,37 @@ import { Clients } from "../tables/schema.ts";
import { BaseInterface } from "./base.ts";
import { Token } from "./token.ts";
type ApplicationType = InferSelectModel<typeof Clients>;
type ClientType = InferSelectModel<typeof Clients>;
export class Application extends BaseInterface<typeof Clients> {
public static $type: ApplicationType;
export class Client extends BaseInterface<typeof Clients> {
public static $type: ClientType;
public async reload(): Promise<void> {
const reloaded = await Application.fromId(this.data.id);
const reloaded = await Client.fromId(this.data.id);
if (!reloaded) {
throw new Error("Failed to reload application");
throw new Error("Failed to reload client");
}
this.data = reloaded.data;
}
public static async fromId(id: string | null): Promise<Application | null> {
public static async fromId(id: string | null): Promise<Client | null> {
if (!id) {
return null;
}
return await Application.fromSql(eq(Clients.id, id));
return await Client.fromSql(eq(Clients.id, id));
}
public static async fromIds(ids: string[]): Promise<Application[]> {
return await Application.manyFromSql(inArray(Clients.id, ids));
public static async fromIds(ids: string[]): Promise<Client[]> {
return await Client.manyFromSql(inArray(Clients.id, ids));
}
public static async fromSql(
sql: SQL<unknown> | undefined,
orderBy: SQL<unknown> | undefined = desc(Clients.id),
): Promise<Application | null> {
): Promise<Client | null> {
const found = await db.query.Clients.findFirst({
where: sql,
orderBy,
@ -55,7 +55,7 @@ export class Application extends BaseInterface<typeof Clients> {
if (!found) {
return null;
}
return new Application(found);
return new Client(found);
}
public static async manyFromSql(
@ -64,7 +64,7 @@ export class Application extends BaseInterface<typeof Clients> {
limit?: number,
offset?: number,
extra?: Parameters<typeof db.query.Clients.findMany>[0],
): Promise<Application[]> {
): Promise<Client[]> {
const found = await db.query.Clients.findMany({
where: sql,
orderBy,
@ -73,30 +73,28 @@ export class Application extends BaseInterface<typeof Clients> {
with: extra?.with,
});
return found.map((s) => new Application(s));
return found.map((s) => new Client(s));
}
public static async getFromToken(
token: string,
): Promise<Application | null> {
public static async getFromToken(token: string): Promise<Client | null> {
const result = await Token.fromAccessToken(token);
return result?.data.client ? new Application(result.data.client) : null;
return result?.data.client ? new Client(result.data.client) : null;
}
public static fromClientId(clientId: string): Promise<Application | null> {
return Application.fromSql(eq(Clients.id, clientId));
public static fromClientId(clientId: string): Promise<Client | null> {
return Client.fromSql(eq(Clients.id, clientId));
}
public async update(
newApplication: Partial<ApplicationType>,
): Promise<ApplicationType> {
newApplication: Partial<ClientType>,
): Promise<ClientType> {
await db
.update(Clients)
.set(newApplication)
.where(eq(Clients.id, this.id));
const updated = await Application.fromId(this.data.id);
const updated = await Client.fromId(this.data.id);
if (!updated) {
throw new Error("Failed to update application");
@ -106,7 +104,7 @@ export class Application extends BaseInterface<typeof Clients> {
return updated.data;
}
public save(): Promise<ApplicationType> {
public save(): Promise<ClientType> {
return this.update(this.data);
}
@ -120,10 +118,10 @@ export class Application extends BaseInterface<typeof Clients> {
public static async insert(
data: InferInsertModel<typeof Clients>,
): Promise<Application> {
): Promise<Client> {
const inserted = (await db.insert(Clients).values(data).returning())[0];
const application = await Application.fromId(inserted.id);
const application = await Client.fromId(inserted.id);
if (!application) {
throw new Error("Failed to insert application");

View file

@ -1,5 +1,5 @@
export { db, setupDatabase } from "../tables/db.ts";
export { Application } from "./application.ts";
export { Client } from "./application.ts";
export { Emoji } from "./emoji.ts";
export { Instance } from "./instance.ts";
export { Like } from "./like.ts";

View file

@ -36,7 +36,7 @@ import {
Notifications,
Users,
} from "../tables/schema.ts";
import { Application } from "./application.ts";
import { Client } from "./application.ts";
import { BaseInterface } from "./base.ts";
import { Emoji } from "./emoji.ts";
import { Instance } from "./instance.ts";
@ -129,7 +129,7 @@ const findManyNotes = async (
},
},
likes: true,
application: true,
client: true,
mentions: {
with: {
user: {
@ -238,7 +238,7 @@ type NoteTypeWithRelations = NoteType & {
emojis: (typeof Emoji.$type)[];
reply: NoteType | null;
quote: NoteType | null;
application: typeof Application.$type | null;
client: typeof Client.$type | null;
pinned: boolean;
reblogged: boolean;
muted: boolean;
@ -514,7 +514,7 @@ export class Note extends BaseInterface<typeof Notes, NoteTypeWithRelations> {
visibility,
sensitive: false,
updatedAt: new Date().toISOString(),
applicationId: null,
clientId: null,
uri: uri?.href,
});
@ -1162,8 +1162,8 @@ export class Note extends BaseInterface<typeof Notes, NoteTypeWithRelations> {
in_reply_to_account_id: data.reply?.authorId || null,
account: this.author.toApi(userFetching?.id === data.authorId),
created_at: new Date(data.createdAt).toISOString(),
application: data.application
? new Application(data.application).toApi()
application: data.client
? new Client(data.client).toApi()
: undefined,
card: null,
content: replacedContent,

View file

@ -10,12 +10,12 @@ import {
import type { z } from "zod/v4";
import { db } from "../tables/db.ts";
import { Tokens } from "../tables/schema.ts";
import type { Application } from "./application.ts";
import type { Client } from "./application.ts";
import { BaseInterface } from "./base.ts";
import { User } from "./user.ts";
type TokenType = InferSelectModel<typeof Tokens> & {
client: typeof Application.$type;
client: typeof Client.$type;
};
export class Token extends BaseInterface<typeof Tokens, TokenType> {

View file

@ -0,0 +1,15 @@
ALTER TABLE "Applications" RENAME TO "Clients";--> statement-breakpoint
ALTER TABLE "Notes" RENAME COLUMN "applicationId" TO "clientId";--> statement-breakpoint
ALTER TABLE "OpenIdLoginFlows" RENAME COLUMN "applicationId" TO "clientId";--> statement-breakpoint
ALTER TABLE "AuthorizationCodes" DROP CONSTRAINT "AuthorizationCodes_clientId_Applications_client_id_fk";
--> statement-breakpoint
ALTER TABLE "Notes" DROP CONSTRAINT "Notes_applicationId_Applications_client_id_fk";
--> statement-breakpoint
ALTER TABLE "OpenIdLoginFlows" DROP CONSTRAINT "OpenIdLoginFlows_applicationId_Applications_client_id_fk";
--> statement-breakpoint
ALTER TABLE "Tokens" DROP CONSTRAINT "Tokens_clientId_Applications_client_id_fk";
--> statement-breakpoint
ALTER TABLE "AuthorizationCodes" ADD CONSTRAINT "AuthorizationCodes_clientId_Clients_client_id_fk" FOREIGN KEY ("clientId") REFERENCES "public"."Clients"("client_id") ON DELETE cascade ON UPDATE cascade;--> statement-breakpoint
ALTER TABLE "Notes" ADD CONSTRAINT "Notes_clientId_Clients_client_id_fk" FOREIGN KEY ("clientId") REFERENCES "public"."Clients"("client_id") ON DELETE set null ON UPDATE cascade;--> statement-breakpoint
ALTER TABLE "OpenIdLoginFlows" ADD CONSTRAINT "OpenIdLoginFlows_clientId_Clients_client_id_fk" FOREIGN KEY ("clientId") REFERENCES "public"."Clients"("client_id") ON DELETE cascade ON UPDATE cascade;--> statement-breakpoint
ALTER TABLE "Tokens" ADD CONSTRAINT "Tokens_clientId_Clients_client_id_fk" FOREIGN KEY ("clientId") REFERENCES "public"."Clients"("client_id") ON DELETE cascade ON UPDATE cascade;

File diff suppressed because it is too large Load diff

View file

@ -365,6 +365,13 @@
"when": 1755729662013,
"tag": "0051_stiff_morbius",
"breakpoints": true
},
{
"idx": 52,
"version": "7",
"when": 1755732000165,
"tag": "0052_complete_hellfire_club",
"breakpoints": true
}
]
}

View file

@ -309,7 +309,7 @@ export const RelationshipsRelations = relations(Relationships, ({ one }) => ({
}),
}));
export const Clients = pgTable("Applications", {
export const Clients = pgTable("Clients", {
id: text("client_id").primaryKey(),
secret: text("secret").notNull(),
redirectUris: text("redirect_uris")
@ -494,7 +494,7 @@ export const Notes = pgTable("Notes", {
}),
sensitive: boolean("sensitive").notNull().default(false),
spoilerText: text("spoiler_text").default("").notNull(),
applicationId: text("applicationId").references(() => Clients.id, {
clientId: text("clientId").references(() => Clients.id, {
onDelete: "set null",
onUpdate: "cascade",
}),
@ -528,8 +528,8 @@ export const NotesRelations = relations(Notes, ({ many, one }) => ({
references: [Notes.id],
relationName: "NoteToQuotes",
}),
application: one(Clients, {
fields: [Notes.applicationId],
client: one(Clients, {
fields: [Notes.clientId],
references: [Clients.id],
}),
quotes: many(Notes, {
@ -703,7 +703,7 @@ export const OpenIdLoginFlows = pgTable("OpenIdLoginFlows", {
clientState: text("client_state"),
clientRedirectUri: text("client_redirect_uri"),
clientScopes: text("client_scopes").array(),
applicationId: text("applicationId").references(() => Clients.id, {
clientId: text("clientId").references(() => Clients.id, {
onDelete: "cascade",
onUpdate: "cascade",
}),
@ -713,8 +713,8 @@ export const OpenIdLoginFlows = pgTable("OpenIdLoginFlows", {
export const OpenIdLoginFlowsRelations = relations(
OpenIdLoginFlows,
({ one }) => ({
application: one(Clients, {
fields: [OpenIdLoginFlows.applicationId],
client: one(Clients, {
fields: [OpenIdLoginFlows.clientId],
references: [Clients.id],
}),
}),