Models
Models
Section titled “Models”Models are defined with defineModel() and field builders from t.
Example
Section titled “Example”import type { InferInput, InferModel } from 'jsorm';import { defineModel, t } from 'jsorm';
const User = defineModel('users', { id: t.number().primary(), name: t.string(), email: t.string().optional(), active: t.boolean().default(true), createdAt: t.date(),});
type UserRecord = InferModel<typeof User>;type UserInput = InferInput<typeof User>;Supported field builders
Section titled “Supported field builders”t.string()t.number()t.boolean()t.date()- relation builders like
t.belongsTo()andt.hasMany()
Common field modifiers
Section titled “Common field modifiers”.optional().nullable().primary().default(value).unique().index()
Best practices
Section titled “Best practices”- Keep table names stable and explicit.
- Put relation definitions next to related foreign key intent.
- Prefer inferred
InferModelandInferInputinstead of manual interfaces.