Metadata

This page describes the methods that analyse a query without running it: the parameters it needs, the shape of its result, the constraints that apply, its analysed tree and the statements it would execute. Use them to validate a query, to build a form or a type for its data, or to see the SQL that λORM generates.

These methods do not access the database, so the connections are not needed: you can call orm.init(schema, false) to load the schema without connecting.

Method Description
orm.parameters(query) The variables the query uses (which go in data), with their types.
orm.model(query) The properties of the result, with their types.
orm.constraints(query) The constraints that apply to the entities of the query.
orm.metadata(query) The analysed tree of the query: each statement with its entity, columns, parameters and constraints.
orm.plan(query, options?) How the query is executed: the dialect, the source and the statement of each part. options.stage selects the stage.
orm.normalize(query) The query in its complete form, for example with the properties of map made explicit.

The query can be a lambda function or a string. The same information is available from the CLI (lambdaorm parameters, model, constraints, metadata, plan) and from the Data Gateway (POST /parameters, /model, /constraints, /metadata, /plan).

Example

import { orm } from '@lambdaorm/lib'
import { Orders } from './domain/model' // model generated by `lambdaorm build`

(async () => {
    try {
        // load the schema without connecting to the databases
        await orm.init('./lambdaORM.yaml', false)
        const query = (id: number) => Orders
            .filter(p => p.id === id)
            .include(p => p.details)
            .map(p => ({ name: p.orderDate, customer: p.customer.name }))

        console.log(JSON.stringify(orm.parameters(query), null, 2))
        console.log(JSON.stringify(orm.model(query), null, 2))
        console.log(JSON.stringify(orm.constraints(query), null, 2))
        console.log(JSON.stringify(orm.metadata(query), null, 2))
        console.log(JSON.stringify(orm.plan(query, { stage: 'MySQL' }), null, 2))
    } catch (error: any) {
        console.error(error)
    } finally {
        await orm.end()
    }
})()

Results

The results include LambdaOrmParentId, the internal key that λORM uses to link the details to their order (see Include).

Parameters

The query needs id, an integer:

[
  { "name": "id", "type": "integer" },
  {
    "name": "details",
    "type": "Orders.details",
    "children": [
      { "name": "LambdaOrmParentId", "type": "[integer]" }
    ]
  }
]

Model

Each order of the result has name, customer and the array details:

[
  { "name": "name", "type": "dateTime" },
  { "name": "customer", "type": "string" },
  {
    "name": "details",
    "type": "Orders.details[]",
    "children": [
      { "name": "orderId", "type": "integer" },
      { "name": "productId", "type": "integer" },
      { "name": "unitPrice", "type": "decimal" },
      { "name": "quantity", "type": "decimal" },
      { "name": "discount", "type": "decimal" },
      { "name": "LambdaOrmParentId", "type": "integer" }
    ]
  }
]

Constraints

{
  "entity": "Orders",
  "constraints": [],
  "children": [
    { "entity": "Orders.details", "constraints": [] }
  ]
}

Metadata

One statement for the orders and, as its child, one for the details (pos fields omitted):

{
  "classtype": "Sentence",
  "name": "select",
  "type": "any",
  "entity": "Orders",
  "alias": "o",
  "clause": "select",
  "columns": [
    { "name": "name", "type": "dateTime" },
    { "name": "customer", "type": "string" },
    { "name": "__id", "type": "integer" }
  ],
  "parameters": [{ "name": "id", "type": "integer" }],
  "constraints": [],
  "values": [],
  "defaults": [],
  "children": [
    {
      "classtype": "Sentence",
      "name": "select",
      "type": "any",
      "entity": "Orders.details",
      "alias": "o1",
      "clause": "select",
      "columns": [
        { "name": "orderId", "type": "integer" },
        { "name": "productId", "type": "integer" },
        { "name": "unitPrice", "type": "decimal" },
        { "name": "quantity", "type": "decimal" },
        { "name": "discount", "type": "decimal" },
        { "name": "LambdaOrmParentId", "type": "integer" }
      ],
      "parameters": [{ "name": "LambdaOrmParentId", "type": "any" }],
      "constraints": [],
      "values": [],
      "defaults": [],
      "children": []
    }
  ]
}

Plan

{
  "entity": "Orders",
  "dialect": "MySQL",
  "source": "MySQL",
  "sentence": "SELECT o.OrderDate AS name, c.CompanyName AS customer, o.OrderID AS `__id` FROM Orders o INNER JOIN Customers c ON c.CustomerID = o.CustomerID WHERE o.OrderID = ? ",
  "children": [
    {
      "entity": "Orders.details",
      "dialect": "MySQL",
      "source": "MySQL",
      "sentence": "SELECT o1.OrderID AS orderId, o1.ProductID AS productId, o1.UnitPrice AS unitPrice, o1.Quantity AS quantity, o1.Discount AS discount, o1.OrderID AS LambdaOrmParentId FROM `Order Details` o1  WHERE  o1.OrderID IN (?) "
    }
  ]
}