@live-change/relations-plugin 0.5.27 → 0.6.2
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/combinations.js +17 -0
- package/itemOf.js +2 -2
- package/itemOfAny.js +5 -4
- package/package.json +3 -3
- package/pluralRelationAnyUtils.js +12 -3
- package/propertyOf.js +1 -1
- package/propertyOfAny.js +8 -6
- package/relatedTo.js +2 -2
- package/relatedToAny.js +2 -2
- package/singularRelationAnyUtils.js +56 -4
- package/utils.js +7 -13
- package/utilsAny.js +18 -3
package/combinations.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
function combinations(x, n ,p=[]) {
|
|
2
|
+
if(x.length == 0 || n > x.length) return []
|
|
3
|
+
if(n == 1 || x.length == 1) return x.map(e=>p.concat([e]))
|
|
4
|
+
let acc = []
|
|
5
|
+
for(let i = 0; i < x.length; i++) acc.push(
|
|
6
|
+
...combinations(x.slice(i+1), n - 1, p.concat([x[i]]))
|
|
7
|
+
)
|
|
8
|
+
return acc
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function allCombinations(x) {
|
|
12
|
+
let acc = []
|
|
13
|
+
for(let i = 1; i<=x.length; i++) acc.push(...combinations(x,i))
|
|
14
|
+
return acc
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
module.exports = { combinations, allCombinations }
|
package/itemOf.js
CHANGED
|
@@ -17,7 +17,7 @@ module.exports = function(service, app) {
|
|
|
17
17
|
context.relationWord = 'Item'
|
|
18
18
|
context.reverseRelationWord = 'Owned'
|
|
19
19
|
|
|
20
|
-
defineProperties(context.model, context.others, context.otherPropertyNames)
|
|
20
|
+
context.identifiers = defineProperties(context.model, context.others, context.otherPropertyNames)
|
|
21
21
|
defineIndex(context.model, context.joinedOthersClassName, context.otherPropertyNames)
|
|
22
22
|
|
|
23
23
|
if(config.sortBy) {
|
|
@@ -44,7 +44,7 @@ module.exports = function(service, app) {
|
|
|
44
44
|
defineUpdateAction(config, context)
|
|
45
45
|
}
|
|
46
46
|
|
|
47
|
-
if(config.
|
|
47
|
+
if(config.deleteAccess || config.writeAccess) {
|
|
48
48
|
defineDeleteAction(config, context)
|
|
49
49
|
}
|
|
50
50
|
})
|
package/itemOfAny.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
const {
|
|
2
|
-
defineAnyProperties,
|
|
2
|
+
defineAnyProperties, defineAnyIndexes,
|
|
3
3
|
processModelsAnyAnnotation
|
|
4
4
|
} = require('./utilsAny.js')
|
|
5
5
|
|
|
@@ -17,8 +17,8 @@ module.exports = function(service, app) {
|
|
|
17
17
|
context.relationWord = 'Item'
|
|
18
18
|
context.reverseRelationWord = 'Owned'
|
|
19
19
|
|
|
20
|
-
defineAnyProperties(context.model, context.otherPropertyNames)
|
|
21
|
-
|
|
20
|
+
context.identifiers = defineAnyProperties(context.model, context.otherPropertyNames)
|
|
21
|
+
defineAnyIndexes(context.model, context.otherPropertyNames)
|
|
22
22
|
|
|
23
23
|
if(config.sortBy) {
|
|
24
24
|
for(const sortFields of config.sortBy) {
|
|
@@ -28,6 +28,7 @@ module.exports = function(service, app) {
|
|
|
28
28
|
|
|
29
29
|
if(config.readAccess) {
|
|
30
30
|
defineView(config, context)
|
|
31
|
+
// TODO: multiple views with all properties combinations
|
|
31
32
|
}
|
|
32
33
|
/// TODO: multiple views with limited fields
|
|
33
34
|
|
|
@@ -44,7 +45,7 @@ module.exports = function(service, app) {
|
|
|
44
45
|
defineUpdateAction(config, context)
|
|
45
46
|
}
|
|
46
47
|
|
|
47
|
-
if(config.
|
|
48
|
+
if(config.deleteAccess || config.writeAccess) {
|
|
48
49
|
defineDeleteAction(config, context)
|
|
49
50
|
}
|
|
50
51
|
})
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@live-change/relations-plugin",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.2",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -21,8 +21,8 @@
|
|
|
21
21
|
"url": "https://www.viamage.com/"
|
|
22
22
|
},
|
|
23
23
|
"dependencies": {
|
|
24
|
-
"@live-change/framework": "^0.
|
|
24
|
+
"@live-change/framework": "^0.6.2",
|
|
25
25
|
"pluralize": "8.0.0"
|
|
26
26
|
},
|
|
27
|
-
"gitHead": "
|
|
27
|
+
"gitHead": "02153d427b43f1766ee50d90cb531f63adeb7b0b"
|
|
28
28
|
}
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
const App = require("@live-change/framework")
|
|
2
2
|
const { PropertyDefinition, ViewDefinition, IndexDefinition, ActionDefinition, EventDefinition } = App
|
|
3
3
|
const { extractTypeAndIdParts, extractIdentifiersWithTypes } = require("./utilsAny.js")
|
|
4
|
-
const {
|
|
4
|
+
const { extractObjectData } = require("./utils.js")
|
|
5
|
+
const { extractRange } = App
|
|
5
6
|
|
|
6
7
|
const pluralize = require('pluralize')
|
|
7
8
|
|
|
@@ -88,6 +89,10 @@ function defineUpdateAction(config, context) {
|
|
|
88
89
|
service.actions[actionName] = new ActionDefinition({
|
|
89
90
|
name: actionName,
|
|
90
91
|
properties: {
|
|
92
|
+
[modelPropertyName]: {
|
|
93
|
+
type: model,
|
|
94
|
+
validation: ['nonEmpty']
|
|
95
|
+
},
|
|
91
96
|
...(model.properties)
|
|
92
97
|
},
|
|
93
98
|
access: config.updateAccess || config.writeAccess,
|
|
@@ -122,7 +127,7 @@ function defineUpdateAction(config, context) {
|
|
|
122
127
|
|
|
123
128
|
function defineDeleteAction(config, context) {
|
|
124
129
|
const {
|
|
125
|
-
service, app, model, modelRuntime, modelPropertyName,
|
|
130
|
+
service, app, model, modelRuntime, modelPropertyName, identifiers,
|
|
126
131
|
otherPropertyNames, joinedOthersPropertyName, modelName, writeableProperties, joinedOthersClassName
|
|
127
132
|
} = context
|
|
128
133
|
const eventName = joinedOthersPropertyName + context.reverseRelationWord + modelName + 'Deleted'
|
|
@@ -130,7 +135,11 @@ function defineDeleteAction(config, context) {
|
|
|
130
135
|
service.actions[actionName] = new ActionDefinition({
|
|
131
136
|
name: actionName,
|
|
132
137
|
properties: {
|
|
133
|
-
|
|
138
|
+
[modelPropertyName]: {
|
|
139
|
+
type: model,
|
|
140
|
+
validation: ['nonEmpty']
|
|
141
|
+
},
|
|
142
|
+
...identifiers
|
|
134
143
|
},
|
|
135
144
|
access: config.deleteAccess || config.writeAccess,
|
|
136
145
|
skipValidation: true,
|
package/propertyOf.js
CHANGED
|
@@ -15,7 +15,7 @@ module.exports = function(service, app) {
|
|
|
15
15
|
context.relationWord = 'Property'
|
|
16
16
|
context.reverseRelationWord = 'Owned'
|
|
17
17
|
|
|
18
|
-
defineProperties(context.model, context.others, context.otherPropertyNames)
|
|
18
|
+
context.identifiers = defineProperties(context.model, context.others, context.otherPropertyNames)
|
|
19
19
|
defineIndex(context.model, context.joinedOthersClassName, context.otherPropertyNames)
|
|
20
20
|
|
|
21
21
|
if(config.readAccess) {
|
package/propertyOfAny.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
const {
|
|
2
|
-
defineAnyProperties,
|
|
2
|
+
defineAnyProperties, defineAnyIndexes,
|
|
3
3
|
processModelsAnyAnnotation, generateAnyId
|
|
4
4
|
} = require('./utilsAny.js')
|
|
5
5
|
|
|
@@ -8,7 +8,7 @@ const {
|
|
|
8
8
|
} = require('./propertyEvents.js')
|
|
9
9
|
|
|
10
10
|
const {
|
|
11
|
-
|
|
11
|
+
defineObjectView, defineRangeViews, defineSetAction, defineUpdateAction, defineSetOrUpdateAction, defineResetAction
|
|
12
12
|
} = require('./singularRelationAnyUtils.js')
|
|
13
13
|
|
|
14
14
|
module.exports = function(service, app) {
|
|
@@ -16,16 +16,18 @@ module.exports = function(service, app) {
|
|
|
16
16
|
|
|
17
17
|
context.relationWord = 'Property'
|
|
18
18
|
context.reverseRelationWord = 'Owned'
|
|
19
|
+
context.partialReverseRelationWord = 'Owned'
|
|
19
20
|
|
|
20
|
-
defineAnyProperties(context.model, context.otherPropertyNames)
|
|
21
|
-
|
|
21
|
+
context.identifiers = defineAnyProperties(context.model, context.otherPropertyNames)
|
|
22
|
+
defineAnyIndexes(context.model, context.otherPropertyNames)
|
|
22
23
|
|
|
23
24
|
if(config.readAccess) {
|
|
24
|
-
|
|
25
|
+
defineObjectView({ ...config, access: config.readAccess }, context)
|
|
26
|
+
defineRangeViews({ ...config, access: config.readAccess }, context)
|
|
25
27
|
}
|
|
26
28
|
if(config.views) {
|
|
27
29
|
for(const view of config.views) {
|
|
28
|
-
|
|
30
|
+
defineObjectView({ ...config, ...view }, context)
|
|
29
31
|
}
|
|
30
32
|
}
|
|
31
33
|
|
package/relatedTo.js
CHANGED
|
@@ -17,7 +17,7 @@ module.exports = function(service, app) {
|
|
|
17
17
|
context.relationWord = 'Friend'
|
|
18
18
|
context.reverseRelationWord = 'Related'
|
|
19
19
|
|
|
20
|
-
defineProperties(context.model, context.others, context.otherPropertyNames)
|
|
20
|
+
context.identifiers = defineProperties(context.model, context.others, context.otherPropertyNames)
|
|
21
21
|
defineIndex(context.model, context.joinedOthersClassName, context.otherPropertyNames)
|
|
22
22
|
|
|
23
23
|
if(config.sortBy) {
|
|
@@ -44,7 +44,7 @@ module.exports = function(service, app) {
|
|
|
44
44
|
defineUpdateAction(config, context)
|
|
45
45
|
}
|
|
46
46
|
|
|
47
|
-
if(config.
|
|
47
|
+
if(config.deleteAccess || config.writeAccess) {
|
|
48
48
|
defineDeleteAction(config, context)
|
|
49
49
|
}
|
|
50
50
|
})
|
package/relatedToAny.js
CHANGED
|
@@ -17,7 +17,7 @@ module.exports = function(service, app) {
|
|
|
17
17
|
context.relationWord = 'Friend'
|
|
18
18
|
context.reverseRelationWord = 'Related'
|
|
19
19
|
|
|
20
|
-
defineAnyProperties(context.model, context.otherPropertyNames)
|
|
20
|
+
context.identifiers = defineAnyProperties(context.model, context.otherPropertyNames)
|
|
21
21
|
defineAnyIndex(context.model, context.joinedOthersClassName, context.otherPropertyNames)
|
|
22
22
|
|
|
23
23
|
if(config.sortBy) {
|
|
@@ -44,7 +44,7 @@ module.exports = function(service, app) {
|
|
|
44
44
|
defineUpdateAction(config, context)
|
|
45
45
|
}
|
|
46
46
|
|
|
47
|
-
if(config.
|
|
47
|
+
if(config.deleteAccess || config.writeAccess) {
|
|
48
48
|
defineDeleteAction(config, context)
|
|
49
49
|
}
|
|
50
50
|
})
|
|
@@ -2,9 +2,26 @@ const App = require("@live-change/framework")
|
|
|
2
2
|
const { PropertyDefinition, ViewDefinition, IndexDefinition, ActionDefinition } = App
|
|
3
3
|
const { extractTypeAndIdParts, extractIdentifiersWithTypes, generateAnyId } = require("./utilsAny.js")
|
|
4
4
|
const { extractObjectData } = require("./utils.js")
|
|
5
|
+
const { allCombinations } = require("./combinations.js")
|
|
5
6
|
|
|
7
|
+
const pluralize = require('pluralize')
|
|
6
8
|
|
|
7
|
-
function
|
|
9
|
+
function createIdentifiersProperties(keys) {
|
|
10
|
+
const identifiers = {}
|
|
11
|
+
if(keys) for(const key of keys) {
|
|
12
|
+
identifiers[key] = {
|
|
13
|
+
type: String,
|
|
14
|
+
validation: ['nonEmpty']
|
|
15
|
+
}
|
|
16
|
+
identifiers[key + 'Type'] = {
|
|
17
|
+
type: String,
|
|
18
|
+
validation: ['nonEmpty']
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
return identifiers
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function defineObjectView(config, context) {
|
|
8
25
|
const { service, modelRuntime, otherPropertyNames, joinedOthersPropertyName, joinedOthersClassName,
|
|
9
26
|
modelName, others, model } = context
|
|
10
27
|
const viewProperties = {}
|
|
@@ -38,6 +55,37 @@ function defineView(config, context) {
|
|
|
38
55
|
})
|
|
39
56
|
}
|
|
40
57
|
|
|
58
|
+
function defineRangeViews(config, context) {
|
|
59
|
+
const { service, modelRuntime, otherPropertyNames, joinedOthersPropertyName, joinedOthersClassName,
|
|
60
|
+
modelName, others, model } = context
|
|
61
|
+
const identifierCombinations = allCombinations(otherPropertyNames).slice(0, -1)
|
|
62
|
+
for(const combination of identifierCombinations) {
|
|
63
|
+
const propsUpperCase = combination.map(prop => prop[0].toUpperCase() + prop.slice(1))
|
|
64
|
+
const indexName = 'by' + combination.map(prop => prop[0].toUpperCase() + prop.slice(1))
|
|
65
|
+
const viewName = combination[0][0].toLowerCase() + combination[0].slice(1) +
|
|
66
|
+
propsUpperCase.slice(1).join('And') + context.partialReverseRelationWord + pluralize(modelName)
|
|
67
|
+
console.log("DEFINE RANGE VIEW", viewName, combination)
|
|
68
|
+
const identifiers = createIdentifiersProperties(combination)
|
|
69
|
+
service.views[viewName] = new ViewDefinition({
|
|
70
|
+
name: viewName,
|
|
71
|
+
properties: {
|
|
72
|
+
...identifiers,
|
|
73
|
+
...App.rangeProperties,
|
|
74
|
+
},
|
|
75
|
+
access(params, context) {
|
|
76
|
+
return config.access ? config.access(params, context) : true
|
|
77
|
+
},
|
|
78
|
+
daoPath(params, { client, context }) {
|
|
79
|
+
const owner = []
|
|
80
|
+
for (const key of combination) {
|
|
81
|
+
owner.push(params[key + 'Type'], params[key])
|
|
82
|
+
}
|
|
83
|
+
return modelRuntime().sortedIndexRangePath(indexName, owner, App.extractRange(params) )
|
|
84
|
+
}
|
|
85
|
+
})
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
41
89
|
function defineSetAction(config, context) {
|
|
42
90
|
const {
|
|
43
91
|
service, app, model, defaults,
|
|
@@ -139,7 +187,7 @@ function defineSetOrUpdateAction(config, context) {
|
|
|
139
187
|
|
|
140
188
|
function defineResetAction(config, context) {
|
|
141
189
|
const {
|
|
142
|
-
service, modelRuntime, modelPropertyName,
|
|
190
|
+
service, modelRuntime, modelPropertyName, identifiers,
|
|
143
191
|
otherPropertyNames, joinedOthersPropertyName, modelName, joinedOthersClassName, model
|
|
144
192
|
} = context
|
|
145
193
|
const eventName = joinedOthersPropertyName + context.reverseRelationWord + modelName + 'Reset'
|
|
@@ -150,7 +198,8 @@ function defineResetAction(config, context) {
|
|
|
150
198
|
[modelPropertyName]: {
|
|
151
199
|
type: model,
|
|
152
200
|
validation: ['nonEmpty']
|
|
153
|
-
}
|
|
201
|
+
},
|
|
202
|
+
...identifiers
|
|
154
203
|
},
|
|
155
204
|
access: config.resetAccess || config.writeAccess,
|
|
156
205
|
queuedBy: otherPropertyNames,
|
|
@@ -168,4 +217,7 @@ function defineResetAction(config, context) {
|
|
|
168
217
|
})
|
|
169
218
|
}
|
|
170
219
|
|
|
171
|
-
module.exports = {
|
|
220
|
+
module.exports = {
|
|
221
|
+
defineObjectView, defineRangeViews,
|
|
222
|
+
defineSetAction, defineUpdateAction, defineSetOrUpdateAction, defineResetAction
|
|
223
|
+
}
|
package/utils.js
CHANGED
|
@@ -9,17 +9,6 @@ function extractIdParts(otherPropertyNames, properties) {
|
|
|
9
9
|
return idParts
|
|
10
10
|
}
|
|
11
11
|
|
|
12
|
-
function extractRange(properties) {
|
|
13
|
-
return {
|
|
14
|
-
gt: properties.gt,
|
|
15
|
-
gte: properties.gte,
|
|
16
|
-
lt: properties.lt,
|
|
17
|
-
lte: properties.lte,
|
|
18
|
-
reverse: properties.reverse,
|
|
19
|
-
limit: properties.limit
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
|
|
23
12
|
function extractIdentifiers(otherPropertyNames, properties) {
|
|
24
13
|
const identifiers = {}
|
|
25
14
|
for (const propertyName of otherPropertyNames) {
|
|
@@ -45,12 +34,17 @@ function extractObjectData(writeableProperties, properties, defaults) {
|
|
|
45
34
|
}
|
|
46
35
|
|
|
47
36
|
function defineProperties(model, types, names) {
|
|
37
|
+
const identifiers = {}
|
|
48
38
|
for (let i = 0; i < types.length; i++) {
|
|
49
|
-
|
|
39
|
+
identifiers[names[i]] = new PropertyDefinition({
|
|
50
40
|
type: types[i],
|
|
51
41
|
validation: ['nonEmpty']
|
|
52
42
|
})
|
|
53
43
|
}
|
|
44
|
+
for(const key in identifiers) {
|
|
45
|
+
model.properties[key] = identifiers[key]
|
|
46
|
+
}
|
|
47
|
+
return identifiers
|
|
54
48
|
}
|
|
55
49
|
|
|
56
50
|
function defineIndex(model, what, props) {
|
|
@@ -123,5 +117,5 @@ function processModelsAnnotation(service, app, annotation, multiple, cb) {
|
|
|
123
117
|
|
|
124
118
|
module.exports = {
|
|
125
119
|
extractIdParts, extractIdentifiers, extractObjectData, defineProperties, defineIndex,
|
|
126
|
-
processModelsAnnotation,
|
|
120
|
+
processModelsAnnotation, generateId
|
|
127
121
|
}
|
package/utilsAny.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
const App = require("@live-change/framework")
|
|
2
2
|
const { PropertyDefinition, ViewDefinition, IndexDefinition, ActionDefinition, EventDefinition } = App
|
|
3
|
+
const { allCombinations } = require("./combinations.js")
|
|
3
4
|
|
|
4
5
|
function extractTypeAndIdParts(otherPropertyNames, properties) {
|
|
5
6
|
const typeAndIdParts = []
|
|
@@ -27,16 +28,21 @@ function generateAnyId(otherPropertyNames, properties) {
|
|
|
27
28
|
}
|
|
28
29
|
|
|
29
30
|
function defineAnyProperties(model, names) {
|
|
31
|
+
const identifiers = {}
|
|
30
32
|
for (let i = 0; i < names.length; i++) {
|
|
31
|
-
|
|
33
|
+
identifiers[names[i]] = new PropertyDefinition({
|
|
32
34
|
type: String,
|
|
33
35
|
validation: ['nonEmpty']
|
|
34
36
|
})
|
|
35
|
-
|
|
37
|
+
identifiers[names[i]+'Type'] = new PropertyDefinition({
|
|
36
38
|
type: String,
|
|
37
39
|
validation: ['nonEmpty']
|
|
38
40
|
})
|
|
39
41
|
}
|
|
42
|
+
for(const key in identifiers) {
|
|
43
|
+
model.properties[key] = identifiers[key]
|
|
44
|
+
}
|
|
45
|
+
return identifiers
|
|
40
46
|
}
|
|
41
47
|
|
|
42
48
|
function defineAnyIndex(model, what, props) {
|
|
@@ -45,6 +51,14 @@ function defineAnyIndex(model, what, props) {
|
|
|
45
51
|
})
|
|
46
52
|
}
|
|
47
53
|
|
|
54
|
+
function defineAnyIndexes(model, props) {
|
|
55
|
+
const propCombinations = allCombinations(props)
|
|
56
|
+
for(const propCombination of propCombinations) {
|
|
57
|
+
const upperCaseProps = propCombination.map(prop => prop[0].toUpperCase() + prop.slice(1))
|
|
58
|
+
defineAnyIndex(model, upperCaseProps.join('And'), propCombination)
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
48
62
|
function processModelsAnyAnnotation(service, app, annotation, multiple, cb) {
|
|
49
63
|
if (!service) throw new Error("no service")
|
|
50
64
|
if (!app) throw new Error("no app")
|
|
@@ -104,6 +118,7 @@ function processModelsAnyAnnotation(service, app, annotation, multiple, cb) {
|
|
|
104
118
|
}
|
|
105
119
|
|
|
106
120
|
module.exports = {
|
|
107
|
-
extractTypeAndIdParts, extractIdentifiersWithTypes, defineAnyProperties,
|
|
121
|
+
extractTypeAndIdParts, extractIdentifiersWithTypes, defineAnyProperties,
|
|
122
|
+
defineAnyIndex, defineAnyIndexes,
|
|
108
123
|
processModelsAnyAnnotation, generateAnyId
|
|
109
124
|
}
|