@live-change/relations-plugin 0.5.20 → 0.5.23

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 ADDED
@@ -0,0 +1,45 @@
1
+ const {
2
+ defineProperties, defineIndex,
3
+ processModelsAnnotation, generateId
4
+ } = require('./utils.js')
5
+
6
+ const { defineSetEvent, defineUpdatedEvent, defineTransferredEvent, defineResetEvent } = require('./propertyEvents.js')
7
+
8
+ const { defineView, defineSetAction, defineUpdateAction, defineResetAction } = require('./singularRelationUtils.js')
9
+
10
+ module.exports = function(service, app) {
11
+ processModelsAnnotation(service, app, 'boundTo', false, (config, context) => {
12
+
13
+ context.relationWord = 'Friend'
14
+ context.reverseRelationWord = 'Bound'
15
+
16
+ defineProperties(context.model, context.others, context.otherPropertyNames)
17
+ defineIndex(context.model, context.joinedOthersClassName, context.otherPropertyNames)
18
+
19
+ if(config.readAccess) {
20
+ defineView({ ...config, access: config.readAccess }, context)
21
+ }
22
+ if(config.views) {
23
+ for(const view of config.views) {
24
+ defineView({ ...config, ...view }, context)
25
+ }
26
+ }
27
+
28
+ defineSetEvent(config, context, generateId)
29
+ defineUpdatedEvent(config, context, generateId)
30
+ defineTransferredEvent(config, context, generateId)
31
+ defineResetEvent(config, context, generateId)
32
+
33
+ if(config.setAccess || config.writeAccess) {
34
+ defineSetAction(config, context)
35
+ }
36
+
37
+ if(config.updateAccess || config.writeAccess) {
38
+ defineUpdateAction(config, context)
39
+ }
40
+
41
+ if(config.resetAccess || config.writeAccess) {
42
+ defineResetAction(config, context);
43
+ }
44
+ })
45
+ }
package/boundToAny.js ADDED
@@ -0,0 +1,46 @@
1
+ const {
2
+ defineAnyProperties, defineAnyIndex,
3
+ processModelsAnyAnnotation, generateAnyId
4
+ } = require('./utilsAny.js')
5
+
6
+ const { defineSetEvent, defineUpdatedEvent, defineTransferredEvent, defineResetEvent } = require('./propertyEvents.js')
7
+
8
+ const { defineView, defineSetAction, defineUpdateAction, defineResetAction } = require('./singularRelationUtils.js')
9
+
10
+ module.exports = function(service, app) {
11
+ processModelsAnyAnnotation(service, app, 'boundToAny', false, (config, context) => {
12
+
13
+ context.relationWord = 'Friend'
14
+ context.reverseRelationWord = 'Bound'
15
+
16
+ defineAnyProperties(context.model, context.otherPropertyNames)
17
+ defineAnyIndex(context.model, context.joinedOthersClassName, context.otherPropertyNames)
18
+
19
+ if(config.readAccess) {
20
+ defineView({ ...config, access: config.readAccess }, context)
21
+ }
22
+ if(config.views) {
23
+ for(const view of config.views) {
24
+ defineView({ ...config, ...view }, context)
25
+ }
26
+ }
27
+
28
+ defineSetEvent(config, context, generateAnyId)
29
+ defineUpdatedEvent(config, context, generateAnyId)
30
+ defineTransferredEvent(config, context, generateAnyId)
31
+ defineResetEvent(config, context, generateAnyId)
32
+
33
+ if(config.setAccess || config.writeAccess) {
34
+ defineSetAction(config, context)
35
+ }
36
+
37
+ if(config.updateAccess || config.writeAccess) {
38
+ defineUpdateAction(config, context)
39
+ }
40
+
41
+ if(config.resetAccess || config.writeAccess) {
42
+ defineResetAction(config, context);
43
+ }
44
+
45
+ })
46
+ }
package/index.js CHANGED
@@ -4,14 +4,23 @@ const itemOf = require('./itemOf.js')
4
4
  const propertyOfAny = require('./propertyOfAny.js')
5
5
  const itemOfAny = require('./itemOfAny.js')
6
6
 
