2024-06-13 02:45:07 +02:00
|
|
|
import type { InferModelFromColumns, InferSelectModel } from "drizzle-orm";
|
|
|
|
|
import type { PgTableWithColumns } from "drizzle-orm/pg-core";
|
|
|
|
|
|
2024-06-13 10:52:03 +02:00
|
|
|
/**
|
|
|
|
|
* BaseInterface is an abstract class that provides a common interface for all models.
|
|
|
|
|
* It includes methods for saving, deleting, updating, and reloading data.
|
|
|
|
|
*
|
|
|
|
|
* @template Table - The type of the table with columns.
|
|
|
|
|
* @template Columns - The type of the columns inferred from the table.
|
|
|
|
|
*/
|
2024-06-13 02:45:07 +02:00
|
|
|
export abstract class BaseInterface<
|
|
|
|
|
// biome-ignore lint/suspicious/noExplicitAny: This is just an extended interface
|
|
|
|
|
Table extends PgTableWithColumns<any>,
|
|
|
|
|
Columns = InferModelFromColumns<Table["_"]["columns"]>,
|
|
|
|
|
> {
|
2024-06-13 10:52:03 +02:00
|
|
|
/**
|
|
|
|
|
* Constructs a new instance of the BaseInterface.
|
|
|
|
|
*
|
|
|
|
|
* @param data - The data for the model.
|
|
|
|
|
*/
|
2024-06-13 02:45:07 +02:00
|
|
|
constructor(public data: Columns) {}
|
|
|
|
|
|
2024-06-13 10:52:03 +02:00
|
|
|
/**
|
|
|
|
|
* Saves the current state of the model to the database.
|
|
|
|
|
*
|
|
|
|
|
* @returns A promise that resolves with the saved model.
|
|
|
|
|
*/
|
2024-06-13 02:45:07 +02:00
|
|
|
public abstract save(): Promise<Columns>;
|
|
|
|
|
|
2024-06-13 10:52:03 +02:00
|
|
|
/**
|
|
|
|
|
* Deletes the model from the database.
|
|
|
|
|
*
|
|
|
|
|
* @param ids - The ids of the models to delete.
|
|
|
|
|
* @returns A promise that resolves when the deletion is complete.
|
|
|
|
|
*/
|
2024-06-13 02:45:07 +02:00
|
|
|
public abstract delete(ids: string[]): Promise<void>;
|
2024-06-13 10:52:03 +02:00
|
|
|
/**
|
|
|
|
|
* Deletes the model from the database.
|
|
|
|
|
*
|
|
|
|
|
* @returns A promise that resolves when the deletion is complete.
|
|
|
|
|
*/
|
2024-06-13 02:45:07 +02:00
|
|
|
public abstract delete(): Promise<void>;
|
|
|
|
|
|
2024-06-13 10:52:03 +02:00
|
|
|
/**
|
|
|
|
|
* Updates the model with new data.
|
|
|
|
|
*
|
|
|
|
|
* @param newData - The new data for the model.
|
|
|
|
|
* @returns A promise that resolves with the updated model.
|
|
|
|
|
*/
|
2024-06-13 02:45:07 +02:00
|
|
|
public abstract update(
|
|
|
|
|
newData: Partial<InferSelectModel<Table>>,
|
|
|
|
|
): Promise<Columns>;
|
|
|
|
|
|
2024-06-13 10:52:03 +02:00
|
|
|
/**
|
|
|
|
|
* Reloads the model from the database.
|
|
|
|
|
*
|
|
|
|
|
* @returns A promise that resolves when the reloading is complete.
|
|
|
|
|
*/
|
2024-06-13 02:45:07 +02:00
|
|
|
public abstract reload(): Promise<void>;
|
|
|
|
|
}
|