Some checks failed
Build & Deploy / 🔍 Prepare (push) Successful in 20s
Build & Deploy / 🧪 QA (push) Failing after 34s
Build & Deploy / 🏗️ Build (push) Has started running
Build & Deploy / 🚀 Deploy (push) Has been cancelled
Build & Deploy / 🧪 Smoke Test (push) Has been cancelled
Build & Deploy / ⚡ Lighthouse (push) Has been cancelled
Build & Deploy / 🔔 Notify (push) Has been cancelled
64 lines
2.1 KiB
Plaintext
64 lines
2.1 KiB
Plaintext
import { eq } from 'drizzle-orm';
|
|
import toSnakeCase from 'to-snake-case';
|
|
import { buildFindManyArgs } from './find/buildFindManyArgs.js';
|
|
import { buildQuery } from './queries/buildQuery.js';
|
|
import { selectDistinct } from './queries/selectDistinct.js';
|
|
import { transform } from './transform/read/index.js';
|
|
import { getTransaction } from './utilities/getTransaction.js';
|
|
export const deleteOne = async function deleteOne({ collection: collectionSlug, req, returning, select, where: whereArg }) {
|
|
const collection = this.payload.collections[collectionSlug].config;
|
|
const tableName = this.tableNameMap.get(toSnakeCase(collection.slug));
|
|
let docToDelete;
|
|
const { joins, selectFields, where } = buildQuery({
|
|
adapter: this,
|
|
fields: collection.flattenedFields,
|
|
locale: req?.locale,
|
|
tableName,
|
|
where: whereArg
|
|
});
|
|
const db = await getTransaction(this, req);
|
|
const selectDistinctResult = await selectDistinct({
|
|
adapter: this,
|
|
db,
|
|
joins,
|
|
query: ({ query })=>query.limit(1),
|
|
selectFields,
|
|
tableName,
|
|
where
|
|
});
|
|
if (selectDistinctResult?.[0]?.id) {
|
|
docToDelete = await db.query[tableName].findFirst({
|
|
where: eq(this.tables[tableName].id, selectDistinctResult[0].id)
|
|
});
|
|
} else {
|
|
const findManyArgs = buildFindManyArgs({
|
|
adapter: this,
|
|
depth: 0,
|
|
fields: collection.flattenedFields,
|
|
joinQuery: false,
|
|
select,
|
|
tableName
|
|
});
|
|
findManyArgs.where = where;
|
|
docToDelete = await db.query[tableName].findFirst(findManyArgs);
|
|
}
|
|
if (!docToDelete) {
|
|
return null;
|
|
}
|
|
const result = returning === false ? null : transform({
|
|
adapter: this,
|
|
config: this.payload.config,
|
|
data: docToDelete,
|
|
fields: collection.flattenedFields,
|
|
joinQuery: false,
|
|
tableName
|
|
});
|
|
await this.deleteWhere({
|
|
db,
|
|
tableName,
|
|
where: eq(this.tables[tableName].id, docToDelete.id)
|
|
});
|
|
return result;
|
|
};
|
|
|
|
//# sourceMappingURL=deleteOne.js.map |