Skip to content

Commit

Permalink
feat(schema-compiler): detect boolean columns for schema generation (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
vasilev-alex authored Aug 26, 2024
1 parent b4754db commit 178d98f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
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

0 comments on commit 178d98f

Please sign in to comment.