Skip to content

Commit

Permalink
Merge branch 'beta' of github.com:payloadcms/payload into fix/increas…
Browse files Browse the repository at this point in the history
…e-type-safety-of-collection-and-global-slugs
  • Loading branch information
r1tsuu committed Nov 15, 2024
2 parents cd457ac + 7c6f419 commit 99cbde3
Show file tree
Hide file tree
Showing 36 changed files with 1,441 additions and 383 deletions.
21 changes: 15 additions & 6 deletions packages/db-mongodb/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@
"import": "./src/index.ts",
"types": "./src/index.ts",
"default": "./src/index.ts"
},
"./migration-utils": {
"import": "./src/exports/migration-utils.ts",
"types": "./src/exports/migration-utils.ts",
"default": "./src/exports/migration-utils.ts"
}
},
"main": "./src/index.ts",
Expand All @@ -41,18 +46,17 @@
"prepublishOnly": "pnpm clean && pnpm turbo build"
},
"dependencies": {
"bson-objectid": "2.0.4",
"http-status": "1.6.2",
"mongoose": "6.12.3",
"mongoose-aggregate-paginate-v2": "1.0.6",
"mongoose-paginate-v2": "1.7.22",
"mongoose": "8.8.1",
"mongoose-aggregate-paginate-v2": "1.1.2",
"mongoose-paginate-v2": "1.8.5",
"prompts": "2.4.2",
"uuid": "10.0.0"
},
"devDependencies": {
"@payloadcms/eslint-config": "workspace:*",
"@types/mongoose-aggregate-paginate-v2": "1.0.6",
"mongodb": "4.17.1",
"@types/mongoose-aggregate-paginate-v2": "1.0.12",
"mongodb": "6.10.0",
"mongodb-memory-server": "^9",
"payload": "workspace:*"
},
Expand All @@ -65,6 +69,11 @@
"import": "./dist/index.js",
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
},
"./migration-utils": {
"import": "./dist/exports/migration-utils.js",
"types": "./dist/exports/migration-utils.d.ts",
"default": "./dist/exports/migration-utils.js"
}
},
"main": "./dist/index.js",
Expand Down
9 changes: 1 addition & 8 deletions packages/db-mongodb/src/connect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,7 @@ export const connect: Connect = async function connect(
if (this.ensureIndexes) {
await Promise.all(
this.payload.config.collections.map(async (coll) => {
await new Promise((resolve, reject) => {
this.collections[coll.slug]?.ensureIndexes(function (err) {
if (err) {
reject(err)
}
resolve(true)
})
})
await this.collections[coll.slug]?.ensureIndexes()
}),
)
}
Expand Down
11 changes: 8 additions & 3 deletions packages/db-mongodb/src/count.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { QueryOptions } from 'mongoose'
import type { CountOptions } from 'mongodb'
import type { Count, PayloadRequest } from 'payload'

import { flattenWhereToOperators } from 'payload'
Expand All @@ -12,7 +12,7 @@ export const count: Count = async function count(
{ collection, locale, req = {} as PayloadRequest, where },
) {
const Model = this.collections[collection]
const options: QueryOptions = await withSession(this, req)
const options: CountOptions = await withSession(this, req)

let hasNearConstraint = false

Expand Down Expand Up @@ -40,7 +40,12 @@ export const count: Count = async function count(
}
}

const result = await Model.countDocuments(query, options)
let result: number
if (useEstimatedCount) {
result = await Model.estimatedDocumentCount({ session: options.session })
} else {
result = await Model.countDocuments(query, options)
}

return {
totalDocs: result,
Expand Down
11 changes: 8 additions & 3 deletions packages/db-mongodb/src/countGlobalVersions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { QueryOptions } from 'mongoose'
import type { CountOptions } from 'mongodb'
import type { CountGlobalVersions, PayloadRequest } from 'payload'

import { flattenWhereToOperators } from 'payload'
Expand All @@ -12,7 +12,7 @@ export const countGlobalVersions: CountGlobalVersions = async function countGlob
{ global, locale, req = {} as PayloadRequest, where },
) {
const Model = this.versions[global]
const options: QueryOptions = await withSession(this, req)
const options: CountOptions = await withSession(this, req)

let hasNearConstraint = false

Expand Down Expand Up @@ -40,7 +40,12 @@ export const countGlobalVersions: CountGlobalVersions = async function countGlob
}
}

