Skip to content

Commit

Permalink
Merge pull request #15 from elyobo/fix-8-directory-vs-schema-structure
Browse files Browse the repository at this point in the history
Respect dataform directory structure
  • Loading branch information
elyobo authored Oct 25, 2022
2 parents 8588a14 + c0804ba commit acd3d09
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
6 changes: 3 additions & 3 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,9 @@ await Promise.all([
// Extract all models docs and tests and write to appropriate files
...(
await tablesToDbtModels(configs, adjustName)
).map(async ({ schema, models }) => {
const dir = path.resolve(DBT_MODELS_DIR, schema)
return writeFile(dir, `_${schema}__models.yml`, models)
).map(async ({ directory, models }) => {
const dir = path.resolve(DBT_MODELS_DIR, directory)
return writeFile(dir, `_${directory}__models.yml`, models)
}),
])

Expand Down
17 changes: 10 additions & 7 deletions src/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,20 @@ export const declarationsToSourceMap = ({ declarations }) =>
* Convert dataform tables to DBT model definitions
*/
export const tablesToDbtModels = async (configs, adjustName) => {
const schemas = configs
const directories = configs
.filter((config) => !['operation', 'assertion'].includes(config.raw.type))
.reduce((acc, config) => {
const { schema } = config.raw.target
if (!acc[schema]) acc[schema] = []
acc[schema].push(config)
const dirPath = path.dirname(config.fileName)
// Handle models in the root definitions directoory
const dirname = dirPath.includes('/') ? path.basename(dirPath) : ''

if (!acc[dirname]) acc[dirname] = []
acc[dirname].push(config)
return acc
}, {})

return Object.entries(schemas).map(([schema, tables]) => ({
schema,
return Object.entries(directories).map(([directory, tables]) => ({
directory,
models: YAML.stringify({
version: 2,
models: tables.map((config) => {
Expand Down Expand Up @@ -138,7 +141,7 @@ export const tablesToDbtModels = async (configs, adjustName) => {
)

return {
name: adjustName(schema, target.name),
name: adjustName(target.schema, target.name),
description,
tests: tableTests.length ? tableTests : undefined,
columns,
Expand Down

0 comments on commit acd3d09

Please sign in to comment.