Skip to content

TWebFaunaDbClientDataSet

Introduction

FaunaDb offers a cloud hosted database and offers access to the data via a REST API. This REST API can be consumed by a TMS WEB Core web client application, allowing you to create web applications with data in the backend hosted by FaunaDb and as such no longer worry about this part of your web application. To make the use of the REST API from FaunaDb to perform CRUD operations on your data easier and codeless, the TWebFaunaDbClientDataset is available that works as a bridge between your FaunaDb hosted data and the DB-aware controls in your TMS WEB Core web client application.

Configuring the FaunaDb server back-end

To create a database with a table hosted on the FaunaDB cloud database, follow the steps below:

1) Sign up with Faunadb and Login to the dashboard

2) Click on “New Database” to create a new database with any name. Save.

3) In the left menu click on “Shell”. From there execute the script to create the needed tables

Script step 1: create two tables

CreateCollection({ name: "Tasks" });
CreateCollection({ name: "users",
  permissions: { create: "public"}, //trying to allow sign up
});

Script step 2: create one user so login will be possible

Create(
  Collection("users"),
  {
    credentials: {password:"1234"},
    data: {
    email: "john@doe.com",
    },
  }
);

Script step 3: create an index on the user table to sort users table

CreateIndex({
  name: "users_by_email",
  permissions: {read:"public"},
  source: Collection("users"),
  terms: [{field: ["data","email"]}],
  unique: true,
});

Script step 4: Assign admin privileges to logged in user to enable changing indexes for example

CreateFunction({
  name: "addPermission",
  role: "admin",
  body: Query(
    Lambda("x",
      Let (
        {curpriveleges:
          {"privileges":
            Union(Select("privileges",
Get(Role("loggedInUser"))),
                    Var("x"))
            }
          },
          Update(Role("loggedInUser"),Var("curpriveleges"))
        )
      )
    )
})

Script step 5: create the role that defines what the logged-in user can do

CreateRole({
  name: "loggedInUser",
  membership: {resource: Collection("users")},
  privileges: [
    {resource: Collection("Tasks"), actions: {read: true, write: true, create: true, delete: true}},
    {resource: Ref("indexes"), actions: {read: true, write: true, create: true, delete: true}},
    {resource: Ref("collections"), actions: {read: true, write: true}},
    {resource: Function("addPermission"), actions: { call: true}}
  ]
});

4) At the end of the result, you should see a line with the Secret: "secret": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

5) Copy the value of the secret to the clipboard and use it in the web client application

Using FaunaDB via TWebFaunaDBClientDataset

To start using your created FaunaDB dataset, drop a TWebFaunaDBClientDataset instance on the TMS WEB Core web client application form.

1) Set the WebFaunaDBClientDataset.ClientKey to the secret obtained while executing the script to create the table

2) Set the name of the data table on FaunaDB you want to work with via WebFaunaDBClientDataset.CollectionName.

3) Set the name of the user table on FaunaDB you want to work with for user management via WebFaunaDBClientDataset.UsersCollectionName

4) Set the name of the created index on the users table via WebFaunaDBClientDataset.UsersIndexName

To login with a user account, use WebFaunaDBClientDataset.Login() passing the username and password. After a successful login, the dataset becomes active and any connected DBaware control will show the data in the dataset.

From this moment on, operations such as edit, insert, delete will be handled via the WebFaunaDBClientDataset on the FaunaDB exposed table. To signup a new user, just use the WebFaunaDBClientDataset.Login() method with last Boolean parameter set to true.

Reference

These are the properties, methods, events of the TWebFaunaDbClientDataset component

Properties

Property Description
Active Set this to True to activate the DataSet. Field definitions must be present along with other properties described below.
ClientKey Sets the FaunaDB unique application secret obtained after creating a collection
CollectionName Sets the name of the collection in FaunaDB to work with as a dataset
KeyfieldName Sets the keyfield for the dataset
MasterSecret
UsersCollectionName Sets the name of the user collection in FaunaDB to work with as a dataset for user management
UsersIndexName Sets the name of the index for unique user identification

Methods

Method Description
Login(UserName,Password: string; IsSignup: boolean) Performs a login on the FaunaDB cloud database. When successful, the data is fetched in the dataset. When IsSignUp is true, a new account is created on the Rad Server instance
Refresh Reloads the data from FaunaDB in the web client dataset
AddSortFieldDef(aField: string; isAscending: Boolean) Sets the sort order of data returned from FaunaDB in the dataset
ClearSortFieldDefs Clears any previously set sort order

Events

The TWebFaunaDBClientDataset exposes the standard TDataSet events and is as such similar in functionality. It offers an additional event for handling a signup attempt with an already existing username

Event Description
OnUserExists(Sender, UserName) Event triggered when a signup is attempted using a username that already existed.