Skip to content

Commit

Permalink
add repo row class registration
Browse files Browse the repository at this point in the history
  • Loading branch information
narthur committed Apr 11, 2024
1 parent 8abb34c commit d1deba7
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 21 deletions.
12 changes: 7 additions & 5 deletions src/codegen/makeRepositoryMethods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ import { Table } from "../codegen.js";
export default function makeRepositoryMethods(tables: Table[]): string {
return tables
.map(({ id, name }) => {
return ` public async getMany${name}(options: ListRowsOptions = {}): Promise<${name}Row[]> {
const {results} = await this.sdk.listRows<${name}RowType>(${id}, options);
return results.map((row) => new ${name}Row({tableId: ${id}, rowId: row.id, row, sdk: this.sdk, repository: this}));
return ` public async getMany${name}<T extends ${name}Row>(options: ListRowsOptions = {}): Promise<T[]> {
const { results } = await this.sdk.listRows<${name}RowType>(${id}, options);
const rowClass = this.getRowClass(${id}) || ${name}Row;
return results.map((row) => new rowClass({ tableId: ${id}, rowId: row.id, row, sdk: this.sdk, repository: this })) as T[];
}
public async getOne${name}(id: number, options: GetRowOptions = {}): Promise<${name}Row> {
public async getOne${name}<T extends ${name}Row>(id: number, options: GetRowOptions = {}): Promise<T> {
const row = await this.sdk.getRow<${name}RowType>(${id}, id, options);
return new ${name}Row({tableId: ${id}, rowId: row.id, row, sdk: this.sdk, repository: this});
const rowClass = this.getRowClass(${id}) || ${name}Row;
return new rowClass({ tableId: ${id}, rowId: row.id, row, sdk: this.sdk, repository: this }) as T;
}
`;
})
Expand Down
22 changes: 19 additions & 3 deletions src/factory.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,26 @@
import { getConfig } from "./getConfig.js";
import { BaserowConfig, getConfig } from "./getConfig.js";
import { BaserowSdk } from "./index.js";
import { Row, RowOptions, RowType } from "./row.js";

interface RowClass {
new (options: RowOptions<RowType>): Row<RowType>;
}

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

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

protected registerRowClass(tableId: number, rowClass: RowClass): void {
this.classes.set(tableId, rowClass);
}

protected getRowClass(tableId: number): RowClass | undefined {
return this.classes.get(tableId);
}
}
2 changes: 1 addition & 1 deletion src/getConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const schema = z.object({
config: z.string(),
});

type BaserowConfig = z.infer<typeof schema>;
export type BaserowConfig = z.infer<typeof schema>;

export function getConfig(): BaserowConfig {
return schema.parse(rc("baserow"));
Expand Down
19 changes: 7 additions & 12 deletions src/row.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,19 @@
import { BaserowSdk } from "./index.js";

export type RowType = Record<string, unknown> & { id: number; order: string };

export type RowOptions<T> = {
tableId: number;
rowId: number;
row: T;
sdk: BaserowSdk;
};
export abstract class Row<T extends RowType> {
protected tableId: number;
protected rowId: number;
protected row: T;
protected sdk: BaserowSdk;

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

0 comments on commit d1deba7

Please sign in to comment.