Search.../

and( )

Adds an and condition to the query.

Description

The and() function adds an and condition to an EventsQueryBuilder. A query with an and returns all the items that match the query as defined up to the and function and also match the query or filter passed to the and function.

Note that when chaining multiple EventsQueryBuilder functions to a query an and condition is assumed. In such cases, you do not need to add a call to the and() function. For example, this query returns results where status is either SCHEDULED or STARTED and a specific event manager created the event.

wixEvents.queryEvents()
.hasSome("status", ["SCHEDULED", "STARTED"])
.eq("createdBy", "4c47c608-cfa8-4037-93ac-738f09560ed3");
javascript | Copy Code

The and() function is needed when performing compound queries. For example, the final query in this set of queries returns results where the event is an introductory class whose title contains either beginner or basic and whose status is either STARTED or ENDED. If both these conditions are true we might want to let the participants for these introductory classes know about upcoming intermediate level classes.

let levelQuery = wixEvents.queryEvents()
.contains("title", "beginner")
.or(
wixEvents.queryEvents()
.contains("title", "basic")
);
let statusQuery = wixEvents.queryEvents()
.hasSome("status", ["STARTED", "ENDED"])
let readyForIntermediateQuery = levelQuery.and(statusQuery);
javascript | Copy Code

The and() function is designed to work with 2 or more queries or filters. If you use it on its own, it will return all the items in a collection.

Syntax

function and(query: EventsQueryBuilder): EventsQueryBuilder

and Parameters

NAME
TYPE
DESCRIPTION
query

Contains functionality for refining a Wix events query.

Returns

Contains functionality for refining a Wix events query.

Return Type:

Was this helpful?

Add an and filter to a query

Copy Code
1const newQuery = query1.and(query2);
Create a compound query, with and and or filters, and run it

The purpose of this query is to retrieve outdated panels and workshops that can be deleted. The outdatedQuery compound query returns panel sessions and workshops that were created or completed before a certain cutoff date (December 31, 2020). The format of the event is determined by checking the event's generated slug. Also, when combined with the ne filter, the query excludes any panels and workshops that might seem outdated but are currently in progress.

Copy Code
1import { wixEvents } from 'wix-events-backend';
2
3// ...
4
5const cutoffDate = new Date("2020-12-31");
6
7const oldDateQuery =
8 wixEvents.queryEvents()
9 .le("_createdDate", cutoffDate)
10 .or(
11 wixEvents.queryEvents()
12 .lt("scheduling.endDate", cutoffDate)
13 );
14
15const slugQuery =
16 wixEvents.queryEvents()
17 .contains("slug", "panel")
18 .or(
19 wixEvents.queryEvents()
20 .contains("slug", "workshop")
21 );
22
23const outdatedQuery = oldDateQuery.and(slugQuery);
24
25return wixEvents.queryEvents()
26 .ne("status", "STARTED").and(outdatedQuery)
27 .find()
28 .then( (results) => {
29 if(results.items.length > 0) {
30 const items = results.items;
31 const firstItem = items[0];
32 const totalCount = results.totalCount;
33 const pageSize = results.pageSize;
34 const currentPage = results.currentPage;
35 const totalPages = results.totalPages;
36 const hasNext = results.hasNext();
37 const hasPrev = results.hasPrev();
38 const length = results.length;
39 const query = results.query;
40 return items;
41 // handle case where matching outdated panels
42 // and workshops that
43 // are not in progress are found,
44 // such as deleting them
45 } else {
46 // handle case where no matching outdated
47 // panels and workshops are found, or
48 // they are still in progress
49 }
50 } )
51 .catch( (error) => {
52 const queryError = error;
53 } );
54
55/*
56 * The results contain outdated events
57 * (created or completed earlier than
58 * January 1, 2021)
59 * with slugs that contain
60 * "panel" or "workshop":
61 *
62 * climate-crisis-panel
63 * basket-weaving-workshop
64 * macrame-workshop
65 *
66 * But not events that are currently
67 * in progress (status = "STARTED")
68 */