mirror of
https://github.com/rgrgogu/new_starr.git
synced 2026-09-27 00:12:54 +08:00
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
46 lines
1.3 KiB
JavaScript
46 lines
1.3 KiB
JavaScript
"use strict";
|
|
|
|
const { Op } = require("sequelize");
|
|
|
|
const onlyDeleted = { deletedAt: { [Op.not]: null } };
|
|
|
|
/**
|
|
* Permanently delete a single archived record.
|
|
* Returns the record on success, null if not found, false if found but not archived.
|
|
*
|
|
* @param {Model} Model - Sequelize model
|
|
* @param {object} where - Where clause (primary key lookup)
|
|
* @param {object} t - Sequelize transaction
|
|
*/
|
|
async function permanentDeleteOne(Model, where, t) {
|
|
const record = await Model.findOne({ where, paranoid: false, transaction: t });
|
|
if (!record) return null;
|
|
if (!record.deletedAt) return false;
|
|
|
|
await record.destroy({ force: true, transaction: t });
|
|
return record;
|
|
}
|
|
|
|
/**
|
|
* Permanently delete many archived records by primary key.
|
|
* Returns the count of deleted records.
|
|
*
|
|
* @param {Model} Model - Sequelize model
|
|
* @param {string} pkColumn - Primary key column name (e.g. "course_id")
|
|
* @param {Array} ids - Array of primary key values to delete
|
|
* @param {object} t - Sequelize transaction
|
|
*/
|
|
async function permanentDeleteMany(Model, pkColumn, ids, t) {
|
|
if (!ids?.length) return 0;
|
|
|
|
const count = await Model.destroy({
|
|
where: { [pkColumn]: ids, ...onlyDeleted },
|
|
transaction: t,
|
|
paranoid: false,
|
|
force: true,
|
|
});
|
|
return count;
|
|
}
|
|
|
|
module.exports = { permanentDeleteOne, permanentDeleteMany };
|