@live-change/relations-plugin 0.8.22 → 0.8.24

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/dataUtils.js CHANGED
@@ -15,7 +15,10 @@ function extractObjectData(writeableProperties, properties, defaults) {
15
15
  objectData[propertyName] = properties[propertyName]
16
16
  }
17
17
  }
18
- return App.utils.mergeDeep({}, defaults, JSON.parse(JSON.stringify(objectData)))
18
+ return App.utils.mergeDeep({},
19
+ defaults,
20
+ JSON.parse(JSON.stringify(objectData))
21
+ )
19
22
  }
20
23
 
21
24
  export {
package/entity.js CHANGED
@@ -108,7 +108,7 @@ function defineDeletedEvent(config, context) {
108
108
 
109
109
  function defineCreateAction(config, context) {
110
110
  const {
111
- service, app, model, defaults, modelPropertyName, modelRuntime, objectType,
111
+ service, app, model, modelPropertyName, modelRuntime, objectType,
112
112
  modelName, writeableProperties
113
113
  } = context
114
114
  const eventName = modelName + 'Created'
@@ -127,7 +127,9 @@ function defineCreateAction(config, context) {
127
127
  const id = properties[modelPropertyName] || app.generateUid()
128
128
  const entity = await modelRuntime().get(id)
129
129
  if(entity) throw 'exists'
130
- const data = extractObjectData(writeableProperties, properties, defaults)
130
+ const data = extractObjectData(writeableProperties, properties,
131
+ App.computeDefaults(model, properties, { client, service } ))
132
+
131
133
  await App.validation.validate({ ...data }, validators,
132
134
  { source: action, action, service, app, client })
133
135
 
@@ -233,7 +235,6 @@ export default function(service, app) {
233
235
  const originalModelProperties = { ...model.properties }
234
236
  const modelProperties = Object.keys(model.properties)
235
237
  const modelPropertyName = modelName.slice(0, 1).toLowerCase() + modelName.slice(1)
236
- const defaults = App.utils.generateDefault(originalModelProperties)
237
238
 
238
239
  function modelRuntime() {
239
240
  return service._runtime.models[modelName]
@@ -248,7 +249,7 @@ export default function(service, app) {
248
249
  const objectType = service.name + '_' + modelName
249
250
 
250
251
  const context = {
251
- service, app, model, originalModelProperties, modelProperties, modelPropertyName, defaults, modelRuntime,
252
+ service, app, model, originalModelProperties, modelProperties, modelPropertyName, modelRuntime,
252
253
  modelName, writeableProperties, annotation, objectType
253
254
  }
254
255
 
package/index.js CHANGED
@@ -11,6 +11,7 @@ import relatedToAny from './relatedToAny.js'
11
11
 
12
12
  import boundTo from './boundTo.js'
13
13
  import boundToAny from './boundToAny.js'
14
+ import saveAuthor from './saveAuthor.js'
14
15
 
15
16
 
16
17
  const processors = [
@@ -18,7 +19,8 @@ const processors = [
18
19
  propertyOf, itemOf,
19
20
  propertyOfAny, itemOfAny,
20
21
  relatedTo, relatedToAny,
21
- boundTo, boundToAny
22
+ boundTo, boundToAny,
23
+ saveAuthor
22
24
  ]
23
25
 
24
26
  const plugin = function(app, services) {
package/itemOf.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  defineProperties, defineIndexes,
3
3
  processModelsAnnotation, addAccessControlParents,
4
- defineDeleteByOwnerEvents, defineParentDeleteTriggers, defineParentCopyTriggers
4
+ defineDeleteByOwnerEvents, defineParentDeleteTriggers, defineParentCopyTriggers, defineAuthorProperties
5
5
  } from './utils.js'
6
6
 
7
7
  import {
@@ -21,6 +21,7 @@ export default function(service, app) {
21
21
  context.reverseRelationWord = 'Owned'
22
22
 
23
23
  context.identifiers = defineProperties(context.model, context.others, context.otherPropertyNames)
24
+
24
25
  addAccessControlParents(context)
25
26
  defineIndexes(context.model, context.otherPropertyNames.map(p => p[0].toLowerCase() + p.slice(1)), context.others)
26
27
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@live-change/relations-plugin",
3
- "version": "0.8.22",
3
+ "version": "0.8.24",
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.22",
25
+ "@live-change/framework": "^0.8.24",
26
26
  "pluralize": "^8.0.0"
27
27
  },
28
- "gitHead": "70a56ac3dc54167a2d32ce47a00150e7a94bd04d"
28
+ "gitHead": "63e942caccbcb1c9bfbd1a3ef1d097124514c5a7"
29
29
  }
@@ -52,7 +52,7 @@ function defineView(config, context) {
52
52
 
53
53
  function defineCreateAction(config, context) {
54
54
  const {
55
- service, app, model, defaults, modelPropertyName, modelRuntime, objectType,
55
+ service, app, model, modelPropertyName, modelRuntime, objectType,
56
56
  otherPropertyNames, joinedOthersPropertyName, modelName, writeableProperties, joinedOthersClassName
57
57
  } = context
58
58
  const eventName = joinedOthersPropertyName + context.reverseRelationWord + modelName + 'Created'
@@ -74,7 +74,8 @@ function defineCreateAction(config, context) {
74
74
  const entity = await modelRuntime().get(id)
75
75
  if(entity) throw 'exists'
76
76
  const identifiers = extractIdentifiersWithTypes(otherPropertyNames, properties)
77
- const data = extractObjectData(writeableProperties, properties, defaults)
77
+ const data = extractObjectData(writeableProperties, properties,
78
+ App.computeDefaults(model, properties, { client, service } ))
78
79
  await App.validation.validate({ ...identifiers, ...data }, validators,
79
80
  { source: action, action, service, app, client })
80
81
  await fireChangeTriggers(context, objectType, identifiers, id, null, data)
@@ -120,7 +121,7 @@ function defineUpdateAction(config, context) {
120
121
  if(!entity) throw 'not_found'
121
122
  const entityTypeAndIdParts = extractTypeAndIdParts(otherPropertyNames, entity)
122
123
  const typeAndIdParts = extractTypeAndIdParts(otherPropertyNames, properties)
123
- if(JSON.stringify(entityTypeAndIdParts) != JSON.stringify(typeAndIdParts)) {
124
+ if(JSON.stringify(entityTypeAndIdParts) !== JSON.stringify(typeAndIdParts)) {
124
125
  throw 'not_authorized'
125
126
  }
126
127
  const identifiers = extractIdentifiersWithTypes(otherPropertyNames, properties)
@@ -171,7 +172,7 @@ function defineDeleteAction(config, context) {
171
172
  const entityTypeAndIdParts = extractTypeAndIdParts(otherPropertyNames, entity)
172
173
  const typeAndIdParts = extractTypeAndIdParts(otherPropertyNames, properties)
173
174
  const identifiers = extractIdentifiers(otherPropertyNames, entity)
174
- if(JSON.stringify(entityTypeAndIdParts) != JSON.stringify(typeAndIdParts)) {
175
+ if(JSON.stringify(entityTypeAndIdParts) !== JSON.stringify(typeAndIdParts)) {
175
176
  throw new Error('not_authorized')
176
177
  }
177
178
  await fireChangeTriggers(context, objectType, identifiers, id,
@@ -45,7 +45,7 @@ function defineView(config, context) {
45
45
 
46
46
  function defineCreateAction(config, context) {
47
47
  const {
48
- service, app, model, defaults, modelPropertyName, modelRuntime, objectType,
48
+ service, app, model, modelPropertyName, modelRuntime, objectType,
49
49
  otherPropertyNames, joinedOthersPropertyName, modelName, writeableProperties, joinedOthersClassName, others
50
50
  } = context
51
51
  const eventName = joinedOthersPropertyName + context.reverseRelationWord + modelName + 'Created'
@@ -67,7 +67,8 @@ function defineCreateAction(config, context) {
67
67
  const entity = await modelRuntime().get(id)
68
68
  if(entity) throw 'exists'
69
69
  const identifiers = extractIdentifiers(otherPropertyNames, properties)
70
- const data = extractObjectData(writeableProperties, properties, defaults)
70
+ const data = extractObjectData(writeableProperties, properties,
71
+ App.computeDefaults(model, properties, { client, service } ))
71
72
  await App.validation.validate({ ...identifiers, ...data }, validators,
72
73
  { source: action, action, service, app, client })
73
74
  await fireChangeTriggers(context, objectType, identifiers, id, null, data)
package/saveAuthor.js ADDED
@@ -0,0 +1,13 @@
1
+ import { defineAuthorProperties } from './utils.js'
2
+
3
+ export default function(service, app) {
4
+ for(let modelName in service.models) {
5
+ const model = service.models[modelName]
6
+ if(model.saveAuthor) {
7
+ model.properties = {
8
+ ...model.properties,
9
+ ...defineAuthorProperties()
10
+ }
11
+ }
12
+ }
13
+ }
@@ -95,7 +95,7 @@ function defineRangeViews(config, context) {
95
95
 
96
96
  function defineSetAction(config, context) {
97
97
  const {
98
- service, app, model, defaults, objectType,
98
+ service, app, model, objectType,
99
99
  otherPropertyNames, joinedOthersPropertyName, modelName, writeableProperties, joinedOthersClassName
100
100
  } = context
101
101
 
@@ -115,7 +115,8 @@ function defineSetAction(config, context) {
115
115
  waitForEvents: true,
116
116
  async execute(properties, { client, service }, emit) {
117
117
  const identifiers = extractIdentifiersWithTypes(otherPropertyNames, properties)
118
- const data = extractObjectData(writeableProperties, properties, defaults)
118
+ const data = extractObjectData(writeableProperties, properties,
119
+ App.computeDefaults(model, properties, { client, service } ))
119
120
  await App.validation.validate({ ...identifiers, ...data }, validators,
120
121
  { source: action, action, service, app, client })
121
122
  await fireChangeTriggers(context, objectType, identifiers, id, null, data)
@@ -170,7 +171,7 @@ function defineUpdateAction(config, context) {
170
171
 
171
172
  function defineSetOrUpdateAction(config, context) {
172
173
  const {
173
- service, app, model, modelRuntime, defaults, objectType,
174
+ service, app, model, modelRuntime, objectType,
174
175
  otherPropertyNames, joinedOthersPropertyName, modelName, writeableProperties, joinedOthersClassName
175
176
  } = context
176
177
  const eventName = joinedOthersPropertyName + context.reverseRelationWord + modelName + 'Updated'
@@ -191,7 +192,10 @@ function defineSetOrUpdateAction(config, context) {
191
192
  const identifiers = extractIdentifiersWithTypes(otherPropertyNames, properties)
192
193
  const id = generateAnyId(otherPropertyNames, properties)
193
194
  const entity = await modelRuntime().get(id)
194
- const data = extractObjectData(writeableProperties, properties, { ...defaults, ...entity } )
195
+ const data = extractObjectData(writeableProperties, properties, {
196
+ ...App.computeDefaults(model, properties, { client, service } ),
197
+ ...entity
198
+ })
195
199
  await App.validation.validate({ ...identifiers, ...data }, validators,
196
200
  { source: action, action, service, app, client })
197
201
  await fireChangeTriggers(context, objectType, identifiers, id,
@@ -39,7 +39,7 @@ function defineView(config, context) {
39
39
 
40
40
  function defineSetAction(config, context) {
41
41
  const {
42
- service, app, model, defaults, objectType,
42
+ service, app, model, objectType,
43
43
  otherPropertyNames, joinedOthersPropertyName, modelName, writeableProperties, joinedOthersClassName, others
44
44
  } = context
45
45
  const eventName = joinedOthersPropertyName + 'Owned' + modelName + 'Set'
@@ -60,7 +60,8 @@ function defineSetAction(config, context) {
60
60
  const idParts = extractIdParts(otherPropertyNames, properties)
61
61
  const id = idParts.length > 1 ? idParts.map(p => JSON.stringify(p)).join(':') : idParts[0]
62
62
  const identifiers = extractIdentifiers(otherPropertyNames, properties)
63
- const data = extractObjectData(writeableProperties, properties, defaults)
63
+ const data = extractObjectData(writeableProperties, properties,
64
+ App.computeDefaults(model, properties, { client, service } ))
64
65
  await App.validation.validate({ ...identifiers, ...data }, validators,
65
66
  { source: action, action, service, app, client })
66
67
  await fireChangeTriggers(context, objectType, identifiers, id, null, data)
@@ -116,7 +117,7 @@ function defineUpdateAction(config, context) {
116
117
 
117
118
  function defineSetOrUpdateAction(config, context) {
118
119
  const {
119
- service, app, model, defaults, modelRuntime, objectType,
120
+ service, app, model, modelRuntime, objectType,
120
121
  otherPropertyNames, joinedOthersPropertyName, modelName, writeableProperties, joinedOthersClassName, others
121
122
  } = context
122
123
  const eventName = joinedOthersPropertyName + 'Owned' + modelName + 'Updated'
@@ -137,7 +138,10 @@ function defineSetOrUpdateAction(config, context) {
137
138
  const identifiers = extractIdentifiers(otherPropertyNames, properties)
138
139
  const id = generateId(otherPropertyNames, properties)
139
140
  const entity = await modelRuntime().get(id)
140
- const data = extractObjectData(writeableProperties, properties, { ...defaults, ...entity })
141
+ const data = extractObjectData(writeableProperties, properties, {
142
+ ...App.computeDefaults(model, properties, { client, service } ),
143
+ ...entity
144
+ })
141
145
  await App.validation.validate({ ...identifiers, ...data }, validators,
142
146
  { source: action, action, service, app, client })
143
147
  await fireChangeTriggers(context, objectType, identifiers, id,
package/utils.js CHANGED
@@ -8,11 +8,11 @@ import {
8
8
  PropertyDefinition, ViewDefinition, IndexDefinition, ActionDefinition, EventDefinition, TriggerDefinition
9
9
  } from "@live-change/framework"
10
10
 
11
- import {
11
+ export {
12
12
  extractObjectData, extractIdentifiers
13
13
  } from './dataUtils.js'
14
14
 
15
- function extractIdParts(otherPropertyNames, properties) {
15
+ export function extractIdParts(otherPropertyNames, properties) {
16
16
  const idParts = []
17
17
  for (const propertyName of otherPropertyNames) {
18
18
  idParts.push(properties[propertyName])
@@ -21,13 +21,13 @@ function extractIdParts(otherPropertyNames, properties) {
21
21
  }
22
22
 
23
23
 
24
- function generateId(otherPropertyNames, properties) {
24
+ export function generateId(otherPropertyNames, properties) {
25
25
  return otherPropertyNames.length > 1
26
26
  ? otherPropertyNames.map(p => JSON.stringify(properties[p])).join(':')
27
27
  : properties[otherPropertyNames[0]]
28
28
  }
29
29
 
30
- function defineProperties(model, types, names) {
30
+ export function defineProperties(model, types, names) {
31
31
  const identifiers = {}
32
32
  for (let i = 0; i < types.length; i++) {
33
33
  identifiers[names[i]] = new PropertyDefinition({
@@ -41,13 +41,28 @@ function defineProperties(model, types, names) {
41
41
  return identifiers
42
42
  }
43
43
 
44
- function defineIndex(model, what, props) {
44
+ export function defineAuthorProperties() {
45
+ return {
46
+ authorType: new PropertyDefinition({
47
+ type: String,
48
+ validation: ['nonEmpty'],
49
+ default: (_props, context) => context.client.user ? 'user_User' : 'session_Session'
50
+ }),
51
+ author: new PropertyDefinition({
52
+ type: String,
53
+ validation: ['nonEmpty'],
54
+ default: (_props, context) => context.client.user ? context.client.user : context.client.session
55
+ })
56
+ }
57
+ }
58
+
59
+ export function defineIndex(model, what, props) {
45
60
  console.log("DEFINE INDEX", model.name, what, props)
46
61
  model.indexes['by' + what] = new IndexDefinition({
47
62
  property: props
48
63
  })
49
64
  }
50
- function defineIndexes(model, props, types) {
65
+ export function defineIndexes(model, props, types) {
51
66
  console.log("DEFINE INDEXES", model.name, props, types)
52
67
  const propCombinations = allCombinations(Object.keys(props))
53
68
  for(const propCombination of propCombinations) {
@@ -59,7 +74,7 @@ function defineIndexes(model, props, types) {
59
74
  }
60
75
  }
61
76
 
62
- function processModelsAnnotation(service, app, annotation, multiple, cb) {
77
+ export function processModelsAnnotation(service, app, annotation, multiple, cb) {
63
78
  if (!service) throw new Error("no service")
64
79
  if (!app) throw new Error("no app")
65
80
 
@@ -75,7 +90,6 @@ function processModelsAnnotation(service, app, annotation, multiple, cb) {
75
90
  const originalModelProperties = { ...model.properties }
76
91
  const modelProperties = Object.keys(model.properties)
77
92
  const modelPropertyName = modelName.slice(0, 1).toLowerCase() + modelName.slice(1)
78
- const defaults = App.utils.generateDefault(originalModelProperties)
79
93
 
80
94
  function modelRuntime() {
81
95
  return service._runtime.models[modelName]
@@ -112,7 +126,7 @@ function processModelsAnnotation(service, app, annotation, multiple, cb) {
112
126
  const objectType = service.name + '_' + modelName
113
127
 
114
128
  const context = {
115
- service, app, model, originalModelProperties, modelProperties, modelPropertyName, defaults, modelRuntime,
129
+ service, app, model, originalModelProperties, modelProperties, modelPropertyName, modelRuntime,
116
130
  otherPropertyNames, joinedOthersPropertyName, modelName, writeableProperties, joinedOthersClassName,
117
131
  others, annotation, objectType, parentsTypes: others
118
132
  }
@@ -123,7 +137,7 @@ function processModelsAnnotation(service, app, annotation, multiple, cb) {
123
137
  }
124
138
  }
125
139
 
126
- function addAccessControlParents(context) {
140
+ export function addAccessControlParents(context) {
127
141
  const { modelRuntime } = context
128
142
  context.model.accessControlParents = async (id) => {
129
143
  const data = await modelRuntime().get(id)
@@ -141,7 +155,7 @@ function addAccessControlParents(context) {
141
155
  )
142
156
  }
143
157
 
144
- function prepareAccessControl(accessControl, names, types) {
158
+ export function prepareAccessControl(accessControl, names, types) {
145
159
  if(typeof accessControl == 'object') {
146
160
  accessControl.objects = accessControl.objects ?? ((params) => names.map((name, index) => ({
147
161
  objectType: types[index],
@@ -150,7 +164,7 @@ function prepareAccessControl(accessControl, names, types) {
150
164
  }
151
165
  }
152
166
 
153
- function defineDeleteByOwnerEvents(config, context, generateId) {
167
+ export function defineDeleteByOwnerEvents(config, context, generateId) {
154
168
  const {
155
169
  service, modelRuntime, joinedOthersPropertyName, modelName, modelPropertyName, otherPropertyNames, reverseRelationWord
156
170
  } = context
@@ -179,23 +193,16 @@ function defineDeleteByOwnerEvents(config, context, generateId) {
179
193
  }])
180
194
  const deletePromises = bucket.map(({to}) => runtime.delete(to))
181
195
  await Promise.all(deletePromises)
182
- } while (bucket.length == bucketSize)
196
+ } while (bucket.length === bucketSize)
183
197
  }
184
198
  })
185
199
  }
186
200
  }
187
201
 
188
- function defineParentDeleteTriggers(config, context) {
202
+ export function defineParentDeleteTriggers(config, context) {
189
203
  registerParentDeleteTriggers(context, config)
190
204
  }
191
205
 
192
- function defineParentCopyTriggers(config, context) {
206
+ export function defineParentCopyTriggers(config, context) {
193
207
  registerParentCopyTriggers(context, config)
194
208
  }
195
-
196
-
197
- export {
198
- extractIdParts, extractIdentifiers, extractObjectData, defineProperties, defineIndex, defineIndexes,
199
- processModelsAnnotation, generateId, addAccessControlParents, prepareAccessControl,
200
- defineDeleteByOwnerEvents, defineParentDeleteTriggers, defineParentCopyTriggers,
201
- }
package/utilsAny.js CHANGED
@@ -87,7 +87,6 @@ function processModelsAnyAnnotation(service, app, annotation, multiple, cb) {
87
87
  const originalModelProperties = { ...model.properties }
88
88
  const modelProperties = Object.keys(model.properties)
89
89
  const modelPropertyName = modelName.slice(0, 1).toLowerCase() + modelName.slice(1)
90
- const defaults = App.utils.generateDefault(originalModelProperties)
91
90
 
92
91
  function modelRuntime() {
93
92
  return service._runtime.models[modelName]
@@ -122,7 +121,7 @@ function processModelsAnyAnnotation(service, app, annotation, multiple, cb) {
122
121
  const { parentsTypes } = config
123
122
 
124
123
  const context = {
125
- service, app, model, originalModelProperties, modelProperties, modelPropertyName, defaults, modelRuntime,
124
+ service, app, model, originalModelProperties, modelProperties, modelPropertyName, modelRuntime,
126
125
  otherPropertyNames, joinedOthersPropertyName, modelName, writeableProperties, joinedOthersClassName, others,
127
126
  objectType, parentsTypes
128
127
  }