49 lines
1.1 KiB
TypeScript
49 lines
1.1 KiB
TypeScript
import type { Table } from "./table";
|
|
|
|
export type ExtractRowShape<T> = T extends Table<infer R> ? R : never;
|
|
|
|
/**
|
|
* Represents an instance of a row in a table of a database.
|
|
* Wraps the raw Row data and provides methods for interacting with it.
|
|
*/
|
|
export abstract class Instance<
|
|
TableType extends Table<unknown>,
|
|
SerializedInstanceType = void,
|
|
> {
|
|
protected row: ExtractRowShape<TableType>;
|
|
protected table: TableType;
|
|
|
|
constructor(table: TableType, row: ExtractRowShape<TableType>) {
|
|
this.row = row;
|
|
this.table = table;
|
|
}
|
|
|
|
/**
|
|
* Saves any changes made to the row back to the database.
|
|
*/
|
|
save() {
|
|
this.table.update(this.row);
|
|
}
|
|
|
|
/**
|
|
* Serializes the instance into a generic format
|
|
*/
|
|
serialize(): SerializedInstanceType {
|
|
throw new Error("Not implemented");
|
|
}
|
|
|
|
/**
|
|
* @returns The row data as a JSON string.
|
|
*/
|
|
toJSON(): string {
|
|
return JSON.stringify(this.row);
|
|
}
|
|
|
|
/**
|
|
* Exports the raw row data.
|
|
* @returns The raw row data.
|
|
*/
|
|
export(): ExtractRowShape<TableType> {
|
|
return this.row;
|
|
}
|
|
}
|