When you enable Velo you also automatically get Wix Data, which lets you work with our built-in databases on your site. You may also want to work with data that you maintain in an external database. Velo lets you connect your site to an external database and then work with that database collection in your site just as you would with our built-in collections.
That means that you can display data from an external collection in Editor elements, use the data to create dynamic pages and connect it to user input elements.
You can learn about adding an external database to your site here. To work with external database collections you need to create an external
database adapter. This adapter implements a Service Provider Interface
(SPI) that enables your site to communicate with your external database
collection. This is the basic structure of the system:
Let's look at each step in more detail:
Wix has created the following prototype adapters on GitHub that you can experiment with and adapt to your needs:
Follow the steps in the README file to deploy and test the prototype adaptor.
The adapter takes the request from your Wix site and converts it into
something your external database can receive. It also takes the response from the external database and converts it into something your Wix site can receive.
This is where you come in. You need to build the adapter that implements the SPI and translates requests and responses between the two systems.
To create the code in your adapter you'll need to understand the structure of the API requests that your site can send to your adapter, and the structure of their associated payloads. You can find that information here.
You'll also need to understand your external database's API. You'll need to understand the format that it expects as a data request as well as the format it uses in its responses.
Your Wix site and your external database will handle their parts. The critical steps in the process, and the ones you need to manage, are steps 2 and 5.
You can see an example implementation of an adapter here.
Step 2 - Adapter translation of data requests from your Wix site to the external database
Your Wix site sends an API request, but it is not in a format that your external database can work with.
You need to add code in your adapter to:
Step 5 - Adapter translation of data requests from the external database to your Wix site
Your external database will receive the request from your adapter and send a response back. This response will be in a format native to your
database, but your Wix site can't work with that response.
You need the code in your adapter to:
The following is information you need to consider when building your adapter and managing your database.
_id
Each table or collection must contain an _id column or attribute of type string.
The Wix Data backend will generate a unique 36-character UUID for each item created in your site.
The `_id` can be any length, but if items are created by your Wix site, it must be at least 36 characters long.
_owner (optional)
The Wix Data backend will send an owner ID when creating a new data item. This is a 36-character UUID identifying the user who created the data.
Use the _owner field to sort or filter data by user.
Dates
All dates must be an ISO string encapsulated in an object. For example: "myDate" :{“$date”:”2001-02-11T00:00:00.000Z”}
Authentication
The External Database Collections SPI uses the request context object to
manage authentication and authorization. HTTP authentication is not
implemented. The settings object within the request context object can be used to send a key for authentication purposes. In the prototype, the secretKey property is used and used to authenticate each request.
The settings object is defined in the Wix Editor when adding the external collection, and sent by the Wix Data backend with every request.
Permissions for external databases
Velo's built in collections include a permissions model, which you can use with your external collections as well. You can also implement your external collection's permissions model, based on how you implement your adapter. Using the context object's members, you can set permissions based on any of the following:
You can also control access to your external collections based on which
endpoints you implement in your adapter. See below for a description of
the required and optional endpoints.
The datasets on your site recognize which endpoints were implemented in
the adapter. For example, if you only implement endpoints that read data, the dataset mode will be read-only.
You can view a read-only version of your collection's permissions in the Site Structure sidebar.
Required and optional endpoints
The following endpoints are mandatory and must be implemented in your adapter. All other endpoints are optional.
Discover the database tables or collections that are visible to your database user.
List the details of a given list of database tables or collections.
Get a specific database row or item based on the item's _id field.
Get a list of items based on a filter.
Get the number of rows or items in a table or collection.
Establish the connection between your site and your adapter.
Working with reference fields
You cannot work with multiple item reference fields in external database collections. You can only use single references in external databases, with the following implementation rules:
Working with dynamic pages
To use Dynamic Pages with external collections at least one of the fields
needs to accept the urlized filter. That field will be used for generating dynamic page links.
Previously published at https://support.wix.com/en/article/velo-working-with-external-database-collections