@live-change/relations-plugin 0.6.0 → 0.6.1

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.
@@ -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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@live-change/relations-plugin",
3
- "version": "0.6.0",
3
+ "version": "0.6.1",
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.6.0",
24
+ "@live-change/framework": "^0.6.1",
25
25
  "pluralize": "8.0.0"
26
26
  },
27
- "gitHead": "5e4250b3a6fcf11e46f36e6b9713c2a9f4a4dc92"
27
+ "gitHead": "64a9e476bdeec727ff5b8bedbaef65078f1b32f2"
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 { extractRange, extractObjectData } = require("./utils.js")
4
+ const { extractObjectData } = require("./utils.js")
5
+ const { extractRange } = App
5
6
 
6
7
  const pluralize = require('pluralize')
7
8
 
package/propertyOfAny.js CHANGED
@@ -1,5 +1,5 @@
1
1
  const {
2
- defineAnyProperties, defineAnyIndex,
2
+ defineAnyProperties, defineAnyIndex, 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
- defineView, defineSetAction, defineUpdateAction, defineSetOrUpdateAction, defineResetAction
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
21
  context.identifiers = defineAnyProperties(context.model, context.otherPropertyNames)
21
- defineAnyIndex(context.model, context.joinedOthersClassName, context.otherPropertyNames)
22
+ defineAnyIndexes(context.model, context.otherPropertyNames)
22
23
 
23
24
  if(config.readAccess) {
24
- defineView({ ...config, access: config.readAccess }, context)
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
- defineView({ ...config, ...view }, context)
30
+ defineObjectView({ ...config, ...view }, context)
29
31
  }
30
32
  }
31
33
 
@@ -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 defineView(config, context) {
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 = { defineView, defineSetAction, defineUpdateAction, defineSetOrUpdateAction, defineResetAction }
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) {
@@ -128,5 +117,5 @@ function processModelsAnnotation(service, app, annotation, multiple, cb) {
128
117
 
129
118
  module.exports = {
130
119
  extractIdParts, extractIdentifiers, extractObjectData, defineProperties, defineIndex,
131
- processModelsAnnotation, extractRange, generateId
120
+ processModelsAnnotation, generateId
132
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 = []
@@ -50,6 +51,14 @@ function defineAnyIndex(model, what, props) {
50
51
  })
51
52
  }
52
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
+
53
62
  function processModelsAnyAnnotation(service, app, annotation, multiple, cb) {
54
63
  if (!service) throw new Error("no service")
55
64
  if (!app) throw new Error("no app")
@@ -109,6 +118,7 @@ function processModelsAnyAnnotation(service, app, annotation, multiple, cb) {
109
118
  }
110
119
 
111
120
  module.exports = {
112
- extractTypeAndIdParts, extractIdentifiersWithTypes, defineAnyProperties, defineAnyIndex,
121
+ extractTypeAndIdParts, extractIdentifiersWithTypes, defineAnyProperties,
122
+ defineAnyIndex, defineAnyIndexes,
113
123
  processModelsAnyAnnotation, generateAnyId
114
124
  }