N
Nuxt4d ago
Ranma

Nuxt & Mikro-ORM

Hi everyone! 👋 I’m currently trying to implement Nuxt with Mikro-ORM, but I haven’t had much success yet. Has anyone here successfully implemented this combination? If so, I’d really appreciate it if you could share how you did it or point me in the right direction. Thanks so much!
7 Replies
kapa.ai
kapa.ai4d ago
Hi @Ranma. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- I apologize, but I don't have any specific information about implementing Mikro-ORM with Nuxt in the provided knowledge sources. The documents don't contain details about integrating Mikro-ORM with Nuxt. However, I can provide some general guidance based on Nuxt's architecture: 1. You would likely need to create a Nuxt module to integrate Mikro-ORM, similar to how other database integrations are done. 2. The integration would probably involve setting up Mikro-ORM in the server-side part of your Nuxt application. 3. You may need to configure Mikro-ORM in your nuxt.config.ts file. For more specific help, I'd recommend: 1. Checking the official Mikro-ORM documentation for any Nuxt-specific integration guides. 2. Looking for community-created modules or examples on GitHub. 3. Asking in the Nuxt community discussions for more targeted assistance. If you're open to alternative ORMs, Nuxt does have some built-in support for other database solutions. For example, there's a recipe for using Drizzle ORM in the Nuxt documentation, which might be easier to implement. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai4d ago
<@200321903448162305> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
Mittchel
Mittchel3d ago
GitHub
Nuxt 3 nitro + mikro-orm · Issue #5603 · nuxt/framework
Environment Operating System: Linux Node Version: v16.15.1 Nuxt Version: 3.0.0-rc.4 Package Manager: [email protected] Builder: vite User Config: - Runtime Modules: - Build Modules: - Reproduction import ...
Mittchel
Mittchel3d ago
And now you can use RequestContext.enter(this.orm.em); as mikro-orm docs say suitable for elysia style middlewares without a next like h3 if you not use something like app.use(fromNodeListener(exampleMiddleware())); something like
/**
* Creates new RequestContext instance and runs the code inside its domain.
* If the handler is async, the return value needs to be awaited.
* Uses `AsyncLocalStorage.enterWith()`, suitable for elysia style middlewares without a `next` callback.
*/
static enter(em, options = {}) {
const ctx = this.createContext(em, options);
this.storage.enterWith(ctx);
}
/**
* Creates new RequestContext instance and runs the code inside its domain.
* If the handler is async, the return value needs to be awaited.
* Uses `AsyncLocalStorage.enterWith()`, suitable for elysia style middlewares without a `next` callback.
*/
static enter(em, options = {}) {
const ctx = this.createContext(em, options);
this.storage.enterWith(ctx);
}
Mittchel
Mittchel3d ago
Use or look at code of that module https://github.com/boenrobot/nuxt-mikro-orm-module
GitHub
GitHub - boenrobot/nuxt-mikro-orm-module: A module to use MikroORM ...
A module to use MikroORM inside Nuxt applications. Contribute to boenrobot/nuxt-mikro-orm-module development by creating an account on GitHub.
Ranma
RanmaOP3d ago
@Mittchel Thank you! I found the articles you mentioned and was stuck at importing the entities. I found a solution by setting the entities in the config file as follows:
// mikro-orm.config.ts
import {
PostgreSqlDriver,
type Options,
ReflectMetadataProvider,
} from '@mikro-orm/postgresql'
import * as entities from './entities/index.js'
import 'dotenv/config'

const config: Options = {
driver: PostgreSqlDriver,
entities: [...Object.values(entities)].filter(
entity => typeof entity === 'function',
),
debug: true,
metadataProvider: ReflectMetadataProvider,
}

export default config
// mikro-orm.config.ts
import {
PostgreSqlDriver,
type Options,
ReflectMetadataProvider,
} from '@mikro-orm/postgresql'
import * as entities from './entities/index.js'
import 'dotenv/config'

const config: Options = {
driver: PostgreSqlDriver,
entities: [...Object.values(entities)].filter(
entity => typeof entity === 'function',
),
debug: true,
metadataProvider: ReflectMetadataProvider,
}

export default config
By doing this there is no need for an extra module
Mittchel
Mittchel3d ago
oh yes, I forgot to mention that
entities: [...Object.values(entities)].filter(
entity => typeof entity === 'function',
),
entities: [...Object.values(entities)].filter(
entity => typeof entity === 'function',
),

Did you find this page helpful?