Skip to content

Commit

Permalink
default to get all
Browse files Browse the repository at this point in the history
  • Loading branch information
narthur committed May 6, 2024
1 parent c9cabc9 commit 07ef1af
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 11 deletions.
27 changes: 27 additions & 0 deletions src/factory.spec.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
41 changes: 35 additions & 6 deletions src/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -29,14 +29,30 @@ export abstract class Factory {
return this.classes.get(tableId) as RowClass<T, R> | undefined;
}

public async getMany<T extends Row, R extends RowType, F extends Factory>(
private async getAll<R extends RowType>(
tableId: number,
defaultClass: RowClass<R, F>,
options: Record<string, unknown> = {},
): Promise<T[]> {
options: Record<string, unknown> & { page?: number } = {},
accumulator: R[] = [],
): Promise<R[]> {
const { results } = await this.sdk.listRows<R>(tableId, options);
if (results.length === 0) {
return accumulator;
}
accumulator.push(...results);
return this.getAll(
tableId,
{ ...options, page: (options.page ?? 0) + 1 },
accumulator,
);
}

private createRows<T extends Row, R extends RowType, F extends Factory>(
tableId: number,
defaultClass: RowClass<R, F>,
rows: R[],
): T[] {
const rowClass = this.getRowClass(tableId) || defaultClass;
return results.map(
return rows.map(
(row) =>
new rowClass({
tableId,
Expand All @@ -47,4 +63,17 @@ export abstract class Factory {
}),
) as T[];
}

public async getMany<T extends Row, R extends RowType, F extends Factory>(
tableId: number,
defaultClass: RowClass<R, F>,
options: Record<string, unknown> = {},
): Promise<T[]> {
const shouldGetAll = options.page === undefined;
const results = shouldGetAll
? await this.getAll<R>(tableId, options)
: (await this.sdk.listRows<R>(tableId, options)).results;

return this.createRows(tableId, defaultClass, results);
}
}
5 changes: 1 addition & 4 deletions src/row.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ export type RowOptions<T extends RowType, R extends Factory> = {
sdk: BaserowSdk;
repository: R;
};
export abstract class Row<
T extends RowType = RowType,
R extends Factory = Factory,
> {
export class Row<T extends RowType = RowType, R extends Factory = Factory> {
protected tableId: number;
protected rowId: number;
protected row: T;
Expand Down
4 changes: 3 additions & 1 deletion vitest.config.ts
Original file line number Diff line number Diff line change
@@ -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 },
});

0 comments on commit 07ef1af

Please sign in to comment.