Skip to content

Commit 71c4458

Browse files
committed
fix: type generation with tablesdb
1 parent 8f410f7 commit 71c4458

File tree

2 files changed

+49
-15
lines changed

2 files changed

+49
-15
lines changed

templates/cli/lib/commands/types.js.twig

Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -100,22 +100,56 @@ const typesCommand = actionRunner(async (rawOutputDirectory, {language, strict})
100100
fs.mkdirSync(outputDirectory, { recursive: true });
101101
}
102102

103-
const collections = localConfig.getCollections();
104-
if (collections.length === 0) {
105-
const configFileName = path.basename(localConfig.path);
106-
throw new Error(`No collections found in configuration. Make sure ${configFileName} exists and contains collections.`);
103+
// Try tables first, fallback to collections
104+
let tables = localConfig.getTables();
105+
let collections = [];
106+
let dataSource = 'tables';
107+
108+
if (tables.length === 0) {
109+
collections = localConfig.getCollections();
110+
dataSource = 'collections';
111+
112+
if (collections.length === 0) {
113+
const configFileName = path.basename(localConfig.path);
114+
throw new Error(`No tables or collections found in configuration. Make sure ${configFileName} exists and contains tables or collections.`);
115+
}
116+
}
117+
118+
// Use tables if available, otherwise use collections
119+
let dataItems = tables.length > 0 ? tables : collections;
120+
const itemType = tables.length > 0 ? 'tables' : 'collections';
121+
122+
// Normalize tables data: rename 'columns' to 'attributes' for template compatibility
123+
if (tables.length > 0) {
124+
dataItems = dataItems.map(table => {
125+
const { columns, ...rest } = table;
126+
return {
127+
...rest,
128+
attributes: (columns || []).map(column => {
129+
if (column.relatedTable) {
130+
const { relatedTable, ...columnRest } = column;
131+
return {
132+
...columnRest,
133+
relatedCollection: relatedTable
134+
};
135+
}
136+
return column;
137+
})
138+
};
139+
});
107140
}
108141

109-
log(`Found ${collections.length} collections: ${collections.map(c => c.name).join(", ")}`);
142+
log(`Found ${dataItems.length} ${itemType}: ${dataItems.map(c => c.name).join(", ")}`);
110143

111-
const totalAttributes = collections.reduce((count, collection) => count + collection.attributes.length, 0);
112-
log(`Found ${totalAttributes} attributes across all collections`);
144+
const totalAttributes = dataItems.reduce((count, item) => count + (item.attributes || []).length, 0);
145+
log(`Found ${totalAttributes} attributes across all ${itemType}`);
113146

114147
const templater = ejs.compile(meta.getTemplate());
115148

116149
if (meta.isSingleFile()) {
117150
const content = templater({
118-
collections,
151+
collections: dataItems,
152+
tables: dataItems,
119153
strict,
120154
...templateHelpers,
121155
getType: meta.getType,
@@ -126,23 +160,23 @@ const typesCommand = actionRunner(async (rawOutputDirectory, {language, strict})
126160
fs.writeFileSync(destination, content);
127161
log(`Added types to ${destination}`);
128162
} else {
129-
for (const collection of collections) {
163+
for (const item of dataItems) {
130164
const content = templater({
131-
collections,
132-
collection,
165+
collections: dataItems,
166+
collection: item,
133167
strict,
134168
...templateHelpers,
135169
getType: meta.getType,
136170
});
137171

138-
const destination = path.join(outputDirectory, meta.getFileName(collection));
172+
const destination = path.join(outputDirectory, meta.getFileName(item));
139173

140174
fs.writeFileSync(destination, content);
141-
log(`Added types for ${collection.name} to ${destination}`);
175+
log(`Added types for ${item.name} to ${destination}`);
142176
}
143177
}
144178

145-
success(`Generated types for all the listed collections`);
179+
success(`Generated types for all the listed ${itemType}`);
146180
});
147181

148182
const types = new Command("types")

templates/cli/lib/config.js.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ const KeysColumns = new Set([
5555
// enum
5656
"elements",
5757
// relationship
58-
"relatedCollection",
58+
"relatedTable",
5959
"relationType",
6060
"twoWay",
6161
"twoWayKey",

0 commit comments

Comments
 (0)