Skip to content

Commit c514fe6

Browse files
authored
Merge pull request #1187 from appwrite/fix-type-generation-tablesdb
fix: type generation with tablesdb
2 parents 8f410f7 + 5b05e81 commit c514fe6

File tree

2 files changed

+51
-15
lines changed

2 files changed

+51
-15
lines changed

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

Lines changed: 50 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -100,22 +100,58 @@ 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(", ")}`);
143+
144+
// Use columns if available, otherwise use attributes
145+
const resourceType = tables.length > 0 ? 'columns' : 'attributes';
110146

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}`);
113149

114150
const templater = ejs.compile(meta.getTemplate());
115151

116152
if (meta.isSingleFile()) {
117153
const content = templater({
118-
collections,
154+
collections: dataItems,
119155
strict,
120156
...templateHelpers,
121157
getType: meta.getType,
@@ -126,23 +162,23 @@ const typesCommand = actionRunner(async (rawOutputDirectory, {language, strict})
126162
fs.writeFileSync(destination, content);
127163
log(`Added types to ${destination}`);
128164
} else {
129-
for (const collection of collections) {
165+
for (const item of dataItems) {
130166
const content = templater({
131-
collections,
132-
collection,
167+
collections: dataItems,
168+
collection: item,
133169
strict,
134170
...templateHelpers,
135171
getType: meta.getType,
136172
});
137173

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

140176
fs.writeFileSync(destination, content);
141-
log(`Added types for ${collection.name} to ${destination}`);
177+
log(`Added types for ${item.name} to ${destination}`);
142178
}
143179
}
144180

145-
success(`Generated types for all the listed collections`);
181+
success(`Generated types for all the listed ${itemType}`);
146182
});
147183

148184
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)