chore: fix postgres adapter types

This commit is contained in:
Elliot DeNolf
2023-09-24 15:52:24 -07:00
parent 8155902476
commit 67103a7059
10 changed files with 70 additions and 33 deletions

View File

@@ -1,7 +1,8 @@
import type { CreateGlobalVersion } from 'payload/database'
import type { PayloadRequest } from 'payload/types'
import type { TypeWithVersion } from 'payload/database'
import type { PayloadRequest, TypeWithID } from 'payload/types'
import { sql } from 'drizzle-orm'
import { type CreateGlobalVersion, type CreateGlobalVersionArgs } from 'payload/database'
import { buildVersionGlobalFields } from 'payload/versions'
import toSnakeCase from 'to-snake-case'
@@ -9,16 +10,16 @@ import type { PostgresAdapter } from './types'
import { upsertRow } from './upsertRow'
export const createGlobalVersion: CreateGlobalVersion = async function createGlobalVersion(
export async function createGlobalVersion<T extends TypeWithID>(
this: PostgresAdapter,
{ autosave, globalSlug, req = {} as PayloadRequest, versionData },
{ autosave, globalSlug, req = {} as PayloadRequest, versionData }: CreateGlobalVersionArgs,
) {
const db = this.sessions?.[req.transactionID] || this.db
const global = this.payload.globals.config.find(({ slug }) => slug === globalSlug)
const globalTableName = toSnakeCase(globalSlug)
const tableName = `_${globalTableName}_v`
const result = await upsertRow({
const result = await upsertRow<TypeWithVersion<T>>({
adapter: this,
data: {
autosave,

View File

@@ -1,5 +1,5 @@
import type { CreateVersion } from 'payload/database'
import type { PayloadRequest } from 'payload/dist/express/types'
import type { CreateVersionArgs, TypeWithVersion } from 'payload/database'
import type { PayloadRequest, TypeWithID } from 'payload/types'
import { sql } from 'drizzle-orm'
import { buildVersionCollectionFields } from 'payload/versions'
@@ -9,16 +9,22 @@ import type { PostgresAdapter } from './types'
import { upsertRow } from './upsertRow'
export const createVersion: CreateVersion = async function createVersion(
export async function createVersion<T extends TypeWithID>(
this: PostgresAdapter,
{ autosave, collectionSlug, parent, req = {} as PayloadRequest, versionData },
{
autosave,
collectionSlug,
parent,
req = {} as PayloadRequest,
versionData,
}: CreateVersionArgs<T>,
) {
const db = this.sessions?.[req.transactionID] || this.db
const collection = this.payload.collections[collectionSlug].config
const collectionTableName = toSnakeCase(collectionSlug)
const tableName = `_${collectionTableName}_v`
const result = await upsertRow({
const result = await upsertRow<TypeWithVersion<T>>({
adapter: this,
data: {
autosave,

View File

@@ -1,5 +1,5 @@
import type { UpdateGlobal } from 'payload/database'
import type { PayloadRequest } from 'payload/types'
import type { UpdateGlobalArgs } from 'payload/database'
import type { PayloadRequest, TypeWithID } from 'payload/types'
import toSnakeCase from 'to-snake-case'
@@ -7,17 +7,17 @@ import type { PostgresAdapter } from './types'
import { upsertRow } from './upsertRow'
export const updateGlobal: UpdateGlobal = async function updateGlobal(
export async function updateGlobal<T extends TypeWithID>(
this: PostgresAdapter,
{ data, req = {} as PayloadRequest, slug },
) {
{ data, req = {} as PayloadRequest, slug }: UpdateGlobalArgs,
): Promise<T> {
const db = this.sessions?.[req.transactionID] || this.db
const globalConfig = this.payload.globals.config.find((config) => config.slug === slug)
const tableName = toSnakeCase(slug)
const existingGlobal = await this.db.query[tableName].findFirst({})
const result = await upsertRow({
const result = await upsertRow<T>({
...(existingGlobal ? { id: existingGlobal.id, operation: 'update' } : { operation: 'create' }),
adapter: this,
data,

View File

@@ -1,5 +1,6 @@
import type { UpdateGlobalVersion } from 'payload/database'
import type { PayloadRequest, SanitizedGlobalConfig } from 'payload/types'
import type { TypeWithVersion } from 'payload/database'
import type { UpdateGlobalVersionArgs } from 'payload/database'
import type { PayloadRequest, SanitizedGlobalConfig, TypeWithID } from 'payload/types'
import { buildVersionGlobalFields } from 'payload/versions'
import toSnakeCase from 'to-snake-case'
@@ -9,9 +10,16 @@ import type { PostgresAdapter } from './types'
import buildQuery from './queries/buildQuery'
import { upsertRow } from './upsertRow'
export const updateGlobalVersion: UpdateGlobalVersion = async function updateVersion(
export async function updateGlobalVersion<T extends TypeWithID>(
this: PostgresAdapter,
{ id, global, locale, req = {} as PayloadRequest, versionData, where: whereArg },
{
id,
global,
locale,
req = {} as PayloadRequest,
versionData,
where: whereArg,
}: UpdateGlobalVersionArgs<T>,
) {
const db = this.sessions?.[req.transactionID] || this.db
const globalConfig: SanitizedGlobalConfig = this.payload.globals.config.find(
@@ -29,7 +37,7 @@ export const updateGlobalVersion: UpdateGlobalVersion = async function updateVer
where: whereToUse,
})
const result = await upsertRow({
const result = await upsertRow<TypeWithVersion<T>>({
id,
adapter: this,
data: versionData,

View File

@@ -1,5 +1,5 @@
import type { UpdateVersion } from 'payload/database'
import type { PayloadRequest, SanitizedCollectionConfig } from 'payload/types'
import type { TypeWithVersion, UpdateVersionArgs } from 'payload/database'
import type { PayloadRequest, SanitizedCollectionConfig, TypeWithID } from 'payload/types'
import { buildVersionCollectionFields } from 'payload/versions'
import toSnakeCase from 'to-snake-case'
@@ -9,9 +9,16 @@ import type { PostgresAdapter } from './types'
import buildQuery from './queries/buildQuery'
import { upsertRow } from './upsertRow'
export const updateVersion: UpdateVersion = async function updateVersion(
export async function updateVersion<T extends TypeWithID>(
this: PostgresAdapter,
{ id, collection, locale, req = {} as PayloadRequest, versionData, where: whereArg },
{
id,
collection,
locale,
req = {} as PayloadRequest,
versionData,
where: whereArg,
}: UpdateVersionArgs<T>,
) {
const db = this.sessions?.[req.transactionID] || this.db
const collectionConfig: SanitizedCollectionConfig = this.payload.collections[collection].config
@@ -27,7 +34,7 @@ export const updateVersion: UpdateVersion = async function updateVersion(
where: whereToUse,
})
const result = await upsertRow({
const result = await upsertRow<TypeWithVersion<T>>({
id,
adapter: this,
data: versionData,

View File

@@ -1,4 +1,4 @@
export { BeginTransaction, CommitTransaction, Connect, Create, CreateArgs, CreateGlobal, CreateGlobalArgs, CreateGlobalVersion, CreateGlobalVersionArgs, CreateMigration, CreateVersion, CreateVersionArgs, DatabaseAdapter, DeleteMany, DeleteManyArgs, DeleteOne, DeleteOneArgs, DeleteVersions, DeleteVersionsArgs, Destroy, Find, FindArgs, FindGlobal, FindGlobalArgs, FindGlobalVersions, FindGlobalVersionsArgs, FindOne, FindOneArgs, FindVersions, FindVersionsArgs, Init, Migration, MigrationData, PaginatedDocs, QueryDrafts, QueryDraftsArgs, RollbackTransaction, Transaction, UpdateGlobal, UpdateGlobalArgs, UpdateOne, UpdateOneArgs, UpdateVersion, UpdateVersionArgs, Webpack, } from './dist/database/types';
export { BeginTransaction, CommitTransaction, Connect, Create, CreateArgs, CreateGlobal, CreateGlobalArgs, CreateGlobalVersion, CreateGlobalVersionArgs, CreateMigration, CreateVersion, CreateVersionArgs, DatabaseAdapter, DeleteMany, DeleteManyArgs, DeleteOne, DeleteOneArgs, DeleteVersions, DeleteVersionsArgs, Destroy, Find, FindArgs, FindGlobal, FindGlobalArgs, FindGlobalVersions, FindGlobalVersionsArgs, FindOne, FindOneArgs, FindVersions, FindVersionsArgs, Init, Migration, MigrationData, PaginatedDocs, QueryDrafts, QueryDraftsArgs, RollbackTransaction, Transaction, TypeWithVersion, UpdateGlobal, UpdateGlobalArgs, UpdateGlobalVersion, UpdateGlobalVersionArgs, UpdateOne, UpdateOneArgs, UpdateVersion, UpdateVersionArgs, Webpack, } from './dist/database/types';
export * from './dist/database/queryValidation/types';
export { combineQueries } from './dist/database/combineQueries';
export { createDatabaseAdapter } from './dist/database/createDatabaseAdapter';

File diff suppressed because one or more lines are too long

View File

@@ -6,6 +6,8 @@ import type { Payload } from '../payload'
import type { Document, PayloadRequest, Where } from '../types'
import type { TypeWithVersion } from '../versions/types'
export type { TypeWithVersion }
export interface DatabaseAdapter {
/**
* Start a transaction, requiring commitTransaction() to be called for any changes to be made.
@@ -252,7 +254,7 @@ export type UpdateGlobalVersionArgs<T = TypeWithID> = {
}
)
export type UpdateGlobalVersion = <T = TypeWithID>(
export type UpdateGlobalVersion = <T extends TypeWithID = TypeWithID>(
args: UpdateGlobalVersionArgs<T>,
) => Promise<TypeWithVersion<T>>
@@ -302,7 +304,7 @@ export type CreateVersionArgs<T = TypeWithID> = {
versionData: T
}
export type CreateVersion = <T = TypeWithID>(
export type CreateVersion = <T extends TypeWithID = TypeWithID>(
args: CreateVersionArgs<T>,
) => Promise<TypeWithVersion<T>>
@@ -317,7 +319,7 @@ export type CreateGlobalVersionArgs<T = TypeWithID> = {
versionData: T
}
export type CreateGlobalVersion = <T = TypeWithID>(
export type CreateGlobalVersion = <T extends TypeWithID = TypeWithID>(
args: CreateGlobalVersionArgs<T>,
) => Promise<TypeWithVersion<T>>
@@ -339,7 +341,7 @@ export type UpdateVersionArgs<T = TypeWithID> = {
}
)
export type UpdateVersion = <T = TypeWithID>(
export type UpdateVersion = <T extends TypeWithID = TypeWithID>(
args: UpdateVersionArgs<T>,
) => Promise<TypeWithVersion<T>>

View File

@@ -37,8 +37,11 @@ export {
QueryDraftsArgs,
RollbackTransaction,
Transaction,
TypeWithVersion,
UpdateGlobal,
UpdateGlobalArgs,
UpdateGlobalVersion,
UpdateGlobalVersionArgs,
UpdateOne,
UpdateOneArgs,
UpdateVersion,

View File

@@ -77,8 +77,9 @@ export const saveVersion = async ({
const updateVersionArgs = {
id: latestVersion.id,
req,
versionData: data,
versionData: data as TypeWithID,
}
if (collection) {
result = await payload.db.updateVersion({
...updateVersionArgs,