Skip to content

Commit

Permalink
chore(db-mongodb): enable strict: true
Browse files Browse the repository at this point in the history
  • Loading branch information
r1tsuu committed Feb 17, 2025
1 parent 749962a commit fb0fa17
Show file tree
Hide file tree
Showing 30 changed files with 378 additions and 145 deletions.
1 change: 1 addition & 0 deletions packages/db-mongodb/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"devDependencies": {
"@payloadcms/eslint-config": "workspace:*",
"@types/mongoose-aggregate-paginate-v2": "1.0.12",
"@types/prompts": "^2.4.5",
"mongodb": "6.12.0",
"mongodb-memory-server": "^10",
"payload": "workspace:*"
Expand Down
8 changes: 7 additions & 1 deletion packages/db-mongodb/src/connect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,15 @@ export const connect: Connect = async function connect(
await this.migrate({ migrations: this.prodMigrations })
}
} catch (err) {
let msg = `Error: cannot connect to MongoDB.`

if (typeof err === 'object' && err && 'message' in err && typeof err.message === 'string') {
msg = `${msg} Details: ${err.message}`
}

this.payload.logger.error({
err,
msg: `Error: cannot connect to MongoDB. Details: ${err.message}`,
msg,
})
process.exit(1)
}
Expand Down
9 changes: 7 additions & 2 deletions packages/db-mongodb/src/count.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
import type { CountOptions } from 'mongodb'
import type { Count } from 'payload'

import { flattenWhereToOperators } from 'payload'
import { APIError, flattenWhereToOperators } from 'payload'

import type { MongooseAdapter } from './index.js'

import { getSession } from './utilities/getSession.js'

