refactor(database): 🚚 Rename Application to Client everywhere

This commit is contained in:
Jesse Wierzbinski 2025-08-21 01:21:32 +02:00
parent 6f97903f3b
commit 1a0a27bee1
No known key found for this signature in database
25 changed files with 2549 additions and 90 deletions

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");