First queries
Once configured, import from jsorm.server.ts and start querying.
Import
Section titled “Import”import { jsorm } from "./models/jsorm.server.js";CREATE
Section titled “CREATE”const { data: user } = await jsorm.users.create({ name: "Juan", email: "juan@test.com",});// Without select — returns full modelconst { data: users } = await jsorm.users.get({ where: { active: true },});
// With select — returns only selected fieldsconst { data: users } = await jsorm.users.get({ select: { id: true, name: true, role: { name: true } }, where: { active: true }, orderBy: { createdAt: "desc" },});const { data: user } = await jsorm.users.first({ select: { name: true, email: true }, where: { email: "juan@test.com" },});PAGINATION
Section titled “PAGINATION”const { data: page, meta } = await jsorm.users.get({ select: { id: true, name: true }, pagination: { page: 1, perPage: 10 },});// meta.page, meta.perPage, meta.total, meta.lastPageUPDATE
Section titled “UPDATE”const { data: affected } = await jsorm.users.update({ where: { id: user.id }, data: { name: "Juan Updated" },});DELETE
Section titled “DELETE”const { data: deleted } = await jsorm.users.delete({ where: { active: false },});COUNT / EXISTS
Section titled “COUNT / EXISTS”const { data: total } = await jsorm.users.count({ where: { active: true },});
const { data: exists } = await jsorm.users.exists({ where: { email: "juan@test.com" },});Multi-database (namespaced)
Section titled “Multi-database (namespaced)”// Main database — direct accessconst { data: users } = await jsorm.users.get();
// Non-main database — namespaced accessconst { data: sessions } = await jsorm.cache.sessions.get();const { data: events } = await jsorm.analytics.events.create({ name: "login" });Raw SQL
Section titled “Raw SQL”const { data: result } = await jsorm.raw({ sql: "SELECT avg(price) FROM products WHERE price > ?", params: [100],});
// In a specific databaseconst { data: analyticsResult } = await jsorm.raw({ db: "analytics", sql: "SELECT * FROM events",});