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 configuration
const dbConfig: DBConfig = {
type: DBType.Postgres, // or DBType.MySQL, DBType.SQLite
connectionString: 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, // versioned
columns: {
id: { type: DataTypes.Integer, required: true },
email: { type: DataTypes.String, length: 100, required: true, unique: true },
},
//timestamps
timestamps: {
createdAt: "created_at",
updatedAt: "update_at",
},
// define relationships
relations: [
{
type: RelationType.OneToMany,
target: () => UserRole,
property: "roles",
foreignKey: "userId",
},
],
// hooks
hooks: {
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, // 5MB
maxFiles: 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 repository
const userRepository = orm.getRepository(User);
// Create a new user
const newUser = await userRepository.create({
email: "ciniso@stabilize.xyz",
name: "Ciniso Matsebula ",
});
// find a single user
const foundUser = await userRepository.findOne(newUser.id);
// update a user
const updatedUser = await userRepository.update(newUser.id,
{ email: "admin@offbytesecure.com" }
);
// delete user
await 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.