top of page

Efficient Data Handling with WiX Code: Querying Collections and Getting Items | VELO.

Updated: Dec 10, 2023

Mastering Data Querying with WIX Velo


Velo— previously known as Corvid—is an important part of the WiX platform that provides web developers with a robust framework for creating dynamic, data-driven online applications. WIX Velo supports efficient data retrieval from collections, executing diverse data operations, and prioritizing process efficiency, which are essential components in developing online applications.


With a primary focus on the functionality and use of the query() method, this article aims to give readers a thorough grasp of data querying within WiX Code. We will explore its usage nuances, optimization strategies, and efficient processing of the query results throughout this article, providing developers with a comprehensive understanding of how to utilize this feature for optimal online application development.

To further enhance your skills in Velo and WiX development, consider enrolling in the Velo for Beginners Course on Udemy. This comprehensive course offers in-depth insights and hands-on learning experiences.

Data Querying:

Data querying is a fundamental step in the field of web application development. It entails taking objects from collections or databases and doing different things with the data. To make this process easier, WiX Code offers a function called query(). This powerful function enables developers to retrieve and work with data inside their applications efficiently. For a more in-depth understanding of how the query() function works within WiX Velo and its comprehensive capabilities, developers can refer to the official documentation available at WiX Velo API Overview.

We dive into an in-depth investigation and examine several topics, including managing and modifying query results and improving query performance through optimization.


Understanding WiX Data Collections

In the context of WiX, a data collection is a structured container for storing and organizing data. It acts as a repository for various types of information, such as user profiles, product details, blog posts, or any other data relevant to your website. These collections are highly customizable, allowing you to define their structure and properties according to your specific needs.


An Example of  Wix Collection
Wix Collections for seamless content organization

How to access Data Collection in Wix:

  • Open the Wix editor.

  • Select "Manage Site" from the menu on the left.

  • To access your data collections, select "Database".

  • Select the collection that you like to examine.

  • Examine each item in the collection to see its fields and information.

  • You can use the Wix Editor to edit, remove, or add new items easily.

  • To sort or locate entries, use the collection's search and filter features.

 The database section in the Wix Editor's sidebar.
Access Database

Create a Wix Collection first
Wix Collections

The query() Function: An Overview


query() in WIX VELO

One of the main foundations of WiX Code is the query() function, which gives developers a powerful way to access and work with data stored in collections. The query() method, a crucial component of the WiX Data API, acts as a portal for running data queries, allowing developers to communicate with and access data from their repositories. This is a thorough explanation of how to use the query() function in a developer's application. For a visual guide complementing this in-depth explanation, developers can watch a detailed demonstration in this explanatory video.



Enhance your Velo and WiX development skills: Consider enrolling in the Velo for Beginners Course on Udemy for a comprehensive learning experience in Velo and WiX functionalities

Objective and Usability:

Data Retrieval: The primary role of the query() function is to retrieve particular data items from databases or collections.

Data Manipulation: It enables the filtering, sorting, and other procedures necessary to extract information that satisfies predetermined criteria.

Within their WiX Code, developers can start a data query by using the query() function. The initial point of contact for accessing and utilizing data collections is this function.

Basic Syntax

The basic syntax for the query() function is as follows:



In this code:

  • We start by importing the wixData module, which provides access to the WiX Data API.

  • We define the collection variable, specifying the collection name we want to query.

  • wixData.query(myCollection): This starts the process of running a query on the given collection, myCollection. You can interact with the data collections on your Wix site using the methods provided by the wixData object.

  • The wixData.query() function is linked to the .find() method. Based on the parameters or filters supplied in the query, it runs the query to locate and obtain items from the designated collection. The () method then handles the obtained results.

  • Then(results) => { /* code inside */ }).then( ): This block is a promise that, if the query operation is successful, it will come to pass. It takes in as a parameter the answers to the query. You can work with, process, or manage the retrieved data inside this block in any way your application requires.


