Getting started
Getting started
Section titled “Getting started”This example uses PostgreSQL, but the API stays the same across adapters.
1. Define models
Section titled “1. Define models”import { defineModel, t } from 'jsorm';
const Role = defineModel('roles', { id: t.number().primary(), name: t.string().unique(),});
const User = defineModel('users', { id: t.number().primary(), name: t.string(), email: t.string().optional(), active: t.boolean().default(true), role: t.belongsTo(Role),});2. Create a connection
Section titled “2. Create a connection”import { connectionDB } from 'jsorm';import { pgAdapter } from 'jsorm-pg';
const db = connectionDB({ adapter: pgAdapter({ name: 'main', connectionString: process.env.DATABASE_URL!, }),});3. Insert data
Section titled “3. Insert data”await db.insert(User, { name: 'Alice', email: 'alice@example.com', role: { connect: 1 },});4. Read nested data
Section titled “4. Read nested data”const users = await db.get(User, { select: { id: true, name: true, role: { name: true }, }, where: { active: true, },});Best practices
Section titled “Best practices”- Start with one adapter and one small feature area.
- Keep connection orchestration in a dedicated module.
- Use relations through the JSON API first, then drop to raw SQL only when needed.