Skip to content

Commit

Permalink
refactor repository codegen
Browse files Browse the repository at this point in the history
  • Loading branch information
narthur committed Apr 11, 2024
1 parent c11298c commit 8abb34c
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 23 deletions.
21 changes: 5 additions & 16 deletions src/codegen.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { BaserowSdk, ListFieldsResponse } from "./index.js";
import fs from "fs";
import makeType from "./codegen/makeType.js";
import makeClassMethods from "./codegen/makeClassMethods.js";
import makeModelMethods from "./codegen/makeModelMethods.js";
import path from "path";
import { getConfig } from "./getConfig.js";
import makeRepositoryMethods from "./codegen/makeRepositoryMethods.js";

export type Table = { id: number; name: string; fields: ListFieldsResponse };

Expand Down Expand Up @@ -67,7 +68,7 @@ export class ${tableName}Row extends Row<${tableName}RowType> {
super(options);
this.repository = options.repository;
}
${makeClassMethods(table.id, tables)}
${makeModelMethods(table.id, tables)}
}`;

fs.writeFileSync(`${outDir}/${tableName}.ts`, typeDef);
Expand All @@ -81,21 +82,9 @@ ${Object.keys(config.tables)
`import { ${tableName}Row, ${tableName}RowType } from './${tableName}.js';`,
)
.join("\n")}
export class Repository extends Factory {
${Object.keys(config.tables)
.map(
(tableName) =>
`public async getMany${tableName}(options: ListRowsOptions = {}): Promise<${tableName}Row[]> {
const {results} = await this.sdk.listRows<${tableName}RowType>(${config.tables[tableName]}, options);
return results.map((row) => new ${tableName}Row({tableId: ${config.tables[tableName]}, rowId: row.id, row, sdk: this.sdk, repository: this}));
}
public async getOne${tableName}(id: number, options: GetRowOptions = {}): Promise<${tableName}Row> {
const row = await this.sdk.getRow<${tableName}RowType>(${config.tables[tableName]}, id, options);
return new ${tableName}Row({tableId: ${config.tables[tableName]}, rowId: row.id, row, sdk: this.sdk, repository: this});
}
`,
)
.join("\n")}
${makeRepositoryMethods(tables)}
}`;

fs.writeFileSync(`${outDir}/Repository.ts`, factoryCode);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { describe, it, expect } from "vitest";
import makeClassMethods from "./makeClassMethods";
import f from "../test/fixtures/fieldDefinition";
import makeModelMethods from "./makeModelMethods";
import f from "../test/fixtures/fieldDefinition.js";
import { ListFieldsResponse } from "../index.js";

function run(fields: ListFieldsResponse = []): string {
return makeClassMethods(1, [{ id: 1, name: "the_table_name", fields }]);
return makeModelMethods(1, [{ id: 1, name: "the_table_name", fields }]);
}

describe("makeClassMethods", () => {
Expand Down Expand Up @@ -134,7 +134,7 @@ describe("makeClassMethods", () => {

it("accepts id array for link_row setters", () => {
expect(
makeClassMethods(1, [
makeModelMethods(1, [
{
id: 1,
name: "the_table_name",
Expand All @@ -147,7 +147,7 @@ describe("makeClassMethods", () => {

it("uses repository to get linked row objects", () => {
expect(
makeClassMethods(1, [
makeModelMethods(1, [
{
id: 1,
name: "the_table_name",
Expand All @@ -160,7 +160,7 @@ describe("makeClassMethods", () => {

it("properly set link row getter return type", () => {
expect(
makeClassMethods(1, [
makeModelMethods(1, [
{
id: 1,
name: "the_table_name",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ListFieldsResponse } from "../index.js";
import { makeGetter } from "./makeGetter.js";
import { makeSetter } from "./makeSetter.js";

export default function makeClassMethods(
export default function makeModelMethods(
tableId: number,
tables: { id: number; name: string; fields: ListFieldsResponse }[],
): string {
Expand Down
18 changes: 18 additions & 0 deletions src/codegen/makeRepositoryMethods.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
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}));
}
public async getOne${name}(id: number, options: GetRowOptions = {}): Promise<${name}Row> {
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});
}
`;
})
.join("\n");
}

0 comments on commit 8abb34c

Please sign in to comment.