55 */
66
77import domainEvents from './domain-events'
8- import { importModelCache , importRemoteCache } from './import-remotes'
8+ import { importModelCache } from './import-remotes'
99
1010const { internalCacheRequest, internalCacheResponse, externalCacheRequest } =
1111 domainEvents
1212
13- const maxwait = process . env . REMOTE_OBJECT_MAXWAIT || 2000
13+ const maxwait = process . env . REMOTE_OBJECT_MAXWAIT || 1000
1414
1515export const relationType = {
1616 /**
@@ -151,21 +151,32 @@ const updateForeignKeys = {
151151 * @param {import('./datasource').default } ds
152152 * @returns
153153 */
154- async function createNewModels ( args , fromModel , relation , ds ) {
154+ async function createModels ( args , fromModel , relation , ds ) {
155155 if ( args . length > 0 ) {
156- const {
157- UseCaseService,
158- importRemoteCache,
159- default : ModelFactory
160- } = require ( '.' )
161- const service = UseCaseService ( relation . modelName . toUpperCase ( ) )
156+ const { UseCaseService } = require ( '.' )
157+ const service = UseCaseService ( relation . modelName )
162158 const newModels = await Promise . all (
163159 args . map ( arg => service . createModel ( arg ) )
164160 )
165161 return updateForeignKeys [ relation . type ] ( fromModel , newModels , relation , ds )
166162 }
167163}
168164
165+ async function createNewModels ( args , rel , datasource ) {
166+ if ( args . length > 0 && rel . type !== 'custom' ) {
167+ // fetch the local ds and create the models
168+ const ds = datasource . factory . getDataSource ( rel . modelName )
169+ return {
170+ yes : true ,
171+ create : async ( ) => await createModels ( args , this , rel , ds )
172+ }
173+ }
174+ return {
175+ yes : false ,
176+ create : ( ) => null
177+ }
178+ }
179+
169180/**
170181 * Find existing, or create new, remote objects from
171182 * the distributed cache and store them in the local cache.
@@ -180,14 +191,13 @@ async function createNewModels (args, fromModel, relation, ds) {
180191export function requireRemoteObject ( model , relation , broker , ...args ) {
181192 const request = internalCacheRequest ( relation . modelName )
182193 const response = internalCacheResponse ( relation . modelName )
183-
184- console . debug ( { fn : requireRemoteObject . name , relation } )
185-
186194 const name = ( model ? model . getName ( ) : relation . modelName ) . toUpperCase ( )
187195 const id = model ? model . getId ( ) : relation . id
188196 const eventSource = name
189197 const eventTarget = model ? relation . modelName . toUpperCase ( ) : null
190198
199+ console . debug ( { fn : requireRemoteObject . name , relation } )
200+
191201 const requestData = {
192202 eventName : request ,
193203 modelName : name ,
@@ -251,20 +261,15 @@ export default function makeRelations (relations, datasource, broker) {
251261 async [ relation ] ( ...args ) {
252262 const local = isRelatedModelLocal ( relModelName )
253263 if ( ! local ) await importModelCache ( relModelName )
254- // Get existing (or create temp) datasource of related object
255- const createNew = args ?. length > 0
256264
257- if ( createNew && rel . type !== 'custom' ) {
258- // fetch the local ds and create the models
259- const ds = datasource . factory . getDataSource ( rel . modelName )
260- return await createNewModels ( args , this , rel , ds )
265+ if ( local ) {
266+ const result = createNewModels ( args , rel , datasource )
267+ if ( result . yes ) return result . create ( )
261268 }
262269
263- // if the object is remote, we now have its code
264- const ds = datasource . factory . getDataSource (
265- rel . modelName ,
266- require ( '.' ) . default . getModelSpec ( relModelName ) . domain
267- )
270+ // If object is remote, we should have its code by now.
271+ // Recreate its datasource, including any customization
272+ const ds = datasource . factory . getDataSource ( relModelName )
268273
269274 const models = await relationType [ rel . type ] ( this , ds , rel , args )
270275
@@ -277,7 +282,7 @@ export default function makeRelations (relations, datasource, broker) {
277282 ...args
278283 )
279284
280- if ( createNew )
285+ if ( args . length > 0 )
281286 updateForeignKeys [ rel . type ] ( this , event . model , rel , ds )
282287
283288 return await relationType [ rel . type ] ( this , ds , rel , args )
0 commit comments