Last active
April 30, 2019 16:23
-
-
Save B4nan/e1a87c76bc6d267cf4f28055d59bb212 to your computer and use it in GitHub Desktop.
Installation of MikroORM
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "compilerOptions": { | |
| "module": "commonjs", | |
| "target": "es2017", | |
| "moduleResolution": "node", | |
| "declaration": true, | |
| "strict": true, | |
| "strictPropertyInitialization": false, | |
| "experimentalDecorators": true | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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` | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const app = express(); | |
| // by providing request context, creating forked EntityManager will be handled automatically | |
| app.use((req, res, next) => { | |
| RequestContext.create(orm.em, next); | |
| }); |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@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 useentitiesDirsTsto make it faster.