Adding a new data model with Tailor Platform

It is very easy to change data schema with Tailor Platform. In Tailor Platform, the main database is called Tailor DB. With Tailor DB, you will be able to modify the data model of our templates, or add new Types (new data model. Equivalent to Table in a Relational Database) very easily.

This tutorial demonstrates how to use Tailor DB to add a new data schema to an existing template.

Tutorials – Create Schema

  • Complete Quickstart first If you haven't yet built the Inventory Management System app from our template.
  • See Core concepts to get an overview of Workspace, Organization, Application and Service.

Tutorial steps

To create a Category resource in Tailor DB, you'll need to:

  1. Define Category schema
  2. Apply the change using tailorctl
  3. Verify schema change through GraphQL

1. Define Category schema

To add the Category data schema, create a new file called category.tf and define the schema. In the file, design the details of the Category object type, such as fields and permission.

resource "tailor_tailordb_type" "category" {
  workspace_id = tailor_workspace.ims.id
  namespace    = tailor_tailordb.ims.namespace
  name         = "Category"
  description  = "Contains information about categories of products."

  settings = {
    publish_record_events = true
  }

  fields = {
    name = {
      type        = "string"
      description = "Name of the category."
    }
    description = {
      type        = "string"
      description = "Description of the category."
    }
    createdAt = {
      type        = "datetime"
      description = "Creation date of the category"
      hooks = {
        create = "(new Date()).toISOString()"
      }
    }
  }

  type_permission = local.permission_everyone
}

To add the Category data schema, create a new file called category.cue in the tailordb/master directory and define the schema.

The following example defines name and description fields for the category, and grants editor access to the users for the resources. The field like id is generated without specifically defined in category.cue. This is a pre-defined field and the name of the field is reserved. Please refer to Auto-generated fields. Specifying the package name is required.

category.cue

package master

import (
  "github.com/tailor-platform/tailorctl/schema/v2/tailordb"
  "tailor.build/template/services/tailordb:permissions"
)

Category: tailordb.#Type & {
  Name:        "Category"
  Description: "Contains information about categories of products."
  Fields: {
    name: {
      Type:        tailordb.#TypeString
      Description: "Name of the category."
    }
    description: {
      Type:        tailordb.#TypeString
      Description: "Description of the category."
    }
    createdAt: tailordb.CreatedAtField
    updatedAt: tailordb.UpdatedAtField
  }

  TypePermission: permissions.editorAccess
}

Add an entry for this schema in tailordb.cue manifest file

Next, add the entry into the tailordb.cue manifest file in the services/tailordb directory.

template/tailordb.cue

package tailordb

import (
  "github.com/tailor-platform/tailorctl/schema/v2/tailordb"
  "tailor.build/template/environment"
  "tailor.build/template/services/tailordb/master"
  ...
)
tailordb.#Spec & {
  Namespace: manifest.#app.namespace,
  Types: [
    ...
    {master.Category},
  ]
}

2. Apply the change using tailorctl

terraform apply

Generate new workspace CUE file and apply the Tailor DB changes.

tailorctl workspace apply -m ./workspace.cue

3. Verify schema change through GraphQL

Now, you can use the following generated GraphQL APIs to access Category data.

  • Query: category and categories
  • Mutation: createCategory, updateCategory, deleteCategory and changeCategory

Try it out to see how easy it is to manage your data!

# Open the GraphQL playground in the browser
tailorctl workspace app open -n ims

Let's create a Category resource with a specified name string.

query

# Create new Category data
mutation {
  createCategory(input: { name: "Gloves" }) {
    id
  }
}

example response

{
  "data": {
    "createCategory": {
      "id": "a80ccddb-2f42-4ac3-a298-a6a766a24316"
    }
  }
}

You can view the Category resource values in the Console.

Tailor Console

You can run the following query in the GraphQL playground. This will show you the values. Replace category's ID with ID obtained from the previous query.

query

# Get Category data
{
  category(id: "a80ccddb-2f42-4ac3-a298-a6a766a24316") {
    id
    name
  }
}

example response

{
  "data": {
    "category": {
      "id": "a80ccddb-2f42-4ac3-a298-a6a766a24316",
      "name": "Gloves"
    }
  }
}

Further information