Part of the alepha package. Import from alepha/orm.
npm install alepha
Postgres client based on Drizzle ORM, Alepha type-safe friendly.
1import { t } from "alepha"; 2import { $entity, $repository, db } from "alepha/postgres"; 3 4const users = $entity({ 5 name: "users", 6 schema: t.object({ 7 id: db.primaryKey(), 8 name: t.text(), 9 email: t.text(),10 }),11});12 13class App {14 users = $repository(users);15 16 getUserByName(name: string) {17 return this.users.findOne({ name: { eq: name } });18 }19}
This is not a full ORM, but rather a set of tools to work with Postgres databases in a type-safe way.
It provides:
$entity and $repository)createdAt, updatedAt, deletedAt, version.sql function.Migrations are supported via Drizzle ORM, you need to use the drizzle-kit CLI tool to generate and run migrations.
Primitives are functions that define and configure various aspects of your application. They follow the convention of starting with $ and return configured primitive instances.
For more details, see the Primitives documentation.
Creates a database entity primitive that defines table structure using TypeBox schemas.
1import { t } from "alepha"; 2import { $entity } from "alepha/orm"; 3 4const userEntity = $entity({ 5 name: "users", 6 schema: t.object({ 7 id: pg.primaryKey(), 8 name: t.text(), 9 email: t.email(),10 }),11});
Get the repository for the given entity.
Creates a PostgreSQL sequence primitive for generating unique numeric values.
Creates a transaction primitive for database operations requiring atomicity and consistency.
This primitive provides a convenient way to wrap database operations in PostgreSQL transactions, ensuring ACID properties and automatic retry logic for version conflicts. It integrates seamlessly with the repository pattern and provides built-in handling for optimistic locking scenarios with automatic retry on version mismatches.
Important Notes:
PgVersionMismatchError for optimistic locking{ tx } option to all repository operations within the transaction