const result = await Model.countDocuments(query, options)
let result: number
if (useEstimatedCount) {
result = await Model.estimatedDocumentCount({ session: options.session })
} else {
result = await Model.countDocuments(query, options)
}

return {
totalDocs: result,
Expand Down
11 changes: 8 additions & 3 deletions packages/db-mongodb/src/countVersions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { QueryOptions } from 'mongoose'
import type { CountOptions } from 'mongodb'
import type { CountVersions, PayloadRequest } from 'payload'

import { flattenWhereToOperators } from 'payload'
Expand All @@ -12,7 +12,7 @@ export const countVersions: CountVersions = async function countVersions(
{ collection, locale, req = {} as PayloadRequest, where },
) {
const Model = this.versions[collection]
const options: QueryOptions = await withSession(this, req)
const options: CountOptions = await withSession(this, req)

let hasNearConstraint = false

Expand Down Expand Up @@ -40,7 +40,12 @@ export const countVersions: CountVersions = async function countVersions(
}
}

const result = await Model.countDocuments(query, options)
let result: number
if (useEstimatedCount) {
result = await Model.estimatedDocumentCount({ session: options.session })
} else {
result = await Model.countDocuments(query, options)
}

return {
totalDocs: result,
Expand Down
4 changes: 2 additions & 2 deletions packages/db-mongodb/src/createVersion.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import mongoose from 'mongoose'
import { Types } from 'mongoose'
import {
buildVersionCollectionFields,
type CreateVersion,
Expand Down Expand Up @@ -57,7 +57,7 @@ export const createVersion: CreateVersion = async function createVersion(
},
],
}
if (data.parent instanceof mongoose.Types.ObjectId) {
if (data.parent instanceof Types.ObjectId) {
parentQuery.$or.push({
parent: {
$eq: data.parent.toString(),
Expand Down
2 changes: 2 additions & 0 deletions packages/db-mongodb/src/exports/migration-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { migrateRelationshipsV2_V3 } from '../predefinedMigrations/migrateRelationshipsV2_V3.js'
export { migrateVersionsV1_V2 } from '../predefinedMigrations/migrateVersionsV1_V2.js'
1 change: 0 additions & 1 deletion packages/db-mongodb/src/find.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ export const find: Find = async function find(
// useEstimatedCount is faster, but not accurate, as it ignores any filters. It is thus set to true if there are no filters.
const useEstimatedCount = hasNearConstraint || !query || Object.keys(query).length === 0
const paginationOptions: PaginateOptions = {
forceCountFn: hasNearConstraint,
lean: true,
leanWithId: true,
options,
Expand Down
1 change: 0 additions & 1 deletion packages/db-mongodb/src/findGlobalVersions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ export const findGlobalVersions: FindGlobalVersions = async function findGlobalV
// useEstimatedCount is faster, but not accurate, as it ignores any filters. It is thus set to true if there are no filters.
const useEstimatedCount = hasNearConstraint || !query || Object.keys(query).length === 0
const paginationOptions: PaginateOptions = {
forceCountFn: hasNearConstraint,
lean: true,
leanWithId: true,
limit,
Expand Down
1 change: 0 additions & 1 deletion packages/db-mongodb/src/findVersions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ export const findVersions: FindVersions = async function findVersions(
// useEstimatedCount is faster, but not accurate, as it ignores any filters. It is thus set to true if there are no filters.
const useEstimatedCount = hasNearConstraint || !query || Object.keys(query).length === 0
const paginationOptions: PaginateOptions = {
forceCountFn: hasNearConstraint,
lean: true,
leanWithId: true,
limit,
Expand Down
8 changes: 4 additions & 4 deletions packages/db-mongodb/src/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ import { getDBName } from './utilities/getDBName.js'

export const init: Init = function init(this: MongooseAdapter) {
this.payload.config.collections.forEach((collection: SanitizedCollectionConfig) => {
const schema = buildCollectionSchema(collection, this.payload.config)
const schema = buildCollectionSchema(collection, this.payload)

if (collection.versions) {
const versionModelName = getDBName({ config: collection, versions: true })

const versionCollectionFields = buildVersionCollectionFields(this.payload.config, collection)

const versionSchema = buildSchema(this.payload.config, versionCollectionFields, {
const versionSchema = buildSchema(this.payload, versionCollectionFields, {
disableUnique: true,
draftsEnabled: true,
indexSortableFields: this.payload.config.indexSortableFields,
Expand Down Expand Up @@ -66,15 +66,15 @@ export const init: Init = function init(this: MongooseAdapter) {
) as CollectionModel
})

this.globals = buildGlobalModel(this.payload.config)
this.globals = buildGlobalModel(this.payload)

this.payload.config.globals.forEach((global) => {
if (global.versions) {
const versionModelName = getDBName({ config: global, versions: true })

const versionGlobalFields = buildVersionGlobalFields(this.payload.config, global)

const versionSchema = buildSchema(this.payload.config, versionGlobalFields, {
const versionSchema = buildSchema(this.payload, versionGlobalFields, {
disableUnique: true,
draftsEnabled: true,
indexSortableFields: this.payload.config.indexSortableFields,
Expand Down
10 changes: 5 additions & 5 deletions packages/db-mongodb/src/models/buildCollectionSchema.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { PaginateOptions, Schema } from 'mongoose'
import type { SanitizedCollectionConfig, SanitizedConfig } from 'payload'
import type { Payload, SanitizedCollectionConfig } from 'payload'

import mongooseAggregatePaginate from 'mongoose-aggregate-paginate-v2'
import paginate from 'mongoose-paginate-v2'
Expand All @@ -9,12 +9,12 @@ import { buildSchema } from './buildSchema.js'

export const buildCollectionSchema = (
collection: SanitizedCollectionConfig,
config: SanitizedConfig,
payload: Payload,
schemaOptions = {},
): Schema => {
const schema = buildSchema(config, collection.fields, {
const schema = buildSchema(payload, collection.fields, {
draftsEnabled: Boolean(typeof collection?.versions === 'object' && collection.versions.drafts),
indexSortableFields: config.indexSortableFields,
indexSortableFields: payload.config.indexSortableFields,
options: {
minimize: false,
timestamps: collection.timestamps !== false,
Expand All @@ -34,7 +34,7 @@ export const buildCollectionSchema = (
schema.index(indexDefinition, { unique: true })
}

if (config.indexSortableFields && collection.timestamps !== false) {
if (payload.config.indexSortableFields && collection.timestamps !== false) {
schema.index({ updatedAt: 1 })
schema.index({ createdAt: 1 })
}
Expand Down
10 changes: 5 additions & 5 deletions packages/db-mongodb/src/models/buildGlobalModel.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { SanitizedConfig } from 'payload'
import type { Payload } from 'payload'

import mongoose from 'mongoose'

Expand All @@ -7,8 +7,8 @@ import type { GlobalModel } from '../types.js'
import { getBuildQueryPlugin } from '../queries/buildQuery.js'
import { buildSchema } from './buildSchema.js'

export const buildGlobalModel = (config: SanitizedConfig): GlobalModel | null => {
if (config.globals && config.globals.length > 0) {
export const buildGlobalModel = (payload: Payload): GlobalModel | null => {
if (payload.config.globals && payload.config.globals.length > 0) {
const globalsSchema = new mongoose.Schema(
{},
{ discriminatorKey: 'globalType', minimize: false, timestamps: true },
Expand All @@ -18,8 +18,8 @@ export const buildGlobalModel = (config: SanitizedConfig): GlobalModel | null =>

const Globals = mongoose.model('globals', globalsSchema, 'globals') as unknown as GlobalModel

Object.values(config.globals).forEach((globalConfig) => {
const globalSchema = buildSchema(config, globalConfig.fields, {
Object.values(payload.config.globals).forEach((globalConfig) => {
const globalSchema = buildSchema(payload, globalConfig.fields, {
options: {
minimize: false,
},
Expand Down
Loading

0 comments on commit 99cbde3

Please sign in to comment.