mirror of
https://github.com/versia-pub/server.git
synced 2026-03-13 22:09:16 +01:00
refactor(database): 🚚 Rename Application to Client everywhere
This commit is contained in:
parent
c55be8a862
commit
87da7796be
25 changed files with 2549 additions and 90 deletions
|
|
@ -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");
|
||||
|
|
|
|||
|
|
@ -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";
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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> {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue