@@ -47,6 +47,10 @@ export type PaginatedItems = {
47
47
data : Item [ ]
48
48
}
49
49
50
+ function ensureArray ( arg : string | string [ ] = [ ] ) : string [ ] {
51
+ return Array . isArray ( arg ) ? arg : [ arg ]
52
+ }
53
+
50
54
function embed ( db : Low < Data > , name : string , item : Item , related : string ) : Item {
51
55
if ( inflection . singularize ( related ) === related ) {
52
56
const relatedData = db . data [ inflection . pluralize ( related ) ] as Item [ ]
@@ -148,13 +152,13 @@ export class Service {
148
152
findById (
149
153
name : string ,
150
154
id : string ,
151
- query : { _embed ?: string [ ] } ,
155
+ query : { _embed ?: string [ ] | string } ,
152
156
) : Item | undefined {
153
157
const value = this . #get( name )
154
158
155
159
if ( Array . isArray ( value ) ) {
156
160
let item = value . find ( ( item ) => item [ 'id' ] === id )
157
- query . _embed ? .forEach ( ( related ) => {
161
+ ensureArray ( query . _embed ) . forEach ( ( related ) => {
158
162
if ( item !== undefined ) item = embed ( this . #db, name , item , related )
159
163
} )
160
164
return item
@@ -184,9 +188,10 @@ export class Service {
184
188
}
185
189
186
190
// Include
187
- query . _embed ? .forEach ( ( related ) => {
188
- if ( items !== undefined && Array . isArray ( items ) )
191
+ ensureArray ( query . _embed ) . forEach ( ( related ) => {
192
+ if ( items !== undefined && Array . isArray ( items ) ) {
189
193
items = items . map ( ( item ) => embed ( this . #db, name , item , related ) )
194
+ }
190
195
} )
191
196
192
197
// Return list if no query params
@@ -209,11 +214,18 @@ export class Service {
209
214
continue
210
215
}
211
216
if (
212
- [ '_sort' , '_start' , '_end' , '_limit' , '_page' , '_per_page' ] . includes (
213
- key ,
214
- )
215
- )
217
+ [
218
+ '_embed' ,
219
+ '_sort' ,
220
+ '_start' ,
221
+ '_end' ,
222
+ '_limit' ,
223
+ '_page' ,
224
+ '_per_page' ,
225
+ ] . includes ( key )
226
+ ) {
216
227
continue
228
+ }
217
229
conds [ key ] = [ Condition . default , value ]
218
230
}
219
231
0 commit comments