Skip to main content

Query Events

Introduction

The query builder serves as the most internal layer for other operations such as Models and Aggregate. Let's explore the events associated with the query builder.

On Creating Event

This event is triggered before creating a new document or documents.

Method Signature:

public onCreating(callback: (payload: CreatingEventPayload) => void): this;
import { query } from "@warlock.js/cascade";

function main() {
query.onCreating(({ collection, query, data, isMany }) => {
// do something
});

// will trigger the `onCreating` event when creating a new document
query.create("users", { name: "John Doe" });

// will trigger the `onCreating` event when creating many documents
query.createMany("users", [{ name: "John Doe" }, { name: "Jane Doe" }]);
}

main();

The payload contains the following properties:

  • collection: The collection name.
  • query: The Collection Query
  • data: The data to be inserted.
  • isMany: A boolean value that indicates if the operation is for many documents or not.

That event is triggered when calling create or createMany methods.

On Created Event

This event is triggered after creating a new document or documents.

Method Signature:

public onCreated(callback: (payload: CreatedEventPayload) => void): this;
import { query } from "@warlock.js/cascade";

function main() {
query.onCreated(({ collection, document, documents, data, isMany }) => {
// do something
});

// will trigger the `onCreated` event after creating a new document
query.create("users", { name: "John Doe" });

// will trigger the `onCreated` event after creating many documents
query.createMany("users", [{ name: "John Doe" }, { name: "Jane Doe" }]);
}

main();

The payload contains the following properties:

  • collection: The collection name.
  • document: The created document, exists when calling create method.
  • documents: The created documents, exists when calling createMany method.
  • data: The data that was inserted.
  • isMany: A boolean value that indicates if the operation is for many documents or not.

That event is triggered when calling create or createMany methods.

On Updating Event

This event is triggered before updating a document or documents.

Method Signature:

public onUpdating(callback: (payload: UpdatingEventPayload) => void): this;
import { query } from "@warlock.js/cascade";

function main() {
query.onUpdating(
({ collection, query, data, isMany, filter, updateOptions, options }) => {
// do something
}
);

// will trigger the `onUpdating` event when updating a document
const filter = { name: "John Doe" };

query.update("users", filter, { name: "Jane Doe New Name" });

// will trigger the `onUpdating` event when updating many documents
query.updateMany("users", filter, {
$set: {
name: "Jane Doe New Name",
},
});
}

The payload contains the following properties:

  • collection: The collection name.
  • query: The Collection Query
  • data: The data to be updated.
  • isMany: A boolean value that indicates if the operation is for many documents or not.
  • filter: The filter that will be used to update the document or documents based on it.
  • updateOptions: This is the Update Filter object which is the second argument of updateMany method
  • options: Update options, will be passed only when calling updateMany method.

That event is triggered when calling update or updateMany methods.

On Updated Event

This event is triggered after updating a document or documents.

Method Signature:

public onUpdated(callback: (payload: UpdatedEventPayload) => void): this;
import { query } from "@warlock.js/cascade";

function main() {
query.onUpdated(
({
collection,
document,
documents,
data,
isMany,
filter,
updateOptions,
options,
result,
}) => {
// do something
}
);

// will trigger the `onUpdated` event after updating a document
const filter = { name: "John Doe" };

query.update("users", filter, { name: "Jane Doe New Name" });

// will trigger the `onUpdated` event after updating many documents
query.updateMany("users", filter, {
$set: {
name: "Jane Doe New Name",
},
});
}

The payload contains the following properties:

  • collection: The collection name.
  • document: The updated document, exists when calling update method.
  • documents: The updated documents, exists when calling updateMany method.
  • data: The data that was updated.
  • isMany: A boolean value that indicates if the operation is for many documents or not.
  • filter: The filter that was used to update the document or documents based on it.
  • updateOptions: This is the Update Filter object which is the second argument of updateMany method.
  • options: Update options, will be passed only when calling updateMany method.
  • result: The Update Result object.

That event is triggered when calling update or updateMany methods.

On Deleting Event

This event is triggered before deleting a document or documents.

Method Signature:

public onDeleting(callback: (payload: DeletingEventPayload) => void): this;
import { query } from "@warlock.js/cascade";

function main() {
query.onDeleting(({ collection, query, filter, isMany }) => {
// do something
});

// will trigger the `onDeleting` event when deleting a document
const filter = { name: "John Doe" };

query.deleteOne("users", filter);

// will trigger the `onDeleting` event when deleting many documents
query.deleteMany("users", filter);
}

The payload contains the following properties:

  • collection: The collection name.
  • query: The Collection Query
  • filter: The filter that will be used to delete the document or documents based on it.
  • isMany: A boolean value that indicates if the operation is for many documents or not.

That event is triggered when calling deleteOne or deleteMany methods.

On Deleted Event

This event is triggered after deleting a document or documents.

Method Signature:

public onDeleted(callback: (payload: DeletedEventPayload) => void): this;
import { query } from "@warlock.js/cascade";

function main() {
query.onDeleted(
({ collection, isDeleted, filter, isMany, count, result }) => {
// do something
}
);

// will trigger the `onDeleted` event after deleting a document
const filter = { name: "John Doe" };

query.delete("users", filter);

// will trigger the `onDeleted` event after deleting many documents
query.deleteMany("users", filter);
}

The payload contains the following properties:

  • collection: The collection name.
  • isDeleted: A boolean value that indicates if the document or documents were deleted or not.
  • filter: The filter that was used to delete the document or documents based on it.
  • isMany: A boolean value that indicates if the operation is for many documents or not.
  • count: The number of deleted documents.
  • result: The Delete Result object.

That event is triggered when calling deleteOne or deleteMany methods.

On Fetching Event

This event is triggered before fetching a document or documents.

Method Signature:

public onFetching(callback: (payload: FetchingEventPayload) => void): this;
import { query } from "@warlock.js/cascade";

function main() {
query.onFetching(({ collection, query, filter, isMany }) => {
// do something
});

// will trigger the `onFetching` event when fetching a document
const filter = { name: "John Doe" };

query.first("users", filter);

// will trigger the `onFetching` event when fetching many documents
query.list("users", filter);
}

The payload contains the following properties:

  • collection: The collection name.
  • query: The Collection Query
  • filter: The filter that will be used to fetch the document or documents based on it.
  • isMany: A boolean value that indicates if the operation is for many documents or not.

That event is triggered when calling first, last, latest, oldest, distinct or list methods.

On Fetched Event

This event is triggered after fetching a document or documents.

Method Signature:

public onFetched(callback: (payload: FetchedEventPayload) => void): this;
import { query } from "@warlock.js/cascade";

function main() {
query.onFetched(({ collection, output, filter, isMany, count }) => {
// do something
});

// will trigger the `onFetched` event after fetching a document
const filter = { name: "John Doe" };

query.first("users", filter);

// will trigger the `onFetched` event after fetching many documents
query.list("users", filter);
}

The payload contains the following properties:

  • collection: The collection name.
  • output: The fetched document or documents.
  • filter: The filter that was used to fetch the document or documents based on it.
  • isMany: A boolean value that indicates if the operation is for many documents or not.
  • count: The number of fetched documents.