Database
Models & ORM
Query and manage your database with CanxJS's elegant ORM. Zero-config setup with MySQL and PostgreSQL support.
Multi-Driver
MySQL primary, PostgreSQL secondary support
Query Builder
Fluent API for building complex queries
Model Classes
Type-safe models with static methods
Connection Pool
Built-in connection pooling for performance
Database Configuration
database.ts
1import { initDatabase, closeDatabase } from "canxjs";23// Initialize database connection4await initDatabase({5driver: "mysql", // or "postgresql"6host: "localhost",7port: 3306,8database: "myapp",9username: "root",10password: "password",11pool: { min: 2, max: 10 },12logging: true // Log SQL queries13});1415// Close connection on shutdown16process.on("SIGTERM", async () => {17await closeDatabase();18});
Defining Models
models/User.ts
1import { Model } from "canxjs";23// Define a User model4export class User extends Model {5protected static tableName = "users";6protected static primaryKey = "id";7protected static timestamps = true; // auto: created_at, updated_at89// Type definition for the model10id!: number;11name!: string;12email!: string;13role!: string;14created_at!: Date;15updated_at!: Date;16}
Basic CRUD Operations
crud.ts
1// Find by primary key2const user = await User.find(1);34// Get all records5const users = await User.all();67// Create a new record8const newUser = await User.create({9name: "John Doe",10email: "john@example.com",11role: "user"12});1314// Update by ID15await User.updateById(1, { name: "Jane Doe" });1617// Delete by ID18await User.deleteById(1);
Query Builder
queries.ts
1// Using the query builder for complex queries2const activeAdmins = await User.query()3.select("id", "name", "email")4.where("role", "=", "admin")5.where("status", "=", "active")6.orderBy("created_at", "desc")7.limit(10)8.get();910// With pagination11const page = 1;12const perPage = 20;13const users = await User.query()14.orderBy("id", "asc")15.limit(perPage)16.offset((page - 1) * perPage)17.get();1819// First record matching condition20const admin = await User.query()21.where("role", "=", "admin")22.first();
Where Conditions
conditions.ts
1// Basic where2const users = await User.query()3.where("status", "=", "active")4.get();56// Multiple conditions (AND)7const results = await User.query()8.where("role", "=", "admin")9.where("status", "=", "active")10.get();1112// OR condition13const results = await User.query()14.where("role", "=", "admin")15.orWhere("role", "=", "moderator")16.get();1718// WHERE IN19const users = await User.query()20.whereIn("id", [1, 2, 3, 4, 5])21.get();2223// NULL checks24const unverified = await User.query()25.whereNull("email_verified_at")26.get();2728const verified = await User.query()29.whereNotNull("email_verified_at")30.get();
Joins
joins.ts
1// Inner join2const postsWithUsers = await Post.query()3.select("posts.*", "users.name as author")4.join("users", "posts.user_id", "=", "users.id")5.get();67// Left join8const results = await User.query()9.select("users.*")10.leftJoin("posts", "users.id", "=", "posts.user_id")11.get();
Aggregates
aggregates.ts
1// Count records2const totalUsers = await User.query().count();34// Sum5const totalSales = await Order.query()6.where("status", "=", "completed")7.sum("amount");89// Average10const avgRating = await Review.query().avg("rating");1112// Group by with aggregates13const salesByCategory = await Product.query()14.select("category")15.groupBy("category")16.get();
Raw Queries
raw.ts
1// Execute raw SQL2const results = await User.query().raw(3"SELECT * FROM users WHERE created_at > ?",4["2024-01-01"]5);
Eager Loading (N+1 Solution)
CanxJS provides powerful eager loading capabilities to solve the N+1 query problem. You can load relationships at query time using with() or on existing models using load().
eager-loading.ts
1// Eager load 'posts' relationship2const users = await User.query()3.with("posts")4.get();56// Eager load multiple relationships7const posts = await Post.query()8.with("author", "comments")9.get();1011// Lazy Eager Loading (on existing instance)12const user = await User.find(1);13await user.load("posts");