in a complex application, you may need to perform additional actions after a database operation. this is where DatabaseTrigger comes in. so you can define a callback to be executed on specific database operations for a collection.
Supported triggers are:
Trigger | Description |
---|---|
find | Triggered when a find query is executed on collection. |
find-one | Triggered when a find one query is executed on collection. |
count | Triggered when a count query is executed on collection. |
update-one | Triggered when a update one query is executed on collection. |
insert-one | Triggered when a insert one query is executed on collection. |
remove-one | Triggered when a remove one query is executed on collection. |
aggregate | Triggered when a aggregate query is executed on collection. |
Example
typescript
import { DatabaseTrigger } from '@server-ts/database';
const trigger = new DatabaseTrigger('insert-one', ({ query, queryResult }) => {
console.log('New document inserted:', queryResult);
try {
// Perform additional actions after document insertion
} catch (error) {
console.error('Error performing additional actions:', error);
}
});
// Use the trigger in a collection definition
const collection = new CollectionDefinition({
triggers: [trigger]
});
Properties
Property | Type | Description |
---|---|---|
callback | (context ) => void | The callback function to be executed |
operation | DatabaseOperation | The database operation that triggers the callback |