export const count: Count = async function count(
this: MongooseAdapter,
{ collection, locale, req, where },
{ collection, locale, req, where = {} },
) {
const Model = this.collections[collection]

if (!Model) {
throw new APIError(`Could not find collection model with the name ${collection}`)
}

const options: CountOptions = {
session: await getSession(this, req),
}
Expand Down
8 changes: 6 additions & 2 deletions packages/db-mongodb/src/countGlobalVersions.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
import type { CountOptions } from 'mongodb'
import type { CountGlobalVersions } from 'payload'

import { flattenWhereToOperators } from 'payload'
import { APIError, flattenWhereToOperators } from 'payload'

import type { MongooseAdapter } from './index.js'

import { getSession } from './utilities/getSession.js'

export const countGlobalVersions: CountGlobalVersions = async function countGlobalVersions(
this: MongooseAdapter,
{ global, locale, req, where },
{ global, locale, req, where = {} },
) {
const Model = this.versions[global]
const options: CountOptions = {
session: await getSession(this, req),
}

if (!Model) {
throw new APIError(`Could not find global ${global} version Mongoose model`)
}

let hasNearConstraint = false

if (where) {
Expand Down
9 changes: 7 additions & 2 deletions packages/db-mongodb/src/countVersions.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
import type { CountOptions } from 'mongodb'
import type { CountVersions } from 'payload'

import { flattenWhereToOperators } from 'payload'
import { APIError, flattenWhereToOperators } from 'payload'

import type { MongooseAdapter } from './index.js'

import { getSession } from './utilities/getSession.js'

export const countVersions: CountVersions = async function countVersions(
this: MongooseAdapter,
{ collection, locale, req, where },
{ collection, locale, req, where = {} },
) {
const Model = this.versions[collection]

if (!Model) {
throw new APIError(`Could not find collection ${collection} version Mongoose model`)
}

const options: CountOptions = {
session: await getSession(this, req),
}
Expand Down
24 changes: 18 additions & 6 deletions packages/db-mongodb/src/create.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { CreateOptions } from 'mongoose'
import type { Create, Document } from 'payload'

import { APIError, type Create, type Document } from 'payload'

import type { MongooseAdapter } from './index.js'

Expand All @@ -9,29 +10,40 @@ import { sanitizeRelationshipIDs } from './utilities/sanitizeRelationshipIDs.js'

export const create: Create = async function create(
this: MongooseAdapter,
{ collection, data, req },
{ collection: collectionSlug, data, req },
) {
const Model = this.collections[collection]
const Model = this.collections[collectionSlug]

if (!Model) {
throw new APIError(`Could not find collection ${collectionSlug} Mongoose model`)
}

const options: CreateOptions = {
session: await getSession(this, req),
}

let doc

const collection = this.payload.collections[collectionSlug]

if (!collection) {
throw new APIError(`Could not find collection ${collectionSlug}`)
}

const sanitizedData = sanitizeRelationshipIDs({
config: this.payload.config,
data,
fields: this.payload.collections[collection].config.fields,
fields: collection.config.fields,
})

if (this.payload.collections[collection].customIDType) {
if (collection.customIDType) {
sanitizedData._id = sanitizedData.id
}

try {
;[doc] = await Model.create([sanitizedData], options)
} catch (error) {
handleError({ collection, error, req })
handleError({ collection: collectionSlug, error, req })
}

// doc.toJSON does not do stuff like converting ObjectIds to string, or date strings to date objects. That's why we use JSON.parse/stringify here
Expand Down
13 changes: 11 additions & 2 deletions packages/db-mongodb/src/createGlobal.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { CreateOptions } from 'mongoose'
import type { CreateGlobal } from 'payload'

import { APIError, type CreateGlobal } from 'payload'

import type { MongooseAdapter } from './index.js'

Expand All @@ -13,13 +14,21 @@ export const createGlobal: CreateGlobal = async function createGlobal(
) {
const Model = this.globals

const globalConfig = this.payload.config.globals.find(
(globalConfig) => globalConfig.slug === slug,
)

if (!globalConfig) {
throw new APIError(`Could not find global with slug ${slug}`)
}

const global = sanitizeRelationshipIDs({
config: this.payload.config,
data: {
globalType: slug,
...data,
},
fields: this.payload.config.globals.find((globalConfig) => globalConfig.slug === slug).fields,
fields: globalConfig.fields,
})

const options: CreateOptions = {
Expand Down
27 changes: 19 additions & 8 deletions packages/db-mongodb/src/createGlobalVersion.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import type { CreateOptions } from 'mongoose'

import { buildVersionGlobalFields, type CreateGlobalVersion, type Document } from 'payload'
import {
APIError,
buildVersionGlobalFields,
type CreateGlobalVersion,
type Document,
} from 'payload'

import type { MongooseAdapter } from './index.js'

Expand All @@ -22,7 +25,18 @@ export const createGlobalVersion: CreateGlobalVersion = async function createGlo
},
) {
const VersionModel = this.versions[globalSlug]
const options: CreateOptions = {

if (!VersionModel) {
throw new APIError(`Could not find global ${globalSlug} version Mongoose model`)
}

const globalConfig = this.payload.config.globals.find((global) => global.slug === globalSlug)

if (!globalConfig) {
throw new APIError(`Could not find global with slug ${globalSlug}`)
}

const options = {
session: await getSession(this, req),
}

Expand All @@ -38,10 +52,7 @@ export const createGlobalVersion: CreateGlobalVersion = async function createGlo
updatedAt,
version: versionData,
},
fields: buildVersionGlobalFields(
this.payload.config,
this.payload.config.globals.find((global) => global.slug === globalSlug),
),
fields: buildVersionGlobalFields(this.payload.config, globalConfig),
})

const [doc] = await VersionModel.create([data], options, req)
Expand Down
5 changes: 3 additions & 2 deletions packages/db-mongodb/src/createMigration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ export const createMigration: CreateMigration = async function createMigration({
const migrationFileContent = migrationTemplate(predefinedMigration)

const [yyymmdd, hhmmss] = new Date().toISOString().split('T')
const formattedDate = yyymmdd.replace(/\D/g, '')
const formattedTime = hhmmss.split('.')[0].replace(/\D/g, '')

const formattedDate = yyymmdd!.replace(/\D/g, '')
const formattedTime = hhmmss!.split('.')[0]!.replace(/\D/g, '')

const timestamp = `${formattedDate}_${formattedTime}`

Expand Down
22 changes: 14 additions & 8 deletions packages/db-mongodb/src/createVersion.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import type { CreateOptions } from 'mongoose'

import { Types } from 'mongoose'
import { buildVersionCollectionFields, type CreateVersion, type Document } from 'payload'
import { APIError, buildVersionCollectionFields, type CreateVersion, type Document } from 'payload'

import type { MongooseAdapter } from './index.js'

Expand All @@ -23,7 +21,18 @@ export const createVersion: CreateVersion = async function createVersion(
},
) {
const VersionModel = this.versions[collectionSlug]
const options: CreateOptions = {

if (!VersionModel) {
throw new APIError(`Could not find collection ${collectionSlug} version Mongoose model`)
}

const collection = this.payload.collections[collectionSlug]

if (!collection) {
throw new APIError(`Could not find collection ${collectionSlug}`)
}

const options = {
session: await getSession(this, req),
}

Expand All @@ -39,10 +48,7 @@ export const createVersion: CreateVersion = async function createVersion(
updatedAt,
version: versionData,
},
fields: buildVersionCollectionFields(
this.payload.config,
this.payload.collections[collectionSlug].config,
),
fields: buildVersionCollectionFields(this.payload.config, collection.config),
})

const [doc] = await VersionModel.create([data], options, req)
Expand Down
8 changes: 7 additions & 1 deletion packages/db-mongodb/src/deleteMany.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { DeleteOptions } from 'mongodb'
import type { DeleteMany } from 'payload'

import { APIError, type DeleteMany } from 'payload'

import type { MongooseAdapter } from './index.js'

Expand All @@ -10,6 +11,11 @@ export const deleteMany: DeleteMany = async function deleteMany(
{ collection, req, where },
) {
const Model = this.collections[collection]

if (!Model) {
throw new APIError(`Could not find collection ${collection} Mongoose model`)
}

const options: DeleteOptions = {
session: await getSession(this, req),
}
Expand Down
20 changes: 16 additions & 4 deletions packages/db-mongodb/src/deleteOne.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { QueryOptions } from 'mongoose'
import type { DeleteOne, Document } from 'payload'

import { APIError, type DeleteOne, type Document } from 'payload'

import type { MongooseAdapter } from './index.js'

Expand All @@ -9,13 +10,24 @@ import { sanitizeInternalFields } from './utilities/sanitizeInternalFields.js'

export const deleteOne: DeleteOne = async function deleteOne(
this: MongooseAdapter,
{ collection, req, select, where },
{ collection: collectionSlug, req, select, where },
) {
const Model = this.collections[collection]
const Model = this.collections[collectionSlug]

if (!Model) {
throw new APIError(`Could not find collection ${collectionSlug} Mongoose model`)
}

const collection = this.payload.collections[collectionSlug]

if (!collection) {
throw new APIError(`Could not find collection ${collectionSlug}`)
}

const options: QueryOptions = {
projection: buildProjectionFromSelect({
adapter: this,
fields: this.payload.collections[collection].config.flattenedFields,
fields: collection.config.flattenedFields,
select,
}),
session: await getSession(this, req),
Expand Down
6 changes: 5 additions & 1 deletion packages/db-mongodb/src/deleteVersions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { DeleteVersions } from 'payload'
import { APIError, type DeleteVersions } from 'payload'

import type { MongooseAdapter } from './index.js'

Expand All @@ -10,6 +10,10 @@ export const deleteVersions: DeleteVersions = async function deleteVersions(
) {
const VersionsModel = this.versions[collection]

if (!VersionsModel) {
throw new APIError(`Could not find collection ${collection} version Mongoose model`)
}

const session = await getSession(this, req)

const query = await VersionsModel.buildQuery({
Expand Down
Loading

0 comments on commit fb0fa17

Please sign in to comment.