Adds `select` which is used to specify the field projection for local and rest API calls. This is available as an optimization to reduce the payload's of requests and make the database queries more efficient. Includes: - [x] generate types for the `select` property - [x] infer the return type by `select` with 2 modes - include (`field: true`) and exclude (`field: false`) - [x] lots of integration tests, including deep fields / localization etc - [x] implement the property in db adapters - [x] implement the property in the local api for most operations - [x] implement the property in the rest api - [x] docs --------- Co-authored-by: Dan Ribbens <dan.ribbens@gmail.com>
58 lines
1.4 KiB
TypeScript
58 lines
1.4 KiB
TypeScript
import type { UpdateOne } from 'payload'
|
|
|
|
import toSnakeCase from 'to-snake-case'
|
|
|
|
import type { DrizzleAdapter } from './types.js'
|
|
|
|
import buildQuery from './queries/buildQuery.js'
|
|
import { selectDistinct } from './queries/selectDistinct.js'
|
|
import { upsertRow } from './upsertRow/index.js'
|
|
|
|
export const updateOne: UpdateOne = async function updateOne(
|
|
this: DrizzleAdapter,
|
|
{ id, collection: collectionSlug, data, joins: joinQuery, locale, req, select, 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))
|
|
const whereToUse = whereArg || { id: { equals: id } }
|
|
let idToUpdate = id
|
|
|
|
const { joins, selectFields, where } = buildQuery({
|
|
adapter: this,
|
|
fields: collection.fields,
|
|
locale,
|
|
tableName,
|
|
where: whereToUse,
|
|
})
|
|
|
|
const selectDistinctResult = await selectDistinct({
|
|
adapter: this,
|
|
chainedMethods: [{ args: [1], method: 'limit' }],
|
|
db,
|
|
joins,
|
|
selectFields,
|
|
tableName,
|
|
where,
|
|
})
|
|
|
|
if (selectDistinctResult?.[0]?.id) {
|
|
idToUpdate = selectDistinctResult?.[0]?.id
|
|
}
|
|
|
|
const result = await upsertRow({
|
|
id: idToUpdate,
|
|
adapter: this,
|
|
data,
|
|
db,
|
|
fields: collection.fields,
|
|
joinQuery,
|
|
operation: 'update',
|
|
req,
|
|
select,
|
|
tableName,
|
|
})
|
|
|
|
return result
|
|
}
|