Back to Mikro Orm

@mikro-orm/mariadb

packages/mariadb/README.md

7.0.142.0 KB
Original Source

@mikro-orm/mariadb

MikroORM driver for MariaDB databases, built on top of the mysql2 library with MariaDB-specific JSON and schema handling.

Installation

sh
npm install @mikro-orm/core @mikro-orm/mariadb

Usage

typescript
import { defineEntity, p, MikroORM } from '@mikro-orm/mariadb';

const AuthorSchema = defineEntity({
  name: 'Author',
  properties: {
    id: p.integer().primary(),
    name: p.string(),
    books: () => p.oneToMany(Book).mappedBy('author'),
  },
});
export class Author extends AuthorSchema.class {}
AuthorSchema.setClass(Author);

const BookSchema = defineEntity({
  name: 'Book',
  properties: {
    id: p.integer().primary(),
    title: p.string(),
    author: () => p.manyToOne(Author).inversedBy('books'),
  },
});
export class Book extends BookSchema.class {}
BookSchema.setClass(Book);

const orm = await MikroORM.init({
  entities: [Author, Book],
  dbName: 'my-db',
});

const author = orm.em.create(Author, { name: 'Jon Snow' });
orm.em.create(Book, { title: 'My Life on The Wall', author });
await orm.em.flush();

const authors = await orm.em.find(
  Author,
  { name: { $like: '%Jon%' } },
  {
    populate: ['books'],
  },
);

Features

Documentation

See the official MikroORM documentation and the SQL usage guide.

License

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