@@ -28,7 +28,7 @@ import { tableize, underscore, foreignKey } from 'inflected';
28
28
29
29
import { BaseModel , buildModels } from './build-models' ;
30
30
import { migrateModels } from './migrate-models' ;
31
- import { groupIdentitiesByType } from './utils' ;
31
+ import { groupRecordsByType } from './utils' ;
32
32
33
33
export interface ProcessorSettings {
34
34
schema : Schema ;
@@ -140,7 +140,9 @@ export class Processor {
140
140
}
141
141
142
142
protected async updateRecord ( op : UpdateRecordOperation , trx : Transaction ) {
143
- const qb = this . queryForType ( trx , op . record . type ) ;
143
+ const qb = this . queryForType ( trx , op . record . type ) . mergeContext ( {
144
+ recordId : op . record . id
145
+ } ) ;
144
146
const data = this . parseOrbitRecord ( op . record ) ;
145
147
146
148
const model = await qb . upsertGraph ( data , {
@@ -153,7 +155,9 @@ export class Processor {
153
155
154
156
protected async removeRecord ( op : RemoveRecordOperation , trx : Transaction ) {
155
157
const { type, id } = op . record ;
156
- const qb = this . queryForType ( trx , type ) ;
158
+ const qb = this . queryForType ( trx , type ) . mergeContext ( {
159
+ recordId : id
160
+ } ) ;
157
161
158
162
const model = ( await qb . findById ( id ) ) as BaseModel ;
159
163
await qb . deleteById ( id ) ;
@@ -166,7 +170,9 @@ export class Processor {
166
170
trx : Transaction
167
171
) {
168
172
const { type, id } = op . record ;
169
- const qb = this . queryForType ( trx , type ) ;
173
+ const qb = this . queryForType ( trx , type ) . mergeContext ( {
174
+ recordId : id
175
+ } ) ;
170
176
171
177
const model = await qb . patchAndFetchById ( id , {
172
178
[ op . attribute ] : op . value
@@ -180,7 +186,9 @@ export class Processor {
180
186
trx : Transaction
181
187
) {
182
188
const { type, id } = op . record ;
183
- const qb = this . queryForType ( trx , type ) ;
189
+ const qb = this . queryForType ( trx , type ) . mergeContext ( {
190
+ recordId : id
191
+ } ) ;
184
192
const relatedId = op . relatedRecord ? op . relatedRecord . id : null ;
185
193
186
194
const model = ( await qb . findById ( id ) ) as BaseModel ;
@@ -198,7 +206,9 @@ export class Processor {
198
206
trx : Transaction
199
207
) {
200
208
const { type, id } = op . record ;
201
- const qb = this . queryForType ( trx , type ) ;
209
+ const qb = this . queryForType ( trx , type ) . mergeContext ( {
210
+ recordId : id
211
+ } ) ;
202
212
const relatedIds = op . relatedRecords . map ( ( { id } ) => id ) ;
203
213
204
214
const model = await qb . upsertGraph (
@@ -221,7 +231,9 @@ export class Processor {
221
231
trx : Transaction
222
232
) {
223
233
const { type, id } = op . record ;
224
- const qb = this . queryForType ( trx , type ) ;
234
+ const qb = this . queryForType ( trx , type ) . mergeContext ( {
235
+ recordId : id
236
+ } ) ;
225
237
const relatedId = op . relatedRecord . id ;
226
238
227
239
const model = ( await qb . findById ( id ) ) as BaseModel ;
@@ -235,7 +247,9 @@ export class Processor {
235
247
trx : Transaction
236
248
) {
237
249
const { type, id } = op . record ;
238
- const qb = this . queryForType ( trx , type ) ;
250
+ const qb = this . queryForType ( trx , type ) . mergeContext ( {
251
+ recordId : id
252
+ } ) ;
239
253
240
254
const model = ( await qb . findById ( id ) ) as BaseModel ;
241
255
const relatedId = op . relatedRecord . id ;
@@ -249,7 +263,9 @@ export class Processor {
249
263
250
264
protected async findRecord ( expression : FindRecord , trx : Transaction ) {
251
265
const { id, type } = expression . record ;
252
- const qb = this . queryForType ( trx , type ) ;
266
+ const qb = this . queryForType ( trx , type ) . mergeContext ( {
267
+ recordId : id
268
+ } ) ;
253
269
254
270
const model = ( await qb . findById ( id ) ) as BaseModel ;
255
271
@@ -266,12 +282,12 @@ export class Processor {
266
282
) ) as BaseModel [ ] ;
267
283
return models . map ( model => model . toOrbitRecord ( ) ) ;
268
284
} else if ( records ) {
269
- const idsByType = groupIdentitiesByType ( records ) ;
285
+ const recordsByType = groupRecordsByType ( records ) ;
270
286
const recordsById : Record < string , OrbitRecord > = { } ;
271
287
272
- for ( let type in idsByType ) {
288
+ for ( let type in recordsByType ) {
273
289
for ( let record of await this . queryForType ( trx , type , false ) . findByIds (
274
- idsByType [ type ]
290
+ recordsByType [ type ]
275
291
) ) {
276
292
recordsById [ record . id ] = record . toOrbitRecord ( ) ;
277
293
}
@@ -292,7 +308,9 @@ export class Processor {
292
308
record : { id, type } ,
293
309
relationship
294
310
} = expression ;
295
- const qb = this . queryForType ( trx , type ) ;
311
+ const qb = this . queryForType ( trx , type ) . mergeContext ( {
312
+ recordId : id
313
+ } ) ;
296
314
const { model : relatedType } = this . schema . getRelationship (
297
315
type ,
298
316
relationship
@@ -320,7 +338,9 @@ export class Processor {
320
338
relationship
321
339
) ;
322
340
323
- let qb = this . queryForType ( trx , type ) ;
341
+ let qb = this . queryForType ( trx , type ) . mergeContext ( {
342
+ recordId : id
343
+ } ) ;
324
344
const parent = ( await qb . findById ( id ) ) as BaseModel ;
325
345
qb = parent
326
346
. $relatedQuery < BaseModel > ( relationship , trx )
@@ -342,7 +362,7 @@ export class Processor {
342
362
343
363
const qb = this . modelForType ( type )
344
364
. query ( trx )
345
- . context ( { orbitType : type } )
365
+ . context ( { recordType : type } )
346
366
. select ( fields ) ;
347
367
348
368
if ( throwIfNotFound ) {
0 commit comments