Skip to content

Commit 1e71775

Browse files
bobbylemmFrantz Kati
authored and
Frantz Kati
committed
feat(revert-deep-populate-feature-2): take out deep populate feature from the rest package
1 parent 941190e commit 1e71775

File tree

2 files changed

+43
-178
lines changed

2 files changed

+43
-178
lines changed

packages/rest/src/index.ts

+42-165
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class Rest {
4646
total,
4747
page:
4848
findOptions.offset ||
49-
(findOptions.offset === 0 && findOptions.limit)
49+
(findOptions.offset === 0 && findOptions.limit)
5050
? Math.ceil((findOptions.offset + 1) / findOptions.limit!)
5151
: 0,
5252
per_page: findOptions.limit ? findOptions.limit : 0,
@@ -113,27 +113,15 @@ class Rest {
113113
const entity = manager.create(
114114
resource.data.pascalCaseName,
115115
body
116-
)
116+
) as any
117117

118118
await manager.persistAndFlush(entity)
119119

120120
await manager.populate(
121121
[entity],
122-
getFindOptionsPopulate(findOptions)
122+
findOptions.populate || []
123123
)
124124

125-
await Utils.graphql.populateFromResolvedNodes(
126-
resources,
127-
manager,
128-
config.databaseConfig.type!,
129-
resource,
130-
getGraphQlInfoObject(
131-
findOptions,
132-
resources,
133-
resource
134-
),
135-
[entity]
136-
)
137125

138126
return response.formatter.created(entity)
139127
}
@@ -168,31 +156,13 @@ class Rest {
168156
] = await manager.findAndCount(
169157
modelName,
170158
parseQueryToWhereOptions(query),
171-
{
172-
...findOptions,
173-
populate: getFindOptionsPopulate(
174-
findOptions
175-
)
176-
}
159+
findOptions
177160
)
178161

179-
await Utils.graphql.populateFromResolvedNodes(
180-
resources,
181-
manager,
182-
config.databaseConfig.type!,
183-
resource,
184-
getGraphQlInfoObject(
185-
findOptions,
186-
resources,
187-
resource
188-
),
189-
entities
190-
)
162+
191163

