Drizzle ORM utilities for AsenaJS - A powerful and type-safe database integration package that provides generic Database services and Repository patterns.
- 🚀 Generic Database Service - Support for multiple database types (PostgreSQL, MySQL, BunSQLite)
- 🎯 Type-Safe Repository Pattern - Full TypeScript support with inferred types
- 🏷️ Decorator-Based Configuration - Easy setup with
@Databaseand@Repositorydecorators - 🔧 AsenaJS Integration - Seamless IoC container integration
- 📦 Multiple Database Support - Connect to different databases simultaneously
- ⚡ Performance Optimized - Connection pooling and efficient query execution
bun add @asenajs/asena-drizzle drizzle-orm
# For PostgreSQL
bun add pg
# For MySQL
bun add mysql2import { Database } from '@asenajs/asena-drizzle';
@Database({
type: 'postgresql',
config: {
host: 'localhost',
port: 5432,
database: 'myapp',
user: 'postgres',
password: 'password',
},
name: 'MainDatabase' // Optional: for multiple databases but we recommend using it
})
export class MyDatabase extends AsenaDatabaseService {}import { BaseRepository, Repository } from '@asenajs/asena-drizzle';
import { pgTable, uuid, text } from 'drizzle-orm/pg-core';
const users = pgTable('users', {
id: uuid('id').primaryKey().defaultRandom(),
name: text('name').notNull(),
email: text('email').notNull().unique(),
});
@Repository({
table: users,
databaseService: 'MainDatabase',
})
export class UserRepository extends BaseRepository<typeof users> {
async findByEmail(email: string) {
return this.findOne(eq(users.email, email));
}
}import { Service } from '@asenajs/asena/decorators';
import { Inject } from '@asenajs/asena/decorators/ioc';
@Service("UserService")
export class UserService {
@Inject("UserRepository")
private userRepository: UserRepository;
async createUser(name: string, email: string) {
return this.userRepository.create({ name, email });
}
async getAllUsers() {
return this.userRepository.findAll();
}
async getUsersPaginated(page = 1, limit = 10) {
return this.userRepository.paginate(page, limit);
}
}postgresql- PostgreSQL using pg (node-postgres) with connection poolingmysql- MySQL using mysql2bun-sql- BunSQL using Bun's SQL interfacesqlite- SQLite (coming soon)
The BaseRepository provides the following built-in methods:
findById(id)- Find record by IDfindAll(where?)- Find all records with optional conditionsfindOne(where)- Find single recordcreate(data)- Create new recordcreateMany(data[])- Create multiple recordsupdateById(id, data)- Update record by IDupdate(where, data)- Update records with conditionsdeleteById(id)- Delete record by IDdelete(where)- Delete records with conditionscount()- Count all recordscountBy(where)- Count records with conditionspaginate(page, limit, where?, orderBy?)- Paginated resultsexists(where)- Check if record exists
@Database({
type: 'postgresql',
config: { /* primary db config */ },
name: 'PrimaryDB'
})
export class PrimaryDatabase extends AsenaDatabaseService {}
@Database({
type: 'mysql',
config: { /* analytics db config */ },
name: 'AnalyticsDB'
})
export class AnalyticsDatabase extends AsenaDatabaseService {}
@Repository({
table: users,
databaseService: 'PrimaryDB',
})
export class UserRepository extends BaseRepository<typeof users> {}
@Repository({
table: events,
databaseService: 'AnalyticsDB',
})
export class EventRepository extends BaseRepository<typeof events> {}@Database({
type: 'postgresql',
config: {
connectionString: process.env.DATABASE_URL,
host: '', port: 0, database: '', user: '', password: ''
}
})
export class DatabaseFromURL extends AsenaDatabaseService {}MIT
Contributions are welcome! Please feel free to submit a Pull Request.