Skip to Content

Ontology as code

Define object types, links, actions, and interfaces in TypeScript with @osdk/maker, compile them to Ontology IR, and develop against them locally.

Foundry docsOntology as code

Ontology as code lets you define Ontology types in TypeScript files that live next to your app. @osdk/maker validates the definitions and compiles them into an Ontology intermediate representation (IR) file, and @osdk/vite-plugin-oac turns that IR into a generated SDK and a mock Foundry server for local development.

Every ontology-as-code package is pre-1.0 and treated as experimental. Definitions, CLI flags, and the IR format can change in any release. Pin exact versions and read the changelog before you upgrade.

Packages

PackageWhat it does
@osdk/makerThe define* functions you write your ontology with, and the maker CLI that compiles it to IR.
@osdk/vite-plugin-oacA Vite plugin that compiles the ontology on every change, generates an SDK from it, and serves a mock Foundry API from the dev server. See Local development.
@osdk/maker-importConverts the metadata of an existing Foundry ontology into TypeScript files you can reference from your definitions. See Import existing types.
@osdk/seed-helpers, @osdk/seed-compilerBuild seed objects and links for the local Ontology server. See Local development.

How it works

  1. Define. You write object types, links, actions, interfaces, shared property types, and value types in .ontology/ontology.mts. Each define* call validates its input and registers the type.
  2. Compile. The maker CLI imports that file and writes the IR to ontology.json, plus value-types.json when you define value types.
  3. Develop locally. In a Vite app, @osdk/vite-plugin-oac runs maker on every change, converts the IR to Ontology metadata, generates an SDK into .osdk/src, and answers OSDK requests from the dev server with seeded data.
  4. Ship. The IR file is the artifact. None of these packages uploads or installs it into a Foundry stack.

Install

npm install --save-dev @osdk/maker

@osdk/maker installs a maker binary. For local development, also install the Vite plugin; see Local development.

The CLI loads your ontology file with Node's own import(), so a .mts or .ts entry file needs a Node.js version that strips TypeScript types natively: 22.18 or later, or 23.6 or later. On older versions, write the file as .mjs.

Project layout

ontology.mts
seed.ts
ontology-schema-lock.json
package.json
vite.config.ts
PathPurpose
.ontology/ontology.mtsYour definitions. The Vite plugin reads exactly this file name.
.ontology/seed.tsMock data for the Vite plugin's local server.
.ontology/ontology-schema-lock.jsonWritten by maker --write-locks once an interface opts into schema migrations. Commit it.
.osdk/srcThe SDK the Vite plugin generates. Do not edit it by hand.

Write an ontology

The ontology file is a module whose top-level define* calls register types as they run. Export each result so other files, such as a seed file or another ontology, can import it.

This file defines the Employee and Office object types, the links between them, a Worker interface, and a modifyEmployee action:

.ontology/ontology.mts
import {
  defineInterface,
  defineLink,
  defineModifyObjectAction,
  defineObject,
  defineSharedPropertyType,
} from "@osdk/maker";

export const name = defineSharedPropertyType({
  apiName: "name",
  type: "string",
  displayName: "Name",
});

export const worker = defineInterface({
  apiName: "Worker",
  displayName: "Worker",
  properties: { name },
});

export const office = defineObject({
  apiName: "Office",
  displayName: "Office",
  pluralDisplayName: "Offices",
  primaryKeyPropertyApiName: "officeId",
  titlePropertyApiName: "name",
  properties: {
    officeId: { type: "string", displayName: "Office ID" },
    name: { type: "string" },
    city: { type: "string" },
  },
});