192164
return response.formatter.ok(
193-
JSON.parse(
194-
CircularJSON.stringify(entities)
195-
),
165+
entities,
196166
this.getPageMetaFromFindOptions(
197167
total,
198168
findOptions
@@ -230,25 +200,7 @@ class Rest {
230200
const entity = await manager.findOne(
231201
modelName as EntityName<AnyEntity<any>>,
232202
params.id as FilterQuery<AnyEntity<any>>,
233-
{
234-
...findOptions,
235-
populate: getFindOptionsPopulate(
236-
findOptions
237-
)
238-
}
239-
)
240-
241-
await Utils.graphql.populateFromResolvedNodes(
242-
resources,
243-
manager,
244-
config.databaseConfig.type!,
245-
resource,
246-
getGraphQlInfoObject(
247-
findOptions,
248-
resources,
249-
resource
250-
),
251-
[entity]
203+
findOptions
252204
)
253205

254206
if (!entity) {
@@ -257,7 +209,7 @@ class Rest {
257209
)
258210
}
259211
return response.formatter.ok(
260-
JSON.parse(CircularJSON.stringify(entity))
212+
entity
261213
)
262214
}
263215
)
@@ -322,9 +274,9 @@ class Rest {
322274
const relatedManyToOne = relatedResource.data.fields.find(
323275
f =>
324276
f.relatedProperty.type ===
325-
resource.data.pascalCaseName &&
277+
resource.data.pascalCaseName &&
326278
f.relatedProperty.reference ===
327-
ReferenceType.MANY_TO_ONE
279+
ReferenceType.MANY_TO_ONE
328280
)!
329281

330282
const [
@@ -345,23 +297,8 @@ class Rest {
345297
}
346298
)
347299

348-
await Utils.graphql.populateFromResolvedNodes(
349-
resources,
350-
manager,
351-
config.databaseConfig.type!,
352-
resource,
353-
getGraphQlInfoObject(
354-
findOptions,
355-
resources,
356-
resource
357-
),
358-
results
359-
)
360-
361300
return response.formatter.ok(
362-
JSON.parse(
363-
CircularJSON.stringify(results)
364-
),
301+
results,
365302
this.getPageMetaFromFindOptions(
366303
count,
367304
findOptions
@@ -376,9 +313,9 @@ class Rest {
376313
const relatedManyToMany = relatedResource.data.fields.find(
377314
f =>
378315
f.relatedProperty.type ===
379-
resource.data.pascalCaseName &&
316+
resource.data.pascalCaseName &&
380317
f.relatedProperty.reference ===
381-
ReferenceType.MANY_TO_MANY
318+
ReferenceType.MANY_TO_MANY
382319
)!
383320

384321
const [
@@ -392,31 +329,11 @@ class Rest {
392329
},
393330
...whereOptions
394331
},
395-
{
396-
...findOptions,
397-
populate: getFindOptionsPopulate(
398-
findOptions
399-
)
400-
}
401-
)
402-
403-
await Utils.graphql.populateFromResolvedNodes(
404-
resources,
405-
manager,
406-
config.databaseConfig.type!,
407-
resource,
408-
getGraphQlInfoObject(
409-
findOptions,
410-
resources,
411-
resource
412-
),
413-
results
332+
findOptions
414333
)
415334

416335
return response.formatter.ok(
417-
JSON.parse(
418-
CircularJSON.stringify(results)
419-
),
336+
results,
420337
this.getPageMetaFromFindOptions(
421338
count,
422339
findOptions
@@ -426,9 +343,9 @@ class Rest {
426343

427344
if (
428345
relatedField.relatedProperty.reference ===
429-
ReferenceType.MANY_TO_ONE ||
346+
ReferenceType.MANY_TO_ONE ||
430347
relatedField.relatedProperty.reference ===
431-
ReferenceType.ONE_TO_ONE
348+
ReferenceType.ONE_TO_ONE
432349
) {
433350
const payload = ((await manager.findOneOrFail(
434351
resource.data.pascalCaseName,
@@ -449,32 +366,10 @@ class Rest {
449366
{
450367
id: payload.id
451368
},
452-
{
453-
...findOptions,
454-
populate: getFindOptionsPopulate(
455-
findOptions
456-
)
457-
}
458-
)
459-
460-
await Utils.graphql.populateFromResolvedNodes(
461-
resources,
462-
manager,
463-
config.databaseConfig.type!,
464-
resource,
465-
getGraphQlInfoObject(
466-
findOptions,
467-
resources,
468-
resource
469-
),
470-
[result]
369+
findOptions
471370
)
472371

473-
return response.formatter.ok(
474-
JSON.parse(
475-
CircularJSON.stringify(result)
476-
)
477-
)
372+
return response.formatter.ok(result)
478373
}
479374
}
480375
)
@@ -529,12 +424,7 @@ class Rest {
529424
const entity = await manager.findOne(
530425
resource.data.pascalCaseName,
531426
params.id,
532-
{
533-
...findOptions,
534-
populate: getFindOptionsPopulate(
535-
findOptions
536-
)
537-
}
427+
findOptions
538428
)
539429

540430
if (!entity) {
@@ -547,19 +437,6 @@ class Rest {
547437

548438
await manager.persistAndFlush(entity)
549439

550-
await Utils.graphql.populateFromResolvedNodes(
551-
resources,
552-
manager,
553-
config.databaseConfig.type!,
554-
resource,
555-
getGraphQlInfoObject(
556-
findOptions,
557-
resources,
558-
resource
559-
),
560-
[entity]
561-
)
562-
563440
return response.formatter.ok(entity)
564441
}
565442
)
@@ -640,32 +517,32 @@ class Rest {
640517
? route.config.path
641518
: `/${route.config.path}`
642519

643-
;(app as any)[route.config.type.toLowerCase()](
644-
path,
645-
646-
...route.config.middleware.map(fn => AsyncHandler(fn)),
647-
AsyncHandler(
648-
async (
649-
request: Request,
650-
response: Response,
651-
next: NextFunction
652-
) => {
653-
await this.authorizeResolver(
654-
request as any,
655-
route
656-
)
520+
; (app as any)[route.config.type.toLowerCase()](
521+
path,
522+
523+
...route.config.middleware.map(fn => AsyncHandler(fn)),
524+
AsyncHandler(
525+
async (
526+
request: Request,
527+
response: Response,
528+
next: NextFunction
529+
) => {
530+
await this.authorizeResolver(
531+
request as any,
532+
route
533+
)
657534

658-
return next()
659-
}
660-
),
661-
AsyncHandler(
662-
async (request: Request, response: Response) =>
663-
route.config.handler(request, response)
535+
return next()
536+
}
537+
),
538+
AsyncHandler(
539+
async (request: Request, response: Response) =>
540+
route.config.handler(request, response)
541+
)
664542
)
665-
)
666543
})
667544
})
668545
}
669546
}
670547

671-
export const rest = () => new Rest()
548+
export const rest = () => new Rest()

packages/rest/src/populate-helpers.ts

+1-13
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,6 @@ export const parseQueryToFindOptions = (
3232
query.page >= 1 ? (query.page - 1) * findOptions.limit : 0
3333
}
3434

35-
if (query.deep_populate) {
36-
const strigifiedQuery = qs.stringify(query.deep_populate, {
37-
encode: false
38-
})
39-
const parsedQuery = qs.parse(strigifiedQuery, {
40-
arrayLimit: 100,
41-
depth: 20
42-
})
43-
44-
findOptions.populate = parsedQuery as any
45-
}
46-
4735
if (query.populate) {
4836
findOptions.populate = query.populate.split(',')
4937
}
@@ -216,4 +204,4 @@ export const getModelFields = (
216204
return acc
217205
}, {})
218206
return result
219-
}
207+
}

0 commit comments

Comments
 (0)