Defining the schema

Schemas are grouping of resources that will be used to provide behaviors and states for business rules

Creating the Schema

To create aschemafor theCategoryentity, we will create aCategorySchema.js file within the domain'sSchemafolder (src/domains/General/Category/Schema/CategorySchema.js) This file should extend theSchemaclass. The initial result will be a document like the one below.

src/domains/General/Category/Schema/CategorySchema.js
import Schema from 'src/app/Agnostic/Schema'

/**
 * @class {CategorySchema}
 */
export default class CategorySchema extends Schema {
  construct () {
  }
}

Importing resources

This definition is the most basic that we can do for this class because theconstructmethod must be implemented even in this first version. In addition to this method, we will make some more configurations, informing the class structure the properties: domain, path and service.

src/domains/General/Category/Schema/CategorySchema.js
import Schema from 'src/app/Agnostic/Schema'
import Service from 'src/domains/General/Category/Schema/CategoryService'
import { path, domain } from 'src/domains/General/Category/settings'

/**
 * @class {CategorySchema}
 */
export default class CategorySchema extends Schema {
  /**
   * @type {string}
   */
  static domain = domain

  /**
   * @type {string}
   */
  static path = path

  /**
   * @type {Rest}
   */
  service = Service

  /**
   * Configure schema
   */
  construct () {
  }
}

The static domain and path properties are imported from thesettings.js that we defined in Configuring the domain and theserviceproperty is theCategoryService.js file that we created in Preparing access to the API.

Creating thenamefield

With our schema started we will configure the fields that will be used by the entity. For this we will use theaddFieldmethod that must be called within theconstruct.

src/domains/General/Category/Schema/CategorySchema.js
// ...

/**
 * @class {CategorySchema}
 */
export default class CategorySchema extends Schema {
  // ...

  /**
   * Configure schema
   */
  construct () {
    this.addField('name')
  }
}

In the code snippet above we declare that ourschemawill work with thenamefield. By default, a created field is not displayed in thetableand is displayed on theformwith a width that occupies 100% of the line in a text field. TheprimaryKeyfield is loaded implicitly in all children ofSchema. Therefore, our class is mapping two fields: id and name. Later on we will see the settings of the fields and everything we can do with them, in addition to other configurable features in ourschema.

In the next step we will see how to add some routes so that we can see our screen being rendered.

Creating the views

Last updated

Was this helpful?