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.
javascript | Copy CodewixEvents.queryEvents().hasSome("status", ["SCHEDULED", "STARTED"]).eq("createdBy", "4c47c608-cfa8-4037-93ac-738f09560ed3");
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.
javascript | Copy Codelet 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);
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
Was this helpful?
and
filter to a query1const newQuery = query1.and(query2);
and
and or
filters, and run itThe 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.
1import { wixEvents } from 'wix-events-backend';23// ...45const cutoffDate = new Date("2020-12-31");67const oldDateQuery =8 wixEvents.queryEvents()9 .le("_createdDate", cutoffDate)10 .or(11 wixEvents.queryEvents()12 .lt("scheduling.endDate", cutoffDate)13 );1415const slugQuery =16 wixEvents.queryEvents()17 .contains("slug", "panel")18 .or(19 wixEvents.queryEvents()20 .contains("slug", "workshop")21 );2223const outdatedQuery = oldDateQuery.and(slugQuery);2425return 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 panels42 // and workshops that43 // are not in progress are found,44 // such as deleting them45 } else {46 // handle case where no matching outdated47 // panels and workshops are found, or48 // they are still in progress49 }50 } )51 .catch( (error) => {52 const queryError = error;53 } );5455/*56 * The results contain outdated events57 * (created or completed earlier than58 * January 1, 2021)59 * with slugs that contain60 * "panel" or "workshop":61 *62 * climate-crisis-panel63 * basket-weaving-workshop64 * macrame-workshop65 *66 * But not events that are currently67 * in progress (status = "STARTED")68 */