AppML Data Sources

A model file doesn't hold the data itself - it describes where the data comes from and which actions are allowed to touch it, from a simple static list to a full database table.

What a Model Describes

The model referenced by appml-data is a JSON configuration, not a data dump. Its job is to name a data source and define the actions available against it, so the template never has to know whether that source is a static file or a live database.

  • An identifier for the data source (for example, a database and table name)
  • The fields that make up each record
  • One or more named actions - commonly list, and optionally insert, update, and delete - each describing how that operation reaches the data source

Example: a database-backed model

{
  "database": "companydata",
  "table": "employees",
  "actions": [
    { "name": "list", "sql": "select * from employees" },
    { "name": "insert", "sql": "insert into employees (firstname,lastname,department) values (@firstname,@lastname,@department)" },
    { "name": "update", "sql": "update employees set firstname=@firstname, lastname=@lastname, department=@department where id=@id" },
    { "name": "delete", "sql": "delete from employees where id=@id" }
  ]
}

Static Files as a Data Source

A model can also point at data that's simply embedded as a plain JSON array, with just a list action and no insert/update/delete defined. That's enough for read-only pages, demos, or working out a template's layout before any real persistence is wired up.

Example: a static model

{
  "records": [
    { "name": "Notebook", "price": 3.5 },
    { "name": "Pen", "price": 1.2 },
    { "name": "Backpack", "price": 25 }
  ],
  "actions": [
    { "name": "list" }
  ]
}
Source typeSetup effortSupports insert/update/delete?
Static JSON recordsLowest - a single fileNo, read-only by default
Database-backed tableHigher - needs an action per operationYes, one action per operation you define
Note: Prototype the template against a static model first; once the layout and placeholders are correct, switching the same page to a database-backed model is a matter of swapping the model file, not the HTML.

Controllers and Actions covers how each action in the model actually reaches server-side logic.

Exercise: AppML Data

What does the appml-data attribute specify?