Skip to content

Templates ​

Workspace Key draft workspace template query xyz security

workspace.templates holds reusable layers, locales, SQL query templates and views, loaded inline or from a src; use to share configuration and add queries.

Resource and sources

workspace.templates maps keys to templates. A template is one of:

  • a layer or locale template, merged into the layers and locales which reference it;
  • a query template: SQL in template, or a module whose default export renders SQL; requests name it with the template param;
  • a view template: HTML for the view API.

The core query templates, eg. wkt, location_get and field_stats, are always available; CUSTOM_TEMPLATES adds more.[1]

Sources ​

src loads a template when it is first used: https: from a URL, file: from the XYZ_CWD directory, cloudfront: and signer prefixes from their providers. ${NAME} in a src is replaced by the SRC_NAME environment variable. module: true compiles a JavaScript module from the src. An unreachable src adds an error to the composed object.

Composition ​

layer.template names a template, or is an object with a key, which the layer is merged into; the layer's own keys win. templates lists more templates merged into the layer in order. Locales compose the same way.[2]include_props and exclude_props limit which keys a template contributes. A template with a role is only merged for users with that role; see roles.

Query templates ​

Requests never send SQL. A query template's SQL uses ${name} for checked identifiers, eg. ${table}, ${filter}, ${viewport}, and %{name} for values, which are sent as query parameters.[3] dbs selects the connection; admin requires an admin user, and layer a layer param; statement_timeout limits the query in milliseconds.

Pitfalls ​

  • ${...} is written into the SQL. Use %{...} for any value a user can choose.
  • A template's role hides it from users without the role, silently.
  • The workspace schemas do not resolve templates. A layer whose template is missing or incomplete passes them, and is dropped or fails when XYZ composes the locale. Template contents are not checked; test the composed workspace with the admin-only /api/workspace/test.

Examples ​

A layer template reused by a locale, and a query template shown in a dataview.

json
{
  "dbs": "DEVDB",
  "templates": {
    "countries_layer": {
      "format": "geojson",
      "table": "naturalearth.countries",
      "geom": "geom_4326",
      "srid": "4326",
      "qID": "id"
    },
    "population_by_continent": {
      "template": "SELECT continent, sum(pop_est) AS population FROM ${table} GROUP BY continent"
    }
  },
  "locale": {
    "layers": {
      "countries": {
        "template": "countries_layer",
        "display": true,
        "dataviews": {
          "continents": {
            "dataview": "Json",
            "query": "population_by_continent",
            "queryparams": { "table": true }
          }
        }
      }
    }
  }
}

  1. workspace/getTemplate.js ↩︎

  2. workspace/composeObj.js mergeTemplateIntoObj ↩︎

  3. query.js getQueryFromTemplate ↩︎

XYZ v5.1.0. Generated from knowledge/ at dd7a6fa27736502733499c1bf485f15659b10338. Concepts marked draft are not reviewed.