Skip to content

Commit

Permalink
registerRowClass types
Browse files Browse the repository at this point in the history
  • Loading branch information
narthur committed Apr 11, 2024
1 parent 77cd94d commit 17fd14a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
18 changes: 9 additions & 9 deletions src/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,30 @@ import { BaserowConfig, getConfig } from "./getConfig.js";
import { BaserowSdk } from "./index.js";
import { Row, RowOptions, RowType } from "./row.js";

interface RowClass<T extends RowType> {
new (options: RowOptions<T>): Row<T>;
interface RowClass<T extends RowType, R extends Factory> {
new (options: RowOptions<T, R>): Row<T, R>;
}

export abstract class Factory {
protected sdk: BaserowSdk;
protected config: BaserowConfig;
protected classes: Map<number, RowClass<RowType>> = new Map();
protected classes: Map<number, RowClass<RowType, Factory>> = new Map();

constructor() {
this.config = getConfig();
this.sdk = new BaserowSdk(this.config.databaseToken);
}

protected registerRowClass<T extends RowType>(
protected registerRowClass<T extends RowType, R extends Factory>(
tableId: number,
rowClass: RowClass<T>,
rowClass: RowClass<T, R>,
): void {
this.classes.set(tableId, rowClass as RowClass<RowType>);
this.classes.set(tableId, rowClass as RowClass<RowType, Factory>);
}

protected getRowClass<T extends RowType>(
protected getRowClass<T extends RowType, R extends Factory>(
tableId: number,
): RowClass<T> | undefined {
return this.classes.get(tableId) as RowClass<T> | undefined;
): RowClass<T, R> | undefined {
return this.classes.get(tableId) as RowClass<T, R> | undefined;
}
}
8 changes: 4 additions & 4 deletions src/row.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@ import { Factory } from "./factory.js";
import { BaserowSdk } from "./index.js";

export type RowType = Record<string, unknown> & { id: number; order: string };
export type RowOptions<T> = {
export type RowOptions<T extends RowType, R extends Factory> = {
tableId: number;
rowId: number;
row: T;
sdk: BaserowSdk;
repository: Factory;
repository: R;
};
export abstract class Row<T extends RowType> {
export abstract class Row<T extends RowType, R extends Factory> {
protected tableId: number;
protected rowId: number;
protected row: T;
protected sdk: BaserowSdk;
protected repository: Factory;

constructor({ tableId, rowId, row, sdk, repository }: RowOptions<T>) {
constructor({ tableId, rowId, row, sdk, repository }: RowOptions<T, R>) {
this.tableId = tableId;
this.rowId = rowId;
this.row = row;
Expand Down

0 comments on commit 17fd14a

Please sign in to comment.