Search.../

and( )

Adds an and condition to the query.

Description

The and() function adds an and condition to a PublicPlansQueryBuilder. A query with an and returns all the items that match the query as defined up to the and function.

Note that when chaining multiple PublicPlansQueryBuilder 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 the plan was created during a certain date range and the plan's slug starts with "gold" (such as gold, gold-vip, gold-trial).

wixPricingPlansBackend.queryPublicPlans()
.between("_createdDate", specificDate, now)
.startsWith("slug", "gold");
javascript | Copy Code

The and() function is needed when performing compound queries. For example, the final query, typeAndDateQuery, in this set of queries returns results where a plan is created after a certain date, and the plan is either a "gold" or a "silver" membership plan, as defined in the plan's slug.

const slugPlanQuery =
wixPricingPlansBackend.queryPublicPlans()
.startsWith("slug", "silver")
.or(
wixPricingPlansBackend.plans.queryPublicPlans()
.startsWith("slug", "gold")
);
const datePlanQuery =
wixPricingPlansBackend.queryPublicPlans()
.between("_createdDate", date, now);
);
const typeAndDateQuery = slugPlanQuery.and(datePlanQuery);
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: PublicPlansQueryBuilder): PublicPlansQueryBuilder

and Parameters

NAME
TYPE
DESCRIPTION
query

A query to add to the initial query as an and condition.

Returns

A PublicPlansQueryBuilder object representing the refined query.

Was this helpful?

Add an and filter to a query

Copy Code
1const newQuery = query1.and(query2);
Create a compound query, add an and filter, and run it

The final query, typeAndDateQuery, in this set of queries returns results where a plan is created after a certain date (November 11, 2020), and the plan is either a "gold" or a "silver" membership plan, as defined in the plan's slug. Also, when combined with the ne filter, the query also excludes the primary plan.

Copy Code
1import wixPricingPlansBackend from 'wix-pricing-plans-backend';
2
3// ...
4
5const now = new Date();
6const date = new Date("2020-11-01");
7
8const slugPlanQuery =
9 wixPricingPlansBackend.queryPublicPlans()
10 .startsWith("slug", "silver")
11 .or(
12 wixPricingPlansBackend.queryPublicPlans()
13 .startsWith("slug", "gold")
14 );
15
16const datePlanQuery =
17 wixPricingPlansBackend.queryPublicPlans()
18 .between("_createdDate", date, now);
19
20const typeAndDateQuery = slugPlanQuery.and(datePlanQuery);
21
22return wixPricingPlansBackend.queryPublicPlans()
23 .ne("primary", true).and(typeAndDateQuery)
24 .find()
25 .then((publicPlans) => {
26 if(publicPlans.items.length > 0) {
27 const items = publicPlans.items;
28 const firstItem = items[0];
29 const totalCount = publicPlans.totalCount;
30 const pageSize = publicPlans.pageSize;
31 const currentPage = publicPlans.currentPage;
32 const totalPages = publicPlans.totalPages;
33 const hasNext = publicPlans.hasNext();
34 const hasPrev = publicPlans.hasPrev();
35 const length = publicPlans.length;
36 const query = publicPlans.query;
37 } else {
38 // handle case where no matching public plans found
39 }
40 } )
41 .catch((error) => {
42 const queryError = error;
43 } );
44
45/*
46 * The results contain public
47 * plans with slugs that start with
48 * "gold" or "silver":
49 *
50 * gold-vip
51 * silver
52 * silver-vip
53 *
54 * But not the primary public plan (slug = gold)
55 *
56 * And no public plans created earlier than
57 * November 11, 2020
58 */