Initial commit
This commit is contained in:
commit
47d0ec687b
15 changed files with 1704 additions and 0 deletions
49
src/instance.ts
Normal file
49
src/instance.ts
Normal file
|
@ -0,0 +1,49 @@
|
|||
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;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue