Usage with data source

Learn how to use templates uses data source.

Templates with data source

Once you have created a data source, you can now reference it in your templates.

Example

Let's say you have a data source called user and you want to use it in your template.

{
  "name": "user",
  "fields": [
    {
      "name": "id",
      "type": "string"
    },
    {
      "name": "name",
      "type": "string"
    },
    {
      "name": "email",
      "type": "string"
    },
    {
      "name": "age",
      "type": "number"
    },
    {
      "name": "createdAt",
      "type": "string"
    },
    {
      "name": "updatedAt",
      "type": "string"
    }
  ]
}

You can reference it in your template like this:

interface {{.schema.name}} {
  {{range .schema.fields}}
    {{.name}}: {{.type}}
  {{end}}
}

Derived will replace the {{.schema.name}} with the name of the data source and the {{range .schema.fields}} with the fields of the data source.

interface User {
  id: string
  name: string
  email: string
  age: number
  createdAt: string
  updatedAt: string
}

Same template can be used for different data sources.

{
  "name": "product",
  "fields": [
    {
      "name": "id",
      "type": "string"
    },
    {
      "name": "name",
      "type": "string"
    },
    {
      "name": "price",
      "type": "number"
    },
  ]
}
interface Product {
  id: string
  name: string
  price: number
}

On this page