Skip to content

defineModel

defineModel() creates a model with typed metadata. Models contain only definitions — no logic, no queries, no providers.

import { defineModel, t, r } from "@jsorm/core";
export const User = defineModel("users", {
db: "main",
fields: {
id: t.uuid().primary().autoCreate(),
name: t.string(100),
email: t.string(255).unique(),
active: t.boolean().default(true),
createdAt: t.dateTime().autoCreate(),
},
relations: {
role: r.belongsTo("role", { localKey: "roleId" }),
posts: r.hasMany("post"),
},
});
Param Type Description
name string Table name in the database
schema.db string Database from config (validated against JsormDatabases)
schema.fields Record<string, FieldChain> Model fields
schema.relations Record<string, RelationDef> Relations (optional)
  • Models only contain metadata
  • Never contain queries, logic, or providers
  • Relations always reference models by name (string)
  • Never use cross imports between models
  • Model name must be singular (User, not Users)