diff --git a/.eslintrc.json b/.eslintrc.json index 25f9f1c..0f593d9 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -13,7 +13,7 @@ "parserOptions": { "ecmaVersion": "latest", "sourceType": "module", - "project": ["tsconfig.dev.json"] + "project": ["tsconfig.json"] }, "plugins": ["@typescript-eslint"], "rules": { diff --git a/package.json b/package.json index 2d1a0af..16d7f33 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,7 @@ "scripts": { "test": "vitest", "prepare": "npm run build", - "build": "rm -rf ./dist && tsc", + "build": "rm -rf ./dist && tsc -p tsconfig.build.json", "lint": "eslint .", "format": "prettier --write .", "checkTs": "tsc --noEmit", diff --git a/src/factory.spec.ts b/src/factory.spec.ts new file mode 100644 index 0000000..6aa9b7d --- /dev/null +++ b/src/factory.spec.ts @@ -0,0 +1,27 @@ +import { describe, it, expect, beforeEach, vi } from "vitest"; +import { Factory } from "./factory.js"; +import { Row } from "./row.js"; +import client from "./client.js"; + +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/tsconfig.build.json b/tsconfig.build.json new file mode 100644 index 0000000..41c105e --- /dev/null +++ b/tsconfig.build.json @@ -0,0 +1,15 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "outDir": "./dist", + "noEmit": false + }, + "exclude": [ + "./dist/**/*", + "**/*.spec.ts", + "vitest.config.ts", + "vitest.setup.ts", + ".baserowrc", + "__generated__" + ] +} diff --git a/tsconfig.dev.json b/tsconfig.dev.json deleted file mode 100644 index b21c370..0000000 --- a/tsconfig.dev.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "extends": "./tsconfig.json", - "compilerOptions": { - "noEmit": true, - "outDir": null - }, - "exclude": ["./dist/**/*", ".baserowrc"] -} diff --git a/tsconfig.json b/tsconfig.json index 7b5faaa..0947fe3 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,24 +1,19 @@ { + "compilerOptions": { "target": "ESNext", "module": "NodeNext", "moduleResolution": "NodeNext", "declaration": true, - "outDir": "./dist", "declarationDir": "./dist", "rootDir": ".", "esModuleInterop": true, "forceConsistentCasingInFileNames": true, "strict": true, "skipLibCheck": true, - "types": ["node"] + "types": ["node"], + "noEmit": true, + "outDir": null }, - "exclude": [ - "./dist/**/*", - "**/*.spec.ts", - "vitest.config.ts", - "vitest.setup.ts", - ".baserowrc", - "__generated__" - ] -} \ No newline at end of file + "exclude": ["./dist/**/*", ".baserowrc"] +} 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 }, +}); diff --git a/vitest.setup.ts b/vitest.setup.ts index 850aba9..5ee1151 100644 --- a/vitest.setup.ts +++ b/vitest.setup.ts @@ -1,3 +1,12 @@ import { vi } from "vitest"; vi.mock("./src/client"); +vi.mock("./src/getConfig", () => ({ + getConfig: vi.fn(() => ({ + url: "the_baserow_url", + tables: {}, + databaseToken: "the_database_token", + outDir: "the_out_dir", + config: "the_config", + })), +}));