@jrmc/adonis-attachment
Version:
Turn any field on your Lucid model to an attachment data type
41 lines (40 loc) • 1.29 kB
JavaScript
/**
* @jrmc/adonis-attachment
*
* @license MIT
* @copyright Jeremy Chaufourier <jeremy@chaufourier.fr>
*/
import RecordWithAttachment from '../services/record_with_attachment.js';
// @afterFind()
export const afterFindHook = async (instance) => {
const modelInstance = instance;
const model = new RecordWithAttachment(modelInstance);
await model.preComputeUrl();
};
// @afterFetch()
// @afterPaginate()
export const afterFetchHook = async (instance) => {
const modelInstances = instance;
await Promise.all(modelInstances.map((row) => afterFindHook(row)));
};
// @beforeSave()
export const beforeSaveHook = async (instance) => {
const modelInstance = instance;
const model = new RecordWithAttachment(modelInstance);
await model.detach();
await model.persist();
await model.transaction();
};
// @afterSave()
export const afterSaveHook = async (instance) => {
const modelInstance = instance;
const model = new RecordWithAttachment(modelInstance);
await model.generateVariants();
};
// @beforeDelete()
export const beforeDeleteHook = async (instance) => {
const modelInstance = instance;
const model = new RecordWithAttachment(modelInstance);
await model.detachAll();
await model.transaction({ enabledRollback: false });
};