Connect to databases and use the built-in ORM.
CanxJS comes with a lightweight yet powerful ORM that supports MySQL, PostgreSQL, and SQLite.
Database configuration lives in src/config/database.ts:
export default {
default: "mysql",
connections: {
mysql: {
driver: "mysql",
host: process.env.DB_HOST,
user: process.env.DB_USER,
// ...
}
}
}Generate a model:
bun run canx make:model Userimport { DB } from "canxjs";
// Fetch all users
const users = await DB.table("users").get();
// Filtering
const activeUsers = await DB.table("users")
.where("status", "active")
.orderBy("created_at", "desc")
.get();import { User } from "./models/User";
const user = await User.find(1);
user.name = "Updated Name";
await user.save();Have questions?
Join the discussion on GitHub