Skip to content

Commit 63a32b1

Browse files
committed
Move things around
1 parent 789dbd7 commit 63a32b1

File tree

1 file changed

+19
-18
lines changed

1 file changed

+19
-18
lines changed

src/processor.ts

+19-18
Original file line numberDiff line numberDiff line change
@@ -308,19 +308,13 @@ export class Processor {
308308
record: { id, type },
309309
relationship
310310
} = expression;
311-
const qb = this.queryForType(trx, type).mergeContext({
311+
312+
let qb = this.queryForType(trx, type).mergeContext({
312313
recordId: id
313314
});
314-
const { model: relatedType } = this.schema.getRelationship(
315-
type,
316-
relationship
317-
);
318-
319315
const parent = (await qb.findById(id)) as BaseModel;
320-
const query = await parent
321-
.$relatedQuery<BaseModel>(relationship, trx)
322-
.select(this.fieldsForType(relatedType as string));
323-
const model = ((await query) as any) as (BaseModel | undefined);
316+
qb = this.queryForRelationship(trx, parent, relationship);
317+
const model = ((await qb) as any) as (BaseModel | undefined);
324318

325319
return model ? model.toOrbitRecord() : null;
326320
}
@@ -333,20 +327,13 @@ export class Processor {
333327
record: { id, type },
334328
relationship
335329
} = expression;
336-
const { model: relatedType } = this.schema.getRelationship(
337-
type,
338-
relationship
339-
);
340330

341331
let qb = this.queryForType(trx, type).mergeContext({
342332
recordId: id
343333
});
344334
const parent = (await qb.findById(id)) as BaseModel;
345-
qb = parent
346-
.$relatedQuery<BaseModel>(relationship, trx)
347-
.select(this.fieldsForType(relatedType as string));
348335
const models = (await this.parseQueryExpression(
349-
qb,
336+
this.queryForRelationship(trx, parent, relationship),
350337
expression
351338
)) as BaseModel[];
352339

@@ -372,6 +359,20 @@ export class Processor {
372359
return qb;
373360
}
374361

362+
queryForRelationship(
363+
trx: Transaction,
364+
model: BaseModel,
365+
relationship: string
366+
) {
367+
const { model: relatedType } = this.schema.getRelationship(
368+
model.orbitType,
369+
relationship
370+
);
371+
const fields = this.fieldsForType(relatedType as string);
372+
373+
return model.$relatedQuery<BaseModel>(relationship, trx).select(fields);
374+
}
375+
375376
protected parseQueryExpressionPage(
376377
qb: QueryBuilder<BaseModel>,
377378
expression: FindRecords | FindRelatedRecords

0 commit comments

Comments
 (0)