Seeding Configuration Data

Last edit: 

Contributors: 

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:

  1. Prepare configuration data files
  2. Create processor partials
  3. Create queries and mutations
  4. Create form configurations
  5. Create migrations
  6. Run migrations

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.

marketplace_builder/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"
]

marketplace_builder/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.


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 Customizations in the future, you can create a new migration, include processor partial without having to repeat the logic behind it.

marketplace_builder/views/partials/migrations/create_brands.liquid


{% query_graph 'get_brands', result_name: 'existing' %}
{% parse_json brand_names %}{% include "data/brand.json" %}{% endparse_json %}

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

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

marketplace_builder/views/partials/migrations/create_product_types.liquid


{% query_graph 'get_product_types', result_name: 'existing' %}
{% parse_json product_type_names %}{% include "data/product_type.json" %}{% endparse_json %}

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

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

Step 3: Create queries and mutations

marketplace_builder/graph_queries/brand/get_brands.graphql

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

marketplace_builder/graph_queries/brand/mutations/create_brand.graphql

mutation create_brand($name: String) {
  customization_create(
    form_configuration_name: "create_brand_form"
    customization: {
      custom_model_type_name: "brand"
      properties: [{ name: "name", value: $name }]
    }
  ) {
    id
  }
}

marketplace_builder/graph_queries/product_type/get_product_types.graphql

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

marketplace_builder/graph_queries/product_type/mutations/create_product_type.graphql

mutation create_product_type($name: String) {
  customization_create(
    form_configuration_name: "create_product_type_form"
    customization: {
      custom_model_type_name: "product_type"
      properties: [{ name: "name", value: $name }]
    }
  ) {
    id
  }
}

Step 4: Create form configurations

marketplace_builder/form_configurations/brand/create_brand_form.liquid

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

marketplace_builder/form_configurations/brand/create_product_type_form.liquid

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

Step 5: Create migrations

marketplace-kit migrations generate development create_brands

marketplace_builder/migrations/TIMESTAMP_create_brands.liquid


{% include "migrations/create_brands" %}


```shell
marketplace-kit migrations generate development create_product_types

marketplace_builder/migrations/TIMESTAMP_create_product_types.liquid


{% include "migrations/create_product_types" %}

Step 6: Run migrations

Deploying your code or executing migrations with the marketplace-kit 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.



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. Check out our Help page, or contact us.