@live-change/relations-plugin 0.8.24 → 0.8.25

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/boundTo.js CHANGED
@@ -5,7 +5,11 @@ import {
5
5
 
6
6
  import { defineSetEvent, defineUpdatedEvent, defineTransferredEvent, defineResetEvent } from './propertyEvents.js'
7
7
 
8
- import { defineView, defineSetAction, defineUpdateAction, defineResetAction } from './singularRelationUtils.js'
8
+ import {
9
+ defineView,
10
+ defineSetAction, defineUpdateAction, defineSetOrUpdateAction, defineResetAction,
11
+ defineSetTrigger, defineUpdateTrigger, defineSetOrUpdateTrigger, defineResetTrigger
12
+ } from './singularRelationUtils.js'
9
13
 
10
14
  export default function(service, app) {
11
15
  processModelsAnnotation(service, app, 'boundTo', false, (config, context) => {
@@ -16,12 +20,12 @@ export default function(service, app) {
16
20
  defineProperties(context.model, context.others, context.otherPropertyNames)
17
21
  defineIndex(context.model, context.joinedOthersClassName, context.otherPropertyNames)
18
22
 
19
- if(config.readAccess || config.readAccessControl || config.writeAccessControl) {
20
- defineView({ ...config, access: config.readAccess }, context)
21
- }
23
+ defineView({ ...config, access: config.readAccess }, context,
24
+ config.readAccess || config.readAccessControl || config.writeAccessControl
25
+ )
22
26
  if(config.views) {
23
27
  for(const view of config.views) {
24
- defineView({ ...config, ...view }, context)
28
+ defineView({ ...config, ...view }, context, !view.internal)
25
29
  }
26
30
  }
27
31
 
@@ -30,6 +34,11 @@ export default function(service, app) {
30
34
  defineTransferredEvent(config, context, generateId)
31
35
  defineResetEvent(config, context, generateId)
32
36
 
37
+ defineSetTrigger(config, context)
38
+ defineUpdateTrigger(config, context)
39
+ defineSetOrUpdateTrigger(config, context)
40
+ defineResetTrigger(config, context)
41
+
33
42
  if(config.setAccess || config.writeAccess || config.setAccessControl || config.writeAccessControl) {
34
43
  defineSetAction(config, context)
35
44
  }
@@ -38,6 +47,11 @@ export default function(service, app) {
38
47
  defineUpdateAction(config, context)
39
48
  }
40
49
 
50
+ if((config.setAccess && config.updateAccess) || config.writeAccess
51
+ || config.setOrUpdateAccessControl || config.writeAccessControl) {
52
+ defineSetOrUpdateAction(config, context)
53
+ }
54
+
41
55
  if(config.resetAccess || config.writeAccess || config.resetAccessControl || config.writeAccessControl) {
42
56
  defineResetAction(config, context);
43
57
  }
package/boundToAny.js CHANGED
@@ -5,7 +5,11 @@ import {
5
5
 
6
6
  import { defineSetEvent, defineUpdatedEvent, defineTransferredEvent, defineResetEvent } from './propertyEvents.js'
7
7
 
8
- import { defineView, defineSetAction, defineUpdateAction, defineResetAction } from './singularRelationUtils.js'
8
+ import {
9
+ defineView,
10
+ defineSetAction, defineUpdateAction, defineResetAction, defineSetOrUpdateAction,
11
+ defineSetTrigger, defineUpdateTrigger, defineSetOrUpdateTrigger, defineResetTrigger
12
+ } from './singularRelationUtils.js'
9
13
 
10
14
  export default function(service, app) {
11
15
  processModelsAnyAnnotation(service, app, 'boundToAny', false, (config, context) => {
@@ -16,12 +20,11 @@ export default function(service, app) {
16
20
  defineAnyProperties(context.model, context.otherPropertyNames)
17
21
  defineAnyIndex(context.model, context.joinedOthersClassName, context.otherPropertyNames)
18
22
 
19
- if(config.readAccess || config.readAccessControl || config.writeAccessControl) {
20
- defineView({ ...config, access: config.readAccess }, context)
21
- }
23
+ defineView({ ...config, access: config.readAccess }, context,
24
+ config.readAccess || config.readAccessControl || config.writeAccessControl)
22
25
  if(config.views) {
23
26
  for(const view of config.views) {
24
- defineView({ ...config, ...view }, context)
27
+ defineView({ ...config, ...view }, context, !view.internal)
25
28
  }
26
29
  }
27
30
 
@@ -30,6 +33,11 @@ export default function(service, app) {
30
33
  defineTransferredEvent(config, context, generateAnyId)
31
34
  defineResetEvent(config, context, generateAnyId)
32
35
 
36
+ defineSetTrigger(config, context)
37
+ defineUpdateTrigger(config, context)
38
+ defineSetOrUpdateTrigger(config, context)
39
+ defineResetTrigger(config, context)
40
+
33
41
  if(config.setAccess || config.writeAccess || config.setAccessControl || config.writeAccessControl) {
34
42
  defineSetAction(config, context)
35
43
  }
@@ -38,6 +46,11 @@ export default function(service, app) {
38
46
  defineUpdateAction(config, context)
39
47
  }
40
48
 
49
+ if((config.setAccess && config.updateAccess) || config.writeAccess
50
+ || config.setOrUpdateAccessControl || config.writeAccessControl) {
51
+ defineSetOrUpdateAction(config, context)
52
+ }
53
+
41
54
  if(config.resetAccess || config.writeAccess || config.resetAccessControl || config.writeAccessControl) {
42
55
  defineResetAction(config, context);
43
56
  }
package/entity.js CHANGED
@@ -5,7 +5,7 @@ import {
5
5
  import { fireChangeTriggers } from "./changeTriggers.js"
6
6
  import App from '@live-change/framework'
7
7
  import {
8
- PropertyDefinition, ViewDefinition, IndexDefinition, ActionDefinition, EventDefinition
8
+ PropertyDefinition, ViewDefinition, IndexDefinition, ActionDefinition, EventDefinition, TriggerDefinition
9
9
  } from "@live-change/framework"
10
10
 
11
11
  const annotation = 'entity'
@@ -18,7 +18,7 @@ function entityAccessControl({service, modelName, modelPropertyName}, accessCont
18
18
  }
19
19
  }
20
20
 
21
- function defineView(config, context) {
21
+ function defineView(config, context, external) {
22
22
  const { service, modelRuntime, modelPropertyName, modelName, model } = context
23
23
  const viewName = (config.prefix || '' ) + (config.prefix ? modelName : modelPropertyName) + (config.suffix || '')
24
24
  service.views[viewName] = new ViewDefinition({
@@ -32,8 +32,9 @@ function defineView(config, context) {
32
32
  returns: {
33
33
  type: model,
34
34
  },
35
- access: config.readAccess,
36
- accessControl: entityAccessControl(context, config.readAccessControl || config.writeAccessControl),
35
+ internal: !external,
36
+ access: external && config.readAccess,
37
+ accessControl: external && entityAccessControl(context, config.readAccessControl || config.writeAccessControl),
37
38
  daoPath(properties, { client, context }) {
38
39
  const id = properties[modelPropertyName]
39
40
  const path = config.fields ? modelRuntime().limitedPath(id, config.fields) : modelRuntime().path(id)
@@ -105,56 +106,111 @@ function defineDeletedEvent(config, context) {
105
106
  })
106
107
  }
107
108
 
109
+ function getCreateFunction( validators, validationContext, config, context) {
110
+ const {
111
+ service, app, model, modelPropertyName, modelRuntime, objectType,
112
+ modelName, writeableProperties
113
+ } = context
114
+ const eventName = modelName + 'Created'
115
+ async function execute(properties, { client, service }, emit) {
116
+ const id = properties[modelPropertyName] || app.generateUid()
117
+ const entity = await modelRuntime().get(id)
118
+ if(entity) throw 'exists'
119
+ const data = extractObjectData(writeableProperties, properties,
120
+ App.computeDefaults(model, properties, { client, service } ))
121
+
122
+ await App.validation.validate({ ...data }, validators,
123
+ validationContext)
124
+
125
+ await fireChangeTriggers(context, objectType, null, id,
126
+ entity ? extractObjectData(writeableProperties, entity, {}) : null, data)
127
+ emit({
128
+ type: eventName,
129
+ [modelPropertyName]: id,
130
+ data
131
+ })
132
+ return id
133
+ }
134
+ return execute
135
+ }
108
136
 
109
137
  function defineCreateAction(config, context) {
110
138
  const {
111
139
  service, app, model, modelPropertyName, modelRuntime, objectType,
112
140
  modelName, writeableProperties
113
141
  } = context
114
- const eventName = modelName + 'Created'
115
142
  const actionName = 'create' + modelName
116
- service.actions[actionName] = new ActionDefinition({
143
+ const action = new ActionDefinition({
117
144
  name: actionName,
118
- properties: {
119
- ...(model.properties)
120
- },
145
+ properties: { ...model.properties },
121
146
  access: config.createAccess || config.writeAccess,
122
147
  accessControl: entityAccessControl(context, config.createAccessControl || config.writeAccessControl),
123
148
  skipValidation: true,
124
149
  //queuedBy: otherPropertyNames,
125
150
  waitForEvents: true,
126
- async execute(properties, { client, service }, emit) {
127
- const id = properties[modelPropertyName] || app.generateUid()
128
- const entity = await modelRuntime().get(id)
129
- if(entity) throw 'exists'
130
- const data = extractObjectData(writeableProperties, properties,
131
- App.computeDefaults(model, properties, { client, service } ))
132
-
133
- await App.validation.validate({ ...data }, validators,
134
- { source: action, action, service, app, client })
135
-
136
- await fireChangeTriggers(context, objectType, null, id,
137
- entity ? extractObjectData(writeableProperties, entity, {}) : null, data)
138
- emit({
139
- type: eventName,
140
- [modelPropertyName]: id,
141
- data
142
- })
143
- return id
144
- }
151
+ execute: () => { throw new Error("not generated yet") }
145
152
  })
146
- const action = service.actions[actionName]
147
153
  const validators = App.validation.getValidators(action, service, action)
154
+ const validationContext = { source: action, action }
155
+ action.execute = getCreateFunction( validators, validationContext, config, context)
156
+ service.actions[actionName] = action
148
157
  }
149
158
 
150
- function defineUpdateAction(config, context) {
159
+ function defineCreateTrigger(config, context) {
160
+ const {
161
+ service, app, model, modelPropertyName, modelRuntime, objectType,
162
+ modelName, writeableProperties
163
+ } = context
164
+ const actionName = 'create' + modelName
165
+ const triggerName = `${service.name}_${actionName}`
166
+ const trigger = new TriggerDefinition({
167
+ name: triggerName,
168
+ properties: { ...model.properties },
169
+ skipValidation: true,
170
+ waitForEvents: true,
171
+ execute: () => { throw new Error("not generated yet") }
172
+ })
173
+ const validators = App.validation.getValidators(trigger, service, trigger)
174
+ const validationContext = { source: trigger, trigger }
175
+ trigger.execute = getCreateFunction( validators, validationContext, config, context)
176
+ service.triggers[triggerName] = [trigger]
177
+ }
178
+
179
+ function getUpdateFunction( validators, validationContext, config, context) {
151
180
  const {
152
181
  service, app, model, modelRuntime, modelPropertyName, objectType,
153
182
  modelName, writeableProperties
154
183
  } = context
155
184
  const eventName = modelName + 'Updated'
185
+ async function execute(properties, { client, service }, emit) {
186
+ const id = properties[modelPropertyName]
187
+ const entity = await modelRuntime().get(id)
188
+ if(!entity) throw 'not_found'
189
+ const data = App.utils.mergeDeep({},
190
+ extractObjectData(writeableProperties, properties, entity),
191
+ App.computeUpdates(model, { ...entity, ...properties }, { client, service })
192
+ )
193
+ await App.validation.validate({ ...data }, validators,
194
+ validationContext)
195
+ await fireChangeTriggers(context, null, id,
196
+ entity ? extractObjectData(writeableProperties, entity, {}) : null, data)
197
+ emit({
198
+ type: eventName,
199
+ [modelPropertyName]: id,
200
+ data
201
+ })
202
+ }
203
+ return execute
204
+ }
205
+
206
+ function defineUpdateAction(config, context) {
207
+ const {
208
+ service, app, model, modelRuntime, modelPropertyName, objectType,
209
+ modelName, writeableProperties
210
+ } = context
156
211
  const actionName = 'update' + modelName
157
- service.actions[actionName] = new ActionDefinition({
212
+ const triggerName = `${service.name}_${actionName}`
213
+ const action = new ActionDefinition({
158
214
  name: actionName,
159
215
  properties: {
160
216
  ...(model.properties),
@@ -167,33 +223,66 @@ function defineUpdateAction(config, context) {
167
223
  skipValidation: true,
168
224
  //queuedBy: otherPropertyNames,
169
225
  waitForEvents: true,
170
- async execute(properties, { client, service }, emit) {
171
- const id = properties[modelPropertyName]
172
- const entity = await modelRuntime().get(id)
173
- if(!entity) throw 'not_found'
174
- const data = extractObjectData(writeableProperties, properties, entity)
175
- await App.validation.validate({ ...data }, validators,
176
- { source: action, action, service, app, client })
177
- await fireChangeTriggers(context, null, id,
178
- entity ? extractObjectData(writeableProperties, entity, {}) : null, data)
179
- emit({
180
- type: eventName,
181
- [modelPropertyName]: id,
182
- data
183
- })
184
- }
226
+ execute: () => { throw new Error("not generated yet") }
185
227
  })
186
- const action = service.actions[actionName]
187
228
  const validators = App.validation.getValidators(action, service, action)
229
+ const validationContext = { source: action, action }
230
+ action.execute = getUpdateFunction( validators, validationContext, config, context)
231
+ service.actions[actionName] = action
188
232
  }
189
233
 
190
- function defineDeleteAction(config, context) {
234
+ function defineUpdateTrigger(config, context) {
235
+ const {
236
+ service, app, model, modelRuntime, modelPropertyName, objectType,
237
+ modelName, writeableProperties
238
+ } = context
239
+ const actionName = 'update' + modelName
240
+ const triggerName = `${service.name}_${actionName}`
241
+ const trigger = new TriggerDefinition({
242
+ name: triggerName,
243
+ properties: {
244
+ ...(model.properties),
245
+ [modelPropertyName]: {
246
+ type: modelPropertyName
247
+ }
248
+ },
249
+ skipValidation: true,
250
+ waitForEvents: true,
251
+ execute: () => { throw new Error("not generated yet") }
252
+ })
253
+ const validators = App.validation.getValidators(trigger, service, trigger)
254
+ const validationContext = { source: trigger, trigger }
255
+ trigger.execute = getUpdateFunction( validators, validationContext, config, context)
256
+ service.triggers[triggerName] = [trigger]
257
+ }
258
+
259
+ function getDeleteFunction(config, context) {
191
260
  const {
192
261
  service, app, model, modelRuntime, modelPropertyName, objectType,
193
262
  otherPropertyNames, modelName, writeableProperties,
194
263
  } = context
195
264
  const eventName = modelName + 'Deleted'
265
+ async function execute(properties, { client, service }, emit) {
266
+ const id = properties[modelPropertyName]
267
+ const entity = await modelRuntime().get(id)
268
+ if(!entity) throw new Error('not_found')
269
+ await fireChangeTriggers(context, objectType, null, id,
270
+ entity ? extractObjectData(writeableProperties, entity, {}) : null, null)
271
+ emit({
272
+ type: eventName,
273
+ [modelPropertyName]: id
274
+ })
275
+ }
276
+ return execute
277
+ }
278
+
279
+ function defineDeleteAction(config, context) {
280
+ const {
281
+ service, app, model, modelRuntime, modelPropertyName, objectType,
282
+ modelName, writeableProperties
283
+ } = context
196
284
  const actionName = 'delete' + modelName
285
+ const triggerName = `${service.name}_${actionName}`
197
286
  service.actions[actionName] = new ActionDefinition({
198
287
  name: actionName,
199
288
  properties: {
@@ -206,20 +295,31 @@ function defineDeleteAction(config, context) {
206
295
  accessControl: entityAccessControl(context, config.deleteAccessControl || config.writeAccessControl),
207
296
  skipValidation: true,
208
297
  waitForEvents: true,
209
- async execute(properties, { client, service }, emit) {
210
- const id = properties[modelPropertyName]
211
- const entity = await modelRuntime().get(id)
212
- if(!entity) throw new Error('not_found')
213
- await fireChangeTriggers(context, objectType, null, id,
214
- entity ? extractObjectData(writeableProperties, entity, {}) : null, null)
215
- emit({
216
- type: eventName,
217
- [modelPropertyName]: id
218
- })
219
- }
298
+ execute: getDeleteFunction(config, context)
220
299
  })
221
300
  }
222
301
 
302
+ function defineDeleteTrigger(config, context) {
303
+ const {
304
+ service, app, model, modelRuntime, modelPropertyName, objectType,
305
+ modelName, writeableProperties
306
+ } = context
307
+ const actionName = 'delete' + modelName
308
+ const triggerName = `${service.name}_${actionName}`
309
+ service.triggers[triggerName] = [new TriggerDefinition({
310
+ name: actionName,
311
+ properties: {
312
+ ...(model.properties),
313
+ [modelPropertyName]: {
314
+ type: modelPropertyName
315
+ }
316
+ },
317
+ skipValidation: true,
318
+ waitForEvents: true,
319
+ execute: getDeleteFunction(config, context)
320
+ })]
321
+ }
322
+
223
323
  export default function(service, app) {
224
324
  if (!service) throw new Error("no service")
225
325
  if (!app) throw new Error("no app")
@@ -253,15 +353,17 @@ export default function(service, app) {
253
353
  modelName, writeableProperties, annotation, objectType
254
354
  }
255
355
 
256
- if (config.readAccess || config.readAccessControl || config.writeAccessControl) {
257
- defineView(config, context)
258
- }
356
+ defineView(config, context, config.readAccess || config.readAccessControl || config.writeAccessControl)
259
357
  /// TODO: multiple views with limited fields
260
358
 
261
359
  defineCreatedEvent(config, context)
262
360
  defineUpdatedEvent(config, context)
263
361
  defineDeletedEvent(config, context)
264
362
 
363
+ defineCreateTrigger(config, context)
364
+ defineUpdateTrigger(config, context)
365
+ defineDeleteTrigger(config, context)
366
+
265
367
  if (config.createAccess || config.writeAccess || config.createAccessControl || config.writeAccessControl) {
266
368
  defineCreateAction(config, context)
267
369
  }
package/itemOf.js CHANGED
@@ -10,7 +10,10 @@ import {
10
10
  } from './itemEvents.js'
11
11
 
12
12
  import {
13
- defineView, defineCreateAction, defineUpdateAction, defineDeleteAction, defineSortIndex,
13
+ defineView,
14
+ defineCreateAction, defineUpdateAction, defineDeleteAction,
15
+ defineCreateTrigger, defineUpdateTrigger, defineDeleteTrigger, defineCopyTrigger,
16
+ defineSortIndex,
14
17
  defineCopyAction, defineCopyOnParentCopyTrigger
15
18
  } from './pluralRelationUtils.js'
16
19
 
@@ -31,9 +34,8 @@ export default function(service, app) {
31
34
  }
32
35
  }
33
36
 
34
- if(config.readAccess || config.readAccessControl || config.writeAccessControl) {
35
- defineView(config, context)
36
- }
37
+ defineView(config, context,
38
+ config.readAccess || config.readAccessControl || config.writeAccessControl)
37
39
  /// TODO: multiple views with limited fields
38
40
 
39
41
  defineCreatedEvent(config, context)
@@ -43,6 +45,11 @@ export default function(service, app) {
43
45
  defineDeleteByOwnerEvents(config, context)
44
46
  defineCopyEvent(config, context)
45
47
 
48
+ defineCreateTrigger(config, context)
49
+ defineUpdateTrigger(config, context)
50
+ defineDeleteTrigger(config, context)
51
+ defineCopyTrigger(config, context)
52
+
46
53
  if(config.createAccess || config.writeAccess || config.createAccessControl || config.writeAccessControl) {
47
54
  defineCreateAction(config, context)
48
55
  }
package/itemOfAny.js CHANGED
@@ -9,7 +9,10 @@ import {
9
9
  } from './itemEvents.js'
10
10
 
11
11
  import {
12
- defineView, defineCreateAction, defineUpdateAction, defineDeleteAction, defineSortIndex
12
+ defineView,
13
+ defineCreateAction, defineUpdateAction, defineDeleteAction,
14
+ defineCreateTrigger, defineUpdateTrigger, defineDeleteTrigger,
15
+ defineSortIndex
13
16
  } from './pluralRelationAnyUtils.js'
14
17
 
15
18
  export default function(service, app) {
@@ -28,10 +31,9 @@ export default function(service, app) {
28
31
  }
29
32
  }
30
33
 
31
- if(config.readAccess || config.writeAccess || config.readAccessControl || config.writeAccessControl) {
32
- defineView(config, context)
33
- // TODO: multiple views with all properties combinations
34
- }
34
+ defineView(config, context,
35
+ config.readAccess || config.writeAccess || config.readAccessControl || config.writeAccessControl)
36
+ /// TODO: multiple views with all properties combinations
35
37
  /// TODO: multiple views with limited fields
36
38
 
37
39
  defineCreatedEvent(config, context)
@@ -40,6 +42,10 @@ export default function(service, app) {
40
42
  defineDeletedEvent(config, context)
41
43
  defineDeleteByOwnerEvents(config, context, generateAnyId)
42
44
 
45
+ defineCreateTrigger(config, context)
46
+ defineUpdateTrigger(config, context)
47
+ defineDeleteTrigger(config, context)
48
+
43
49
  if(config.createAccess || config.writeAccess || config.createAccessControl || config.writeAccessControl) {
44
50
  defineCreateAction(config, context)
45
51
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@live-change/relations-plugin",
3
- "version": "0.8.24",
3
+ "version": "0.8.25",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -22,8 +22,8 @@
22
22
  },
23
23
  "type": "module",
24
24
  "dependencies": {
25
- "@live-change/framework": "^0.8.24",
25
+ "@live-change/framework": "^0.8.25",
26
26
  "pluralize": "^8.0.0"
27
27
  },
28
- "gitHead": "63e942caccbcb1c9bfbd1a3ef1d097124514c5a7"
28
+ "gitHead": "1aa24b8ec3bfaa3576b72b514b2a97b061cb57fc"
29
29
  }