Skip to content

Commit 6d0c66a

Browse files
authored
feat(schema-compiler): Introduce public field (alias for shown) (#6361)
1 parent 0daa5c1 commit 6d0c66a

File tree

6 files changed

+142
-53
lines changed

6 files changed

+142
-53
lines changed

packages/cubejs-schema-compiler/src/compiler/CubeToMetaTransformer.js

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,26 @@ export class CubeToMetaTransformer {
1515
}
1616

1717
compile(cubes, errorReporter) {
18-
// eslint-disable-next-line no-multi-assign
19-
this.cubes = this.queries = this.cubeSymbols.cubeList
18+
this.cubes = this.cubeSymbols.cubeList
2019
.filter(this.cubeValidator.isCubeValid.bind(this.cubeValidator))
2120
.map((v) => this.transform(v, errorReporter.inContext(`${v.name} cube`)))
2221
.filter(Boolean);
22+
23+
/**
24+
* @deprecated
25+
* @protected
26+
*/
27+
this.queries = this.cubes;
2328
}
2429

30+
/**
31+
* @protected
32+
*/
2533
transform(cube) {
2634
const cubeTitle = cube.title || this.titleize(cube.name);
27-
35+
2836
return {
37+
isVisible: this.isVisible(cube, true),
2938
config: {
3039
name: cube.name,
3140
title: cubeTitle,
@@ -87,13 +96,24 @@ export class CubeToMetaTransformer {
8796
)(this.queries);
8897
}
8998

99+
/**
100+
* @protected
101+
*/
90102
isVisible(symbol, defaultValue) {
103+
if (symbol.public != null) {
104+
return symbol.public;
105+
}
106+
107+
// TODO: Deprecated, should be removed in the future
91108
if (symbol.visible != null) {
92109
return symbol.visible;
93110
}
111+
112+
// TODO: Deprecated, should be removed in the futur
94113
if (symbol.shown != null) {
95114
return symbol.shown;
96115
}
116+
97117
return defaultValue;
98118
}
99119

packages/cubejs-schema-compiler/src/compiler/CubeValidator.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export const nonStringFields = new Set([
1515
'unionWithSourceData',
1616
'rewriteQueries',
1717
'shown',
18+
'public',
1819
'subQuery',
1920
'propagateFiltersToSubQuery',
2021
'incremental',
@@ -74,6 +75,7 @@ const BaseDimensionWithoutSubQuery = {
7475
valuesAsSegments: Joi.boolean().strict(),
7576
primaryKey: Joi.boolean().strict(),
7677
shown: Joi.boolean().strict(),
78+
public: Joi.boolean().strict(),
7779
title: Joi.string(),
7880
description: Joi.string(),
7981
suggestFilterValues: Joi.boolean().strict(),
@@ -96,8 +98,11 @@ const BaseDimension = Object.assign({
9698
const BaseMeasure = {
9799
aliases: Joi.array().items(Joi.string()),
98100
format: Joi.any().valid('percent', 'currency', 'number'),
99-
shown: Joi.boolean().strict(),
101+
public: Joi.boolean().strict(),
102+
// TODO: Deprecate and remove, please use public
100103
visible: Joi.boolean().strict(),
104+
// TODO: Deprecate and remove, please use public
105+
shown: Joi.boolean().strict(),
101106
cumulative: Joi.boolean().strict(),
102107
filters: Joi.array().items(
103108
Joi.object().keys({
@@ -462,6 +467,7 @@ const baseSchema = {
462467
description: Joi.string(),
463468
rewriteQueries: Joi.boolean().strict(),
464469
shown: Joi.boolean().strict(),
470+
public: Joi.boolean().strict(),
465471
joins: Joi.object().pattern(identifierRegex, Joi.object().keys({
466472
sql: Joi.func().required(),
467473
relationship: Joi.any().valid('hasMany', 'belongsTo', 'hasOne').required()
@@ -509,8 +515,6 @@ const baseSchema = {
509515
meta: Joi.any()
510516
})),
511517
preAggregations: PreAggregationsAlternatives,
512-
includes: Joi.func(),
513-
excludes: Joi.func(),
514518
};
515519

516520
const cubeSchema = inherit(baseSchema, {
@@ -522,6 +526,8 @@ const cubeSchema = inherit(baseSchema, {
522526

523527
const viewSchema = inherit(baseSchema, {
524528
isView: Joi.boolean().strict(),
529+
includes: Joi.func(),
530+
excludes: Joi.func(),
525531
});
526532

527533
function formatErrorMessageFromDetails(explain, d) {

packages/cubejs-schema-compiler/test/unit/PrepareCompiler.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { prepareCompiler as originalPrepareCompiler } from '../../src/compiler/P
33
export const prepareCompiler = (content, options) => originalPrepareCompiler({
44
localPath: () => __dirname,
55
dataSchemaFiles: () => Promise.resolve([
6-
{ fileName: 'main.js', content }
6+
{ fileName: 'main.js', content: Array.isArray(content) ? content.join('\r\n') : content }
77
])
88
}, { adapter: 'postgres', ...options });
99

packages/cubejs-schema-compiler/test/unit/cube-validator.test.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,51 @@ describe('Cube Validation', () => {
1515
console.log('CubePropContextTranspiler.transpiledFieldsPatterns =', transpiledFieldsPatterns);
1616
});
1717

18+
it('cube all ways - correct', async () => {
19+
const cubeValidator = new CubeValidator(new CubeSymbols());
20+
const cube = {
21+
name: 'name',
22+
sql: () => 'SELECT * FROM public.Users',
23+
public: true,
24+
measures: {
25+
min: {
26+
public: true,
27+
sql: () => 'amount',
28+
type: 'min'
29+
},
30+
max: {
31+
// old way
32+
shown: true,
33+
sql: () => 'amount',
34+
type: 'max'
35+
},
36+
},
37+
dimensions: {
38+
createdAt: {
39+
public: true,
40+
sql: () => 'created_at',
41+
type: 'time'
42+
},
43+
pkey: {
44+
// old way
45+
shown: true,
46+
sql: () => 'id',
47+
type: 'number',
48+
primaryKey: true
49+
},
50+
},
51+
fileName: 'fileName',
52+
};
53+
54+
const validationResult = cubeValidator.validate(cube, {
55+
error: (message, e) => {
56+
console.log(message);
57+
}
58+
});
59+
60+
expect(validationResult.error).toBeFalsy();
61+
});
62+
1863
it('cube defined with sql - correct', async () => {
1964
const cubeValidator = new CubeValidator(new CubeSymbols());
2065
const cube = {

packages/cubejs-schema-compiler/test/unit/schema.test.ts

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,11 @@
11
import { prepareCompiler } from './PrepareCompiler';
2-
3-
export function makeCubeSchema({ preAggregations }) {
4-
return `
5-
cube('CubeA', {
6-
sql: \`select * from test\`,
7-
8-
measures: {
9-
count: {
10-
type: 'count'
11-
}
12-
},
13-
14-
dimensions: {
15-
id: {
16-
type: 'number',
17-
sql: 'id',
18-
primaryKey: true
19-
},
20-
type: {
21-
type: 'string',
22-
sql: 'type',
23-
},
24-
createdAt: {
25-
type: 'time',
26-
sql: 'created_at'
27-
},
28-
},
29-
30-
segments: {
31-
sfUsers: {
32-
sql: \`\${CUBE}.location = 'San Francisco'\`
33-
}
34-
},
35-
36-
preAggregations: {
37-
${preAggregations}
38-
},
39-
})
40-
`;
41-
}
2+
import { createCubeSchema } from './utils';
423

434
describe('Schema Testing', () => {
445
const schemaCompile = async () => {
456
const { compiler, cubeEvaluator } = prepareCompiler(
46-
makeCubeSchema({
7+
createCubeSchema({
8+
name: 'CubeA',
479
preAggregations: `
4810
main: {
4911
type: 'originalSql',
@@ -208,7 +170,8 @@ describe('Schema Testing', () => {
208170
const logger = jest.fn();
209171

210172
const { compiler } = prepareCompiler(
211-
makeCubeSchema({
173+
createCubeSchema({
174+
name: 'CubeA',
212175
preAggregations: `
213176
main: {
214177
type: 'originalSql',
@@ -248,4 +211,41 @@ describe('Schema Testing', () => {
248211
'You specified both buildRangeEnd and refreshRangeEnd, buildRangeEnd will be used.'
249212
]);
250213
});
214+
215+
it('visibility modifier', async () => {
216+
const { compiler, metaTransformer } = prepareCompiler([
217+
createCubeSchema({
218+
name: 'CubeA',
219+
publicly: false
220+
}),
221+
createCubeSchema({
222+
name: 'CubeB',
223+
publicly: true
224+
}),
225+
createCubeSchema({
226+
name: 'CubeC',
227+
shown: false
228+
})
229+
]);
230+
await compiler.compile();
231+
232+
expect(metaTransformer.cubes[0]).toMatchObject({
233+
isVisible: false,
234+
config: {
235+
name: 'CubeA',
236+
}
237+
});
238+
expect(metaTransformer.cubes[1]).toMatchObject({
239+
isVisible: true,
240+
config: {
241+
name: 'CubeB',
242+
}
243+
});
244+
expect(metaTransformer.cubes[2]).toMatchObject({
245+
isVisible: false,
246+
config: {
247+
name: 'CubeC',
248+
}
249+
});
250+
});
251251
});

packages/cubejs-schema-compiler/test/unit/utils.ts

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
interface CreateCubeSchemaOptions {
22
name: string,
3+
publicly?: boolean,
4+
shown?: boolean,
35
sqlTable?: string,
46
refreshKey?: string,
57
preAggregations?: string,
68
}
79

8-
export function createCubeSchema({ name, refreshKey = '', preAggregations = '', sqlTable }: CreateCubeSchemaOptions): string {
10+
export function createCubeSchema({ name, refreshKey = '', preAggregations = '', sqlTable, publicly, shown }: CreateCubeSchemaOptions): string {
911
return `
1012
cube('${name}', {
1113
${sqlTable ? `sqlTable: \`${sqlTable}\`` : 'sql: `select * from cards`'},
1214
15+
${publicly !== undefined ? `public: ${publicly},` : ''}
16+
${shown !== undefined ? `shown: ${shown},` : ''}
1317
${refreshKey}
14-
18+
1519
measures: {
1620
count: {
1721
type: 'count'
@@ -29,19 +33,33 @@ export function createCubeSchema({ name, refreshKey = '', preAggregations = '',
2933
type: \`min\`
3034
}
3135
},
32-
36+
3337
dimensions: {
3438
id: {
3539
type: 'number',
3640
sql: 'id',
3741
primaryKey: true
3842
},
43+
type: {
44+
type: 'string',
45+
sql: 'type'
46+
},
3947
createdAt: {
4048
type: 'time',
4149
sql: 'created_at'
4250
},
51+
location: {
52+
type: 'string',
53+
sql: 'location'
54+
}
4355
},
44-
56+
57+
segments: {
58+
sfUsers: {
59+
sql: \`\${CUBE}.location = 'San Francisco'\`
60+
}
61+
},
62+
4563
preAggregations: {
4664
${preAggregations}
4765
}

0 commit comments

Comments
 (0)