export const employee = defineObject({
  apiName: "Employee",
  displayName: "Employee",
  pluralDisplayName: "Employees",
  primaryKeyPropertyApiName: "employeeId",
  titlePropertyApiName: "fullName",
  properties: {
    employeeId: { type: "integer", displayName: "Employee ID" },
    fullName: { type: "string", displayName: "Full name" },
    jobTitle: { type: "string" },
    department: { type: "string" },
    startDate: { type: "date" },
    salary: { type: "double" },
    skills: { type: "string", array: true },
    leadId: { type: "integer" },
    officeId: { type: "string" },
  },
  implementsInterfaces: [
    {
      implements: worker,
      propertyMapping: [{ interfaceProperty: "name", mapsTo: "fullName" }],
    },
  ],
});

export const officeOccupants = defineLink({
  apiName: "office-occupants",
  one: { object: office, metadata: { apiName: "occupants" } },
  toMany: { object: employee, metadata: { apiName: "office" } },
  manyForeignKeyProperty: "officeId",
});

export const employeeReports = defineLink({
  apiName: "employee-reports",
  one: { object: employee, metadata: { apiName: "reports" } },
  toMany: { object: employee, metadata: { apiName: "lead" } },
  manyForeignKeyProperty: "leadId",
});

export const modifyEmployee = defineModifyObjectAction({
  apiName: "modify-employee",
  objectType: employee,
});

In the SDK generated from this file, Employee has the links office and lead (one object each) and reports (many), Office has occupants, and the action is modifyEmployee: action API names must be lowercase and hyphenated, and the SDK generator converts them to camelCase. The leadId and officeId properties are the foreign keys the links join on.

Compile it:

npx maker -i .ontology/ontology.mts -o ontology.json

Each define* function throws when its input is invalid, for example when a title or primary key property is missing, an API name breaks the naming rules, or two types share an API name. maker reports the first error and exits with code 1; run it with -v to print the stack trace.

API names and namespaces

--apiNamespace com.example prefixes the API names of the object types, interfaces, shared property types, and action types you define, so Employee becomes com.example.Employee. Pass names without the prefix in your definitions. Link type API names are not prefixed, and value types are prefixed only when you set namespacePrefix: true.

NameRule
Object typeStarts with a letter, then letters and digits, with optional .-separated segments, at most 100 characters.
Property, link sideStarts with a letter, then letters, digits, and _, at most 100 characters.
Action typeLowercase letters and digits, in segments separated by - or ..
Link typeConverted to a lowercase, hyphenated ID (officeOccupants and office_occupants both become office-occupants), which must start with a letter.
Object types, interfaces, properties, and link sidesCannot be a reserved word: ontology, object, property, link, relation, rid, primaryKey, typeId, or ontologyObject, in any case.

maker CLI

maker [options]
OptionTypeDefaultDescription
-i, --inputstring.ontology/ontology.tsThe ontology file to load.
-o, --outputstringontology.jsonWhere to write the IR.
--valueTypesOutputstringvalue-types.jsonWhere to write value types. Written only when the ontology defines or imports value types.
--apiNamespacestring""Prefix for API names, such as com.example. A trailing . is added for you. Must match ^[a-z0-9-]+(\.[a-z0-9-]+)*\.$.
-d, --outputDirstringAlso write each defined type as a TypeScript module under <dir>/codegen, with an index.ts that re-exports them. Other ontologies can import this output; see Import existing types.
--dependenciesstringWrite the namespaces and versions of imported ontology packages to this JSON file.
--generateCodeSnippetsbooleanfalseWrite OSDK usage snippets for each object type, action type, and interface.
--codeSnippetPackageNamestring""The SDK package name shown in the snippets.
--codeSnippetDirstring./Where to write the snippets.
--randomnessKeystringA UUID recorded in the IR to keep generated entities unique. Rejected if it is not a UUID.
--lockfilestringontology-schema-lock.json next to the inputThe interface schema lockfile.
--writeLocksbooleanUpdate the lockfile instead of failing when it is out of date.
-y, --yesbooleanAccept detected schema migration finalizations and deletions without a prompt. Requires --writeLocks.
-v, --verbosecount-v logs debug output and stack traces; -vv logs trace output.

Next steps

On this page