import type { DeepPartial } from 'ts-essentials' import type { CollectionSlug, FindOptions, Payload, RequestContext, TypedLocale, User, } from '../../../index.js' import type { PayloadRequest, PopulateType, SelectType, Sort, TransformCollectionWithSelect, Where, } from '../../../types/index.js' import type { SharedLocalAPIOptions } from '../../../types/operations.js' import type { File } from '../../../uploads/types.js' import type { CreatePayloadRequestArgs } from '../../../utilities/createPayloadRequest.js' import type { BulkOperationResult, DraftFlagFromCollectionSlug, RequiredDataFromCollectionSlug, SelectFromCollectionSlug, } from '../../config/types.js' import { APIError } from '../../../errors/index.js' import { getFileByPath } from '../../../uploads/getFileByPath.js' import { createPayloadRequest } from '../../../utilities/createPayloadRequest.js' import { updateOperation } from '../update.js' import { updateByIDOperation } from '../updateByID.js' export type BaseOptions = { /** * Whether the current update should be marked as from autosave. * `versions.drafts.autosave` should be specified. */ autosave?: boolean /** * the Collection slug to operate against. */ collection: TSlug /** * [Context](https://payloadcms.com/docs/hooks/context), which will then be passed to `context` and `req.context`, * which can be read by hooks. Useful if you want to pass additional information to the hooks which * shouldn't be necessarily part of the document, for example a `triggerBeforeChange` option which can be read by the BeforeChange hook * to determine if it should run or not. */ context?: RequestContext /** * The document / documents data to update. */ data: DeepPartial> /** * [Control auto-population](https://payloadcms.com/docs/queries/depth) of nested relationship and upload fields. */ depth?: number /** * When set to `true`, a [database transactions](https://payloadcms.com/docs/database/transactions) will not be initialized. * @default false */ disableTransaction?: boolean /** * Specify a [fallback locale](https://payloadcms.com/docs/configuration/localization) to use for any returned documents. */ fallbackLocale?: false | TypedLocale /** * A `File` object when updating a collection with `upload: true`. */ file?: File /** * A file path when creating a collection with `upload: true`. */ filePath?: string /** * Specify [locale](https://payloadcms.com/docs/configuration/localization) for any returned documents. */ locale?: TypedLocale /** * By default, document locks are ignored (`true`). Set to `false` to enforce locks and prevent operations when a document is locked by another user. [More details](https://payloadcms.com/docs/admin/locked-documents). * @default true */ overrideLock?: boolean /** * If you are uploading a file and would like to replace * the existing file instead of generating a new filename, * you can set the following property to `true` */ overwriteExistingFiles?: boolean /** * Specify [populate](https://payloadcms.com/docs/queries/select#populate) to control which fields to include to the result from populated documents. */ populate?: PopulateType /** * Publish the document / documents in all locales. Only applies when localization is enabled * and the collection has localized fields. * * @default undefined */ publishAllLocales?: boolean /** * The `PayloadRequest` object. You can pass it to thread the current [transaction](https://payloadcms.com/docs/database/transactions), user and locale to the operation. * Recommended to pass when using the Local API from hooks, as usually you want to execute the operation within the current transaction. */ req?: Partial /** * Opt-in to receiving hidden fields. By default, they are hidden from returned documents in accordance to your config. * @default false */ showHiddenFields?: boolean /** * When set to `true`, the operation will update both normal and trashed (soft-deleted) documents. * To update only trashed documents, pass `trash: true` and combine with a `where` clause filtering by `deletedAt`. * By default (`false`), the update will only include normal documents and exclude those with a `deletedAt` field. * @default false */ trash?: boolean /** * Unpublish the document / documents in all locales. Only applies when localization is enabled * and the collection has localized fields. */ unpublishAllLocales?: boolean /** * If you set `overrideAccess` to `false`, you can pass a user to use against the access control checks. */ user?: null | User } & Pick, 'select'> & Pick export type ByIDOptions< TSlug extends CollectionSlug, TSelect extends SelectFromCollectionSlug, > = { /** * The ID of the document to update. */ id: number | string /** * Limit documents to update */ limit?: never /** * Sort the documents, can be a string or an array of strings * @example '-createdAt' // Sort DESC by createdAt * @example ['group', '-createdAt'] // sort by 2 fields, ASC group and DESC createdAt */ sort?: never /** * A filter [query](https://payloadcms.com/docs/queries/overview) */ where?: never } & BaseOptions & DraftFlagFromCollectionSlug export type ManyOptions< TSlug extends CollectionSlug, TSelect extends SelectFromCollectionSlug, > = { /** * The ID of the document to update. */ id?: never /** * Limit documents to update */ limit?: number /** * Sort the documents, can be a string or an array of strings * @example '-createdAt' // Sort DESC by createdAt * @example ['group', '-createdAt'] // sort by 2 fields, ASC group and DESC createdAt */ sort?: Sort /** * A filter [query](https://payloadcms.com/docs/queries/overview) */ where: Where } & BaseOptions & DraftFlagFromCollectionSlug export type Options< TSlug extends CollectionSlug, TSelect extends SelectFromCollectionSlug, > = ByIDOptions | ManyOptions async function updateLocal< TSlug extends CollectionSlug, TSelect extends SelectFromCollectionSlug, >( payload: Payload, options: ByIDOptions, ): Promise> async function updateLocal< TSlug extends CollectionSlug, TSelect extends SelectFromCollectionSlug, >( payload: Payload, options: ManyOptions, ): Promise> async function updateLocal< TSlug extends CollectionSlug, TSelect extends SelectFromCollectionSlug, >( payload: Payload, options: Options, ): Promise | TransformCollectionWithSelect> async function updateLocal< TSlug extends CollectionSlug, TSelect extends SelectFromCollectionSlug, >( payload: Payload, options: Options, ): Promise | TransformCollectionWithSelect> { const { id, autosave, collection: collectionSlug, data, depth, disableTransaction, draft, file, filePath, limit, overrideAccess = false, overrideLock, overwriteExistingFiles = false, populate, publishAllLocales, select, showHiddenFields, sort, trash = false, unpublishAllLocales, where, } = options const collection = payload.collections[collectionSlug] if (!collection) { throw new APIError( `The collection with slug ${String(collectionSlug)} can't be found. Update Operation.`, ) } const req = await createPayloadRequest({ ...(options as Omit), payload, }) req.file = file ?? (await getFileByPath(filePath!)) const args = { id, autosave, collection, data, depth, disableTransaction, draft, limit, overrideAccess, overrideLock, overwriteExistingFiles, payload, populate, publishAllLocales, req, select, showHiddenFields, sort, trash, unpublishAllLocales, where, } if (options.id) { // @ts-expect-error - vestiges of when tsconfig was not strict. Feel free to improve return updateByIDOperation(args) } // @ts-expect-error - vestiges of when tsconfig was not strict. Feel free to improve return updateOperation(args) } export { updateLocal }