How to Design CockroachDB Power Apps with Database-Like Connectivity



This article shows how to gain real-time, database-like connectivity from mobile and tablet Power Apps.

Microsoft Power Apps provides a drag and drop interface, underpinned by a rich set of formulas, for generating mobile and tablet apps that are connected to data. The CData API Server extends Power Apps with connectivity to remote data sources, without a need to maintain a separate copy of the data in the Power Apps Common Data Service. The CData API Server provides database-like connectivity for CockroachDB, augmenting the functionality of SaaS APIs and NoSQL databases with an in-memory SQL-92 engine.

The CData API Server also supports the Swagger metadata standard, whose UI-generation and code-generation possibilities are utilized across Azure App Service, Power Automate, and Power Apps. With Swagger, Power Apps generates a complete set of formulas for working with CockroachDB -- this article shows how to use these formulas to connect your PowerApp to remote CockroachDB data.

Set Up the API Server

If you have not already done so, download the CData API Server. Once you have installed the API Server, follow the steps below to begin producing secure CockroachDB OData services:

Connect to CockroachDB

To work with CockroachDB data from Microsoft Power Apps, we start by creating and configuring a CockroachDB connection. Follow the steps below to configure the API Server to connect to CockroachDB data:

  1. First, navigate to the Connections page.
  2. Click Add Connection and then search for and select the CockroachDB connection.
  3. Enter the necessary authentication properties to connect to CockroachDB.

    Set the following to connect to CockroachDB.

    • Server: The host name or IP address of the server.
    • Port: The port number of the CockroachDB server. If not specified, the default port is 26257.
    • Database: The name of the Cockroach database. If not specified, you connect to the user's default database.
    • User: The Cockroach DB user account used to authenticate.
    • Password: The password used to authenticate the user.
  4. After configuring the connection, click Save & Test to confirm a successful connection.

Configure API Server Users

Next, create a user to access your CockroachDB data through the API Server. You can add and configure users on the Users page. Follow the steps below to configure and create a user:

  1. On the Users page, click Add User to open the Add User dialog.
  2. Next, set the Role, Username, and Privileges properties and then click Add User.
  3. An Authtoken is then generated for the user. You can find the Authtoken and other information for each user on the Users page:

Creating API Endpoints for CockroachDB

Having created a user, you are ready to create API endpoints for the CockroachDB tables:

  1. First, navigate to the API page and then click Add Table .
  2. Select the connection you wish to access and click Next.
  3. With the connection selected, create endpoints by selecting each table and then clicking Confirm.

Gather the OData Url

Having configured a connection to CockroachDB data, created a user, and added resources to the API Server, you now have an easily accessible REST API based on the OData protocol for those resources. From the API page in API Server, you can view and copy the API Endpoints for the API:

Retrieve the Swagger Metadata

You will use the metadata to create a Custom API connection. You can obtain the Swagger definition by making the following request in your browser and then saving the resulting JSON file:

http://MySite:MyPort/api.rsc/$oas?version=2

Connect to API Server in Power Apps

The following procedure shows how to create a simple app that searches remote CockroachDB data.

  1. In Microsoft Power Apps, click Custom connectors.
  2. Click Create custom connector and choose Import an OpenAPI file.
  3. Name the connector, browse to the JSON file, and click Continue.
  4. Fill in the relevant General information, ensure that Base URL is of the form /api.rsc/@myauthtoken (where myauthtoken is the AuthToken for a configure API Server user), and click Continue.
  5. Select No authentication for the Authentication type. Click Continue.
  6. Review the Action and Reference definitions and click Create connector.
  7. To test the connector, you will need to create a new connection. Click Test, click New Connection under Connections, and click Create.
  8. Navigate back to the connector from the Custom connectors menu and click Test. From here, you can test the available operations.

Connect CockroachDB to a Power App

Follow the steps below to connect to CockroachDB from a Power App:

  1. From the Power Apps main menu, click Create an app and select the on-premises or cloud Power Apps Studio.
  2. Select a blank app (choose Phone layout or Tablet layout).
  3. In the View tab, click Data Sources and click Add data source.
  4. Click the Connection you created to test the connector.

Populate a Gallery

Follow the steps below to create a simple app that can search CockroachDB data. You will use Power Apps formulas to bind CockroachDB rows to rows in a gallery control.

  1. In the View tab, click Gallery -> Vertical to add a Gallery.

  2. After selecting a gallery, assign the Items property of the gallery to CockroachDB data on the Advanced tab of the gallery settings. The formula below will allow you to access columns in the Orders table.

    ForAll(CDataSwaggerAPI.getAllOrders().value, {myShipName: ShipName, myShipCity: ShipCity})
  3. Assign CockroachDB columns to UI elements by clicking the element and then setting the Text property (on the Advanced tab of the UI element) to ThisItem.myShipName or ThisItem.myShipCity.

Search CockroachDB Data

To filter the records displayed by the gallery, add a TextInput to your Screen, clear the Text property for the TextInput, and set the Items property of the gallery to a formula like the one below, replacing TextInput1 with the name of the TextInput control in your gallery, if necessary:

If(IsBlank(TextInput1.Text), ForAll(CDataSwaggerAPI.getAllOrders().value, {myShipName: ShipName, myShipCity: ShipCity}), ForAll(CDataSwaggerAPI.getAllOrders({'$filter':Concatenate("contains(ShipName,",TextInput1.Text,")")}).value, {myShipName: ShipName, myShipCity: ShipCity}))

The formula builds an OData query that the API Server executes against the remote CockroachDB data, ensuring that the search is run against the current data without first pulling in every record into the app. You can find more information on the supported OData in the API Server help documentation.

Edit CockroachDB Data

Follow the steps below to load an editable screen that shows the fields of the CockroachDB record selected in the gallery.

  1. On the Insert tab, click New Screen->Blank and name the screen "Details".
  2. Tie the gallery to the new screen: Select the arrow button in the first entry of the gallery and in the OnSelect field in the Advanced properties, enter the following:

    Navigate( Details, None )
  3. In the Details screen, from the Insert tab, add a label "Id" and another label for the Id value. Set the Text property to BrowseGallery.Selected.Id

For each column you will need to do the following. Note that for Custom APIs form elements cannot detect which requests need to be formulated to the API Server, so you will need to write the data modification formulas manually.

  1. Add a label for the field.
  2. Add a text input from the Text menu to the screen and set the text property to the value from the selected item from the gallery (i.e.: BrowseGallery.Selected.myShipName).

To give your app basic update functionality and navigation, add Submit and Back buttons:

  1. For the Submit button, set the OnChange property to the following: CDataSwaggerAPI.updateOrders(BrowseGallery.Selected.myId,BrowseGallery.Selected.myId,{ShipName:TextInput1.Text,ShipCity:TextInput2.Text})
  2. For the Back button, set the OnSelect field to the following: Navigate( BrowseScreen, None )

Your mobile or tablet app can now browse, search, and update CockroachDB data.

Ready to get started?

Learn more or sign up for a free trial:

CData API Server