Back to Mikro Orm

@mikro-orm/sql

packages/sql/README.md

7.0.142.0 KB
Original Source

@mikro-orm/sql

Shared SQL driver base for MikroORM, built on top of Kysely. Provides the QueryBuilder, SQL connection management, and schema generation used by all SQL driver packages.

Installation

This package is automatically installed as a dependency of SQL driver packages (@mikro-orm/postgresql, @mikro-orm/mysql, etc.). You typically don't need to install it directly.

sh
npm install @mikro-orm/core @mikro-orm/postgresql  # installs @mikro-orm/sql automatically

Usage

QueryBuilder

typescript
import { MikroORM } from '@mikro-orm/postgresql';

const orm = await MikroORM.init({ ... });
const em = orm.em;

// Type-safe query builder
const qb = em.createQueryBuilder(Author);
const authors = await qb
  .select('*')
  .where({ books: { title: { $like: '%ORM%' } } })
  .orderBy({ name: 'asc' })
  .limit(10)
  .getResult();

Kysely Integration

Access the type-safe Kysely query builder directly for advanced queries:

typescript
const result = await em
  .getKysely()
  .selectFrom('author')
  .innerJoin('book', 'book.author_id', 'author.id')
  .select(['author.name', 'book.title'])
  .execute();

Features

  • QueryBuilder — type-safe SQL query building with automatic joins and aliasing
  • Kysely integration — direct access to type-safe Kysely query builder
  • Schema generation — create, update, and drop database schemas
  • Raw queriesraw() and sql tagged template helpers
  • Connection pooling and transaction management

Documentation

See the official MikroORM documentation and the Kysely integration guide.

License

Copyright © 2018-present Martin Adámek. Licensed under the MIT License.