Skip to content

Commit

Permalink
work on getters and setters
Browse files Browse the repository at this point in the history
  • Loading branch information
narthur committed Apr 11, 2024
1 parent 29ead4b commit 36d924b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export class ${tableName}Row extends Row<${tableName}RowType> {
super(options);
this.repository = options.repository;
}
${makeClassMethods(table.id, tables)}
${makeClassMethods(table.id, tables)}
}`;

fs.writeFileSync(`${outDir}/${tableName}.ts`, typeDef);
Expand Down
6 changes: 2 additions & 4 deletions src/codegen/makeGetter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,7 @@ function getBody(field: FieldDefinition, tables: Table[]): string {

if (field.type === "link_row") {
const foreignTable = getForeignTable(field, tables);
return `return Promise.all(${query}.map((r) => {
return this.repository.${toCamelCase(`get one ${foreignTable.name}`)}(r.id);
}));`;
return `return Promise.all(${query}.map((r) => this.repository.${toCamelCase(`get one ${foreignTable.name}`)}(r.id)));`;
}

if (field.type === "date" || field.formula_type === "date") {
Expand All @@ -99,5 +97,5 @@ export function makeGetter(
const rt = getReturnType(field, tables);
const bd = getBody(field, tables);

return `\tpublic ${fn}(): ${rt} { ${bd} }`;
return `\n public ${fn}(): ${rt} {\n ${bd}\n }`;
}
20 changes: 11 additions & 9 deletions src/codegen/makeSetter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@ import { FieldDefinition } from "../index.js";
import { makeFieldType } from "./makeFieldType.js";
import { toCamelCase } from "./toCamelCase.js";

export function makeSetter(field: FieldDefinition): string {
function getInputType(field: FieldDefinition): string {
if (field.type === "link_row") {
return `public ${toCamelCase(`set ${field.name}`)}(value: number[]): Promise<void> {
return this.setField("${field.name}", value);
}`;
return "number[]";
}

return `public ${toCamelCase(`set ${field.name}`)}(value: ${makeFieldType(
field,
)}): Promise<void> {
return this.setField("${field.name}", value);
}`;
return makeFieldType(field);
}

export function makeSetter(field: FieldDefinition): string {
const fn = toCamelCase(`set ${field.name}`);
const it = getInputType(field);
const bd = `return this.setField("${field.name}", value);`;

return `\n public ${fn}(value: ${it}): Promise<void> {\n ${bd}\n }`;
}

0 comments on commit 36d924b

Please sign in to comment.