Skip to content

Commit

Permalink
chore(db-postgres): fix relations to subBlocks
Browse files Browse the repository at this point in the history
  • Loading branch information
DanRibbens committed May 28, 2024
1 parent 148b8c8 commit d97bacf
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 16 deletions.
17 changes: 12 additions & 5 deletions packages/db-postgres/src/schema/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,8 @@ export const buildTable = ({
result._parentID = one(table, {
fields: [localesTable._parentID],
references: [table.id],
// name the relationship by what the many() relationName is
relationName: '_locales',
})

localizedRelations.forEach(({ type, target }, key) => {
Expand All @@ -209,7 +211,9 @@ export const buildTable = ({
})
}
if (type === 'many') {
result[key] = many(adapter.tables[target])
result[key] = many(adapter.tables[target], {
relationName: key,
})
}
})

Expand Down Expand Up @@ -262,6 +266,7 @@ export const buildTable = ({
parent: one(table, {
fields: [textsTable.parent],
references: [table.id],
relationName: '_texts',
}),
}))
}
Expand Down Expand Up @@ -310,6 +315,7 @@ export const buildTable = ({
parent: one(table, {
fields: [numbersTable.parent],
references: [table.id],
relationName: '_numbers',
}),
}))
}
Expand Down Expand Up @@ -409,6 +415,7 @@ export const buildTable = ({
result[idColumnName] = one(adapter.tables[relatedTableName], {
fields: [relationshipsTable[idColumnName]],
references: [adapter.tables[relatedTableName].id],
relationName: relationTo,
})
})

Expand All @@ -430,20 +437,20 @@ export const buildTable = ({
})
}
if (type === 'many') {
result[key] = many(adapter.tables[target])
result[key] = many(adapter.tables[target], { relationName: key })
}
})

if (hasLocalizedField) {
result._locales = many(localesTable)
result._locales = many(localesTable, { relationName: '_locales' })
}

if (hasManyTextField) {
result._texts = many(textsTable)
result._texts = many(textsTable, { relationName: '_texts' })
}

if (hasManyNumberField) {
result._numbers = many(numbersTable)
result._numbers = many(numbersTable, { relationName: '_numbers' })
}

if (relationships.size && relationshipsTable) {
Expand Down
29 changes: 18 additions & 11 deletions packages/db-postgres/src/schema/traverseFields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ export const traverseFields = ({
parent: one(adapter.tables[parentTableName], {
fields: [adapter.tables[selectTableName].parent],
references: [adapter.tables[parentTableName].id],
relationName: fieldName,
}),
}),
)
Expand Down Expand Up @@ -394,11 +395,14 @@ export const traverseFields = ({
_parentID: one(adapter.tables[parentTableName], {
fields: [adapter.tables[arrayTableName]._parentID],
references: [adapter.tables[parentTableName].id],
relationName: fieldName,
}),
}

if (hasLocalesTable(field.fields)) {
result._locales = many(adapter.tables[`${arrayTableName}${adapter.localesSuffix}`])
result._locales = many(adapter.tables[`${arrayTableName}${adapter.localesSuffix}`], {
relationName: '_locales',
})
}

subRelationsToBuild.forEach(({ type, localized, target }, key) => {
Expand All @@ -413,7 +417,7 @@ export const traverseFields = ({
})
}
if (type === 'many') {
result[key] = many(adapter.tables[target])
result[key] = many(adapter.tables[target], { relationName: key })
}
})

Expand Down Expand Up @@ -493,32 +497,34 @@ export const traverseFields = ({
adapter.relations[`relations_${blockTableName}`] = relations(
adapter.tables[blockTableName],
({ many, one }) => {
const result: Record<string, Relation<string>> = {}

result._parentID = one(adapter.tables[rootTableName], {
fields: [adapter.tables[blockTableName]._parentID],
references: [adapter.tables[rootTableName].id],
})
const result: Record<string, Relation<string>> = {
_parentID: one(adapter.tables[rootTableName], {
fields: [adapter.tables[blockTableName]._parentID],
references: [adapter.tables[rootTableName].id],
relationName: `_blocks_${block.slug}`,
}),
}

if (hasLocalesTable(block.fields)) {
result._locales = many(
adapter.tables[`${blockTableName}${adapter.localesSuffix}`],
{ relationName: '_locales' },
)
}

subRelationsToBuild.forEach(({ type, localized, target }, key) => {
if (type === 'one') {
const arrayWithLocalized = localized
const blockWithLocalized = localized
? `${blockTableName}${adapter.localesSuffix}`
: blockTableName
result[key] = one(adapter.tables[target], {
fields: [adapter.tables[arrayWithLocalized][key]],
fields: [adapter.tables[blockWithLocalized][key]],
references: [adapter.tables[target].id],
relationName: key,
})
}
if (type === 'many') {
result[key] = many(adapter.tables[target])
result[key] = many(adapter.tables[target], { relationName: key })
}
})

Expand All @@ -534,6 +540,7 @@ export const traverseFields = ({
tableLocales: adapter.tables[`${blockTableName}${adapter.localesSuffix}`],
})
}
// blocks relationships are defined from the collection or globals table down to the block, bypassing any subBlocks
rootRelationsToBuild.set(`_blocks_${block.slug}`, {
type: 'many',
// blocks are not localized on the parent table
Expand Down

0 comments on commit d97bacf

Please sign in to comment.