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
).
javascript | Copy CodewixPricingPlansBackend.queryPublicPlans().between("_createdDate", specificDate, now).startsWith("slug", "gold");
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.
javascript | Copy Codeconst 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);
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
Returns
A PublicPlansQueryBuilder
object representing the refined query.
Return Type:
Was this helpful?
and
filter to a query1const newQuery = query1.and(query2);
and
filter, and run itThe 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.
1import wixPricingPlansBackend from 'wix-pricing-plans-backend';23// ...45const now = new Date();6const date = new Date("2020-11-01");78const slugPlanQuery =9 wixPricingPlansBackend.queryPublicPlans()10 .startsWith("slug", "silver")11 .or(12 wixPricingPlansBackend.queryPublicPlans()13 .startsWith("slug", "gold")14 );1516const datePlanQuery =17 wixPricingPlansBackend.queryPublicPlans()18 .between("_createdDate", date, now);1920const typeAndDateQuery = slugPlanQuery.and(datePlanQuery);2122return 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 found39 }40 } )41 .catch((error) => {42 const queryError = error;43 } );4445/*46 * The results contain public47 * plans with slugs that start with48 * "gold" or "silver":49 *50 * gold-vip51 * silver52 * silver-vip53 *54 * But not the primary public plan (slug = gold)55 *56 * And no public plans created earlier than57 * November 11, 202058 */