7
- module.exports = function(app, services) {
7
+ const relatedTo = require('./relatedTo.js')
8
+ const relatedToAny = require('./relatedToAny.js')
8
9
 
9
- app.defaultProcessors.push(propertyOf)
10
- app.defaultProcessors.push(itemOf)
10
+ const boundTo = require('./boundTo.js')
11
+ const boundToAny = require('./boundToAny.js')
11
12
 
12
- app.defaultProcessors.push(propertyOfAny)
13
- app.defaultProcessors.push(itemOfAny)
13
+ const processors = [
14
+ propertyOf, itemOf,
15
+ propertyOfAny, itemOfAny,
16
+ relatedTo, relatedToAny,
17
+ boundTo, boundToAny
18
+ ]
14
19
 
20
+ module.exports = function(app, services) {
21
+ app.defaultProcessors.push(...processors)
15
22
  }
16
23
 
17
- module.exports.processors = [ propertyOf, itemOf ]
24
+ module.exports.processors = [
25
+ ...processors
26
+ ]
package/itemOf.js CHANGED
@@ -1,169 +1,21 @@
1
- const App = require("@live-change/framework")
2
- const { PropertyDefinition, ViewDefinition, IndexDefinition, ActionDefinition, EventDefinition } = App
3
-
4
1
  const {
5
- extractIdParts, extractRange, extractIdentifiers, extractObjectData, defineProperties, defineIndex,
2
+ defineProperties, defineIndex,
6
3
  processModelsAnnotation
7
4
  } = require('./utils.js')
8
5
 
9
- function defineView(config, context) {
10
- const { service, modelRuntime, otherPropertyNames, joinedOthersPropertyName, joinedOthersClassName,
11
- modelName, others, model } = context
12
- const indexName = 'by'+context.joinedOthersClassName
13
- const viewProperties = {}
14
- for (let i = 0; i < others.length; i++) {
15
- viewProperties[otherPropertyNames[i]] = new PropertyDefinition({
16
- type: others[i],
17
- validation: ['nonEmpty']
18
- })
19
- }
20
- const viewName = joinedOthersPropertyName + 'Owned' + modelName + 's'
21
- service.views[viewName] = new ViewDefinition({
22
- name: viewName,
23
- properties: {
24
- ...viewProperties,
25
- ...App.utils.rangeProperties
26
- },
27
- returns: {
28
- type: Array,
29
- of: {
30
- type: model
31
- }
32
- },
33
- access: config.readAccess,
34
- daoPath(properties, { client, context }) {
35
- const idParts = extractIdParts(otherPropertyNames, properties)
36
- const range = extractRange(properties)
37
- const path = modelRuntime().sortedIndexRangePath(indexName, idParts, range)
38
- return path
39
- }
40
- })
41
- }
42
-
43
6
  const {
44
7
  defineCreatedEvent, defineUpdatedEvent, defineDeletedEvent, defineTransferredEvent,
45
8
  } = require('./itemEvents.js')
46
- const { defineTransferEvent } = require("./itemEvents");
47
9
 
48
- function defineCreateAction(config, context) {
49
- const {
50
- service, app, model, defaults, modelPropertyName, modelRuntime,
51
- otherPropertyNames, joinedOthersPropertyName, modelName, writeableProperties, joinedOthersClassName
52
- } = context
53
- const eventName = joinedOthersPropertyName + 'Owned' + modelName + 'Created'
54
- const actionName = 'set' + joinedOthersClassName + 'Owned' + modelName
55
- service.actions[actionName] = new ActionDefinition({
56
- name: actionName,
57
- properties: {
58
- ...(model.properties)
59
- },
60
- access: config.createAccess || config.writeAccess,
61
- skipValidation: true,
62
- //queuedBy: otherPropertyNames,
63
- waitForEvents: true,
64
- async execute(properties, { client, service }, emit) {
65
- const id = properties[modelPropertyName] || app.generateUid()
66
- const entity = await modelRuntime().get(id)
67
- if(entity) throw 'exists'
68
- const identifiers = extractIdentifiers(otherPropertyNames, properties)
69
- const data = extractObjectData(writeableProperties, properties, defaults)
70
- await App.validation.validate(data, validators, { source: action, action, service, app, client })
71
- emit({
72
- type: eventName,
73
- [modelPropertyName]: id,
74
- identifiers, data
75
- })
76
- }
77
- })
78
- const action = service.actions[actionName]
79
- const validators = App.validation.getValidators(action, service, action)
80
- }
81
-
82
- function defineUpdateAction(config, context) {
83
- const {
84
- service, app, model, modelRuntime, modelPropertyName,
85
- otherPropertyNames, joinedOthersPropertyName, modelName, writeableProperties, joinedOthersClassName
86
- } = context
87
- const eventName = joinedOthersPropertyName + 'Owned' + modelName + 'Updated'
88
- const actionName = 'update' + joinedOthersClassName + 'Owned' + modelName
89
- service.actions[actionName] = new ActionDefinition({
90
- name: actionName,
91
- properties: {
92
- ...(model.properties)
93
- },
94
- access: config.updateAccess || config.writeAccess,
95
- skipValidation: true,
96
- //queuedBy: otherPropertyNames,
97
- waitForEvents: true,
98
- async execute(properties, {client, service}, emit) {
99
- const id = properties[modelPropertyName]
100
- const entity = await modelRuntime().get(id)
101
- if(!entity) throw 'not_found'
102
- const entityIdParts = extractIdParts(otherPropertyNames, entity)
103
- const idParts = extractIdParts(otherPropertyNames, properties)
104
- if(JSON.stringify(entityIdParts) != JSON.stringify(idParts)) {
105
- throw 'not_authorized'
106
- }
107
- const identifiers = extractIdentifiers(otherPropertyNames, properties)
108
- const data = extractObjectData(writeableProperties, properties, entity)
109
- await App.validation.validate(data, validators, { source: action, action, service, app, client })
110
- emit({
111
- type: eventName,
112
- [modelPropertyName]: id,
113
- identifiers,
114
- data
115
- })
116
- }
117
- })
118
- const action = service.actions[actionName]
119
- const validators = App.validation.getValidators(action, service, action)
120
- }
121
-
122
- function defineDeleteAction(config, context) {
123
- const {
124
- service, app, model, modelRuntime, modelPropertyName,
125
- otherPropertyNames, joinedOthersPropertyName, modelName, writeableProperties, joinedOthersClassName
126
- } = context
127
- const eventName = joinedOthersPropertyName + 'Owned' + modelName + 'Deleted'
128
- const actionName = 'delete' + joinedOthersClassName + 'Owned' + modelName
129
- service.actions[actionName] = new ActionDefinition({
130
- name: actionName,
131
- properties: {
132
- ...(model.properties)
133
- },
134
- access: config.deleteAccess || config.writeAccess,
135
- skipValidation: true,
136
- //queuedBy: otherPropertyNames,
137
- waitForEvents: true,
138
- async execute(properties, {client, service}, emit) {
139
- const id = properties[modelPropertyName]
140
- const entity = await modelRuntime().get(id)
141
- if(!entity) throw new Error('not_found')
142
- const entityIdParts = extractIdParts(otherPropertyNames, entity)
143
- const idParts = extractIdParts(otherPropertyNames, properties)
144
- if(JSON.stringify(entityIdParts) != JSON.stringify(idParts)) {
145
- throw new Error('not_authorized')
146
- }
147
- emit({
148
- type: eventName,
149
- [modelPropertyName]: id
150
- })
151
- }
152
- })
153
- }
154
-
155
- function defineSortIndex(context, sortFields) {
156
- if(!Array.isArray(sortFields)) sortFields = [sortFields]
157
- console.log("DEFINE SORT INDEX", sortFields)
158
- const sortFieldsUc = sortFields.map(fd=>fd.slice(0, 1).toUpperCase() + fd.slice(1))
159
- const indexName = 'by' + context.joinedOthersClassName + sortFieldsUc.join('')
160
- context.model.indexes[indexName] = new IndexDefinition({
161
- property: [...context.otherPropertyNames, ...sortFields]
162
- })
163
- }
10
+ const {
11
+ defineView, defineCreateAction, defineUpdateAction, defineDeleteAction, defineSortIndex
12
+ } = require('./pluralRelationUtils.js')
164
13
 
165
14
  module.exports = function(service, app) {
166
- processModelsAnnotation(service, app, 'itemOf', (config, context) => {
15
+ processModelsAnnotation(service, app, 'itemOf', false, (config, context) => {
16
+
17
+ context.relationWord = 'Item'
18
+ context.reverseRelationWord = 'Owned'
167
19
 
168
20
  defineProperties(context.model, context.others, context.otherPropertyNames)
169
21
  defineIndex(context.model, context.joinedOthersClassName, context.otherPropertyNames)
@@ -184,7 +36,7 @@ module.exports = function(service, app) {
184
36
  defineTransferredEvent(config, context)
185
37
  defineDeletedEvent(config, context)
186
38
 
187
- if(config.setAccess || config.writeAccess) {
39
+ if(config.createAccess || config.writeAccess) {
188
40
  defineCreateAction(config, context)
189
41
  }
190
42
 
package/itemOfAny.js CHANGED
@@ -1,177 +1,21 @@
1
- const App = require("@live-change/framework")
2
- const { PropertyDefinition, ViewDefinition, IndexDefinition, ActionDefinition, EventDefinition } = App
3
-
4
- const {
5
- extractRange, extractObjectData
6
- } = require('./utils.js')
7
-
8
1
  const {
9
- extractTypeAndIdParts, extractIdentifiersWithTypes, defineAnyProperties, defineAnyIndex,
2
+ defineAnyProperties, defineAnyIndex,
10
3
  processModelsAnyAnnotation
11
4
  } = require('./utilsAny.js')
12
5
 
13
- function defineView(config, context) {
14
- const { service, modelRuntime, otherPropertyNames, joinedOthersPropertyName, joinedOthersClassName,
15
- modelName, others, model } = context
16
- const indexName = 'by'+context.joinedOthersClassName
17
- const viewProperties = {}
18
- for (let i = 0; i < others.length; i++) {
19
- viewProperties[otherPropertyNames[i]] = new PropertyDefinition({
20
- type: 'String',
21
- validation: ['nonEmpty']
22
- })
23
- viewProperties[otherPropertyNames[i] + 'Type'] = new PropertyDefinition({
24
- type: 'String',
25
- validation: ['nonEmpty']
26
- })
27
- }
28
- const viewName = joinedOthersPropertyName + 'Owned' + modelName + 's'
29
- service.views[viewName] = new ViewDefinition({
30
- name: viewName,
31
- properties: {
32
- ...viewProperties,
33
- ...App.utils.rangeProperties
34
- },
35
- returns: {
36
- type: Array,
37
- of: {
38
- type: model
39
- }
40
- },
41
- access: config.readAccess,
42
- daoPath(properties, { client, context }) {
43
- const typeAndIdParts = extractTypeAndIdParts(otherPropertyNames, properties)
44
- const range = extractRange(properties)
45
- const path = modelRuntime().sortedIndexRangePath(indexName, typeAndIdParts, range)
46
- return path
47
- }
48
- })
49
- }
50
-
51
6
  const {
52
7
  defineCreatedEvent, defineUpdatedEvent, defineDeletedEvent, defineTransferredEvent,
53
8
  } = require('./itemEvents.js')
54
- const { defineTransferEvent } = require("./itemEvents");
55
9
 
56
- function defineCreateAction(config, context) {
57
- const {
58
- service, app, model, defaults, modelPropertyName, modelRuntime,
59
- otherPropertyNames, joinedOthersPropertyName, modelName, writeableProperties, joinedOthersClassName
60
- } = context
61
- const eventName = joinedOthersPropertyName + 'Owned' + modelName + 'Created'
62
- const actionName = 'set' + joinedOthersClassName + 'Owned' + modelName
63
- service.actions[actionName] = new ActionDefinition({
64
- name: actionName,
65
- properties: {
66
- ...(model.properties)
67
- },
68
- access: config.createAccess || config.writeAccess,
69
- skipValidation: true,
70
- //queuedBy: otherPropertyNames,
71
- waitForEvents: true,
72
- async execute(properties, { client, service }, emit) {
73
- const id = properties[modelPropertyName] || app.generateUid()
74
- const entity = await modelRuntime().get(id)
75
- if(entity) throw 'exists'
76
- const identifiers = extractIdentifiersWithTypes(otherPropertyNames, properties)
77
- const data = extractObjectData(writeableProperties, properties, defaults)
78
- await App.validation.validate(data, validators, { source: action, action, service, app, client })
79
- emit({
80
- type: eventName,
81
- [modelPropertyName]: id,
82
- identifiers, data
83
- })
84
- }
85
- })
86
- const action = service.actions[actionName]
87
- const validators = App.validation.getValidators(action, service, action)
88
- }
89
-
90
- function defineUpdateAction(config, context) {
91
- const {
92
- service, app, model, modelRuntime, modelPropertyName,
93
- otherPropertyNames, joinedOthersPropertyName, modelName, writeableProperties, joinedOthersClassName
94
- } = context
95
- const eventName = joinedOthersPropertyName + 'Owned' + modelName + 'Updated'
96
- const actionName = 'update' + joinedOthersClassName + 'Owned' + modelName
97
- service.actions[actionName] = new ActionDefinition({
98
- name: actionName,
99
- properties: {
100
- ...(model.properties)
101
- },
102
- access: config.updateAccess || config.writeAccess,
103
- skipValidation: true,
104
- //queuedBy: otherPropertyNames,
105
- waitForEvents: true,
106
- async execute(properties, { client, service }, emit) {
107
- const id = properties[modelPropertyName]
108
- const entity = await modelRuntime().get(id)
109
- if(!entity) throw 'not_found'
110
- const entityTypeAndIdParts = extractTypeAndIdParts(otherPropertyNames, entity)
111
- const typeAndIdParts = extractTypeAndIdParts(otherPropertyNames, properties)
112
- if(JSON.stringify(entityTypeAndIdParts) != JSON.stringify(typeAndIdParts)) {
113
- throw 'not_authorized'
114
- }
115
- const identifiers = extractIdentifiersWithTypes(otherPropertyNames, properties)
116
- const data = extractObjectData(writeableProperties, properties, entity)
117
- await App.validation.validate(data, validators, { source: action, action, service, app, client })
118
- emit({
119
- type: eventName,
120
- [modelPropertyName]: id,
121
- identifiers,
122
- data
123
- })
124
- }
125
- })
126
- const action = service.actions[actionName]
127
- const validators = App.validation.getValidators(action, service, action)
128
- }
129
-
130
- function defineDeleteAction(config, context) {
131
- const {
132
- service, app, model, modelRuntime, modelPropertyName,
133
- otherPropertyNames, joinedOthersPropertyName, modelName, writeableProperties, joinedOthersClassName
134
- } = context
135
- const eventName = joinedOthersPropertyName + 'Owned' + modelName + 'Deleted'
136
- const actionName = 'delete' + joinedOthersClassName + 'Owned' + modelName
137
- service.actions[actionName] = new ActionDefinition({
138
- name: actionName,
139
- properties: {
140
- ...(model.properties)
141
- },
142
- access: config.deleteAccess || config.writeAccess,
143
- skipValidation: true,
144
- //queuedBy: otherPropertyNames,
145
- waitForEvents: true,
146
- async execute(properties, { client, service }, emit) {
147
- const id = properties[modelPropertyName]
148
- const entity = await modelRuntime().get(id)
149
- if(!entity) throw new Error('not_found')
150
- const entityTypeAndIdParts = extractTypeAndIdParts(otherPropertyNames, entity)
151
- const typeAndIdParts = extractTypeAndIdParts(otherPropertyNames, properties)
152
- if(JSON.stringify(entityTypeAndIdParts) != JSON.stringify(typeAndIdParts)) {
153
- throw new Error('not_authorized')
154
- }
155
- emit({
156
- type: eventName,
157
- [modelPropertyName]: id
158
- })
159
- }
160
- })
161
- }
162
-
163
- function defineSortIndex(context, sortFields) {
164
- if(!Array.isArray(sortFields)) sortFields = [sortFields]
165
- console.log("DEFINE SORT INDEX", sortFields)
166
- const sortFieldsUc = sortFields.map(fd => fd.slice(0, 1).toUpperCase() + fd.slice(1))
167
- const indexName = 'by' + context.joinedOthersClassName + sortFieldsUc.join('')
168
- context.model.indexes[indexName] = new IndexDefinition({
169
- property: [...(context.otherPropertyNames.map(prop => [prop + 'Type', prop])), ...sortFields]
170
- })
171
- }
10
+ const {
11
+ defineView, defineCreateAction, defineUpdateAction, defineDeleteAction, defineSortIndex
12
+ } = require('./pluralRelationAnyUtils.js')
172
13
 
173
14
  module.exports = function(service, app) {
174
- processModelsAnyAnnotation(service, app, 'itemOfAny', (config, context) => {
15
+ processModelsAnyAnnotation(service, app, 'itemOfAny',false, (config, context) => {
16
+
17
+ context.relationWord = 'Item'
18
+ context.reverseRelationWord = 'Owned'
175
19
 
176
20
  defineAnyProperties(context.model, context.otherPropertyNames)
177
21
  defineAnyIndex(context.model, context.joinedOthersClassName, context.otherPropertyNames)
@@ -192,7 +36,7 @@ module.exports = function(service, app) {
192
36
  defineTransferredEvent(config, context)
193
37
  defineDeletedEvent(config, context)
194
38
 
195
- if(config.setAccess || config.writeAccess) {
39
+ if(config.createAccess || config.writeAccess) {
196
40
  defineCreateAction(config, context)
197
41
  }
198
42
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@live-change/relations-plugin",
3
- "version": "0.5.20",
3
+ "version": "0.5.23",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -21,7 +21,8 @@
21
21
  "url": "https://www.viamage.com/"
22
22
  },
23
23
  "dependencies": {
24
- "@live-change/framework": "^0.5.20"
24
+ "@live-change/framework": "^0.5.23",
25
+ "pluralize": "8.0.0"
25
26
  },
26
- "gitHead": "d37be156c4f76af3ead4ad0aae3f80281191bf57"
27
+ "gitHead": "ddd822ce5b366e93309073e8f92a87d1e52fc751"
27
28
  }
@@ -0,0 +1,163 @@
1
+ const App = require("@live-change/framework")
2
+ const { PropertyDefinition, ViewDefinition, IndexDefinition, ActionDefinition, EventDefinition } = App
3
+ const { extractTypeAndIdParts, extractIdentifiersWithTypes } = require("./utilsAny.js")
4
+ const { extractRange, extractObjectData } = require("./utils.js")
5
+
6
+ const pluralize = require('pluralize')
7
+
8
+ function defineView(config, context) {
9
+ const { service, modelRuntime, otherPropertyNames, joinedOthersPropertyName, joinedOthersClassName,
10
+ modelName, others, model } = context
11
+ const indexName = 'by'+context.joinedOthersClassName
12
+ const viewProperties = {}
13
+ for (let i = 0; i < others.length; i++) {
14
+ viewProperties[otherPropertyNames[i]] = new PropertyDefinition({
15
+ type: 'String',
16
+ validation: ['nonEmpty']
17
+ })
18
+ viewProperties[otherPropertyNames[i] + 'Type'] = new PropertyDefinition({
19
+ type: 'String',
20
+ validation: ['nonEmpty']
21
+ })
22
+ }
23
+ const viewName = joinedOthersPropertyName + context.reverseRelationWord + pluralize(modelName)
24
+ service.views[viewName] = new ViewDefinition({
25
+ name: viewName,
26
+ properties: {
27
+ ...viewProperties,
28
+ ...App.utils.rangeProperties
29
+ },
30
+ returns: {
31
+ type: Array,
32
+ of: {
33
+ type: model
34
+ }
35
+ },
36
+ access: config.readAccess,
37
+ daoPath(properties, { client, context }) {
38
+ const typeAndIdParts = extractTypeAndIdParts(otherPropertyNames, properties)
39
+ const range = extractRange(properties)
40
+ const path = modelRuntime().sortedIndexRangePath(indexName, typeAndIdParts, range)
41
+ return path
42
+ }
43
+ })
44
+ }
45
+
46
+ function defineCreateAction(config, context) {
47
+ const {
48
+ service, app, model, defaults, modelPropertyName, modelRuntime,
49
+ otherPropertyNames, joinedOthersPropertyName, modelName, writeableProperties, joinedOthersClassName
50
+ } = context
51
+ const eventName = joinedOthersPropertyName + context.reverseRelationWord + modelName + 'Created'
52
+ const actionName = 'create' + joinedOthersClassName + context.reverseRelationWord + modelName
53
+ service.actions[actionName] = new ActionDefinition({
54
+ name: actionName,
55
+ properties: {
56
+ ...(model.properties)
57
+ },
58
+ access: config.createAccess || config.writeAccess,
59
+ skipValidation: true,
60
+ //queuedBy: otherPropertyNames,
61
+ waitForEvents: true,
62
+ async execute(properties, { client, service }, emit) {
63
+ const id = properties[modelPropertyName] || app.generateUid()
64
+ const entity = await modelRuntime().get(id)
65
+ if(entity) throw 'exists'
66
+ const identifiers = extractIdentifiersWithTypes(otherPropertyNames, properties)
67
+ const data = extractObjectData(writeableProperties, properties, defaults)
68
+ await App.validation.validate(data, validators, { source: action, action, service, app, client })
69
+ emit({
70
+ type: eventName,
71
+ [modelPropertyName]: id,
72
+ identifiers, data
73
+ })
74
+ }
75
+ })
76
+ const action = service.actions[actionName]
77
+ const validators = App.validation.getValidators(action, service, action)
78
+ }
79
+
80
+ function defineUpdateAction(config, context) {
81
+ const {
82
+ service, app, model, modelRuntime, modelPropertyName,
83
+ otherPropertyNames, joinedOthersPropertyName, modelName, writeableProperties, joinedOthersClassName
84
+ } = context
85
+ const eventName = joinedOthersPropertyName + context.reverseRelationWord + modelName + 'Updated'
86
+ const actionName = 'update' + joinedOthersClassName + context.reverseRelationWord + modelName
87
+ service.actions[actionName] = new ActionDefinition({
88
+ name: actionName,
89
+ properties: {
90
+ ...(model.properties)
91
+ },
92
+ access: config.updateAccess || config.writeAccess,
93
+ skipValidation: true,
94
+ //queuedBy: otherPropertyNames,
95
+ waitForEvents: true,
96
+ async execute(properties, { client, service }, emit) {
97
+ const id = properties[modelPropertyName]
98
+ const entity = await modelRuntime().get(id)
99
+ if(!entity) throw 'not_found'
100
+ const entityTypeAndIdParts = extractTypeAndIdParts(otherPropertyNames, entity)
101
+ const typeAndIdParts = extractTypeAndIdParts(otherPropertyNames, properties)
102
+ if(JSON.stringify(entityTypeAndIdParts) != JSON.stringify(typeAndIdParts)) {
103
+ throw 'not_authorized'
104
+ }
105
+ const identifiers = extractIdentifiersWithTypes(otherPropertyNames, properties)
106
+ const data = extractObjectData(writeableProperties, properties, entity)
107
+ await App.validation.validate(data, validators, { source: action, action, service, app, client })
108
+ emit({
109
+ type: eventName,
110
+ [modelPropertyName]: id,
111
+ identifiers,
112
+ data
113
+ })
114
+ }
115
+ })
116
+ const action = service.actions[actionName]
117
+ const validators = App.validation.getValidators(action, service, action)
118
+ }
119
+
120
+ function defineDeleteAction(config, context) {
121
+ const {
122
+ service, app, model, modelRuntime, modelPropertyName,
123
+ otherPropertyNames, joinedOthersPropertyName, modelName, writeableProperties, joinedOthersClassName
124
+ } = context
125
+ const eventName = joinedOthersPropertyName + context.reverseRelationWord + modelName + 'Deleted'
126
+ const actionName = 'delete' + joinedOthersClassName + context.reverseRelationWord + modelName
127
+ service.actions[actionName] = new ActionDefinition({
128
+ name: actionName,
129
+ properties: {
130
+ ...(model.properties)
131
+ },
132
+ access: config.deleteAccess || config.writeAccess,
133
+ skipValidation: true,
134
+ //queuedBy: otherPropertyNames,
135
+ waitForEvents: true,
136
+ async execute(properties, { client, service }, emit) {
137
+ const id = properties[modelPropertyName]
138
+ const entity = await modelRuntime().get(id)
139
+ if(!entity) throw new Error('not_found')
140
+ const entityTypeAndIdParts = extractTypeAndIdParts(otherPropertyNames, entity)
141
+ const typeAndIdParts = extractTypeAndIdParts(otherPropertyNames, properties)
142
+ if(JSON.stringify(entityTypeAndIdParts) != JSON.stringify(typeAndIdParts)) {
143
+ throw new Error('not_authorized')
144
+ }
145
+ emit({
146
+ type: eventName,
147
+ [modelPropertyName]: id
148
+ })
149
+ }
150
+ })
151
+ }
152
+
153
+ function defineSortIndex(context, sortFields) {
154
+ if(!Array.isArray(sortFields)) sortFields = [sortFields]
155
+ console.log("DEFINE SORT INDEX", sortFields)
156
+ const sortFieldsUc = sortFields.map(fd => fd.slice(0, 1).toUpperCase() + fd.slice(1))
157
+ const indexName = 'by' + context.joinedOthersClassName + sortFieldsUc.join('')
158
+ context.model.indexes[indexName] = new IndexDefinition({
159
+ property: [...(context.otherPropertyNames.map(prop => [prop + 'Type', prop])), ...sortFields]
160
+ })
161
+ }
162
+
163
+ module.exports = { defineView, defineCreateAction, defineUpdateAction, defineDeleteAction, defineSortIndex }