Skip to content

Commit

Permalink
feat(api-gateway, server-core): Added dataSources list to extended me…
Browse files Browse the repository at this point in the history
…ta (#5743)
  • Loading branch information
wolfeeeg authored Dec 9, 2022
1 parent 03f0cb1 commit 2c5db32
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 3 deletions.
4 changes: 2 additions & 2 deletions packages/cubejs-api-gateway/src/gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ class ApiGateway {
const metaConfigExtended = await this.getCompilerApi(context).metaConfigExtended({
requestId: context.requestId,
});
const { metaConfig, cubeDefinitions } = metaConfigExtended;
const { metaConfig, cubeDefinitions, dataSources } = metaConfigExtended;

const cubes = this.filterVisibleItemsInMeta(context, metaConfig)
.map((meta) => meta.config)
Expand All @@ -479,7 +479,7 @@ class ApiGateway {
joins: transformJoins(cubeDefinitions[cube.name]?.joins),
preAggregations: transformPreAggregations(cubeDefinitions[cube.name]?.preAggregations),
}));
res({ cubes });
res({ cubes, dataSources });
} catch (e) {
this.handleError({
e,
Expand Down
7 changes: 6 additions & 1 deletion packages/cubejs-server-core/src/core/CompilerApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ export class CompilerApi {
return this.compilers;
}

async getDataSources(cubeEvaluator) {
return Promise.all(cubeEvaluator.cubeNames().map(async cube => cubeEvaluator.cubeFromPath(cube).dataSource ?? 'default'));
}

async createQueryFactory(compilers) {
const { cubeEvaluator } = compilers;

Expand Down Expand Up @@ -180,9 +184,10 @@ export class CompilerApi {
}

async metaConfigExtended(options) {
const { metaTransformer } = await this.getCompilers(options);
const { metaTransformer, cubeEvaluator } = await this.getCompilers(options);
return {
metaConfig: metaTransformer?.cubes,
dataSources: await this.getDataSources(cubeEvaluator),
cubeDefinitions: metaTransformer?.cubeEvaluator?.cubeDefinitions,
};
}
Expand Down
51 changes: 51 additions & 0 deletions packages/cubejs-server-core/test/unit/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,34 @@ cube('Bar', {
]),
};

const repositoryWithDataSource: SchemaFileRepository = {
localPath: () => __dirname,
dataSchemaFiles: () => Promise.resolve([
{
fileName: 'main.js', content: `
cube('Bar', {
sql: 'select * from bar',
measures: {
count: {
type: 'count'
}
},
dataSource: 'dataSource',
dimensions: {
time: {
sql: 'timestamp',
type: 'time'
}
}
});
`,
},
]),
};

const repositoryWithoutContent: SchemaFileRepository = {
localPath: () => __dirname,
dataSchemaFiles: () => Promise.resolve([{ fileName: 'main.js', content: '' }]),
Expand Down Expand Up @@ -335,6 +363,29 @@ describe('index.test', () => {
expect(metaConfigExtended).toHaveProperty('metaConfig');
expect(metaConfigExtended.metaConfig.length).toBeGreaterThan(0);
expect(metaConfigExtended).toHaveProperty('cubeDefinitions');
expect(metaConfigExtended).toHaveProperty('dataSources');
expect(metaConfigExtended.dataSources).toEqual(['default']);
expect(metaConfigExtendedSpy).toHaveBeenCalled();
metaConfigExtendedSpy.mockClear();
});
});

describe('CompilerApi with dataSource', () => {
const logger = jest.fn(() => {});
const compilerApi = new CompilerApi(
repositoryWithDataSource,
async () => 'mysql',
{ logger }
);
const metaConfigExtendedSpy = jest.spyOn(compilerApi, 'metaConfigExtended');

test('CompilerApi metaConfigExtended', async () => {
const metaConfigExtended = await compilerApi.metaConfigExtended({ requestId: 'XXX' });
expect(metaConfigExtended).toHaveProperty('metaConfig');
expect(metaConfigExtended.metaConfig.length).toBeGreaterThan(0);
expect(metaConfigExtended).toHaveProperty('cubeDefinitions');
expect(metaConfigExtended).toHaveProperty('dataSources');
expect(metaConfigExtended.dataSources).toEqual(['dataSource']);
expect(metaConfigExtendedSpy).toHaveBeenCalled();
metaConfigExtendedSpy.mockClear();
});
Expand Down

0 comments on commit 2c5db32

Please sign in to comment.