import type { PaginateOptions, Schema, SchemaOptions } from 'mongoose' import type { Payload, SanitizedCollectionConfig } from 'payload' import paginate from 'mongoose-paginate-v2' import type { MongoSchemaBuildContext } from './schemaBuildContext.js' import { getBuildQueryPlugin } from '../queries/getBuildQueryPlugin.js' import { buildSchema } from './buildSchema.js' export const buildCollectionSchema = ({ collection, payload, schemaBuildContext, schemaOptions = {}, }: { collection: SanitizedCollectionConfig payload: Payload schemaBuildContext: MongoSchemaBuildContext schemaOptions?: SchemaOptions }): Schema => { const schema = buildSchema({ buildSchemaOptions: { draftsEnabled: Boolean( typeof collection?.versions === 'object' && collection.versions.drafts, ), indexSortableFields: payload.config.indexSortableFields, options: { minimize: false, timestamps: collection.timestamps !== false, ...schemaOptions, }, }, compoundIndexes: collection.sanitizedIndexes, configFields: collection.fields, flattenedFields: collection.flattenedFields, payload, schemaBuildContext, schemaPath: `collection:${collection.slug}`, }) if (Array.isArray(collection.upload.filenameCompoundIndex)) { const indexDefinition = collection.upload.filenameCompoundIndex.reduce>( (acc, index) => { acc[index] = 1 return acc }, {}, ) schema.index(indexDefinition, { unique: true }) } schema .plugin(paginate, { useEstimatedCount: true }) .plugin(getBuildQueryPlugin({ collectionSlug: collection.slug })) return schema }