Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use user field name when getting linked rows #34

Merged
merged 1 commit into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion src/codegen/makeGetter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,17 @@ function run(field: Partial<FieldDefinition> = {}): string {
{
id: 1,
name: "table_name",
fields: [],
fields: [
{
id: 2,
name: "field_name",
table_id: 0,
order: 0,
type: "",
primary: false,
read_only: false,
},
],
},
]);
}
Expand Down Expand Up @@ -98,6 +108,14 @@ describe("makeGetter", () => {
},
"this.getLinkedRows",
],
[
{
type: "link_row",
link_row_table_id: 1,
link_row_related_field_id: 2,
},
`"field_name"`,
],
])("%s => `%s`", (field, expected) => {
expect(run(field)).toContain(expected);
});
Expand Down
12 changes: 7 additions & 5 deletions src/codegen/makeGetter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ import { FieldDefinition, ListFieldsResponse } from "../index.js";
import { getRawType } from "./getRawType.js";
import { toCamelCase } from "./toCamelCase.js";

function getForeignTable(
field: FieldDefinition,
tables: Table[],
): { id: number; name: string } {
function getForeignTable(field: FieldDefinition, tables: Table[]): Table {
if (!field.link_row_table_id) {
throw new Error("link_row_table_id is missing");
}
Expand Down Expand Up @@ -59,7 +56,12 @@ function getBody(field: FieldDefinition, tables: Table[]): string {

if (field.type === "link_row") {
const foreignTable = getForeignTable(field, tables);
return `return this.getLinkedRows(${field.link_row_table_id}, ${field.link_row_related_field_id}, ${foreignTable.name}Row);`;
const tableId = field.link_row_table_id;
const fieldId = field.link_row_related_field_id;
const foreignField = foreignTable.fields.find((f) => f.id === fieldId);
const fieldName = foreignField?.name;
const className = `${foreignTable.name}Row`;
return `return this.getLinkedRows(${tableId}, "${fieldName}", ${className});`;
}

if (field.type === "date" || field.formula_type === "date") {
Expand Down
4 changes: 2 additions & 2 deletions src/row.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { describe, it, expect, vi } from "vitest";

class MyRow extends Row {
public async doSomething(): Promise<Row<RowType, Factory>[]> {
return this.getLinkedRows(1, 2, MyRow);
return this.getLinkedRows(1, "the_field", MyRow);
}
}

Expand All @@ -31,7 +31,7 @@ describe("Row", () => {
filters: expect.objectContaining({
filters: expect.arrayContaining([
expect.objectContaining({
field: 2,
field: "the_field",
}),
]),
}),
Expand Down
5 changes: 2 additions & 3 deletions src/row.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,16 @@ export abstract class Row<

protected getLinkedRows<T extends Row, R extends RowType, F extends Factory>(
tableId: number,
fieldId: number,
field: string,
defaultClass: RowClass<R, F>,
): Promise<T[]> {
return this.repository.getMany(tableId, defaultClass, {
user_field_names: false,
filters: {
filter_type: "AND",
filters: [
{
type: "link_row_has",
field: fieldId,
field,
value: this.getId().toString(),
},
],
Expand Down
5 changes: 3 additions & 2 deletions tsconfig.dev.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"noEmit": true
"noEmit": true,
"outDir": null
},
"exclude": ["dist", ".baserowrc"]
"exclude": ["./dist/**/*", ".baserowrc"]
}
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"types": ["node"]
},
"exclude": [
"dist",
"./dist/**/*",
"**/*.spec.ts",
"vitest.config.ts",
"vitest.setup.ts",
Expand Down