Wix Blocks lets you integrate CMS database collections into your app seamlessly. While managing collections in Blocks shares similarities with managing collections in Wix sites, there are a few key differences. Explore these distinctions in detail in this article.
To see examples of collections in Blocks, open the following templates and go to the Databases tab.
Important:
The most important thing to understand about a Blocks collection is that it is a placeholder for the data of any site it's installed on. This is because a Blocks collection can be used on multiple sites. These sites can be very different from each other and have their own databases. Think about a collection that holds customer information. Every site can have a list of their own customers, and your widget can apply to all of them.
When you create a collection in Blocks, you define the fields of the collection, and make it possible to refer to it in the app's code. You can also add default data in Blocks, but you don’t have to. If you do add default data, it is automatically installed on the site together with the app, and can later be replaced by data from any site the app is installed on.
If you change the structure of a collection in Blocks in a future version of your app, it impacts any site it's installed on. So work with caution and don't make changes that can break sites.
Data that you add to your Blocks collections will be imported to the site in which your app is installed, providing your site creators with default data for your app. If you choose to add default data, note that:
To add a collection to your app:
When you import a Blocks app that has a collection to your site, the collection will appear in the Content Collections section in the CMS panel. Your app namespace will appear next to this collection, to indicate that it's from Blocks. From now on you can handle your collection like in any other Wix site.
Important: If you delete the app from the site, the collection will be deleted as well.
To access your collection and perform various actions, query, insert, etc, use the following syntax with the items
submodule in the data
SDK module. For example, this is how you query a collection (note that @wix/data
cannot be tested in Blocks Preview and must be tested on a site):
You can also use the deprecated wix-data Velo API for this purpose:
Use data-binding to connect an element to a collection field, or let site-builders connect it themselves. This can be done easily without code.
You might want to reference a collection on your site directly from your widget code without adding it to Blocks. There are two ways to do this:
If you know the name and structure of the collection, you can use it in your widget code. For example, if Wix Stores is installed on the site, you can use the Stores/Products
collection. Note that if the site is not under your account, you will need to add app permissions.
You can pass the name of the collection or other information about the collection as properties in the Widget API.
There are two types of permissions to consider when building apps that handle collections.
Collection permissions determine what users can do in a collection you created in Blocks, when your app is installed on their site. This means whether users can view, add, update or delete items in your collection.
To configure collection permissions, click the three dots next to the collection name and select Permissions & privacy.
App permissions determine what your app can do on a site collection that does not belong to your app. The specific permissions you need to add depends on the function you are using. For example, query() requires the READ DATA ITEMS
permission.
To configure app permissions, click on the Wix Blocks icon. Then click App > App Permissions.