Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(db-postgres): cummulative updates #6033

Merged
merged 2 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 2 additions & 5 deletions packages/db-postgres/src/count.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,21 @@ import type { Count } from 'payload/database'
import type { SanitizedCollectionConfig } from 'payload/types'

import { sql } from 'drizzle-orm'
import toSnakeCase from 'to-snake-case'

import type { ChainedMethods } from './find/chainMethods'
import type { PostgresAdapter } from './types'

import { chainMethods } from './find/chainMethods'
import buildQuery from './queries/buildQuery'
import { getTableName } from './schema/getTableName'

export const count: Count = async function count(
this: PostgresAdapter,
{ collection, locale, req, where: whereArg },
) {
const collectionConfig: SanitizedCollectionConfig = this.payload.collections[collection].config

const tableName = getTableName({
adapter: this,
config: collectionConfig,
})
const tableName = this.tableNameMap.get(toSnakeCase(collectionConfig.slug))

const db = this.sessions[req.transactionID]?.db || this.drizzle
const table = this.tables[tableName]
Expand Down
9 changes: 4 additions & 5 deletions packages/db-postgres/src/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import type { Create } from 'payload/database'

import type { PostgresAdapter } from './types'

import { getTableName } from './schema/getTableName'
import { upsertRow } from './upsertRow'
import toSnakeCase from 'to-snake-case'

export const create: Create = async function create(
this: PostgresAdapter,
Expand All @@ -12,17 +12,16 @@ export const create: Create = async function create(
const db = this.sessions[req.transactionID]?.db || this.drizzle
const collection = this.payload.collections[collectionSlug].config

const tableName = this.tableNameMap.get(toSnakeCase(collection.slug))

const result = await upsertRow({
adapter: this,
data,
db,
fields: collection.fields,
operation: 'create',
req,
tableName: getTableName({
adapter: this,
config: collection,
}),
tableName,
})

return result
Expand Down
10 changes: 5 additions & 5 deletions packages/db-postgres/src/createGlobal.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import type { CreateGlobalArgs } from 'payload/database'
import type { PayloadRequest, TypeWithID } from 'payload/types'

import toSnakeCase from 'to-snake-case'

import type { PostgresAdapter } from './types'

import { getTableName } from './schema/getTableName'
import { upsertRow } from './upsertRow'

export async function createGlobal<T extends TypeWithID>(
Expand All @@ -13,17 +14,16 @@ export async function createGlobal<T extends TypeWithID>(
const db = this.sessions[req.transactionID]?.db || this.drizzle
const globalConfig = this.payload.globals.config.find((config) => config.slug === slug)

const tableName = this.tableNameMap.get(toSnakeCase(globalConfig.slug))

const result = await upsertRow<T>({
adapter: this,
data,
db,
fields: globalConfig.fields,
operation: 'create',
req,
tableName: getTableName({
adapter: this,
config: globalConfig,
}),
tableName,
})

return result
Expand Down
15 changes: 6 additions & 9 deletions packages/db-postgres/src/createGlobalVersion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import type { PayloadRequest, TypeWithID } from 'payload/types'
import { sql } from 'drizzle-orm'
import { type CreateGlobalVersionArgs } from 'payload/database'
import { buildVersionGlobalFields } from 'payload/versions'
import toSnakeCase from 'to-snake-case'

import type { PostgresAdapter } from './types'

import { getTableName } from './schema/getTableName'
import { upsertRow } from './upsertRow'

export async function createGlobalVersion<T extends TypeWithID>(
Expand All @@ -16,11 +16,8 @@ export async function createGlobalVersion<T extends TypeWithID>(
) {
const db = this.sessions[req.transactionID]?.db || this.drizzle
const global = this.payload.globals.config.find(({ slug }) => slug === globalSlug)
const tableName = getTableName({
adapter: this,
config: global,
versions: true,
})

const tableName = this.tableNameMap.get(`_${toSnakeCase(global.slug)}${this.versionsSuffix}`)

const result = await upsertRow<TypeWithVersion<T>>({
adapter: this,
Expand All @@ -40,9 +37,9 @@ export async function createGlobalVersion<T extends TypeWithID>(

if (global.versions.drafts) {
await db.execute(sql`
UPDATE ${table}
SET latest = false
WHERE ${table.id} != ${result.id};
UPDATE ${table}
SET latest = false
WHERE ${table.id} != ${result.id};
`)
}

Expand Down
39 changes: 17 additions & 22 deletions packages/db-postgres/src/createVersion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import type { PayloadRequest, TypeWithID } from 'payload/types'

import { sql } from 'drizzle-orm'
import { buildVersionCollectionFields } from 'payload/versions'
import toSnakeCase from 'to-snake-case'

import type { PostgresAdapter } from './types'

import { getTableName } from './schema/getTableName'
import { upsertRow } from './upsertRow'

export async function createVersion<T extends TypeWithID>(
Expand All @@ -21,19 +21,20 @@ export async function createVersion<T extends TypeWithID>(
) {
const db = this.sessions[req.transactionID]?.db || this.drizzle
const collection = this.payload.collections[collectionSlug].config
const tableName = getTableName({
adapter: this,
config: collection,
versions: true,
})
const defaultTableName = toSnakeCase(collection.slug)

const tableName = this.tableNameMap.get(`_${defaultTableName}${this.versionsSuffix}`)

const version = { ...versionData }
if (version.id) delete version.id

const result = await upsertRow<TypeWithVersion<T>>({
adapter: this,
data: {
autosave,
latest: true,
parent,
version: versionData,
version,
},
db,
fields: buildVersionCollectionFields(collection),
Expand All @@ -43,25 +44,19 @@ export async function createVersion<T extends TypeWithID>(
})

const table = this.tables[tableName]

const relationshipsTable =
this.tables[
getTableName({
adapter: this,
config: collection,
relationships: true,
versions: true,
})
]
this.tables[`_${defaultTableName}${this.versionsSuffix}${this.relationshipsSuffix}`]

if (collection.versions.drafts) {
await db.execute(sql`
UPDATE ${table}
SET latest = false
FROM ${relationshipsTable}
WHERE ${table.id} = ${relationshipsTable.parent}
AND ${relationshipsTable.path} = ${'parent'}
AND ${relationshipsTable[`${collectionSlug}ID`]} = ${parent}
AND ${table.id} != ${result.id};
UPDATE ${table}
SET latest = false
FROM ${relationshipsTable}
WHERE ${table.id} = ${relationshipsTable.parent}
AND ${relationshipsTable.path} = ${'parent'}
AND ${relationshipsTable[`${collectionSlug}ID`]} = ${parent}
AND ${table.id} != ${result.id};
`)
}

Expand Down
5 changes: 3 additions & 2 deletions packages/db-postgres/src/deleteMany.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@ import type { DeleteMany } from 'payload/database'
import type { PayloadRequest } from 'payload/types'

import { inArray } from 'drizzle-orm'
import toSnakeCase from 'to-snake-case'

import type { PostgresAdapter } from './types'

import { findMany } from './find/findMany'
import { getTableName } from './schema/getTableName'

export const deleteMany: DeleteMany = async function deleteMany(
this: PostgresAdapter,
{ collection, req = {} as PayloadRequest, where },
) {
const db = this.sessions[req.transactionID]?.db || this.drizzle
const collectionConfig = this.payload.collections[collection].config
const tableName = getTableName({ adapter: this, config: collectionConfig })

const tableName = this.tableNameMap.get(toSnakeCase(collectionConfig.slug))

const result = await findMany({
adapter: this,
Expand Down
9 changes: 4 additions & 5 deletions packages/db-postgres/src/deleteOne.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import type { DeleteOne } from 'payload/database'
import type { PayloadRequest } from 'payload/types'

import { eq } from 'drizzle-orm'
import toSnakeCase from 'to-snake-case'

import type { PostgresAdapter } from './types'

import { buildFindManyArgs } from './find/buildFindManyArgs'
import buildQuery from './queries/buildQuery'
import { selectDistinct } from './queries/selectDistinct'
import { getTableName } from './schema/getTableName'
import { transform } from './transform/read'

export const deleteOne: DeleteOne = async function deleteOne(
Expand All @@ -17,10 +17,9 @@ export const deleteOne: DeleteOne = async function deleteOne(
) {
const db = this.sessions[req.transactionID]?.db || this.drizzle
const collection = this.payload.collections[collectionSlug].config
const tableName = getTableName({
adapter: this,
config: collection,
})

const tableName = this.tableNameMap.get(toSnakeCase(collection.slug))

let docToDelete: Record<string, unknown>

const { joinAliases, joins, selectFields, where } = await buildQuery({
Expand Down
11 changes: 5 additions & 6 deletions packages/db-postgres/src/deleteVersions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import type { PayloadRequest, SanitizedCollectionConfig } from 'payload/types'

import { inArray } from 'drizzle-orm'
import { buildVersionCollectionFields } from 'payload/versions'
import toSnakeCase from 'to-snake-case'

import type { PostgresAdapter } from './types'

import { findMany } from './find/findMany'
import { getTableName } from './schema/getTableName'

export const deleteVersions: DeleteVersions = async function deleteVersion(
this: PostgresAdapter,
Expand All @@ -16,11 +16,10 @@ export const deleteVersions: DeleteVersions = async function deleteVersion(
const db = this.sessions[req.transactionID]?.db || this.drizzle
const collectionConfig: SanitizedCollectionConfig = this.payload.collections[collection].config

const tableName = getTableName({
adapter: this,
config: collectionConfig,
versions: true,
})
const tableName = this.tableNameMap.get(
`_${toSnakeCase(collectionConfig.slug)}${this.versionsSuffix}`,
)

const fields = buildVersionCollectionFields(collectionConfig)

const { docs } = await findMany({
Expand Down
9 changes: 4 additions & 5 deletions packages/db-postgres/src/find.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import type { Find } from 'payload/database'
import type { PayloadRequest, SanitizedCollectionConfig } from 'payload/types'

import toSnakeCase from 'to-snake-case'

import type { PostgresAdapter } from './types'

import { findMany } from './find/findMany'
import { getTableName } from './schema/getTableName'

export const find: Find = async function find(
this: PostgresAdapter,
Expand All @@ -21,10 +22,8 @@ export const find: Find = async function find(
) {
const collectionConfig: SanitizedCollectionConfig = this.payload.collections[collection].config
const sort = typeof sortArg === 'string' ? sortArg : collectionConfig.defaultSort
const tableName = getTableName({
adapter: this,
config: collectionConfig,
})

const tableName = this.tableNameMap.get(toSnakeCase(collectionConfig.slug))

return findMany({
adapter: this,
Expand Down
32 changes: 10 additions & 22 deletions packages/db-postgres/src/find/traverseFields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@
import type { Field } from 'payload/types'

import { fieldAffectsData, tabHasName } from 'payload/types'
import toSnakeCase from 'to-snake-case'

import type { PostgresAdapter } from '../types'
import type { Result } from './buildFindManyArgs'

import { getTableName } from '../schema/getTableName'

type TraverseFieldArgs = {
_locales: Record<string, unknown>
adapter: PostgresAdapter
Expand Down Expand Up @@ -79,20 +78,11 @@ export const traverseFields = ({
with: {},
}

const arrayTableName = getTableName({
adapter,
config: field,
parentTableName: currentTableName,
prefix: `${currentTableName}_${path}`,
})
const arrayTableName = adapter.tableNameMap.get(
`${currentTableName}_${path}${toSnakeCase(field.name)}`,
)

const arrayTableNameWithLocales = getTableName({
adapter,
config: field,
locales: true,
parentTableName: currentTableName,
prefix: `${currentTableName}_${path}`,
})
const arrayTableNameWithLocales = `${arrayTableName}${adapter.localesSuffix}`

if (adapter.tables[arrayTableNameWithLocales]) withArray.with._locales = _locales
currentArgs.with[`${path}${field.name}`] = withArray
Expand Down Expand Up @@ -142,15 +132,13 @@ export const traverseFields = ({
with: {},
}

const tableName = getTableName({
adapter,
config: block,
parentTableName: topLevelTableName,
prefix: `${topLevelTableName}_blocks_`,
})
const tableName = adapter.tableNameMap.get(
`${topLevelTableName}_blocks_${toSnakeCase(block.slug)}`,
)

if (adapter.tables[`${tableName}${adapter.localesSuffix}`])
if (adapter.tables[`${tableName}${adapter.localesSuffix}`]) {
withBlock.with._locales = _locales
}
topLevelArgs.with[blockKey] = withBlock

traverseFields({
Expand Down
9 changes: 4 additions & 5 deletions packages/db-postgres/src/findGlobal.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
import type { FindGlobal } from 'payload/database'

import toSnakeCase from 'to-snake-case'

import type { PostgresAdapter } from './types'

import { findMany } from './find/findMany'
import { getTableName } from './schema/getTableName'

export const findGlobal: FindGlobal = async function findGlobal(
this: PostgresAdapter,
{ slug, locale, req, where },
) {
const globalConfig = this.payload.globals.config.find((config) => config.slug === slug)
const tableName = getTableName({
adapter: this,
config: globalConfig,
})

const tableName = this.tableNameMap.get(toSnakeCase(globalConfig.slug))

const {
docs: [doc],
Expand Down