Skip to content

Commit 0206a80

Browse files
committed
fix relations
1 parent 6338130 commit 0206a80

File tree

2 files changed

+39
-34
lines changed

2 files changed

+39
-34
lines changed

src/domain/make-relations.js

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
*/
66

77
import domainEvents from './domain-events'
8-
import { importModelCache, importRemoteCache } from './import-remotes'
8+
import { importModelCache } from './import-remotes'
99

1010
const { 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

1515
export 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) {
180191
export 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)

src/domain/use-cases/load-models.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@ import { isMainThread } from 'worker_threads'
44
import { Writable, Transform } from 'node:stream'
55
import Serializer from '../serializer'
66

7-
function nextModelFn(port, mf) {
7+
function nextModelFn (port, mf) {
88
mf
99
.getModelSpecs()
1010
.filter(spec => spec.ports)
1111
.map(spec =>
1212
Object.entries(spec.ports)
1313
.filter(p => port.consumesEvent === port)
14-
.reduce(p => spec.modelName)
14+
.reduce(p => spec.modelName, [])
1515
)[0]
1616
}
1717

18-
function startWorkflow(model, mf) {
18+
function startWorkflow (model, mf) {
1919
const history = model.getPortFlow()
2020
const ports = model.getPorts()
2121

@@ -54,27 +54,27 @@ export default function ({ modelName, repository, broker, models }) {
5454
const rehydrate = new Transform({
5555
objectMode: true,
5656

57-
transform(chunk, _endcoding, next) {
57+
transform (chunk, _endcoding, next) {
5858
const model = models.loadModel(broker, repository, chunk, modelName)
5959
repository.saveSync(model.getId(), model)
6060
this.push(model)
6161
next()
62-
},
62+
}
6363
})
6464

6565
const resumeWorkflow = new Writable({
6666
objectMode: true,
6767

68-
write(chunk, _encoding, next) {
69-
startWorkflow(chunk)
68+
write (chunk, _encoding, next) {
69+
startWorkflow(chunk, models)
7070
next()
7171
return true
7272
},
7373

74-
end(chunk, _encoding, done) {
75-
startWorkflow(chunk)
74+
end (chunk, _encoding, done) {
75+
startWorkflow(chunk, models)
7676
done()
77-
},
77+
}
7878
})
7979

8080
repository.list({ transform: rehydrate, writable: resumeWorkflow })

0 commit comments

Comments
 (0)