From 07ef1afcb84ba0a5d8c45ae18d75b257b6b975fb Mon Sep 17 00:00:00 2001 From: Nathan Arthur Date: Mon, 6 May 2024 10:33:54 -0400 Subject: [PATCH] default to get all --- src/factory.spec.ts | 27 +++++++++++++++++++++++++++ src/factory.ts | 41 +++++++++++++++++++++++++++++++++++------ src/row.ts | 5 +---- vitest.config.ts | 4 +++- 4 files changed, 66 insertions(+), 11 deletions(-) create mode 100644 src/factory.spec.ts diff --git a/src/factory.spec.ts b/src/factory.spec.ts new file mode 100644 index 0000000..4ed1c2b --- /dev/null +++ b/src/factory.spec.ts @@ -0,0 +1,27 @@ +import { describe, it, expect, beforeEach, vi } from "vitest"; +import { Factory } from "./factory"; +import { Row } from "./row"; +import client from "./client"; + +const factory = new Factory(); + +describe("Factory", () => { + beforeEach(() => { + vi.mocked(client.get) + .mockResolvedValue({ + data: { + results: [], + }, + }) + .mockResolvedValueOnce({ + data: { + results: [{}], + }, + }); + }); + + it("gets all", async () => { + await factory.getMany(1, Row); + expect(client.get).toBeCalledTimes(2); + }); +}); diff --git a/src/factory.ts b/src/factory.ts index 5cce1b2..56fdced 100644 --- a/src/factory.ts +++ b/src/factory.ts @@ -2,7 +2,7 @@ import { BaserowConfig, getConfig } from "./getConfig.js"; import { BaserowSdk, RowClass } from "./index.js"; import { Row, RowType } from "./row.js"; -export abstract class Factory { +export class Factory { public readonly config: BaserowConfig; protected sdk: BaserowSdk; @@ -29,14 +29,30 @@ export abstract class Factory { return this.classes.get(tableId) as RowClass | undefined; } - public async getMany( + private async getAll( tableId: number, - defaultClass: RowClass, - options: Record = {}, - ): Promise { + options: Record & { page?: number } = {}, + accumulator: R[] = [], + ): Promise { const { results } = await this.sdk.listRows(tableId, options); + if (results.length === 0) { + return accumulator; + } + accumulator.push(...results); + return this.getAll( + tableId, + { ...options, page: (options.page ?? 0) + 1 }, + accumulator, + ); + } + + private createRows( + tableId: number, + defaultClass: RowClass, + rows: R[], + ): T[] { const rowClass = this.getRowClass(tableId) || defaultClass; - return results.map( + return rows.map( (row) => new rowClass({ tableId, @@ -47,4 +63,17 @@ export abstract class Factory { }), ) as T[]; } + + public async getMany( + tableId: number, + defaultClass: RowClass, + options: Record = {}, + ): Promise { + const shouldGetAll = options.page === undefined; + const results = shouldGetAll + ? await this.getAll(tableId, options) + : (await this.sdk.listRows(tableId, options)).results; + + return this.createRows(tableId, defaultClass, results); + } } diff --git a/src/row.ts b/src/row.ts index 349b397..57bf369 100644 --- a/src/row.ts +++ b/src/row.ts @@ -9,10 +9,7 @@ export type RowOptions = { sdk: BaserowSdk; repository: R; }; -export abstract class Row< - T extends RowType = RowType, - R extends Factory = Factory, -> { +export class Row { protected tableId: number; protected rowId: number; protected row: T; diff --git a/vitest.config.ts b/vitest.config.ts index 79777f8..38976e2 100644 --- a/vitest.config.ts +++ b/vitest.config.ts @@ -1,2 +1,4 @@ import { defineConfig } from "vitest/config"; -export default defineConfig({ test: { setupFiles: "vitest.setup.ts" } }); +export default defineConfig({ + test: { setupFiles: "vitest.setup.ts", mockReset: true }, +});