## Description Prior to this change, the `defaultValue` for fields have only been used in the application layer of Payload. With this change, you get the added benefit of having the database columns get the default also. This is especially helpful when adding new columns to postgres with existing data to avoid needing to write complex migrations. In MongoDB this change applies the default to the Mongoose model which is useful when calling payload.db.create() directly. This only works for statically defined values. 🙏 A big thanks to @r1tsuu for the feature and implementation idea as I lifted some code from PR https://github.com/payloadcms/payload/pull/6983 - [x] I have read and understand the [CONTRIBUTING.md](https://github.com/payloadcms/payload/blob/main/CONTRIBUTING.md) document in this repository. ## Type of change - [x] New feature (non-breaking change which adds functionality) - [x] This change requires a documentation update ## Checklist: - [x] I have added tests that prove my fix is effective or that my feature works - [x] Existing test suite passes locally with my changes - [x] I have made corresponding changes to the documentation
74 lines
1.9 KiB
TypeScript
74 lines
1.9 KiB
TypeScript
import type { DeleteOne, PayloadRequest } from 'payload'
|
|
|
|
import { eq } from 'drizzle-orm'
|
|
import toSnakeCase from 'to-snake-case'
|
|
|
|
import type { DrizzleAdapter } from './types.js'
|
|
|
|
import { buildFindManyArgs } from './find/buildFindManyArgs.js'
|
|
import buildQuery from './queries/buildQuery.js'
|
|
import { selectDistinct } from './queries/selectDistinct.js'
|
|
import { transform } from './transform/read/index.js'
|
|
|
|
export const deleteOne: DeleteOne = async function deleteOne(
|
|
this: DrizzleAdapter,
|
|
{ collection: collectionSlug, req = {} as PayloadRequest, where: whereArg },
|
|
) {
|
|
const db = this.sessions[await req?.transactionID]?.db || this.drizzle
|
|
const collection = this.payload.collections[collectionSlug].config
|
|
|
|
const tableName = this.tableNameMap.get(toSnakeCase(collection.slug))
|
|
|
|
let docToDelete: Record<string, unknown>
|
|
|
|
const { joins, selectFields, where } = await buildQuery({
|
|
adapter: this,
|
|
fields: collection.fields,
|
|
locale: req.locale,
|
|
tableName,
|
|
where: whereArg,
|
|
})
|
|
|
|
const selectDistinctResult = await selectDistinct({
|
|
adapter: this,
|
|
chainedMethods: [{ args: [1], method: 'limit' }],
|
|
db,
|
|
joins,
|
|
selectFields,
|
|
tableName,
|
|
where,
|
|
})
|
|
|
|
if (selectDistinctResult?.[0]?.id) {
|
|
docToDelete = await db.query[tableName].findFirst({
|
|
where: eq(this.tables[tableName].id, selectDistinctResult[0].id),
|
|
})
|
|
} else {
|
|
const findManyArgs = buildFindManyArgs({
|
|
adapter: this,
|
|
depth: 0,
|
|
fields: collection.fields,
|
|
tableName,
|
|
})
|
|
|
|
findManyArgs.where = where
|
|
|
|
docToDelete = await db.query[tableName].findFirst(findManyArgs)
|
|
}
|
|
|
|
const result = transform({
|
|
adapter: this,
|
|
config: this.payload.config,
|
|
data: docToDelete,
|
|
fields: collection.fields,
|
|
})
|
|
|
|
await this.deleteWhere({
|
|
db,
|
|
tableName,
|
|
where: eq(this.tables[tableName].id, docToDelete.id),
|
|
})
|
|
|
|
return result
|
|
}
|