Skip to content

Commit

Permalink
Merge pull request #37 from narthur/get-all
Browse files Browse the repository at this point in the history
default to get all
  • Loading branch information
narthur committed May 6, 2024
2 parents c9cabc9 + 2c7d324 commit a512c9f
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 32 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"parserOptions": {
"ecmaVersion": "latest",
"sourceType": "module",
"project": ["tsconfig.dev.json"]
"project": ["tsconfig.json"]
},
"plugins": ["@typescript-eslint"],
"rules": {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
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.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);
});
});
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
15 changes: 15 additions & 0 deletions tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "./dist",
"noEmit": false
},
"exclude": [
"./dist/**/*",
"**/*.spec.ts",
"vitest.config.ts",
"vitest.setup.ts",
".baserowrc",
"__generated__"
]
}
8 changes: 0 additions & 8 deletions tsconfig.dev.json

This file was deleted.

17 changes: 6 additions & 11 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -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__"
]
}
"exclude": ["./dist/**/*", ".baserowrc"]
}
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 },
});
9 changes: 9 additions & 0 deletions vitest.setup.ts
Original file line number Diff line number Diff line change
@@ -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",
})),
}));

0 comments on commit a512c9f

Please sign in to comment.