Output:


A WIX data API, query() output
query() output

Chaining Query Methods
  1. The query() function enables developers to chain different query methods to filter, sort, and manipulate data according to their needs. WiX Velo's query() function is a valuable tool. It is possible to combine these techniques to build accurate and intricate questions. This is a thorough breakdown of typical query techniques.

  2. Equal Comparison, or eq(): This function looks for items with a given field with a value equal to that specified. For example, there are items where the value in the "name" column is exactly "John".

  3. ne(): It searches for items when a given field has a value that is not equal to that supplied. Identifying objects, for instance, whose "status" field is not "completed".

  4. includes(): This function looks for items with a particular value in a given field. It helps locate objects that have a specific set of characters or words contained in a field.

  5. Ascending(): This sorting method uses a defined field to determine the order of items in ascending order. For example, arranging a group of goods according to price, lowest to highest.

  6. Descending(): Using a designated field as the basis, this approach sorts the objects in descending order. Putting things in the opposite order of ascending is what we call descendant ordering.

Asynchronous and Synchronous Queries

WiX Code supports both asynchronous and synchronous query execution. An asynchronous query allows your application to continue performing other tasks while waiting for the query to be completed. In contrast, a synchronous query blocks further execution until the query is finished.

Asynchronous Query


In the asynchronous query example, we use the await keyword to wait for the query to finish. This is beneficial when your application needs to perform other tasks while waiting for the results.

There isn't direct support for synchronous queries in Wix Data or generally in JavaScript-based asynchronous activities. The primary way to query data using Wix Data is through promises and asynchronous execution.


Handling Query Results: After executing a query, you will receive a result object containing the items from the collection that match your query criteria. Effectively handling these results is vital for your application's functionality.




In the code above, the if statement checks if results.items array has a length greater than zero. If it does, you can process and utilize the retrieved items. If the array is empty, your application should be prepared to handle this scenario gracefully, which might involve displaying a message or taking other appropriate actions.


In-Depth Exploration of the eq() Method in WiX Code for Data Querying


The eq()function overview:

The eq() method, short for "equals," is employed to filter data within a collection based on specific criteria. The eq() method within the WiX Data API is a pivotal tool for filtering data based on equality conditions. In this comprehensive guide, we will delve into the eq() method, exploring its usage, parameters, and the crucial concept of case sensitivity while drawing upon insights from various sources. It enables you to define an equality condition between a field in your collection and a filter value, ensuring that only items meeting this condition are returned in the query results.

For a more in-depth understanding of methods like eq() and other essential functionalities within WiX, consider exploring our Velo for Beginners Course on Udemy.


Case Sensitivity with eq()

When using the eq() method, it's important to note that it's case-sensitive. For instance, if you use "John" as the filter value, the method will exclusively match items that exactly contain "John" in the fieldName. It won't include items with different capitalizations, such as "john" or "JOHN," as matches due to its strict adherence to the exact letter casing during the Comparison.



Only items with "john" in the specified field will be returned. "John" or "JOHN" will not be considered matches.

More Insights on eq(0)

While exploring the eq() method, it's essential to note that this behavior aligns with the general practices of database querying. In most database systems, comparisons are often case-sensitive by default. However, some databases offer options for case-insensitive searches if required.


Exploration of the Contains() Method in WiX Code for Data Querying


Within the WiX Data API, the contains() method plays a crucial role. In this comprehensive guide, we will explore the contains() method in detail, covering its syntax and advantages.

Understanding the contains() Method

A vital component of the WiX Data API is the contains() method, which provides a reliable way to filter across data collections. Its main purpose is to filter data according to whether a given field includes a substring or a defined value. When working with huge datasets, this feature comes in quite handy as it allows you to perform partial text-based searches or find things that fit certain criteria in a larger dataset.


Syntax of contains()

Real-World Example: Searching for Employees

Imagine a company maintaining an employee database where each employee record includes a "Name" field representing the employee's name. To find all employees with the name "John," you can use the contains() method as shown below:




