UNPKG

@warlock.js/cascade

Version:

ORM for managing databases

103 lines (102 loc) 2.35 kB
class ModelEvents { collection; /** * Event callbacks */ callbacks = { saving: [], saved: [], creating: [], created: [], updating: [], updated: [], deleting: [], deleted: [], fetching: [], }; /** * {@inheritdoc} */ constructor(collection) { this.collection = collection; // } /** * Add callback when model is about to be created or updated * * Triggered before saving the model */ onSaving(callback) { this.callbacks.saving.push(callback); return this; } /** * Add callback when model is created or updated * * Triggered after saving the model */ onSaved(callback) { this.callbacks.saved.push(callback); return this; } /** * Add callback when model is about to be created */ onCreating(callback) { this.callbacks.creating.push(callback); return this; } /** * Add callback when model is created */ onCreated(callback) { this.callbacks.created.push(callback); return this; } /** * Add callback when model is about to be updated */ onUpdating(callback) { this.callbacks.updating.push(callback); return this; } /** * Add callback when model is updated */ onUpdated(callback) { this.callbacks.updated.push(callback); return this; } /** * Add callback when model is about to be deleted */ onDeleting(callback) { this.callbacks.deleting.push(callback); return this; } /** * Add callback when model is deleted */ onDeleted(callback) { this.callbacks.deleted.push(callback); return this; } /** * Add callback when model is about to be fetched */ onFetching(callback) { this.callbacks.fetching.push(callback); return this; } /** * Trigger the given event */ async trigger(event, ...args) { const callbacks = this.callbacks[event]; if (!callbacks) return; for (const callback of callbacks) { await callback(...args); } } }export{ModelEvents};//# sourceMappingURL=model-events.js.map