@@ -100,22 +100,58 @@ const typesCommand = actionRunner(async (rawOutputDirectory, {language, strict})
100
100
fs.mkdirSync(outputDirectory, { recursive: true });
101
101
}
102
102
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
+ });
107
140
}
108
141
109
- log(`Found ${collections.length} collections: ${collections.map(c => c.name).join(", ")}`);
142
+ log(`Found ${dataItems.length} ${itemType}: ${dataItems.map(c => c.name).join(", ")}`);
143
+
144
+ // Use columns if available, otherwise use attributes
145
+ const resourceType = tables.length > 0 ? 'columns' : 'attributes';
110
146
111
- const totalAttributes = collections .reduce((count, collection ) => count + collection .attributes.length, 0);
112
- log(`Found ${totalAttributes} attributes across all collections `);
147
+ const totalAttributes = dataItems .reduce((count, item ) => count + (item .attributes || []) .length, 0);
148
+ log(`Found ${totalAttributes} ${resourceType} across all ${itemType} `);
113
149
114
150
const templater = ejs.compile(meta.getTemplate());
115
151
116
152
if (meta.isSingleFile()) {
117
153
const content = templater({
118
- collections,
154
+ collections: dataItems ,
119
155
strict,
120
156
...templateHelpers,
121
157
getType: meta.getType,
@@ -126,23 +162,23 @@ const typesCommand = actionRunner(async (rawOutputDirectory, {language, strict})
126
162
fs.writeFileSync(destination, content);
127
163
log(`Added types to ${destination}`);
128
164
} else {
129
- for (const collection of collections ) {
165
+ for (const item of dataItems ) {
130
166
const content = templater({
131
- collections,
132
- collection,
167
+ collections: dataItems ,
168
+ collection: item ,
133
169
strict,
134
170
...templateHelpers,
135
171
getType: meta.getType,
136
172
});
137
173
138
- const destination = path.join(outputDirectory, meta.getFileName(collection ));
174
+ const destination = path.join(outputDirectory, meta.getFileName(item ));
139
175
140
176
fs.writeFileSync(destination, content);
141
- log(`Added types for ${collection .name} to ${destination}`);
177
+ log(`Added types for ${item .name} to ${destination}`);
142
178
}
143
179
}
144
180
145
- success(`Generated types for all the listed collections `);
181
+ success(`Generated types for all the listed ${itemType} `);
146
182
});
147
183
148
184
const types = new Command("types")
0 commit comments