AppML Defining Models

An AppML model is a JSON configuration file that names the data source for a page and declares which fields and CRUD actions the page is permitted to use against it.

The Model as a Contract Between Page and Data

In AppML, a model is not markup and not code - it is a small JSON document that sits between the HTML page and the underlying data source. It tells the AppML library where the data comes from (a database table, a stored query, or in simpler setups a flat file), which fields exist on each record, and which operations - listing, inserting, updating, deleting - are allowed on that data. The HTML page never talks to the database directly; it talks to the model, and the model talks to whatever sits behind it.

  • A data source reference (a connection name, table name, or file path)
  • An optional ordering or filter rule for how records are listed
  • A fields definition describing each column's name and, optionally, its type or validation
  • A set of allowed actions: list, insert, update, delete

Example: an employees model

{
  "connection": "hyring_learn_db",
  "table": "employees",
  "orderby": "lastname",
  "fields": [
    { "name": "id", "type": "number", "primaryKey": true },
    { "name": "firstname", "type": "text", "required": true },
    { "name": "lastname", "type": "text", "required": true },
    { "name": "department", "type": "text" },
    { "name": "salary", "type": "number" }
  ],
  "actions": {
    "list": true,
    "insert": true,
    "update": true,
    "delete": true
  }
}

Once a model exists, the HTML page links to it with a single attribute, and every {{fieldname}} placeholder in that page's markup is resolved against the fields the model exposes. If a field isn't declared in the model, it can't be displayed or edited on the page - the model is the single source of truth for what data is reachable.

Note: Keep one model per table or view. Cramming multiple unrelated tables into a single model file makes the field list confusing and makes it harder to reason about which actions apply to which data.
Model propertyPurpose
connection / tableIdentifies the data source the model describes
fieldsLists each usable column and its name/type
actionsDeclares which of list, insert, update, delete are permitted
orderbySets the default sort order for listed records

Linking a page to its model

<html>
<head>
  <script src="appml.js"></script>
</head>
<body appml-data="employees.json">
  <div appml-repeat="records">
    {{firstname}} {{lastname}} - {{department}}
  </div>
</body>
</html>

Because the model is just JSON, it can be edited without touching the page's HTML or any server code - adding a field, disabling delete, or changing the sort order is a configuration change, not a rewrite.

Exercise: AppML Models

What does an AppML model primarily define?