({ description, ...dataModel }: DataModel)
| 96 | |
| 97 | /** Describe each semantic object separately, retaining source mappings, properties and observations. */ |
| 98 | export function stringifyDataModel({ description, ...dataModel }: DataModel): string { |
| 99 | const attributes = (value: object): string => |
| 100 | markdownTable(Object.entries(value).map(([key, value]) => ({ Attribute: key, Value: value }))); |
| 101 | const metadata = ({ properties, metrics, ...value }: { properties?: Properties; metrics?: Properties }): string => |
| 102 | [ |
| 103 | attributes(value), |
| 104 | properties && Object.keys(properties).length ? `Properties:\n\n${attributes(properties)}` : '', |
| 105 | metrics && Object.keys(metrics).length ? `Metrics:\n\n${attributes(metrics)}` : '', |
| 106 | ] |
| 107 | .filter(Boolean) |
| 108 | .join('\n\n'); |
| 109 | const sections = ['# Data Model']; |
| 110 | |
| 111 | if (description !== undefined) sections.push(attributes({ description })); |
| 112 | |
| 113 | if (dataModel.properties && Object.keys(dataModel.properties).length) { |
| 114 | sections.push('## Properties', attributes(dataModel.properties)); |
| 115 | } |
| 116 | |
| 117 | if (dataModel.models?.length) { |
| 118 | sections.push('## Models'); |
| 119 | for (const { name, columns, ...model } of dataModel.models) { |
| 120 | sections.push(`### Model: ${escapeMarkdown(name)}`, metadata(model)); |
| 121 | if (columns?.length) { |
| 122 | sections.push('#### Columns'); |
| 123 | for (const { name, type, ...column } of columns) { |
| 124 | const summary = [ |
| 125 | `${name} (${type})`, |
| 126 | column.isCalculated && column.expression ? `[calculated: ${column.expression}]` : '', |
| 127 | column.relationship ? `[relationship: ${column.relationship}]` : '', |
| 128 | column.notNull ? 'NOT NULL' : '', |
| 129 | column.isPrimaryKey ? 'PRIMARY KEY' : '', |
| 130 | ] |
| 131 | .filter(Boolean) |
| 132 | .join(' '); |
| 133 | sections.push(`- ${escapeMarkdown(summary)}`, metadata(column)); |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | if (dataModel.relationships?.length) { |
| 140 | sections.push('## Relationships'); |
| 141 | for (const { name, ...relationship } of dataModel.relationships) { |
| 142 | const [from, to] = relationship.models; |
| 143 | sections.push( |
| 144 | `### Relationship: ${escapeMarkdown(name)}`, |
| 145 | escapeMarkdown(`${from} → ${to} (${relationship.joinType})`), |
| 146 | metadata(relationship) |
| 147 | ); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | if (dataModel.views?.length) { |
| 152 | sections.push('## Views'); |
| 153 | for (const { name, ...view } of dataModel.views) { |
| 154 | sections.push(`### View: ${escapeMarkdown(name)}`, metadata(view)); |
| 155 | } |
no test coverage detected