Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(schema-compiler): detect boolean columns for schema generation #8637

Merged
merged 2 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ enum ColumnType {
Time = 'time',
Number = 'number',
String = 'string',
Boolean = 'boolean',
}

export enum MemberType {
Expand Down Expand Up @@ -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'
);
Expand All @@ -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[] = [];
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ describe('ScaffoldingSchema', () => {
name: 'customer_id',
type: 'integer',
attributes: []
}, {
name: 'bool_value',
type: 'boolean',
attributes: []
}],
customers: [{
name: 'id',
Expand Down Expand Up @@ -301,6 +305,14 @@ describe('ScaffoldingSchema', () => {
],
title: 'Id',
isPrimaryKey: true
},
{
isPrimaryKey: false,
name: 'bool_value',
title: 'Bool Value',
types: [
'boolean'
],
}
],
joins: [
Expand Down Expand Up @@ -433,6 +445,14 @@ describe('ScaffoldingSchema', () => {
],
title: 'Id',
isPrimaryKey: true
},
{
isPrimaryKey: false,
name: 'bool_value',
title: 'Bool Value',
types: [
'boolean'
],
}
],
joins: [
Expand Down
Loading