SQL

This bundle integrates Kaviar's Core with MikroORM. A solution that allows us to easily work with SQL databases in a type-safe way.

Install#

npm i --save @kaviar/mikro-orm-bundle @mikro-orm/core @mikro-orm/mysql

You can install @mikro-orm/postgresql and others too. Please refer to the official documentation

import { MikroORMBundle } from "@kaviar/mikro-orm-bundle";
const kernel = new Kernel({
bundles: [
// ...
new MikroORMBundle({
options: {
// MikroORM Configuration
},
}),
],
});

Usage#

import { ORM } from "@kaviar/mikro-orm-bundle";
const orm = container.get(ORM); // ORM is a Token
// use orm as you normally would

Entities#

The default way of adding entities is through options.entities or through specifying a place and path for reading files and loading them.

However, when dealing with multiple bundles that extend this logic, we provide the following concept:

class AppBundle extends Bundle {
async prepare() {
const ormBundle = container.get(MikroORMBundle);
ormBundle.load([Entity1, Entity2]);
}
}

CLI Commands#

If you want to run mikro-orm cli commands follow the guide here: https://mikro-orm.io/docs/installation#setting-up-the-commandline-tool

The only difference is how the config file looks like. You will basically have to isolate your file kernel like this:

import { ORM, MikroORMBundle } from "@kaviar/mikroorm-bundle";
// A good idea at this stage would be to create a bundles/mikro-orm.ts which you import in your main kernel and the config file
const kernel = new Kernel({
bundles: [
new MikroORMBundle({
options: {
// your options
},
}),
],
});
await kernel.init();
const orm = kernel.container.get(ORM);