## Description This is the beta (v3) PR for the v2 PR [here](https://github.com/payloadcms/payload/pull/6857) Addresses #6800, #5108 - [x] I have read and understand the [CONTRIBUTING.md](https://github.com/payloadcms/payload/blob/main/CONTRIBUTING.md) document in this repository. ## Type of change - [x] Bug fix (non-breaking change which fixes an issue) ## Checklist: - [x] I have added tests that prove my fix is effective or that my feature works - [x] Existing test suite passes locally with my changes
41 lines
1012 B
TypeScript
41 lines
1012 B
TypeScript
import type { DeleteMany, PayloadRequest } from 'payload'
|
|
|
|
import { inArray } from 'drizzle-orm'
|
|
import toSnakeCase from 'to-snake-case'
|
|
|
|
import type { PostgresAdapter } from './types.js'
|
|
|
|
import { findMany } from './find/findMany.js'
|
|
|
|
export const deleteMany: DeleteMany = async function deleteMany(
|
|
this: PostgresAdapter,
|
|
{ collection, req = {} as PayloadRequest, where },
|
|
) {
|
|
const db = this.sessions[await req.transactionID]?.db || this.drizzle
|
|
const collectionConfig = this.payload.collections[collection].config
|
|
|
|
const tableName = this.tableNameMap.get(toSnakeCase(collectionConfig.slug))
|
|
|
|
const result = await findMany({
|
|
adapter: this,
|
|
fields: collectionConfig.fields,
|
|
limit: 0,
|
|
locale: req.locale,
|
|
page: 1,
|
|
pagination: false,
|
|
req,
|
|
tableName,
|
|
where,
|
|
})
|
|
|
|
const ids = []
|
|
|
|
result.docs.forEach((data) => {
|
|
ids.push(data.id)
|
|
})
|
|
|
|
if (ids.length > 0) {
|
|
await db.delete(this.tables[tableName]).where(inArray(this.tables[tableName].id, ids))
|
|
}
|
|
}
|