@live-change/vote-service 0.3.31 → 0.3.40

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.
Files changed (3) hide show
  1. package/LICENSE.md +11 -0
  2. package/index.js +215 -0
  3. package/package.json +4 -4
package/LICENSE.md ADDED
@@ -0,0 +1,11 @@
1
+ Copyright 2019-2022 Michał Łaszczewski
2
+
3
+ Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
4
+
5
+ 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
6
+
7
+ 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
8
+
9
+ 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
10
+
11
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package/index.js CHANGED
@@ -22,6 +22,221 @@ const Vote = definition.model({
22
22
  }*/],
23
23
  defaultValue: 0
24
24
  }
25
+ },
26
+ indexes: {
27
+ byOnExtended: {
28
+ property: ['onType', 'on'],
29
+ function: async function(input, output) {
30
+ function mapper(obj) {
31
+ return obj && {
32
+ id: `"${obj.onType}":"${obj.on}"_${obj.id}`,
33
+ onType: obj.onType,
34
+ on: obj.on,
35
+ to: obj.id,
36
+ power: obj.power
37
+ }
38
+ }
39
+ await input.table('vote_Vote').onChange(
40
+ (obj, oldObj) => output.change(mapper(obj), mapper(oldObj))
41
+ )
42
+ }
43
+ },
44
+ voteCount: {
45
+ property: ['onType', 'on'],
46
+ //dependsOn: ['byOn'],
47
+ function: async function(input, output) {
48
+ const sourceIndex = await input.index('vote_Vote_byOnExtended')
49
+ await sourceIndex.onChange(
50
+ async (obj, oldObj) => {
51
+ const prefix = ((obj && obj.id) || (oldObj && oldObj.id)).split('"_"')[0]+'"'
52
+ if(!prefix) {
53
+ output.debug("NO PREFIX FOR CHANGE", obj, oldObj)
54
+ return
55
+ }
56
+ output.debug("PREFIX", prefix.split(':'))
57
+ const [onType, on] = prefix.split(':').map(x => JSON.parse(x))
58
+ const votes = await sourceIndex.get({
59
+ gt: prefix + '_',
60
+ lt: prefix + '_\xFF',
61
+ limit: 1000
62
+ })
63
+ let voteSum = 0
64
+ for(const vote of votes) {
65
+ voteSum += vote.power || 0
66
+ }
67
+ output.put({
68
+ id: prefix,
69
+ onType, on,
70
+ sum: voteSum,
71
+ count: votes.length
72
+ })
73
+ }
74
+ )
75
+ }
76
+ },
77
+ byTypeAndSum: {
78
+ function: async function(input, output) {
79
+ function mapper(obj) {
80
+ return obj && {
81
+ id: `${JSON.stringify(obj.onType)}:${obj.sum.toFixed(3).padStart(12, '0')}:${JSON.stringify(obj.on)}`,
82
+ onType: obj.onType,
83
+ on: obj.on,
84
+ sum: obj.sum,
85
+ count: obj.count,
86
+ }
87
+ }
88
+ await input.index('vote_Vote_voteCount').onChange(
89
+ (obj, oldObj) => output.change(mapper(obj), mapper(oldObj))
90
+ )
91
+ }
92
+ },
93
+ byTypeAndCount: {
94
+ function: async function(input, output) {
95
+ function mapper(obj) {
96
+ return obj && {
97
+ id: `${JSON.stringify(obj.onType)}:${obj.count.toFixed().padStart(8, '0')}:${JSON.stringify(obj.on)}`,
98
+ onType: obj.onType,
99
+ on: obj.on,
100
+ sum: obj.sum,
101
+ count: obj.count,
102
+ }
103
+ }
104
+ await input.index('vote_Vote_voteCount').onChange(
105
+ (obj, oldObj) => output.change(mapper(obj), mapper(oldObj))
106
+ )
107
+ }
108
+ },
109
+ byTypeAndAvg: {
110
+ function: async function(input, output) {
111
+ function mapper(obj) {
112
+ return obj && obj.count > 0 && {
113
+ id: `${JSON.stringify(obj.onType)}:${(obj.sum / obj.count).toFixed(3).padStart(12, '0')}:${JSON.stringify(obj.on)}`,
114
+ onType: obj.onType,
115
+ on: obj.on,
116
+ sum: obj.sum,
117
+ count: obj.count,
118
+ }
119
+ }
120
+ await input.index('vote_Vote_voteCount').onChange(
121
+ (obj, oldObj) => output.change(mapper(obj), mapper(oldObj))
122
+ )
123
+ }
124
+ }
125
+ }
126
+ })
127
+
128
+ const countType = {
129
+ type: Object,
130
+ properties: {
131
+ onType: {
132
+ type: String
133
+ },
134
+ on: {
135
+ type: String
136
+ },
137
+ sum: {
138
+ type: Number
139
+ },
140
+ count: {
141
+ type: Number
142
+ }
143
+ }
144
+ }
145
+
146
+ definition.view({
147
+ name: "votesSum",
148
+ properties: {
149
+ onType: {
150
+ type: String,
151
+ validation: ['nonEmpty']
152
+ },
153
+ on: {
154
+ type: String,
155
+ validation: ['nonEmpty']
156
+ }
157
+ },
158
+ returns: countType,
159
+ async daoPath({ onType, on }, { client, service }, method) {
160
+ const id = `${JSON.stringify(onType)}:${JSON.stringify(on)}`
161
+ return ['database', 'indexObject', app.databaseName, 'vote_Vote_voteCount', id]
162
+ }
163
+ })
164
+
165
+ definition.view({
166
+ name: "votesByTypeAndSum",
167
+ properties: {
168
+ onType: {
169
+ type: String,
170
+ validation: ['nonEmpty']
171
+ },
172
+ ...App.rangeProperties
173
+ },
174
+ returns: {
175
+ type: Array,
176
+ of: countType
177
+ },
178
+ async daoPath(props , { client, service }, method) {
179
+ const { onType } = props
180
+ const range = App.utils.extractRange(props)
181
+ const prefix = `${JSON.stringify(onType)}`
182
+ const prefixedRange = App.utils.prefixRange(range, prefix, prefix)
183
+ //console.log("PREF RANGE", prefixedRange)
184
+ const daoPath = ['database', 'indexRange', app.databaseName, 'vote_Vote_byTypeAndSum', prefixedRange]
185
+ //console.log("DAO PATH", daoPath)
186
+ //console.log("OBJECTS", app.dao.get(daoPath))
187
+ return daoPath
188
+ }
189
+ })
190
+
191
+ definition.view({
192
+ name: "votesByTypeAndCount",
193
+ properties: {
194
+ onType: {
195
+ type: String,
196
+ validation: ['nonEmpty']
197
+ },
198
+ ...App.rangeProperties
199
+ },
200
+ returns: {
201
+ type: Array,
202
+ of: countType
203
+ },
204
+ async daoPath(props , { client, service }, method) {
205
+ const { onType } = props
206
+ const range = App.utils.extractRange(props)
207
+ const prefix = `${JSON.stringify(onType)}`
208
+ const prefixedRange = App.utils.prefixRange(range, prefix, prefix)
209
+ //console.log("PREF RANGE", prefixedRange)
210
+ const daoPath = ['database', 'indexRange', app.databaseName, 'vote_Vote_byTypeAndCount', prefixedRange]
211
+ //console.log("DAO PATH", daoPath)
212
+ //console.log("OBJECTS", app.dao.get(daoPath))
213
+ return daoPath
214
+ }
215
+ })
216
+
217
+ definition.view({
218
+ name: "votesByTypeAndAvg",
219
+ properties: {
220
+ onType: {
221
+ type: String,
222
+ validation: ['nonEmpty']
223
+ },
224
+ ...App.rangeProperties
225
+ },
226
+ returns: {
227
+ type: Array,
228
+ of: countType
229
+ },
230
+ async daoPath(props , { client, service }, method) {
231
+ const { onType } = props
232
+ const range = App.utils.extractRange(props)
233
+ const prefix = `${JSON.stringify(onType)}`
234
+ const prefixedRange = App.utils.prefixRange(range, prefix, prefix)
235
+ //console.log("PREF RANGE", prefixedRange)
236
+ const daoPath = ['database', 'indexRange', app.databaseName, 'vote_Vote_byTypeAndAvg', prefixedRange]
237
+ //console.log("DAO PATH", daoPath)
238
+ //console.log("OBJECTS", app.dao.get(daoPath))
239
+ return daoPath
25
240
  }
26
241
  })
27
242
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@live-change/vote-service",
3
- "version": "0.3.31",
3
+ "version": "0.3.40",
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.7.34",
25
- "@live-change/relations-plugin": "0.7.34"
24
+ "@live-change/framework": "0.7.38",
25
+ "@live-change/relations-plugin": "0.7.38"
26
26
  },
27
- "gitHead": "ab367db8f3b8d25d19adcb6cdc15a9c9e25c6e60"
27
+ "gitHead": "d72cc68ee4c46242e5f6a51c303be4177a1c33cf"
28
28
  }