Skip to Content

Derived properties

Add computed properties to an object set with withProperties, from linked objects, link aggregations, and expressions, then filter and sort on them.

Foundry docsDerived properties

withProperties adds properties that the server computes at query time, such as an employee's number of reports or their lead's name. You can select, filter, sort, and aggregate on them like stored properties, and nothing is written to the Ontology.

src/teamSizes.ts
import { Employee } from "@my-app/sdk";
import { client } from "./client";

const page = await client(Employee)
  .withProperties({
    reportCount: (base) => base.pivotTo("reports").aggregate("$count"),
    leadName: (base) => base.pivotTo("lead").selectProperty("fullName"),
  })
  .where({ reportCount: { $gt: 5 } })
  .fetchPage({
    $select: ["fullName", "reportCount", "leadName"],
    $orderBy: { reportCount: "desc" },
  });

for (const employee of page.data) {
  console.log(employee.fullName, employee.reportCount, employee.leadName);
}

withProperties takes an object that maps each new property name to a function. The function receives a builder for the object it computes on, and returns a definition. The result is a new object set with the new properties typed: reportCount is a number and leadName is string | undefined.

Builder methods

Start from the builder and chain:

MethodAvailable onReturns
pivotTo(link)Every builderA builder over the linked objects. After a many link, such as reports, the builder has where, pivotTo, and aggregate, but not selectProperty.
where(clause)Every builderThe builder, filtered. See Filter objects.
selectProperty(property)The base object, and builders reached through single links onlyThe property's value.
aggregate(metric, options?)Builders reached through any linkOne value computed over the linked objects.

selectProperty after a many link does not typecheck: an employee has many reports, so there is no single report name to select. Aggregate instead, for example with collectList.

aggregate takes $count or "<property>:<metric>":

MetricProperty typesOptionsResult type
$countNot applicableNonenumber
sum, avgNumericNonenumber | undefined
min, maxNumeric, date, timestampNoneThe property's type, or undefined
approximateDistinct, exactDistinctAllNonenumber
approximatePercentileNumeric{ percentile }number | undefined
collectListAll{ limit }, up to 100. Default 100.An array of the property's type
collectSetAll{ limit }, up to 100. Default 100.An array of distinct values
src/teamStats.ts
import { Employee } from "@my-app/sdk";
import { client } from "./client";

const page = await client(Employee)
  .withProperties({
    avgReportSalary: (base) => base.pivotTo("reports").aggregate("salary:avg"),
    reportNames: (base) =>
      base.pivotTo("reports").aggregate("fullName:collectList", { limit: 20 }),
    engineeringReports: (base) =>
      base.pivotTo("reports").where({ department: "Engineering" }).aggregate("$count"),
    skipLevelCount: (base) => base.pivotTo("reports").pivotTo("reports").aggregate("$count"),
  })
  .fetchPage({ $pageSize: 20 });

On a single link, selectProperty reads one property of the linked object. Chain single links to go further:

src/leadInfo.ts
import { Employee } from "@my-app/sdk";
import { client } from "./client";

const page = await client(Employee)
  .withProperties({
    officeCity: (base) => base.pivotTo("office").selectProperty("city"),
    leadsLeadName: (base) => base.pivotTo("lead").pivotTo("lead").selectProperty("fullName"),
  })
  .where({ officeCity: "London" })
  .fetchPage({ $select: ["fullName", "officeCity", "leadsLeadName"] });

Expressions

A numeric definition, from selectProperty on an integer, short, long, float, or double property, or a numeric aggregation, has expression methods. Each binary method takes a number or another numeric definition:

MethodResult
add(x), subtract(x), multiply(x), divide(x)Arithmetic.
min(x), max(x)The smaller or larger value.
abs(), negate()The absolute value, or the value times -1.

A date or timestamp definition has:

MethodResult
min(other), max(other)The earlier or later of two date or timestamp definitions.
extractPart(part)The "DAYS", "MONTHS", "QUARTERS", or "YEARS" part, as a string.
src/expressions.ts
import { Employee } from "@my-app/sdk";
import { client } from "./client";

const page = await client(Employee)
  .withProperties({
    salaryPerReport: (base) =>
      base.selectProperty("salary").divide(base.pivotTo("reports").aggregate("$count")),
    proposedSalary: (base) => base.selectProperty("salary").multiply(1.05),
    startYear: (base) => base.selectProperty("startDate").extractPart("YEARS"),
    earlierStart: (base) =>
      base.selectProperty("startDate").min(base.pivotTo("lead").selectProperty("startDate")),
  })
  .fetchPage({ $pageSize: 20 });

Arithmetic on two integers returns an integer. When either side is a double, a float, or a number literal, the result is a double.

Use derived properties

Once added, a derived property works everywhere a stored property does on that object set:

UseExample
Filter.where({ reportCount: { $gte: 3 } })
Sort.fetchPage({ $orderBy: { reportCount: "desc" } })
Select.fetchPage({ $select: ["fullName", "reportCount"] })
Aggregate.aggregate({ $select: { "reportCount:avg": "unordered" } })
Load by key.fetchOne(12345, { $select: ["reportCount"] })

Derived properties are returned when you omit $select. If you pass $select, list them in it, along with every stored property you need.

src/avgTeamSize.ts
import { Employee } from "@my-app/sdk";
import { client } from "./client";

const byDepartment = await client(Employee)
  .withProperties({
    reportCount: (base) => base.pivotTo("reports").aggregate("$count"),
  })
  .where({ reportCount: { $gt: 0 } })
  .aggregate({
    $select: { "reportCount:avg": "unordered", $count: "unordered" },
    $groupBy: { department: "exact" },
  });

Each call to withProperties returns a new object set, and the properties exist only on it and on the sets derived from it. To reuse a set of definitions, store the object set in a variable.

On this page