Skip to content

Instantly share code, notes, and snippets.

@B4nan
Last active April 30, 2019 16:23
Show Gist options
  • Select an option

  • Save B4nan/e1a87c76bc6d267cf4f28055d59bb212 to your computer and use it in GitHub Desktop.

Select an option

Save B4nan/e1a87c76bc6d267cf4f28055d59bb212 to your computer and use it in GitHub Desktop.
Installation of MikroORM
# using yarn
$ yarn add mikro-orm mongodb # for mongo
$ yarn add mikro-orm mysql2 # for mysql
$ yarn add mikro-orm pg # for postgresql
$ yarn add mikro-orm sqlite # for sqlite
# or npm
$ npm i -s mikro-orm mongodb # for mongo
$ npm i -s mikro-orm mysql2 # for mysql
$ npm i -s mikro-orm pg # for postgresql
$ npm i -s mikro-orm sqlite # for sqlite
{
"compilerOptions": {
"module": "commonjs",
"target": "es2017",
"moduleResolution": "node",
"declaration": true,
"strict": true,
"strictPropertyInitialization": false,
"experimentalDecorators": true
}
}
const orm = await MikroORM.init({
entities: [Author, Book, BookTag],
dbName: 'my-db-name',
clientUrl: '...', // defaults to 'mongodb://127.0.0.1:27017' for mongodb driver
type: 'mongo', // one of 'mysql', 'postgresql', 'sqlite', defaults to 'mongo'
autoFlush: false, // read more here: https://b4nan.github.io/mikro-orm/unit-of-work/
});
console.log(orm.em); // access EntityManager via `em` property
const app = express();
app.use((req, res, next) => {
req.em = orm.em.fork(); // save the fork to `req` object
});
app.get('/books', async (req, res) => {
const books = await req.em.find(Book); // use the fork via `req.em`
});
const app = express();
// by providing request context, creating forked EntityManager will be handled automatically
app.use((req, res, next) => {
RequestContext.create(orm.em, next);
});
@B4nan
Copy link
Copy Markdown
Author

B4nan commented Mar 3, 2019

@stalniy After few hours of thinking and prototyping, I found a way to get around this and provide the API you suggested. Take a look at mikro-orm/mikro-orm#18 :]

I also removed the need for entitiesDirsTs, using tsconfig.json by default. It is a bit slower but one can still use entitiesDirsTs to make it faster.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment