Stabilize Class
Core Stabilize class for database connections and repository management
Constructor
new Stabilize(config: DBConfig,cacheConfig?: CacheConfig,loggerConfig?: LoggerConfig)
Creates a new Stabilize instance with database, cache, and logger configuration.
Parameters:
- configDatabase configuration (required)
- cacheConfigCache configuration (optional)
- loggerConfigLogger configuration (optional)
example/stabilize-constructor.tstypescript
import { Stabilize, DBType, LogLevel } from "stabilize-orm";const stabilize = new Stabilize({type: DBType.Postgres,connectionString: process.env.NEON_DATABASE_URL,retryAttempts: 3,retryDelay: 1000,maxJitter: 100,},{enabled: true,ttl: 60,redisUrl: process.env.REDIS_URL,cachePrefix: "myapp:",strategy: "cache-aside",},{level: LogLevel.Info,filePath: "./logs/stabilize.log",});
getRepository()
getRepository<T>(model: new (...args: any[]) => T): Repository<T>
Gets a repository for a given model, used to perform CRUD operations.
example/get-repository.tstypescript
import { User } from "./models/User";const userRepository = stabilize.getRepository(User);const user = await userRepository.findOne(1);
transaction()
async transaction<T>(callback: (txClient: DBClient) => Promise<T>): Promise<T>
Executes a callback within a database transaction, ensuring all operations are atomic.
example/transaction.tstypescript
const userRepo = stabilize.getRepository(User);const profileRepo = stabilize.getRepository(Profile);await stabilize.transaction(async (txClient) => {const user = await userRepo.create({ name: "John Doe" },{},txClient);await profileRepo.create({ userId: user.id, bio: "Hello" },{},txClient);});
getCacheStats()
async getCacheStats(): Promise<CacheStats>
Retrieves statistics from the cache, if it is enabled.
example/cache-stats.tstypescript
const stats = await stabilize.getCacheStats();console.log(`Hits: ${stats.hits}, Misses: ${stats.misses}`);
close()
async close(): Promise<void>
Closes the database connection and disconnects the cache client for a graceful shutdown.
example/close.tstypescript
await stabilize.close();console.log("Connections closed.");
