Skip to content

Commit

Permalink
chore(db-postgres): WIP ongoing issue with relationName for blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
DanRibbens committed May 27, 2024
1 parent 148b8c8 commit 61051b2
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 29 deletions.
23 changes: 13 additions & 10 deletions packages/db-postgres/src/schema/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ type Args = {
disableNotNull: boolean
disableUnique: boolean
fields: Field[]
rootRelationsToBuild?: RelationMap
rootRelationships?: Set<string>
rootTableIDColType?: string
rootTableName?: string
Expand All @@ -67,8 +66,6 @@ export const buildTable = ({
disableNotNull,
disableUnique = false,
fields,
rootRelationsToBuild,
rootRelationships,
rootTableIDColType,
rootTableName: incomingRootTableName,
tableName,
Expand All @@ -87,7 +84,7 @@ export const buildTable = ({
let numbersTable: GenericTable | PgTableWithColumns<any>

// Relationships to the base collection
const relationships: Set<string> = rootRelationships || new Set()
const relationships: Set<string> = new Set()

let relationshipsTable: GenericTable | PgTableWithColumns<any>

Expand Down Expand Up @@ -116,7 +113,6 @@ export const buildTable = ({
parentTableName: tableName,
relationsToBuild,
relationships,
rootRelationsToBuild: rootRelationsToBuild || relationsToBuild,
rootTableIDColType: rootTableIDColType || idColType,
rootTableName,
versions,
Expand Down Expand Up @@ -198,6 +194,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 +207,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 +262,7 @@ export const buildTable = ({
parent: one(table, {
fields: [textsTable.parent],
references: [table.id],
relationName: '_texts',
}),
}))
}
Expand Down Expand Up @@ -310,6 +311,7 @@ export const buildTable = ({
parent: one(table, {
fields: [numbersTable.parent],
references: [table.id],
relationName: '_numbers',
}),
}))
}
Expand Down Expand Up @@ -409,6 +411,7 @@ export const buildTable = ({
result[idColumnName] = one(adapter.tables[relatedTableName], {
fields: [relationshipsTable[idColumnName]],
references: [adapter.tables[relatedTableName].id],
relationName: relationTo,
})
})

Expand All @@ -430,20 +433,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
43 changes: 24 additions & 19 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 @@ -363,7 +364,6 @@ export const traverseFields = ({
disableNotNull: disableNotNullFromHere,
disableUnique,
fields: disableUnique ? idToUUID(field.fields) : field.fields,
rootRelationsToBuild,
rootRelationships: relationships,
rootTableIDColType,
rootTableName,
Expand Down Expand Up @@ -394,11 +394,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 +416,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 @@ -472,7 +475,6 @@ export const traverseFields = ({
disableNotNull: disableNotNullFromHere,
disableUnique,
fields: disableUnique ? idToUUID(block.fields) : block.fields,
rootRelationsToBuild,
rootRelationships: relationships,
rootTableIDColType,
rootTableName,
Expand All @@ -490,35 +492,44 @@ export const traverseFields = ({
hasManyNumberField = subHasManyNumberField
}

relationsToBuild.set(`_blocks_${block.slug}`, {
type: 'many',
// blocks have their own localized table, independent of the base table.
localized: false,
target: blockTableName,
})

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,12 +545,6 @@ export const traverseFields = ({
tableLocales: adapter.tables[`${blockTableName}${adapter.localesSuffix}`],
})
}
rootRelationsToBuild.set(`_blocks_${block.slug}`, {
type: 'many',
// blocks are not localized on the parent table
localized: false,
target: blockTableName,
})
})

break
Expand Down

0 comments on commit 61051b2

Please sign in to comment.