Benefits of the contains()

The Contains() function has the following benefits

  1. Partial Text-based Searches: This feature allows developers to do searches based on matches in partial text, which opens more creative and adaptable methods for retrieving data.

  2. Effective Data Filtering: Developers can improve the accuracy of data operations by employing the Contains() function to filter datasets effectively. This allows them to focus on features that meet predetermined criteria.

  3. Flexibility in Dataset Manipulation: Its features facilitate flexible data manipulation by allowing pertinent data to be extracted based on partial matches or particular substrings inside fields, which improves data handling skills.

Unlocking Precise Data in Wix Velo: Getting Items using WiX Data API .get()


Data Retrieval with .get()

The wixData.get() function attempts to fetch an item from the 'CollectionExample' collection using the specified item ID. It does so asynchronously, using promises to handle the success or failure of the retrieval process.



Results:

Upon successful retrieval, if the item is found, the code checks if the retrieved item is not null. If the item exists, it logs 'Item retrieved:' along with the item's details to the console.

For example, if the item is not found or the retrieval returns null, the console logs' item not found or is null.'


A WIX data API .get() output
WIX .get() output

Use of get Method in Wix Velo to Retrieve Data from Collections


Effortless Retrieval of Specific Information with get()

Within Wix Velo, the get() function is essential for retrieving specific information from collections kept in the database of your website. It functions as a digital key that grants immediate access to a single, designated object. Think of it like a digital librarian quickly bringing up one book from a large library's shelves. The procedure can be streamlined and made more efficient by using the get() method to retrieve this specific data without going through the entire collection.


When you need to access a particular record using its unique identifier—like an ID or key—this is how you should use it. You may simplify and improve the readability of your code by using the get() method. To further understand and implement the power of get(), watch this helpful tutorial: Get() Function Tutorial. Moreover, for user convenience, implementing a button for searching can enhance the user experience, allowing quick access to specific records without the need to enter the ID manually.


The repeater allows each item to be visually represented within a container, enabling user interaction by triggering a function to show the first name of the specific data item when a container within the repeater is clicked. The repeater essentially organizes and presents data items fetched from the collection, enabling a user to interact with individual items displayed within it.



Clicking on the container provides the first name :


Access First name By clicking Container
Get First Name: Click Container

The Importance of Real-Time Data Updates:

It is of utmost importance to ensure that the data is updated in real-time. Failure to do so may display outdated or stale data when new information gets added to the dataset. The reason for this discrepancy is that even when updates or adjustments have been made, the repeater may still exhibit the previous dataset it loaded because it isn't consistently updated. As a result, users who interact with the repeater may read information that isn't the most recent or up-to-date.


Ensuring Updated Displayed Information:

It becomes crucial to put in place procedures that allow the repeater to update or refresh its displayed data in real-time to guarantee that users have access to the most recent information. To guarantee that consumers are provided with the most recent information, the repeater should rapidly reflect any changes made to the data in the source dataset. If this isn't done, there may be differences between the dataset and the information shown, leading to users depending on possibly stale or obsolete information.


Mitigating Discrepancies through Synchronized Updates:

It is crucial to refresh or update the content of a repeater along with the changes in the underlying dataset. This helps avoid inconsistencies and provides users with the most current and accurate information. Real-time updating methods enhance the repeater's overall user experience and ensure that users always have access to the latest and most relevant data.


Conclusion

WiX's Data API provides several methods for extracting, sorting, and filtering data with precision. The eq() method ensures exact matches, while query() retrieves data based on specific conditions. If you need to perform partial text searches, contains() can be of help, and get() fetches specific items by ID. These methods can significantly enhance data-driven applications, improving user experience and overall efficiency.

For an in-depth exploration of leveraging these methods and mastering data extraction in WiX, enroll in our Velo for Beginners Course on Udemy.













819 views0 comments

Kommentare


bottom of page