@live-change/relations-plugin 0.1.9 → 0.1.10
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/index.js +3 -1
- package/itemOf.js +172 -2
- package/package.json +1 -1
- package/propertyOf.js +12 -5
package/index.js
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
const propertyOf = require('./propertyOf.js')
|
|
2
|
+
const itemOf = require('./itemOf.js')
|
|
2
3
|
|
|
3
4
|
module.exports = function(app, services) {
|
|
4
5
|
|
|
5
6
|
app.defaultProcessors.push(propertyOf)
|
|
7
|
+
app.defaultProcessors.push(itemOf)
|
|
6
8
|
}
|
|
7
9
|
|
|
8
|
-
module.exports.processors = [ propertyOf ]
|
|
10
|
+
module.exports.processors = [ propertyOf, itemOf ]
|
package/itemOf.js
CHANGED
|
@@ -5,6 +5,7 @@ const {
|
|
|
5
5
|
extractIdParts, extractRange, extractIdentifiers, extractObjectData, defineProperties, defineIndex,
|
|
6
6
|
processModelsAnnotation
|
|
7
7
|
} = require('./utils.js')
|
|
8
|
+
const {generateId} = require("./utils");
|
|
8
9
|
|
|
9
10
|
function defineView(config, context) {
|
|
10
11
|
const { service, modelRuntime, otherPropertyNames, joinedOthersPropertyName, joinedOthersClassName,
|
|
@@ -40,17 +41,186 @@ function defineView(config, context) {
|
|
|
40
41
|
})
|
|
41
42
|
}
|
|
42
43
|
|
|
44
|
+
async function defineCreatedEvent(config, context) {
|
|
45
|
+
const {
|
|
46
|
+
service, modelRuntime, joinedOthersPropertyName, modelName, modelPropertyName
|
|
47
|
+
} = context
|
|
48
|
+
const eventName = joinedOthersPropertyName + 'Owned' + modelName + 'Created'
|
|
49
|
+
service.events[eventName] = new EventDefinition({
|
|
50
|
+
name: eventName,
|
|
51
|
+
execute(properties) {
|
|
52
|
+
const id = properties[modelPropertyName]
|
|
53
|
+
return modelRuntime().create({ ...properties.data, ...properties.identifiers, id })
|
|
54
|
+
}
|
|
55
|
+
})
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function defineCreateAction(config, context) {
|
|
59
|
+
const {
|
|
60
|
+
service, app, model, defaults, modelPropertyName, modelRuntime,
|
|
61
|
+
otherPropertyNames, joinedOthersPropertyName, modelName, writeableProperties, joinedOthersClassName
|
|
62
|
+
} = context
|
|
63
|
+
const eventName = joinedOthersPropertyName + 'Owned' + modelName + 'Created'
|
|
64
|
+
const actionName = 'set' + joinedOthersClassName + 'Owned' + modelName
|
|
65
|
+
service.actions[actionName] = new ActionDefinition({
|
|
66
|
+
name: actionName,
|
|
67
|
+
properties: {
|
|
68
|
+
...(model.properties)
|
|
69
|
+
},
|
|
70
|
+
access: config.createAccess || config.writeAccess,
|
|
71
|
+
skipValidation: true,
|
|
72
|
+
//queuedBy: otherPropertyNames,
|
|
73
|
+
waitForEvents: true,
|
|
74
|
+
async execute(properties, { client, service }, emit) {
|
|
75
|
+
const id = properties[modelPropertyName] || app.generateUid()
|
|
76
|
+
const entity = await modelRuntime().get(id)
|
|
77
|
+
if(entity) throw 'exists'
|
|
78
|
+
const identifiers = extractIdentifiers(otherPropertyNames, properties)
|
|
79
|
+
const data = extractObjectData(writeableProperties, properties, defaults)
|
|
80
|
+
await App.validation.validate(data, validators, { source: action, action, service, app, client })
|
|
81
|
+
emit({
|
|
82
|
+
type: eventName,
|
|
83
|
+
[modelPropertyName]: id,
|
|
84
|
+
identifiers, data
|
|
85
|
+
})
|
|
86
|
+
}
|
|
87
|
+
})
|
|
88
|
+
const action = service.actions[actionName]
|
|
89
|
+
const validators = App.validation.getValidators(action, service, action)
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
async function defineUpdatedEvent(config, context) {
|
|
93
|
+
const {
|
|
94
|
+
service, modelRuntime, joinedOthersPropertyName, modelName, modelPropertyName
|
|
95
|
+
} = context
|
|
96
|
+
const eventName = joinedOthersPropertyName + 'Owned' + modelName + 'Updated'
|
|
97
|
+
service.events[eventName] = new EventDefinition({
|
|
98
|
+
name: eventName,
|
|
99
|
+
execute(properties) {
|
|
100
|
+
const id = properties[modelPropertyName]
|
|
101
|
+
return modelRuntime().update(id, { ...properties.data, ...properties.identifiers, id })
|
|
102
|
+
}
|
|
103
|
+
})
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
async function defineUpdateAction(config, context) {
|
|
107
|
+
const {
|
|
108
|
+
service, app, model, modelRuntime, modelPropertyName,
|
|
109
|
+
otherPropertyNames, joinedOthersPropertyName, modelName, writeableProperties, joinedOthersClassName
|
|
110
|
+
} = context
|
|
111
|
+
const eventName = joinedOthersPropertyName + 'Owned' + modelName + 'Updated'
|
|
112
|
+
const actionName = 'update' + joinedOthersClassName + 'Owned' + modelName
|
|
113
|
+
service.actions[actionName] = new ActionDefinition({
|
|
114
|
+
name: actionName,
|
|
115
|
+
properties: {
|
|
116
|
+
...(model.properties)
|
|
117
|
+
},
|
|
118
|
+
access: config.updateAccess || config.writeAccess,
|
|
119
|
+
skipValidation: true,
|
|
120
|
+
//queuedBy: otherPropertyNames,
|
|
121
|
+
waitForEvents: true,
|
|
122
|
+
async execute(properties, {client, service}, emit) {
|
|
123
|
+
const id = properties[modelPropertyName]
|
|
124
|
+
const entity = await modelRuntime().get(id)
|
|
125
|
+
if(!entity) throw 'not_found'
|
|
126
|
+
const entityIdParts = extractIdParts(otherPropertyNames, entity)
|
|
127
|
+
const idParts = extractIdParts(otherPropertyNames, properties)
|
|
128
|
+
if(JSON.stringify(entityIdParts) != JSON.stringify(idParts)) {
|
|
129
|
+
throw 'not_authorized'
|
|
130
|
+
}
|
|
131
|
+
const identifiers = extractIdentifiers(otherPropertyNames, properties)
|
|
132
|
+
const data = extractObjectData(writeableProperties, properties, entity)
|
|
133
|
+
await App.validation.validate(data, validators, { source: action, action, service, app, client })
|
|
134
|
+
emit({
|
|
135
|
+
type: eventName,
|
|
136
|
+
[modelPropertyName]: id,
|
|
137
|
+
identifiers,
|
|
138
|
+
data
|
|
139
|
+
})
|
|
140
|
+
}
|
|
141
|
+
})
|
|
142
|
+
const action = service.actions[actionName]
|
|
143
|
+
const validators = App.validation.getValidators(action, service, action)
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
async function defineDeletedEvent(config, context) {
|
|
147
|
+
const {
|
|
148
|
+
service, modelRuntime, joinedOthersPropertyName, modelName, modelPropertyName
|
|
149
|
+
} = context
|
|
150
|
+
const eventName = joinedOthersPropertyName + 'Owned' + modelName + 'Deleted'
|
|
151
|
+
service.events[eventName] = new EventDefinition({
|
|
152
|
+
name: eventName,
|
|
153
|
+
execute(properties) {
|
|
154
|
+
const id = properties[modelPropertyName]
|
|
155
|
+
return modelRuntime().delete(id)
|
|
156
|
+
}
|
|
157
|
+
})
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
async function defineDeleteAction(config, context) {
|
|
161
|
+
const {
|
|
162
|
+
service, app, model, modelRuntime, modelPropertyName,
|
|
163
|
+
otherPropertyNames, joinedOthersPropertyName, modelName, writeableProperties, joinedOthersClassName
|
|
164
|
+
} = context
|
|
165
|
+
const eventName = joinedOthersPropertyName + 'Owned' + modelName + 'Deleted'
|
|
166
|
+
const actionName = 'delete' + joinedOthersClassName + 'Owned' + modelName
|
|
167
|
+
service.actions[actionName] = new ActionDefinition({
|
|
168
|
+
name: actionName,
|
|
169
|
+
properties: {
|
|
170
|
+
...(model.properties)
|
|
171
|
+
},
|
|
172
|
+
access: config.deleteAccess || config.writeAccess,
|
|
173
|
+
skipValidation: true,
|
|
174
|
+
//queuedBy: otherPropertyNames,
|
|
175
|
+
waitForEvents: true,
|
|
176
|
+
async execute(properties, {client, service}, emit) {
|
|
177
|
+
const id = properties[modelPropertyName]
|
|
178
|
+
const entity = await modelRuntime().get(id)
|
|
179
|
+
if(!entity) throw new Error('not_found')
|
|
180
|
+
const entityIdParts = extractIdParts(otherPropertyNames, entity)
|
|
181
|
+
const idParts = extractIdParts(otherPropertyNames, properties)
|
|
182
|
+
if(JSON.stringify(entityIdParts) != JSON.stringify(idParts)) {
|
|
183
|
+
throw new Error('not_authorized')
|
|
184
|
+
}
|
|
185
|
+
emit({
|
|
186
|
+
type: eventName,
|
|
187
|
+
[modelPropertyName]: id
|
|
188
|
+
})
|
|
189
|
+
}
|
|
190
|
+
})
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
function defineSortIndex(context, sortFields) {
|
|
194
|
+
if(!Array.isArray(sortFields)) sortFields = [sortFields]
|
|
195
|
+
console.log("DEFINE SORT INDEX", sortFields)
|
|
196
|
+
const sortFieldsUc = sortFields.map(fd=>fd.slice(0, 1).toUpperCase() + fd.slice(1))
|
|
197
|
+
const indexName = 'by' + context.joinedOthersClassName + sortFieldsUc.join('')
|
|
198
|
+
context.model.indexes[indexName] = new IndexDefinition({
|
|
199
|
+
property: [...context.otherPropertyNames, ...sortFields]
|
|
200
|
+
})
|
|
201
|
+
}
|
|
202
|
+
|
|
43
203
|
module.exports = function(service, app) {
|
|
44
|
-
processModelsAnnotation(service, app, '
|
|
204
|
+
processModelsAnnotation(service, app, 'itemOf', (config, context) => {
|
|
45
205
|
|
|
46
206
|
defineProperties(context.model, context.others, context.otherPropertyNames)
|
|
47
207
|
defineIndex(context.model, context.joinedOthersClassName, context.otherPropertyNames)
|
|
48
208
|
|
|
209
|
+
if(config.sortBy) {
|
|
210
|
+
for(const sortFields of config.sortBy) {
|
|
211
|
+
defineSortIndex(context, sortFields)
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
49
215
|
if(config.readAccess) {
|
|
50
216
|
defineView(config, context)
|
|
51
217
|
}
|
|
52
218
|
/// TODO: multiple views with limited fields
|
|
53
219
|
|
|
220
|
+
defineCreatedEvent(config, context)
|
|
221
|
+
defineUpdatedEvent(config, context)
|
|
222
|
+
defineDeletedEvent(config, context)
|
|
223
|
+
|
|
54
224
|
if(config.setAccess || config.writeAccess) {
|
|
55
225
|
defineCreateAction(config, context)
|
|
56
226
|
}
|
|
@@ -60,7 +230,7 @@ module.exports = function(service, app) {
|
|
|
60
230
|
}
|
|
61
231
|
|
|
62
232
|
if(config.resetAccess || config.writeAccess) {
|
|
63
|
-
|
|
233
|
+
defineDeleteAction(config, context)
|
|
64
234
|
}
|
|
65
235
|
})
|
|
66
236
|
}
|
package/package.json
CHANGED
package/propertyOf.js
CHANGED
|
@@ -114,7 +114,8 @@ async function defineUpdateAction(config, context) {
|
|
|
114
114
|
waitForEvents: true,
|
|
115
115
|
async execute(properties, {client, service}, emit) {
|
|
116
116
|
const identifiers = extractIdentifiers(otherPropertyNames, properties)
|
|
117
|
-
const
|
|
117
|
+
const id = generateId(otherPropertyNames, properties)
|
|
118
|
+
const entity = await modelRuntime().get(id)
|
|
118
119
|
if (!entity) throw new Error('not_found')
|
|
119
120
|
const data = extractObjectData(writeableProperties, properties, entity)
|
|
120
121
|
await App.validation.validate(data, validators, { source: action, action, service, app, client })
|
|
@@ -144,19 +145,26 @@ async function defineResetEvent(config, context) {
|
|
|
144
145
|
|
|
145
146
|
async function defineResetAction(config, context) {
|
|
146
147
|
const {
|
|
147
|
-
service, modelRuntime,
|
|
148
|
-
otherPropertyNames, joinedOthersPropertyName, modelName, joinedOthersClassName
|
|
148
|
+
service, modelRuntime, modelPropertyName,
|
|
149
|
+
otherPropertyNames, joinedOthersPropertyName, modelName, joinedOthersClassName, model
|
|
149
150
|
} = context
|
|
150
151
|
const eventName = joinedOthersPropertyName + 'Owned' + modelName + 'Reset'
|
|
151
152
|
const actionName = 'reset' + joinedOthersClassName + 'Owned' + modelName
|
|
152
153
|
service.actions[actionName] = new ActionDefinition({
|
|
153
154
|
name: actionName,
|
|
155
|
+
properties: {
|
|
156
|
+
[modelPropertyName]: {
|
|
157
|
+
type: model,
|
|
158
|
+
validation: ['nonEmpty']
|
|
159
|
+
}
|
|
160
|
+
},
|
|
154
161
|
access: config.resetAccess || config.writeAccess,
|
|
155
162
|
queuedBy: otherPropertyNames,
|
|
156
163
|
waitForEvents: true,
|
|
157
164
|
async execute(properties, {client, service}, emit) {
|
|
158
165
|
const identifiers = extractIdentifiers(otherPropertyNames, properties)
|
|
159
|
-
const
|
|
166
|
+
const id = generateId(otherPropertyNames, properties)
|
|
167
|
+
const entity = await modelRuntime().get(id)
|
|
160
168
|
if (!entity) throw new Error('not_found')
|
|
161
169
|
emit({
|
|
162
170
|
type: eventName,
|
|
@@ -174,7 +182,6 @@ module.exports = function(service, app) {
|
|
|
174
182
|
defineProperties(context.model, context.others, context.otherPropertyNames)
|
|
175
183
|
defineIndex(context.model, context.joinedOthersClassName, context.otherPropertyNames)
|
|
176
184
|
|
|
177
|
-
|
|
178
185
|
if(config.readAccess) {
|
|
179
186
|
defineView(config, context)
|
|
180
187
|
}
|