Seeding Configuration Data

This guide will help you seed initial configuration data in your application.

Every T-shirt in the store has two predefined properties - brand, and type. Values for these properties should be stored in a database, and you should have an option to updated them as needed. This can be achieved in two ways:

  1. By creating forms in the admin section for managing both brand and type properties and then adding all values by hand
  2. By seeding all data from configuration files, omitting the UI altogether.

This tutorial describes the latter approach to adding configuration data.

Requirements

To follow this tutorial, you should be familiar with basic platformOS concepts, and the topics in the Get Started section. You should have followed this tutorial series up to the previous part "Using Shared Partials".

Steps

Seeding configuration data is a six-step process:

Step 1: Prepare configuration data files

Brand and product type models are pretty basic ones, they only contain a name of the object. In the future though, you might want to extend them with descriptions, photos, icons, etc. Storing these values in a partial would quickly become very cumbersome and limit the usefulness of GraphQL searches.

app/views/partials/data/brand.json.liquid

[
  "adidas",
  "adidas Originals",
  "Airstep Socks",
  "Alexander Del Rossa",
  "alpine swiss",
  "Amazon Essentials",
  "Arctic Extreme",
  "ARRIS",
  "boxercraft",
  "Calvin Klein",
  "Carhartt",
  "Champion",
  "Columbia",
  "Copper Fit",
  "CYZ Collection",
  "Debra Weitzner",
  "DG Hill",
  "Dickies",
  "Dockers",
  "Enerwear",
  "FITEXTREME",
  "fresh tees",
  "Fruit of the Loom",
  "Gildan",
  "Gold Toe",
  "Hanes",
  "Hanes Ultimate",
  "Happypop",
  "HSELL",
  "Legendary Whitetails",
  "Lucky Brand",
  "Marino Avenue",
  "Mottee&Zconia",
  "NY Threads",
  "ororo",
  "People Socks",
  "PUMA",
  "Ross Michaels",
  "Saucony",
  "Socksmith",
  "Southpole",
  "Stance",
  "Tesla",
  "Thermajohn",
  "Under Armour",
  "USBingoshop",
  "Wantdo",
  "WANTDO",
  "Wrangler"
]

app/views/partials/data/product_type.json.liquid

["Crew Neck", "V Neck", "Henley - Y Neck", "Polo", "Scoop Neck"]

.json part of the file name is optional, it just helps identify file type at a glance.


Tip


Many editors allow you to set syntax highlighting depending on more than just one extension. For example, in VS Code you can add below entry in editor settings.

"files.associations": { "*.json.liquid": "json" }

Step 2: Create processor partials

Processor files are supposed to be included inside of the migration file and should allow multiple execution without creating any duplicates. This way if you need to add more Models in the future, you can create a new migration, include processor partial without having to repeat the logic behind it.

app/views/partials/migrations/create_brands.liquid


{% graphql existing = 'get_brands' %}
{% parse_json brand_names %}{% include "data/brand.json" %}{% endparse_json %}

{% assign existing_names = existing.models.results | map: "name" %}

{% for name in brand_names %}
  {% unless existing_names contains name %}
    {% graphql res = "create_brand", name: name %}
  {% endunless %}
{% endfor %}

app/views/partials/migrations/create_product_types.liquid


{% graphql existing = 'get_product_types' %}
{% parse_json product_type_names %}{% include "data/product_type.json" %}{% endparse_json %}

{% assign existing_names = existing.models.results | map: "name" %}

{% for name in product_type_names %}
  {% unless existing_names contains name %}
    {% graphql res = "create_product_type", name: name %}
  {% endunless %}
{% endfor %}

Step 3: Create queries and mutations

app/graphql/brand/get_brands.graphql

query get_brands {
  models(filter: { name: { exact: "brand"} }, per_page: 999) {
    results {
      id
      name: property(name: "name")
    }
  }
}

app/graphql/brand/mutations/create_brand.graphql

mutation create_brand($name: String) {
  model_create(
    form_name: "create_brand_form"
    model: {
      model_schema_name: "brand"
      properties: [{ name: "name", value: $name }]
    }
  ) {
    id
  }
}

app/graphql/product_type/get_product_types.graphql

query get_product_types {
  models(name: "product_type", per_page: 999) {
    results {
      id
      name: property(name: "name")
    }
  }
}

app/graphql/product_type/mutations/create_product_type.graphql

mutation create_product_type($name: String) {
  model_create(
    form_name: "create_product_type_form"
    model: {
      model_schema_name: "product_type"
      properties: [{ name: "name", value: $name }]
    }
  ) {
    id
  }
}

Step 4: Create form configurations

app/forms/brand/create_brand_form.liquid

name: create_brand_form
resource: brand
fields:
  properties:
    name:
      validation:
        presence: true
---

app/forms/brand/create_product_type_form.liquid

name: create_product_type_form
resource: product_type
fields:
  properties:
    name:
      validation:
        presence: true
---

Step 5: Create migrations

pos-cli migrations generate development create_brands

app/migrations/TIMESTAMP_create_brands.liquid


{% include "migrations/create_brands" %}


```shell
pos-cli migrations generate development create_product_types

app/migrations/TIMESTAMP_create_product_types.liquid


{% include "migrations/create_product_types" %}

Step 6: Run migrations

Deploying your code or executing migrations with the pos-cli migration run command will save all of the data to the database. In the future, in order to add new brands, you can simply edit brand.yml and add a new migration that includes the processor file.


Note


There is quite a lot of overlap between brand and product_type models. Above example could be streamlined and optimized, to use common queries, Forms, etc.


Warning

Be careful when creating processor files that either modify or delete existing Models. Make sure that you do not inadvertently create data inconsistencies with missing reference to existing models.

Next steps

Congratulations! You’ve seeded your application with initial configuration data. You are now ready to create product management section in your admin panel.

Questions?

We are always happy to help with any questions you may have.