Skip to content
Closed
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
13 changes: 13 additions & 0 deletions lib/commands/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,19 @@ const typesCommand = actionRunner(async (rawOutputDirectory, {language, strict})
relatedCollection: relatedTable
};
}
// Handle relationship attributes missing relatedTable/relatedCollection
if (column.type === 'relationship' && !column.relatedCollection && column.twoWayKey) {
// Try to find the related collection by looking for a collection that has the twoWayKey attribute
const relatedTable = tables.find(table =>
table.columns && table.columns.some(col => col.key === column.twoWayKey)
);
if (relatedTable) {
return {
...column,
relatedCollection: relatedTable.$id
};
}
}
return column;
})
};
Expand Down
10 changes: 6 additions & 4 deletions lib/type-generation/languages/typescript.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ class TypeScript extends LanguageMeta {
type = "boolean";
break;
case AttributeType.RELATIONSHIP:
if (!attribute.relatedCollection) {
console.warn(`Relationship attribute '${attribute.key}' is missing relatedCollection field. This usually indicates malformed relationship data.`,attribute);
type = "any";
break;
}
const relatedCollection = collections.find(c => c.$id === attribute.relatedCollection);
if (!relatedCollection) {
throw new Error(`Related collection with ID '${attribute.relatedCollection}' not found.`);
Expand All @@ -44,9 +49,6 @@ class TypeScript extends LanguageMeta {
if (attribute.array) {
type += "[]";
}
if (!attribute.required && attribute.default === null) {
type += " | null";
}
return type;
}

Expand Down Expand Up @@ -90,7 +92,7 @@ export enum <%- toPascalCase(attribute.key) %> {
<% for (const [index, collection] of Object.entries(collections)) { -%>
export type <%- toPascalCase(collection.name) %> = Models.Row & {
<% for (const attribute of collection.attributes) { -%>
<%- strict ? toCamelCase(attribute.key) : attribute.key %>: <%- getType(attribute, collections) %>;
<%- strict ? toCamelCase(attribute.key) : attribute.key %><%- attribute.required ? '' : '?' %>: <%- getType(attribute, collections) %>;
<% } -%>
}<% if (index < collections.length - 1) { %>
<% } %>
Expand Down