teamplay 0.3.4 → 0.3.6

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 (2) hide show
  1. package/orm/sub.js +37 -4
  2. package/package.json +8 -8
package/orm/sub.js CHANGED
@@ -3,6 +3,7 @@ import Signal, { SEGMENTS, isPublicCollectionSignal, isPublicDocumentSignal } fr
3
3
  import { docSubscriptions } from './Doc.js'
4
4
  import { querySubscriptions, getQuerySignal } from './Query.js'
5
5
  import { aggregationSubscriptions, getAggregationSignal } from './Aggregation.js'
6
+ import isServer from '../utils/isServer.js'
6
7
 
7
8
  export default function sub ($signal, params) {
8
9
  // TODO: temporarily disable support for multiple subscriptions
@@ -20,9 +21,7 @@ export default function sub ($signal, params) {
20
21
  if (arguments.length !== 2) throw Error(ERRORS.subQueryArguments(...arguments))
21
22
  return query$($signal[SEGMENTS][0], params)
22
23
  } else if (isClientAggregationFunction($signal)) {
23
- params = $signal(sanitizeAggregationParams(params))
24
- if (Array.isArray(params)) params = { $aggregate: params }
25
- return aggregation$($signal.collection, params)
24
+ return getAggregationFromFunction($signal, $signal.collection, params)
26
25
  } else if (isAggregationHeader($signal)) {
27
26
  params = {
28
27
  $aggregationName: $signal.name,
@@ -30,7 +29,15 @@ export default function sub ($signal, params) {
30
29
  }
31
30
  return aggregation$($signal.collection, params)
32
31
  } else if (isAggregationFunction($signal)) {
33
- throw Error(ERRORS.gotAggregationFunction($signal))
32
+ if (isServer) {
33
+ if (!params?.$collection) throw Error(ERRORS.subServerAggregationCollection($signal, params))
34
+ params = { ...params }
35
+ const collection = params.$collection
36
+ delete params.$collection
37
+ return getAggregationFromFunction($signal, collection, params)
38
+ } else {
39
+ throw Error(ERRORS.gotAggregationFunction($signal))
40
+ }
34
41
  } else if (typeof $signal === 'function' && !($signal instanceof Signal)) {
35
42
  return api$($signal, params)
36
43
  } else {
@@ -38,6 +45,21 @@ export default function sub ($signal, params) {
38
45
  }
39
46
  }
40
47
 
48
+ function getAggregationFromFunction (fn, collection, params) {
49
+ params = sanitizeAggregationParams(params) // clones it, so mutation becomes safe
50
+ let session
51
+ if (params.$session) {
52
+ session = params.$session
53
+ delete params.$session
54
+ }
55
+ session ??= {}
56
+ // should match the context in @teamplay/backend/features/serverAggregate.js
57
+ const context = { collection, session, isServer }
58
+ params = fn(params, context)
59
+ if (Array.isArray(params)) params = { $aggregate: params }
60
+ return aggregation$(collection, params)
61
+ }
62
+
41
63
  function doc$ ($doc) {
42
64
  const promise = docSubscriptions.subscribe($doc)
43
65
  if (!promise) return $doc
@@ -104,5 +126,16 @@ const ERRORS = {
104
126
 
105
127
  Got:
106
128
  ${aggregationFn.toString()}
129
+ `,
130
+ subServerAggregationCollection: ($signal, params) => `
131
+ sub($$aggregation, params):
132
+ Server-side aggregation function must receive the collection name from the params.
133
+ Make sure you pass the collection name as $collection in the params object
134
+ when running aggregation from the server code:
135
+ sub($$aggregation, { $collection: 'collectionName', ...actualParams })
136
+
137
+ Got:
138
+ Aggregation: ${$signal}
139
+ Params: ${params}
107
140
  `
108
141
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "teamplay",
3
- "version": "0.3.4",
3
+ "version": "0.3.6",
4
4
  "description": "Full-stack signals ORM with multiplayer",
5
5
  "type": "module",
6
6
  "main": "index.js",
@@ -23,12 +23,12 @@
23
23
  },
24
24
  "dependencies": {
25
25
  "@nx-js/observer-util": "^4.1.3",
26
- "@teamplay/backend": "^0.3.4",
27
- "@teamplay/cache": "^0.3.4",
28
- "@teamplay/channel": "^0.3.4",
29
- "@teamplay/debug": "^0.3.4",
30
- "@teamplay/schema": "^0.3.4",
31
- "@teamplay/utils": "^0.3.4",
26
+ "@teamplay/backend": "^0.3.6",
27
+ "@teamplay/cache": "^0.3.6",
28
+ "@teamplay/channel": "^0.3.6",
29
+ "@teamplay/debug": "^0.3.6",
30
+ "@teamplay/schema": "^0.3.6",
31
+ "@teamplay/utils": "^0.3.6",
32
32
  "diff-match-patch": "^1.0.5",
33
33
  "events": "^3.3.0",
34
34
  "json0-ot-diff": "^1.1.2",
@@ -63,5 +63,5 @@
63
63
  ]
64
64
  },
65
65
  "license": "MIT",
66
- "gitHead": "108c0d37aea7cbe37686fafaf129e941937d7707"
66
+ "gitHead": "ea4cc604f47a48c67579af40eb1e719091bc2bcb"
67
67
  }