nestjs-query-mikro-orm 0.1.3 โ†’ 0.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,56 +1,59 @@
1
1
  # nestjs-query-mikro-orm
2
2
 
3
- A [NestJS Query](https://github.com/tripss/nestjs-query) adapter for [MikroORM](https://mikro-orm.io/).
3
+ A NestJS Query adapter for MikroORM.
4
+
5
+ This package provides a QueryService implementation for MikroORM entities, designed to work with the NestJS Query ecosystem while staying database-agnostic.
6
+
7
+ ## What You Get โœจ
8
+
9
+ - ๐Ÿš€ Full QueryService CRUD support for MikroORM repositories
10
+ - ๐Ÿ” Filtering, sorting, paging, and count support
11
+ - ๐Ÿ”— Relation operations:
12
+ - queryRelations
13
+ - findRelation
14
+ - countRelations
15
+ - aggregateRelations
16
+ - add/set/remove relation helpers
17
+ - ๐Ÿ“Š Aggregate queries with groupBy support
18
+ - ๐Ÿงน Soft delete support
19
+ - โš™๏ธ Configurable relation loading behavior:
20
+ - native-first relation loading
21
+ - MikroORM relation strategy control (`select-in`, `joined`, `balanced`)
22
+ - optional dataloader forwarding for native relation loads
23
+ - ๐Ÿง  TypeScript-first API
24
+ - ๐Ÿ“ฆ ESM and CJS package output
25
+
26
+ ## Installation ๐Ÿ“ฅ
4
27
 
5
- This library provides a seamless integration between NestJS Query and MikroORM, allowing you to build powerful GraphQL APIs with minimal boilerplate.
6
-
7
- ## Features
8
-
9
- - ๐Ÿš€ Full NestJS Query support with MikroORM
10
- - ๐Ÿ“ฆ Type-safe query building
11
- - ๐Ÿ” Advanced filtering and sorting
12
- - ๐Ÿ”— Relation query support
13
- - ๐Ÿ“Š Aggregation queries
14
- - ๐ŸŽฏ Soft delete support
15
- - โœจ Built with TypeScript
16
- - ๐Ÿ“ฆ Dual-format support (ESM & CommonJS)
17
-
18
- ## Compatibility
19
-
20
- This package supports both **ES Modules (ESM)** and **CommonJS (CJS)** for maximum compatibility across different Node.js environments and bundlers.
21
-
22
- - **ESM**: `import { MikroOrmQueryService } from 'nestjs-query-mikro-orm'`
23
- - **CommonJS**: `const { MikroOrmQueryService } = require('nestjs-query-mikro-orm')`
28
+ ```bash
29
+ pnpm add nestjs-query-mikro-orm
24
30
 
25
- The package automatically detects the import method and serves the appropriate format.
31
+ # peer dependencies (minimum typical setup)
32
+ pnpm add @mikro-orm/core @mikro-orm/decorators @mikro-orm/nestjs
33
+ pnpm add @nestjs/common @nestjs/core @ptc-org/nestjs-query-core
34
+ pnpm add class-transformer reflect-metadata rxjs
35
+ ```
26
36
 
27
- ## Installation
37
+ If you plan to enable dataloader-based native relation loading, also install:
28
38
 
29
39
  ```bash
30
- pnpm add nestjs-query-mikro-orm
31
-
32
- # Install peer dependencies if you haven't already
33
- pnpm add @mikro-orm/core @nestjs/common @nestjs/core @ptc-org/nestjs-query-core reflect-metadata rxjs
40
+ pnpm add dataloader
34
41
  ```
35
42
 
36
- ## Quick Start
43
+ ## Compatibility โœ…
37
44
 
38
- ### 1. Setup MikroORM Module
45
+ - Node.js: >= 18
46
+ - NestJS: 10 or 11
47
+ - MikroORM: 7
48
+ - @ptc-org/nestjs-query-core: 9.4+
39
49
 
40
- ```typescript
41
- import { NestQueryMikroOrmModule } from 'nestjs-query-mikro-orm';
42
- import { Module } from '@nestjs/common';
43
- import { UserEntity } from './user.entity';
50
+ The package supports both ESM and CommonJS consumers.
44
51
 
45
- @Module({
46
- imports: [NestQueryMikroOrmModule.forFeature([UserEntity])],
47
- })
48
- export class UserModule {}
49
- ```
52
+ ## Quick Start ๐Ÿš€
50
53
 
51
- ### 2. Create Your Entity
54
+ ### 1. Define an entity
52
55
 
53
- ```typescript
56
+ ```ts
54
57
  import { Entity, PrimaryKey, Property } from '@mikro-orm/core';
55
58
 
56
59
  @Entity()
@@ -63,112 +66,144 @@ export class UserEntity {
63
66
 
64
67
  @Property()
65
68
  email!: string;
66
-
67
- @Property()
68
- createdAt: Date = new Date();
69
69
  }
70
70
  ```
71
71
 
72
- ### 3. Use the Query Service
72
+ ### 2. Register the feature module
73
+
74
+ ```ts
75
+ import { Module } from '@nestjs/common';
76
+ import { NestjsQueryMikroOrmModule } from 'nestjs-query-mikro-orm';
77
+ import { UserEntity } from './user.entity';
78
+
79
+ @Module({
80
+ imports: [NestjsQueryMikroOrmModule.forFeature([UserEntity])],
81
+ })
82
+ export class UserModule {}
83
+ ```
73
84
 
74
- ```typescript
85
+ ### 3. Use the service
86
+
87
+ ```ts
75
88
  import { Injectable } from '@nestjs/common';
89
+ import { InjectRepository } from '@mikro-orm/nestjs';
90
+ import { EntityRepository } from '@mikro-orm/core';
76
91
  import { MikroOrmQueryService } from 'nestjs-query-mikro-orm';
77
92
  import { UserEntity } from './user.entity';
78
93
 
79
94
  @Injectable()
80
95
  export class UserService extends MikroOrmQueryService<UserEntity> {
81
- // Your custom methods here
96
+ constructor(@InjectRepository(UserEntity) readonly repo: EntityRepository<UserEntity>) {
97
+ super(repo);
98
+ }
82
99
  }
83
100
  ```
84
101
 
85
- ## Development
102
+ ## Feature Module Options โš™๏ธ
86
103
 
87
- ### Prerequisites
104
+ You can pass options to forFeature to configure generated QueryService providers.
88
105
 
89
- - Node.js >= 18
90
- - pnpm >= 9
106
+ `forFeature` supports both signatures:
91
107
 
92
- ### Setup
108
+ - `forFeature([Entity], 'contextName')`
109
+ - `forFeature([Entity], { contextName, queryServiceOpts })`
93
110
 
94
- ```bash
95
- # Install dependencies
96
- pnpm install
111
+ ```ts
112
+ import { NestjsQueryMikroOrmModule } from 'nestjs-query-mikro-orm';
97
113
 
98
- # Setup git hooks
99
- pnpm prepare
114
+ NestjsQueryMikroOrmModule.forFeature([UserEntity], {
115
+ contextName: 'default',
116
+ queryServiceOpts: {
117
+ useSoftDelete: true,
118
+ relationLoading: {
119
+ strategy: 'select-in',
120
+ useDataloader: true,
121
+ },
122
+ },
123
+ });
100
124
  ```
101
125
 
102
- ### Available Scripts
126
+ ### relationLoading options
103
127
 
104
- ```bash
105
- # Build the library
106
- pnpm build
128
+ - strategy:
129
+ - `select-in`: force MikroORM select-in loading strategy for native relation population
130
+ - `joined`: force MikroORM joined loading strategy for native relation population
131
+ - `balanced`: force MikroORM balanced loading strategy for native relation population
132
+ - omitted: use MikroORM default strategy
133
+ - useDataloader:
134
+ - when `true`, native relation load paths forward `dataloader: true` to MikroORM relation loading
107
135
 
108
- # Build in watch mode
109
- pnpm dev
136
+ ## Soft Delete ๐Ÿงน
110
137
 
111
- # Run tests
112
- pnpm test
138
+ Enable soft delete behavior by passing useSoftDelete in service options.
113
139
 
114
- # Run tests in watch mode
115
- pnpm test:watch
140
+ ```ts
141
+ super(repo, { useSoftDelete: true });
142
+ ```
116
143
 
117
- # Run tests with coverage
118
- pnpm test:coverage
144
+ When enabled, read operations automatically include deletedAt = null filtering, and restoreOne/restoreMany become available.
119
145
 
120
- # Lint code
121
- pnpm lint
146
+ ## Relation Loading Notes ๐Ÿ”„
122
147
 
123
- # Fix lint issues
124
- pnpm lint:fix
148
+ - Relation reads are native-first and use MikroORM population/identity-map semantics.
149
+ - If native extraction cannot resolve a relation shape (common with not-managed/custom hydrator flows), the adapter safely falls back to condition-based relation querying.
150
+ - `useDataloader` is applied to native relation loading paths, including batched relation resolution.
151
+ - For best dataloader results, enable MikroORM dataloaders in ORM config and execute relation loads in a batched execution frame.
125
152
 
126
- # Format code
127
- pnpm format
153
+ ## Integration with NestJS Query GraphQL ๐Ÿงฉ
128
154
 
129
- # Check formatting
130
- pnpm format:check
155
+ This library focuses on QueryService behavior. You can plug it into your NestJS Query GraphQL setup as your backing service implementation.
131
156
 
132
- # Type check
133
- pnpm typecheck
134
- ```
157
+ Use relation decorators/resolvers from your GraphQL layer as usual; relation methods from this adapter are compatible with request-scoped loader patterns used by NestJS Query GraphQL.
158
+
159
+ ## Public API ๐Ÿ“š
160
+
161
+ Main exports include:
135
162
 
136
- ### Git Hooks
163
+ - NestjsQueryMikroOrmModule
164
+ - MikroOrmQueryService
165
+ - RelationQueryService
166
+ - Query builder helpers from the query export
137
167
 
138
- This project uses Husky for git hooks:
168
+ ## Development ๐Ÿ› ๏ธ
139
169
 
140
- - **pre-commit**: Runs lint-staged to format and lint staged files
141
- - **pre-push**: Runs lint, typecheck, and tests before pushing
142
- - **commit-msg**: Validates commit messages using commitlint
170
+ ### Prerequisites ๐Ÿ“‹
143
171
 
144
- ### Commit Convention
172
+ - Node.js >= 18
173
+ - pnpm >= 9
145
174
 
146
- This project follows [Conventional Commits](https://www.conventionalcommits.org/):
175
+ ### Setup ๐Ÿงช
147
176
 
148
177
  ```bash
149
- feat: add new feature
150
- fix: fix bug
151
- docs: update documentation
152
- style: format code
153
- refactor: refactor code
154
- test: add tests
155
- chore: update dependencies
178
+ pnpm install
179
+ pnpm prepare
156
180
  ```
157
181
 
158
- ## License
182
+ ### Scripts ๐Ÿƒ
159
183
 
160
- MIT
184
+ ```bash
185
+ pnpm build
186
+ pnpm dev
187
+ pnpm test
188
+ pnpm test:watch
189
+ pnpm test:coverage
190
+ pnpm lint
191
+ pnpm lint:fix
192
+ pnpm format
193
+ pnpm format:check
194
+ pnpm typecheck
195
+ ```
161
196
 
162
- ## Author
197
+ ## Contributing ๐Ÿค
163
198
 
164
- Manuel Antunes
199
+ Contributions are welcome.
165
200
 
166
- ## Contributing
201
+ 1. Fork the repository
202
+ 2. Create your branch
203
+ 3. Commit your changes
204
+ 4. Push your branch
205
+ 5. Open a pull request
167
206
 
168
- Contributions are welcome! Please feel free to submit a Pull Request.
207
+ ## License ๐Ÿ“„
169
208
 
170
- 1. Fork the repository
171
- 2. Create your feature branch (`git checkout -b feature/amazing-feature`)
172
- 3. Commit your changes (`git commit -m 'feat: add amazing feature'`)
173
- 4. Push to the branch (`git push origin feature/amazing-feature`)
174
- 5. Open a Pull Request
209
+ MIT