|
1
|
--- |
|
2
|
sidebar_label: "find" |
|
3
|
--- |
|
4
|
|
|
5
|
import { TypeList } from "docs-ui" |
|
6
|
|
|
7
|
export const metadata = { |
|
8
|
title: `find Method - Data Model Repository Reference`, |
|
9
|
} |
|
10
|
|
|
11
|
# {metadata.title} |
|
12
|
|
|
13
|
The `find` method of a data model repository retrieves a list of records. |
|
14
|
|
|
15
|
<Note> |
|
16
|
|
|
17
|
This reference assumes you've already resolved the data model repository, as explained in the [Data Model Repository Reference](../../page.mdx) documentation. |
|
18
|
|
|
19
|
</Note> |
|
20
|
|
|
21
|
## `find` Parameters |
|
22
|
|
|
23
|
<TypeList |
|
24
|
types={[ |
|
25
|
{ |
|
26
|
type: "`object`", |
|
27
|
name: "data", |
|
28
|
description: "Options to filter and configure the query to retrieve the records.", |
|
29
|
required: false, |
|
30
|
children: [ |
|
31
|
{ |
|
32
|
type: "`object`", |
|
33
|
name: "where", |
|
34
|
description: "Filters to apply to the query. The keys are the property names of the data model, and the values are the values to filter by.", |
|
35
|
required: false, |
|
36
|
}, |
|
37
|
{ |
|
38
|
type: "`object`", |
|
39
|
name: "options", |
|
40
|
description: "Options to configure the query.", |
|
41
|
required: false, |
|
42
|
children: [ |
|
43
|
{ |
|
44
|
type: "`string[]`", |
|
45
|
name: "populate", |
|
46
|
description: "An array of relation names to retrieve.", |
|
47
|
required: false, |
|
48
|
}, |
|
49
|
{ |
|
50
|
type: "`string[]`", |
|
51
|
name: "fields", |
|
52
|
description: "An array of property names to retrieve.", |
|
53
|
required: false, |
|
54
|
}, |
|
55
|
{ |
|
56
|
type: "`number`", |
|
57
|
name: "limit", |
|
58
|
description: "The number of records to retrieve.", |
|
59
|
required: false, |
|
60
|
defaultValue: "`15`", |
|
61
|
}, |
|
62
|
{ |
|
63
|
type: "`number`", |
|
64
|
name: "offset", |
|
65
|
description: "The number of records to skip before the retrieved records.", |
|
66
|
required: false, |
|
67
|
defaultValue: "`0`", |
|
68
|
}, |
|
69
|
{ |
|
70
|
type: "`object`", |
|
71
|
name: "orderBy", |
|
72
|
description: "An object whose keys are the property names of the data model, and values are the sorting order.", |
|
73
|
required: false, |
|
74
|
}, |
|
75
|
{ |
|
76
|
type: "`string | string[]`", |
|
77
|
name: "groupBy", |
|
78
|
description: "Group the records by one or more properties.", |
|
79
|
required: false, |
|
80
|
}, |
|
81
|
{ |
|
82
|
type: "`select-in` \\| `joined`", |
|
83
|
name: "strategy", |
|
84
|
description: "The strategy to use to retrieve the records. Learn more in the [MikroORM documentation](https://mikro-orm.io/docs/loading-strategies). If you don't specify a strategy and you specify the `limit` or `offset` options, the default strategy is `select-in`. Otherwise, the default strategy is `joined`.", |
|
85
|
required: false, |
|
86
|
} |
|
87
|
] |
|
88
|
} |
|
89
|
] |
|
90
|
} |
|
91
|
]} |
|
92
|
sectionTitle="find Parameters" |
|
93
|
openedLevel={1} |
|
94
|
/> |
|
95
|
|
|
96
|
--- |
|
97
|
|
|
98
|
## Retrieve List of Records |
|
99
|
|
|
100
|
To retrieve a list of records matching a set of filters, use the `find` method of the data model repository: |
|
101
|
|
|
102
|
```ts title="src/modules/blog/service.ts" |
|
103
|
import { |
|
104
|
InjectTransactionManager, |
|
105
|
MedusaContext, |
|
106
|
MedusaService, |
|
107
|
InjectManager, |
|
108
|
} from "@medusajs/framework/utils" |
|
109
|
import { Context, InferTypeOf, DAL } from "@medusajs/framework/types" |
|
110
|
import { EntityManager } from "@medusajs/framework/mikro-orm/knex" |
|
111
|
import Post from "./models/post" |
|
112
|
|
|
113
|
class BlogModuleService extends MedusaService({ |
|
114
|
Post, |
|
115
|
}){ |
|
116
|
// ... |
|
117
|
@InjectTransactionManager() |
|
118
|
protected async doSomething_( |
|
119
|
@MedusaContext() sharedContext?: Context<EntityManager> |
|
120
|
): Promise<any> { |
|
121
|
const posts = await this.postRepository_.find() |
|
122
|
|
|
123
|
return posts |
|
124
|
} |
|
125
|
|
|
126
|
@InjectManager() |
|
127
|
async doSomething( |
|
128
|
id: string, |
|
129
|
@MedusaContext() sharedContext?: Context<EntityManager> |
|
130
|
): Promise<any> { |
|
131
|
return await this.doSomething_(id, sharedContext) |
|
132
|
} |
|
133
|
} |
|
134
|
``` |
|
135
|
|
|
136
|
### Returns |
|
137
|
|
|
138
|
By default, the method returns an array of the first `15` records of the data model. |
|
139
|
|
|
140
|
--- |
|
141
|
|
|
142
|
## Filter Records |
|
143
|
|
|
144
|
You can filter the retrieved records by passing filters as a first parameter of the `find` method: |
|
145
|
|
|
146
|
```ts title="src/modules/blog/service.ts" |
|
147
|
import { |
|
148
|
InjectTransactionManager, |
|
149
|
MedusaContext, |
|
150
|
MedusaService, |
|
151
|
InjectManager, |
|
152
|
} from "@medusajs/framework/utils" |
|
153
|
import { Context, InferTypeOf, DAL } from "@medusajs/framework/types" |
|
154
|
import { EntityManager } from "@medusajs/framework/mikro-orm/knex" |
|
155
|
import Post from "./models/post" |
|
156
|
|
|
157
|
class BlogModuleService extends MedusaService({ |
|
158
|
Post, |
|
159
|
}){ |
|
160
|
// ... |
|
161
|
@InjectTransactionManager() |
|
162
|
protected async doSomething_( |
|
163
|
id: string[], |
|
164
|
@MedusaContext() sharedContext?: Context<EntityManager> |
|
165
|
): Promise<any> { |
|
166
|
const posts = await this.postRepository_.find({ |
|
167
|
where: { |
|
168
|
id, |
|
169
|
}, |
|
170
|
}) |
|
171
|
|
|
172
|
return posts |
|
173
|
} |
|
174
|
|
|
175
|
@InjectManager() |
|
176
|
async doSomething( |
|
177
|
id: string, |
|
178
|
@MedusaContext() sharedContext?: Context<EntityManager> |
|
179
|
): Promise<any> { |
|
180
|
return await this.doSomething_(id, sharedContext) |
|
181
|
} |
|
182
|
} |
|
183
|
``` |
|
184
|
|
|
185
|
### Parameters |
|
186
|
|
|
187
|
The first object parameter is an object that has a `where` property used to apply filters. `where`'s value is an object whose keys are the property names of the data model, and values are the values to filter by. |
|
188
|
|
|
189
|
In the example above, you filter the retrieved posts by their `id` property. |
|
190
|
|
|
191
|
<Note> |
|
192
|
|
|
193
|
Refer to the [Filtering](../../tips/filtering/page.mdx) reference for more information on how to filter records. |
|
194
|
|
|
195
|
</Note> |
|
196
|
|
|
197
|
### Returns |
|
198
|
|
|
199
|
The method returns an array of the first `15` records matching the filters. |
|
200
|
|
|
201
|
--- |
|
202
|
|
|
203
|
## Retrieve Relations |
|
204
|
|
|
205
|
<Note> |
|
206
|
|
|
207
|
This applies to relations between data models of the same module. To retrieve linked records of different modules, use [Query](!docs!/learn/fundamentals/query). |
|
208
|
|
|
209
|
</Note> |
|
210
|
|
|
211
|
```ts title="src/modules/blog/service.ts" |
|
212
|
import { |
|
213
|
InjectTransactionManager, |
|
214
|
MedusaContext, |
|
215
|
MedusaService, |
|
216
|
InjectManager, |
|
217
|
} from "@medusajs/framework/utils" |
|
218
|
import { Context, InferTypeOf, DAL } from "@medusajs/framework/types" |
|
219
|
import { EntityManager } from "@medusajs/framework/mikro-orm/knex" |
|
220
|
import Post from "./models/post" |
|
221
|
|
|
222
|
class BlogModuleService extends MedusaService({ |
|
223
|
Post, |
|
224
|
}){ |
|
225
|
// ... |
|
226
|
@InjectTransactionManager() |
|
227
|
protected async doSomething_( |
|
228
|
id: string[], |
|
229
|
@MedusaContext() sharedContext?: Context<EntityManager> |
|
230
|
): Promise<any> { |
|
231
|
const posts = await this.postRepository_.find({ |
|
232
|
where: { |
|
233
|
id, |
|
234
|
}, |
|
235
|
options: { |
|
236
|
populate: ["author"], |
|
237
|
}, |
|
238
|
}) |
|
239
|
|
|
240
|
return posts |
|
241
|
} |
|
242
|
|
|
243
|
@InjectManager() |
|
244
|
async doSomething( |
|
245
|
id: string, |
|
246
|
@MedusaContext() sharedContext?: Context<EntityManager> |
|
247
|
): Promise<any> { |
|
248
|
return await this.doSomething_(id, sharedContext) |
|
249
|
} |
|
250
|
} |
|
251
|
``` |
|
252
|
|
|
253
|
### Parameters |
|
254
|
|
|
255
|
The first object parameter of `find` accepts an `option` property, whose value is an object of retrieval options. |
|
256
|
|
|
257
|
It accepts a `populate` property, whose value is an array of relation names to retrieve. |
|
258
|
|
|
259
|
In the example above, you retrieve the `author` relation of the posts. |
|
260
|
|
|
261
|
### Returns |
|
262
|
|
|
263
|
The method returns an array of the first `15` records. Each record will have the specified relations populated: |
|
264
|
|
|
265
|
```json title="Example" |
|
266
|
[ |
|
267
|
{ |
|
268
|
"id": "1", |
|
269
|
"author": { |
|
270
|
"id": "1", |
|
271
|
"name": "John Doe", |
|
272
|
// ... |
|
273
|
}, |
|
274
|
// ... |
|
275
|
} |
|
276
|
] |
|
277
|
``` |
|
278
|
|
|
279
|
|
|
280
|
--- |
|
281
|
|
|
282
|
## Select Properties |
|
283
|
|
|
284
|
```ts title="src/modules/blog/service.ts" |
|
285
|
import { |
|
286
|
InjectTransactionManager, |
|
287
|
MedusaContext, |
|
288
|
MedusaService, |
|
289
|
InjectManager, |
|
290
|
} from "@medusajs/framework/utils" |
|
291
|
import { Context, InferTypeOf, DAL } from "@medusajs/framework/types" |
|
292
|
import { EntityManager } from "@medusajs/framework/mikro-orm/knex" |
|
293
|
import Post from "./models/post" |
|
294
|
|
|
295
|
class BlogModuleService extends MedusaService({ |
|
296
|
Post, |
|
297
|
}){ |
|
298
|
// ... |
|
299
|
@InjectTransactionManager() |
|
300
|
protected async doSomething_( |
|
301
|
id: string, |
|
302
|
@MedusaContext() sharedContext?: Context<EntityManager> |
|
303
|
): Promise<any> { |
|
304
|
const posts = await this.postRepository_.find({ |
|
305
|
where: { |
|
306
|
id, |
|
307
|
}, |
|
308
|
options: { |
|
309
|
fields: ["title"], |
|
310
|
}, |
|
311
|
}) |
|
312
|
|
|
313
|
return posts |
|
314
|
} |
|
315
|
|
|
316
|
@InjectManager() |
|
317
|
async doSomething( |
|
318
|
id: string, |
|
319
|
@MedusaContext() sharedContext?: Context<EntityManager> |
|
320
|
): Promise<any> { |
|
321
|
return await this.doSomething_(id, sharedContext) |
|
322
|
} |
|
323
|
} |
|
324
|
``` |
|
325
|
|
|
326
|
### Parameters |
|
327
|
|
|
328
|
By default, retrieved records have all their properties. The first object parameter of `find` accepts in its `options` property a `fields` property, whose value is an array of property names to retrieve. |
|
329
|
|
|
330
|
In the example above, you retrieve only the `title` property of the posts. |
|
331
|
|
|
332
|
### Returns |
|
333
|
|
|
334
|
The method returns an array of the first `15` records. Each record will have only the specified properties: |
|
335
|
|
|
336
|
```json title="Example" |
|
337
|
[ |
|
338
|
{ |
|
339
|
"id": "1", |
|
340
|
"title": "My first post" |
|
341
|
} |
|
342
|
] |
|
343
|
``` |
|
344
|
|
|
345
|
--- |
|
346
|
|
|
347
|
## Paginate Relations |
|
348
|
|
|
349
|
```ts title="src/modules/blog/service.ts" |
|
350
|
import { |
|
351
|
InjectTransactionManager, |
|
352
|
MedusaContext, |
|
353
|
MedusaService, |
|
354
|
InjectManager, |
|
355
|
} from "@medusajs/framework/utils" |
|
356
|
import { Context, InferTypeOf, DAL } from "@medusajs/framework/types" |
|
357
|
import { EntityManager } from "@medusajs/framework/mikro-orm/knex" |
|
358
|
import Post from "./models/post" |
|
359
|
|
|
360
|
class BlogModuleService extends MedusaService({ |
|
361
|
Post, |
|
362
|
}){ |
|
363
|
// ... |
|
364
|
@InjectTransactionManager() |
|
365
|
protected async doSomething_( |
|
366
|
id: string, |
|
367
|
@MedusaContext() sharedContext?: Context<EntityManager> |
|
368
|
): Promise<any> { |
|
369
|
const posts = await this.postRepository_.find({ |
|
370
|
where: { |
|
371
|
id, |
|
372
|
}, |
|
373
|
options: { |
|
374
|
limit: 10, |
|
375
|
offset: 10, |
|
376
|
}, |
|
377
|
}) |
|
378
|
|
|
379
|
return posts |
|
380
|
} |
|
381
|
|
|
382
|
@InjectManager() |
|
383
|
async doSomething( |
|
384
|
id: string, |
|
385
|
@MedusaContext() sharedContext?: Context<EntityManager> |
|
386
|
): Promise<any> { |
|
387
|
return await this.doSomething_(id, sharedContext) |
|
388
|
} |
|
389
|
} |
|
390
|
``` |
|
391
|
|
|
392
|
### Parameters |
|
393
|
|
|
394
|
By default, the `find` method retrieves the first `15` records. |
|
395
|
|
|
396
|
To change pagination configurations, you can pass to the first object parameter the `options` property, whose value is an object that accepts the following properties for pagination: |
|
397
|
|
|
398
|
- `limit`: a number indicating how many records to retrieve. By default, it's `15`. |
|
399
|
- `offset`: a number indicating how many records to skip before the retrieved records. By default, it's `0`. |
|
400
|
|
|
401
|
In the example above, you retrieve `10` records, skipping the first `10` records. |
|
402
|
|
|
403
|
### Returns |
|
404
|
|
|
405
|
The method returns an array of records. The number of records is less than or equal to `limit`'s value. |
|
406
|
|
|
407
|
--- |
|
408
|
|
|
409
|
## Sort Records |
|
410
|
|
|
411
|
<Note> |
|
412
|
|
|
413
|
As of [Medusa v2.11.0](https://github.com/medusajs/medusa/releases/tag/v2.11.0), MikroORM dependencies are included in the `@medusajs/framework` package. If you're using an older version of Medusa, change the import statement to `@mikro-orm/knex`. |
|
414
|
|
|
415
|
</Note> |
|
416
|
|
|
417
|
```ts title="src/modules/blog/service.ts" |
|
418
|
import { |
|
419
|
InjectTransactionManager, |
|
420
|
MedusaContext, |
|
421
|
MedusaService, |
|
422
|
InjectManager, |
|
423
|
} from "@medusajs/framework/utils" |
|
424
|
import { Context, InferTypeOf, DAL } from "@medusajs/framework/types" |
|
425
|
import { EntityManager } from "@medusajs/framework/mikro-orm/knex" |
|
426
|
import Post from "./models/post" |
|
427
|
|
|
428
|
class BlogModuleService extends MedusaService({ |
|
429
|
Post, |
|
430
|
}){ |
|
431
|
// ... |
|
432
|
@InjectTransactionManager() |
|
433
|
protected async doSomething_( |
|
434
|
id: string, |
|
435
|
@MedusaContext() sharedContext?: Context<EntityManager> |
|
436
|
): Promise<any> { |
|
437
|
const posts = await this.postRepository_.find({ |
|
438
|
where: { |
|
439
|
id, |
|
440
|
}, |
|
441
|
options: { |
|
442
|
orderBy: { |
|
443
|
title: "ASC", |
|
444
|
}, |
|
445
|
}, |
|
446
|
}) |
|
447
|
|
|
448
|
return posts |
|
449
|
} |
|
450
|
|
|
451
|
@InjectManager() |
|
452
|
async doSomething( |
|
453
|
id: string, |
|
454
|
@MedusaContext() sharedContext?: Context<EntityManager> |
|
455
|
): Promise<any> { |
|
456
|
return await this.doSomething_(id, sharedContext) |
|
457
|
} |
|
458
|
} |
|
459
|
``` |
|
460
|
|
|
461
|
### Parameters |
|
462
|
|
|
463
|
To sort records by one or more properties, the first object parameter of `find` accepts in its `options` property an `orderBy` property. Its value is an object whose keys are the property names of the data model, and values are the sorting order. |
|
464
|
|
|
465
|
The sorting order can be one of the following: |
|
466
|
|
|
467
|
- `ASC` to sort by this property in the ascending order. |
|
468
|
- `DESC` to sort by this property in the descending order. |
|
469
|
|
|
470
|
### Returns |
|
471
|
|
|
472
|
The method returns an array of the first `15` records sorted by the specified properties. |