The Wix Data Helpers package allows you to easily add common wix-data operations to your site.
This package does not require any initial setup.
This package includes a number of public files. To use the functions in the files in your backend/page/public code, import them with the following syntax:
The following files are included in the package:
This file contains a function for finding and removing a single item with a specified value from a database collection.
findOneAndRemove()
Queries the specified collection to find and remove a single item whose specified property value matches the criteria defined in a WixDataQuery. If multiple items match the WixDataQuery criteria, only the first returned item (according to the current sort) is removed. Returns the removed item, or null if the item was not found.
Note: Removed items are not recoverable.
Example:
This file contains a function for finding and removing all items with a specified value from a collection.
findAllAndRemove()
Queries the specified database collection to find and remove all items whose specified property value matches the criteria defined in a WixDataQuery. Returns the removed items, or null if no matching items were found.
Note: Removed items are not recoverable.
Example:
This file contains a function for finding an item in a collection with the specific property name, search value, and optionally a reference field.
findDatabaseItem()
Queries the specified collection with the specified propertyName, value, and reference field you want to include in your WixDataQuery filter.
Example:
collectionId
: ID of the collection to search.propertyName
: Name of the property (field) to search in.searchValue
: Value to search for.referenceToInclude
: (Optional) Reference field. If the matching item includes the reference field, the search results will include the full referenced item.This file contains a function for getting an item by ID from a collection, and updating the item’s property value with the specified value.
getAndUpdate()
Gets an item by ID from a specified collection, and updates the item’s property value with the specified value.
Example:
This file contains a function for getting multiple items by their IDs from a collection.
getMultipleItems()
Gets multiple items from a specified database collection. Returns the items in an array.
Example:
This file contains a function for getting a random item from a collection.
getRandomItem()
Gets a random item from a specified collection.
Example:
This file contains an object you can use to make a cached query on a collection.
Create a new instance of the DataCache object to cache a query for a specific amount of time.
Build your data query with the findAndCache object.
maxAge
: Duration of the cache in millisecondsExample:
This section demonstrates one example of how you can work with the package, by using the getRandomItem()
function to select a random winner from your list of site visitors.
Create a database collection called ‘Visitors’ with your ’visitors’ names.
Add the following elements to your site:
Import the getRandomItem()
function from the package to your page code and run it when the button is clicked.
1.0 Initial version
WixData, cache, random, update, queue, boilerplate