Skip to content

Commit be66dc9

Browse files
committed
process and write only one required file
1 parent 249fda0 commit be66dc9

File tree

2 files changed

+24
-12
lines changed

2 files changed

+24
-12
lines changed

packages/cubejs-schema-compiler/src/compiler/converters/CubeSchemaConverter.ts

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,21 +36,27 @@ export class CubeSchemaConverter {
3636

3737
public constructor(protected fileRepository: any, protected converters: CubeConverterInterface[]) {}
3838

39-
protected async prepare(): Promise<void> {
39+
/**
40+
* Parse Schema files from the repository and create a mapping of cube names to schema files.
41+
* If optional `cubeName` parameter is passed - only file with asked cube is parsed and stored.
42+
* @param cubeName
43+
* @protected
44+
*/
45+
protected async prepare(cubeName?: string): Promise<void> {
4046
this.dataSchemaFiles = await this.fileRepository.dataSchemaFiles();
4147

4248
this.dataSchemaFiles.forEach((schemaFile) => {
4349
if (schemaFile.fileName.endsWith('.js')) {
44-
this.transformJS(schemaFile);
50+
this.transformJS(schemaFile, cubeName);
4551
} else if ((schemaFile.fileName.endsWith('.yml') || schemaFile.fileName.endsWith('.yaml')) && !schemaFile.content.match(JINJA_SYNTAX)) {
4652
// Jinja-templated data models are not supported in Rollup Designer yet, so we're ignoring them,
4753
// and if user has chosen the cube from such file - it won't be found during generation.
48-
this.transformYaml(schemaFile);
54+
this.transformYaml(schemaFile, cubeName);
4955
}
5056
});
5157
}
5258

53-
protected transformYaml(schemaFile: SchemaFile) {
59+
protected transformYaml(schemaFile: SchemaFile, filterCubeName?: string) {
5460
if (!schemaFile.content.trim()) {
5561
return;
5662
}
@@ -84,18 +90,22 @@ export class CubeSchemaConverter {
8490

8591
const cubeName = (cubeNamePair?.value as YAML.Scalar).value;
8692

87-
if (cubeName && typeof cubeName === 'string') {
93+
if (cubeName && typeof cubeName === 'string' && (!filterCubeName || cubeName === filterCubeName)) {
8894
this.parsedFiles[cubeName] = {
8995
fileName: schemaFile.fileName,
9096
yaml: yamlDoc,
9197
cubeDefinition: cubeNode,
9298
};
99+
100+
if (cubeName === filterCubeName) {
101+
return;
102+
}
93103
}
94104
}
95105
}
96106
}
97107

98-
protected transformJS(schemaFile: SchemaFile) {
108+
protected transformJS(schemaFile: SchemaFile, filterCubeName?: string) {
99109
const ast = this.parseJS(schemaFile);
100110

101111
traverse(ast, {
@@ -117,7 +127,7 @@ export class CubeSchemaConverter {
117127
throw new Error(`Error parsing ${schemaFile.fileName}`);
118128
}
119129

120-
if (t.isObjectExpression(args[1]?.node) && ast != null) {
130+
if (t.isObjectExpression(args[1]?.node) && ast != null && (!filterCubeName || cubeName === filterCubeName)) {
121131
this.parsedFiles[cubeName] = {
122132
fileName: schemaFile.fileName,
123133
ast,
@@ -150,8 +160,8 @@ export class CubeSchemaConverter {
150160
}
151161
}
152162

153-
public async generate() {
154-
await this.prepare();
163+
public async generate(cubeName?: string) {
164+
await this.prepare(cubeName);
155165

156166
this.converters.forEach((converter) => {
157167
converter.convert(this.parsedFiles);

packages/cubejs-server-core/src/core/DevServer.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -620,7 +620,7 @@ export class DevServer {
620620
]);
621621

622622
try {
623-
await schemaConverter.generate();
623+
await schemaConverter.generate(cubeName);
624624
} catch (error) {
625625
return res.status(400).json({ error: (error as Error).message || error });
626626
}
@@ -629,9 +629,11 @@ export class DevServer {
629629
({ cubeName: currentCubeName }) => currentCubeName === cubeName
630630
);
631631

632-
if (file) {
633-
this.cubejsServer.repository.writeDataSchemaFile(file.fileName, file.source);
632+
if (!file) {
633+
return res.status(400).json({ error: `The schema file for "${cubeName}" cube was not found or could not be updated. Only JS and non-templated YAML files are supported.` });
634634
}
635+
636+
this.cubejsServer.repository.writeDataSchemaFile(file.fileName, file.source);
635637
return res.json('ok');
636638
}));
637639
}

0 commit comments

Comments
 (0)