Quick Start
Get up and running with Stabilize ORM in under 5 minutes
1
Install Stabilize ORM
bun add stabilize-orm
2
Configure Database Connection
Create a configuration file for your database
config/database.tstypescript
import { DBType, type DBConfig } from "stabilize-orm";// database configurationconst dbConfig: DBConfig = {type: DBType.Postgres, // or DBType.MySQL, DBType.SQLiteconnectionString: process.env.DATABASE_URL,retryAttempts: 3,retryDelay: 1000,};export default dbConfig;
3
Define Your First Model
Create a model to represent your data
models/user.tstypescript
import { defineModel, DataTypes, RelationType } from "stabilize-orm";import { UserRole } from "./UserRole";const User = defineModel({tableName: "users",versioned: true, // versionedcolumns: {id: { type: DataTypes.Integer, required: true },email: { type: DataTypes.String, length: 100, required: true, unique: true },},//timestampstimestamps: {createdAt: "created_at",updatedAt: "update_at",},// define relationshipsrelations: [{type: RelationType.OneToMany,target: () => UserRole,property: "roles",foreignKey: "userId",},],// hookshooks: {beforeSave: (entity) => {}),afterSave: (entity) => {}),beforeCreate: (entity) => {}),afterCreate: (entity) => {}),beforeUpdate: (entity) => {}),afterCreate: (entity) => {}),},})export { User };
4
Initialize Stabilize
Initialize stabilize instance, configure cache and logger
db/index.tstypescript
import { Stabilize,type CacheConfig, type LoggerConfig, LogLevel } from "stabilize-orm";import dbConfig from "./database";const cacheConfig: CacheConfig = {enabled: process.env.CACHE_ENABLED === "true",redisUrl: process.env.REDIS_URL,ttl: 60,};const loggerConfig: LoggerConfig = {level: LogLevel.Info,filePath: "logs/stabilize.log",maxFileSize: 5 * 1024 * 1024, // 5MBmaxFiles: 3,};export const orm = new Stabilize(dbConfig, cacheConfig, loggerConfig);
5
Start Querying
Use the repository to interact with your data
import { orm } from "./db";// create a user repositoryconst userRepository = orm.getRepository(User);// Create a new userconst newUser = await userRepository.create({email: "ciniso@stabilize.xyz",name: "Ciniso Matsebula ",});// find a single userconst foundUser = await userRepository.findOne(newUser.id);// update a userconst updatedUser = await userRepository.update(newUser.id,{ email: "admin@offbytesecure.com" });// delete userawait userRepository.delete(newUser.id);
You're All Set!
You now have a working Stabilize ORM setup. Explore the documentation to learn about advanced features like relationships, transactions, and caching.
