From 06f96386d3211c5dc7edad809d99e7373159076f Mon Sep 17 00:00:00 2001 From: Alex Vasilev Date: Mon, 26 Aug 2024 18:59:45 +0200 Subject: [PATCH 1/2] feat(schema-compiler): detect boolean columns for schema generation --- .../src/scaffolding/ScaffoldingSchema.ts | 22 +++++++++++-------- .../test/unit/scaffolding-schema.test.ts | 22 ++++++++++++++++++- 2 files changed, 34 insertions(+), 10 deletions(-) diff --git a/packages/cubejs-schema-compiler/src/scaffolding/ScaffoldingSchema.ts b/packages/cubejs-schema-compiler/src/scaffolding/ScaffoldingSchema.ts index de20457335d63..48c1a3863472b 100644 --- a/packages/cubejs-schema-compiler/src/scaffolding/ScaffoldingSchema.ts +++ b/packages/cubejs-schema-compiler/src/scaffolding/ScaffoldingSchema.ts @@ -7,6 +7,7 @@ enum ColumnType { Time = 'time', Number = 'number', String = 'string', + Boolean = 'boolean', } export enum MemberType { @@ -281,7 +282,7 @@ export class ScaffoldingSchema { protected dimensionColumns(tableDefinition: any) { const dimensionColumns = tableDefinition.filter( - column => !column.name.startsWith('_') && this.columnType(column) === 'string' || + column => !column.name.startsWith('_') && ['string', 'boolean'].includes(this.columnType(column)) || column.attributes?.includes('primaryKey') || this.fixCase(column.name) === 'id' ); @@ -297,18 +298,18 @@ export class ScaffoldingSchema { return dimensionColumns.concat(timeColumns); } - + private fixCase(value: string) { if (this.options.snakeCase) { return toSnakeCase(value); } - + return value.toLocaleLowerCase(); } protected joins(tableName: TableName, tableDefinition: ColumnData[]) { const cubeName = (name: string) => (this.options.snakeCase ? toSnakeCase(name) : inflection.camelize(name)); - + return R.unnest(tableDefinition .map(column => { let columnsToJoin: ColumnsToJoin[] = []; @@ -330,7 +331,7 @@ export class ScaffoldingSchema { this.tableNamesToTables[inflection.tableize(withoutId)] || this.tableNamesToTables[this.fixCase(withoutId)] || this.tableNamesToTables[(inflection.tableize(this.fixCase(withoutId)))]; - + if (!tablesToJoin) { return null; } @@ -380,13 +381,16 @@ export class ScaffoldingSchema { protected columnType(column): ColumnType { const type = this.fixCase(column.type); - if (['time', 'date'].find(t => type.indexOf(t) !== -1)) { + + if (['time', 'date'].find(t => type.includes(t))) { return ColumnType.Time; - } else if (['int', 'dec', 'double', 'numb'].find(t => type.indexOf(t) !== -1)) { + } else if (['int', 'dec', 'double', 'numb'].find(t => type.includes(t))) { // enums are not Numbers return ColumnType.Number; - } else { - return ColumnType.String; + } else if (['bool'].find(t => type.includes(t))) { + return ColumnType.Boolean; } + + return ColumnType.String; } } diff --git a/packages/cubejs-schema-compiler/test/unit/scaffolding-schema.test.ts b/packages/cubejs-schema-compiler/test/unit/scaffolding-schema.test.ts index 8ee9993bc6c0d..01a1f81bf4bce 100644 --- a/packages/cubejs-schema-compiler/test/unit/scaffolding-schema.test.ts +++ b/packages/cubejs-schema-compiler/test/unit/scaffolding-schema.test.ts @@ -1,6 +1,6 @@ import { ScaffoldingSchema } from '../../src/scaffolding/ScaffoldingSchema'; -describe('ScaffoldingSchema', () => { +describe.only('ScaffoldingSchema', () => { const schemas = { public: { orders: [{ @@ -15,6 +15,10 @@ describe('ScaffoldingSchema', () => { name: 'customer_id', type: 'integer', attributes: [] + }, { + name: 'bool_value', + type: 'bool', + attributes: [] }], customers: [{ name: 'id', @@ -301,6 +305,14 @@ describe('ScaffoldingSchema', () => { ], title: 'Id', isPrimaryKey: true + }, + { + isPrimaryKey: false, + name: 'bool_value', + title: 'Bool Value', + types: [ + 'boolean' + ], } ], joins: [ @@ -433,6 +445,14 @@ describe('ScaffoldingSchema', () => { ], title: 'Id', isPrimaryKey: true + }, + { + isPrimaryKey: false, + name: 'bool_value', + title: 'Bool Value', + types: [ + 'boolean' + ], } ], joins: [ From 16390cfc3382e3930644c87c1077a51c28743956 Mon Sep 17 00:00:00 2001 From: Alex Vasilev Date: Mon, 26 Aug 2024 20:41:51 +0200 Subject: [PATCH 2/2] only --- .../test/unit/scaffolding-schema.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/cubejs-schema-compiler/test/unit/scaffolding-schema.test.ts b/packages/cubejs-schema-compiler/test/unit/scaffolding-schema.test.ts index 01a1f81bf4bce..829a7263ae3b4 100644 --- a/packages/cubejs-schema-compiler/test/unit/scaffolding-schema.test.ts +++ b/packages/cubejs-schema-compiler/test/unit/scaffolding-schema.test.ts @@ -1,6 +1,6 @@ import { ScaffoldingSchema } from '../../src/scaffolding/ScaffoldingSchema'; -describe.only('ScaffoldingSchema', () => { +describe('ScaffoldingSchema', () => { const schemas = { public: { orders: [{ @@ -17,7 +17,7 @@ describe.only('ScaffoldingSchema', () => { attributes: [] }, { name: 'bool_value', - type: 'bool', + type: 'boolean', attributes: [] }], customers: [{