To use the data module, import wixData from the wix-data module:

import wixData from 'wix-data';
javascript | Copy Code

The wix-data API can be used in your site’s front-end or backend code.

The wix-data API includes functions for managing the data in your collections, building queries on collections, and registering hooks.

For more information, see Working with the Data API and Overview of the Wix Data and Wix Dataset APIs.

Data Types

The Wix Data API works with a schemaless database behind the scenes. In theory, you can store any type of data in any database collection field. In practice, each collection has a non-enforced schema that you should conform to. This schema is used to determine which page elements can connect to which fields and to provide a better experience in the Content Manager. For example, if you set a field's type to Date and Time, you can then connect that field to elements that work with dates, such as DatePicker and Text elements. Also, in the Content Manager, values will appear as dates and you can add new values using a date picker.

The following is a list of the field types from database collections and their corresponding JavaScript data types. When you retrieve data from a collection, that data will be represented in the JavaScript data types that correspond to your collection's field types. When you add or update data in a collection, you should provide the data using the JavaScript data types that correspond to your collection's field types.

Field TypeData TypeNotes
AddressJavaScript objectAn Address object as described here.
ArrayJavaScript array
AudioJavaScript stringA Media Manager audio URL as described here.
BooleanJavaScript boolean
Date and TimeJavaScript Date object
DocumentJavaScript stringA Media Manager document URL as described here.
ImageJavaScript stringAn image URL as described here.
Media GalleryJavaScript arrayEach element in the array is either an ImageItem or VideoItem as described here.
NumberJavaScript number
ObjectJavaScript object
Rich TextJavaScript stringA string which can contain a subset of HTML tags.
Reference/Multiple Items ReferenceJavaScript stringJavaScript array of strings An item ID or multiple items IDs from the referenced collection.
TagsJavaScript string array
TextJavaScript string
TimeJavaScript stringTime in the format.
URLJavaScript stringA valid URL.
VideoJavaScriptstring A video URL as described here.

Wix-Data and Eventual Consistency

The wix-data API is eventually consistent, meaning that sometimes updates to your database collection are not immediate. There may be a short delay until the system is up-to-date with your recent changes. During the delay, the data you get back may not reflect those changes. For example, initially wixData.get('Collection', 'x'); may resolve to null following a wixData.insert('Collection', { _id: 'x' });.

To solve this problem, you can use the setTimeout() function in JavaScript to delay retrieving data following any changes to your database collection. Note that in rare cases with high volume traffic interacting with your database collection, data may take longer to update. In such cases, you can write a function to re-run your data retrieval function a number of times to ensure that your data is current.

Was this helpful?