teamplay 0.5.0-alpha.11 → 0.5.0-alpha.12
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.
|
@@ -3,15 +3,17 @@ import type {
|
|
|
3
3
|
QuerySubscriptions,
|
|
4
4
|
QuerySignalOptions
|
|
5
5
|
} from './Query.js'
|
|
6
|
+
import type { PathSegment } from './types/path.js'
|
|
6
7
|
|
|
7
8
|
export const IS_AGGREGATION: unique symbol
|
|
8
9
|
export const AGGREGATIONS: '$aggregations'
|
|
9
10
|
export const aggregationSubscriptions: QuerySubscriptions
|
|
10
11
|
export function getAggregationSignal (collectionName: string, params: unknown, options?: QuerySignalOptions): Signal
|
|
11
12
|
export function isAggregationSignal ($signal: unknown): boolean | undefined
|
|
13
|
+
export function getAggregationRowId (row: unknown, collectionName?: string): string | undefined
|
|
12
14
|
export function getAggregationDocId (
|
|
13
|
-
segments: readonly
|
|
15
|
+
segments: readonly PathSegment[],
|
|
14
16
|
rootId?: string,
|
|
15
|
-
method?: (path:
|
|
17
|
+
method?: (path: PathSegment[]) => unknown
|
|
16
18
|
): string | undefined
|
|
17
|
-
export function getAggregationCollectionName (segments: readonly
|
|
19
|
+
export function getAggregationCollectionName (segments: readonly PathSegment[]): string | undefined
|
package/dist/orm/Aggregation.js
CHANGED
|
@@ -8,6 +8,7 @@ import { delPrivateData, getPrivateData, setPrivateData } from './privateData.js
|
|
|
8
8
|
import { setSignalRuntimeDescriptor } from "./signalRuntimeDescriptor.js";
|
|
9
9
|
export const IS_AGGREGATION = Symbol('is aggregation signal');
|
|
10
10
|
export const AGGREGATIONS = '$aggregations';
|
|
11
|
+
const DEFAULT_AGGREGATION_ID_FIELDS = ['_id', 'id'];
|
|
11
12
|
class Aggregation extends Query {
|
|
12
13
|
_initData() {
|
|
13
14
|
this._syncAllRootsData();
|
|
@@ -39,17 +40,27 @@ aggregationSubscriptions.runtimeKind = 'aggregation';
|
|
|
39
40
|
function injectAggregationIds(extra, collectionName) {
|
|
40
41
|
if (!Array.isArray(extra))
|
|
41
42
|
return;
|
|
42
|
-
const idFields =
|
|
43
|
+
const idFields = getCollectionIdFields(collectionName);
|
|
43
44
|
for (const doc of extra) {
|
|
44
45
|
if (!isPlainObject(doc))
|
|
45
46
|
continue;
|
|
46
|
-
const docId = doc
|
|
47
|
+
const docId = getAggregationRowId(doc, collectionName);
|
|
47
48
|
if (docId == null)
|
|
48
49
|
continue;
|
|
49
|
-
|
|
50
|
-
doc
|
|
51
|
-
|
|
52
|
-
|
|
50
|
+
for (const field of idFields) {
|
|
51
|
+
if (doc[field] !== docId)
|
|
52
|
+
doc[field] = docId;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
export function getAggregationRowId(row, collectionName) {
|
|
57
|
+
if (!isPlainObject(row))
|
|
58
|
+
return;
|
|
59
|
+
const idFields = getAggregationIdFields(collectionName);
|
|
60
|
+
for (const field of idFields) {
|
|
61
|
+
const value = row[field];
|
|
62
|
+
if (typeof value === 'string')
|
|
63
|
+
return value;
|
|
53
64
|
}
|
|
54
65
|
}
|
|
55
66
|
export function getAggregationSignal(collectionName, params, options) {
|
|
@@ -80,7 +91,7 @@ export function isAggregationSignal($signal) {
|
|
|
80
91
|
return true;
|
|
81
92
|
}
|
|
82
93
|
// example: ['$aggregations', '{"active":true}', 42]
|
|
83
|
-
// AND only if
|
|
94
|
+
// AND only if the aggregation row carries a source document id field
|
|
84
95
|
export function getAggregationDocId(segments, rootId, method) {
|
|
85
96
|
if (!(segments.length >= 3))
|
|
86
97
|
return;
|
|
@@ -88,15 +99,16 @@ export function getAggregationDocId(segments, rootId, method) {
|
|
|
88
99
|
return;
|
|
89
100
|
if (!(typeof segments[2] === 'number'))
|
|
90
101
|
return;
|
|
102
|
+
const collectionName = getAggregationCollectionName(segments);
|
|
103
|
+
const idFields = getAggregationIdFields(collectionName);
|
|
91
104
|
if (typeof method !== 'function') {
|
|
92
105
|
method = path => rootId == null ? getRaw(path) : getPrivateData(rootId, path);
|
|
93
106
|
}
|
|
94
|
-
const
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
return id;
|
|
107
|
+
for (const field of idFields) {
|
|
108
|
+
const id = method([...segments.slice(0, 3), field]);
|
|
109
|
+
if (typeof id === 'string')
|
|
110
|
+
return id;
|
|
111
|
+
}
|
|
100
112
|
}
|
|
101
113
|
export function getAggregationCollectionName(segments) {
|
|
102
114
|
if (!(segments.length >= 2))
|
|
@@ -117,3 +129,15 @@ function parseAggregationSignalOptions(options) {
|
|
|
117
129
|
const { root, ...signalOptions } = options;
|
|
118
130
|
return { root, signalOptions };
|
|
119
131
|
}
|
|
132
|
+
function getAggregationIdFields(collectionName) {
|
|
133
|
+
const idFields = getCollectionIdFields(collectionName);
|
|
134
|
+
return uniq(idFields.concat(DEFAULT_AGGREGATION_ID_FIELDS));
|
|
135
|
+
}
|
|
136
|
+
function getCollectionIdFields(collectionName) {
|
|
137
|
+
return collectionName
|
|
138
|
+
? getIdFieldsForSegments([collectionName, ''])
|
|
139
|
+
: [];
|
|
140
|
+
}
|
|
141
|
+
function uniq(values) {
|
|
142
|
+
return Array.from(new Set(values));
|
|
143
|
+
}
|
|
@@ -5,7 +5,7 @@ import { getRoot, ROOT, ROOT_ID, getRootSignal, GLOBAL_ROOT_ID, unregisterRootFi
|
|
|
5
5
|
import { isPrivateMutationForbidden } from "../connection.js";
|
|
6
6
|
import { docSubscriptions } from '../Doc.js';
|
|
7
7
|
import { IS_QUERY, getQuerySignal, querySubscriptions } from '../Query.js';
|
|
8
|
-
import { IS_AGGREGATION, aggregationSubscriptions, getAggregationSignal } from '../Aggregation.js';
|
|
8
|
+
import { AGGREGATIONS, IS_AGGREGATION, aggregationSubscriptions, getAggregationSignal } from '../Aggregation.js';
|
|
9
9
|
import { getIdFieldsForSegments, isIdFieldPath, isPublicDocPath, normalizeIdFields, isPlainObject } from "../idFields.js";
|
|
10
10
|
import { incrementPublic as _incrementPublic, arrayPushPublic as _arrayPushPublic, arrayUnshiftPublic as _arrayUnshiftPublic, arrayInsertPublic as _arrayInsertPublic, arrayPopPublic as _arrayPopPublic, arrayShiftPublic as _arrayShiftPublic, arrayRemovePublic as _arrayRemovePublic, arrayMovePublic as _arrayMovePublic, setPublicDocReplace as _setPublicDocReplace, stringInsertPublic as _stringInsertPublic, stringRemovePublic as _stringRemovePublic } from '../dataTree.js';
|
|
11
11
|
import { on as onCustomEvent, removeListener as removeCustomEventListener } from './eventsCompat.js';
|
|
@@ -28,6 +28,8 @@ class SignalCompat extends Signal {
|
|
|
28
28
|
return super.path();
|
|
29
29
|
}
|
|
30
30
|
getId() {
|
|
31
|
+
if (isAggregationValuePath(this[SEGMENTS]))
|
|
32
|
+
return super.getId();
|
|
31
33
|
const $target = resolveRefSignal(this);
|
|
32
34
|
if ($target !== this)
|
|
33
35
|
return $target.getId();
|
|
@@ -612,6 +614,11 @@ function readRefValue($signal) {
|
|
|
612
614
|
throw err;
|
|
613
615
|
}
|
|
614
616
|
}
|
|
617
|
+
function isAggregationValuePath(segments) {
|
|
618
|
+
return Array.isArray(segments) &&
|
|
619
|
+
segments.length >= 3 &&
|
|
620
|
+
segments[0] === AGGREGATIONS;
|
|
621
|
+
}
|
|
615
622
|
function resolveRefSignal($signal) {
|
|
616
623
|
const directTarget = resolveRefSignalSafe($signal);
|
|
617
624
|
if (directTarget && directTarget !== $signal)
|
|
@@ -36,14 +36,14 @@ export function getSignalId($signal, rootId, readPath) {
|
|
|
36
36
|
throw Error('Can\'t get the id of a collection');
|
|
37
37
|
if (isDirectPublicDocumentSegments(segments))
|
|
38
38
|
return getLeafId(segments);
|
|
39
|
+
if (segments[0] === AGGREGATIONS && segments.length === 3) {
|
|
40
|
+
return getAggregationDocId(segments, rootId, readPath);
|
|
41
|
+
}
|
|
39
42
|
if (readPath) {
|
|
40
43
|
const valueId = getValueIdFromPaths(segments, readPath);
|
|
41
44
|
if (valueId.found)
|
|
42
45
|
return valueId.id;
|
|
43
46
|
}
|
|
44
|
-
if (segments[0] === AGGREGATIONS && segments.length === 3) {
|
|
45
|
-
return getAggregationDocId(segments, rootId);
|
|
46
|
-
}
|
|
47
47
|
return getLeafId(segments);
|
|
48
48
|
}
|
|
49
49
|
export function getSignalCollection($signal) {
|
package/dist/orm/signalReads.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { AGGREGATIONS, IS_AGGREGATION } from './Aggregation.js';
|
|
1
|
+
import { AGGREGATIONS, IS_AGGREGATION, getAggregationCollectionName, getAggregationRowId } from './Aggregation.js';
|
|
2
2
|
import { HASH, IS_QUERY, QUERIES } from './Query.js';
|
|
3
3
|
import { SEGMENTS } from "./signalSymbols.js";
|
|
4
4
|
export function readSignalValue($signal, context, method, rawMethod) {
|
|
@@ -41,7 +41,8 @@ export function getSignalIds($signal, context) {
|
|
|
41
41
|
const docs = context.readPrivateData(rootId, $signal[SEGMENTS], false);
|
|
42
42
|
if (!Array.isArray(docs))
|
|
43
43
|
return [];
|
|
44
|
-
|
|
44
|
+
const collectionName = getAggregationCollectionName($signal[SEGMENTS]);
|
|
45
|
+
return docs.map(doc => getAggregationRowId(doc, collectionName)).filter(isString);
|
|
45
46
|
}
|
|
46
47
|
context.error('Signal.getIds() can only be used on query signals or aggregation signals. ' +
|
|
47
48
|
'Received a regular signal: ' + JSON.stringify($signal[SEGMENTS]));
|
|
@@ -55,13 +56,6 @@ export function isAggregationValueSignal($signal) {
|
|
|
55
56
|
const segments = $signal[SEGMENTS];
|
|
56
57
|
return segments.length >= 2 && segments[0] === AGGREGATIONS;
|
|
57
58
|
}
|
|
58
|
-
function getAggregationRowId(doc) {
|
|
59
|
-
const row = doc;
|
|
60
|
-
if (typeof row?._id === 'string')
|
|
61
|
-
return row._id;
|
|
62
|
-
if (typeof row?.id === 'string')
|
|
63
|
-
return row.id;
|
|
64
|
-
}
|
|
65
59
|
function isString(value) {
|
|
66
60
|
return typeof value === 'string';
|
|
67
61
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "teamplay",
|
|
3
|
-
"version": "0.5.0-alpha.
|
|
3
|
+
"version": "0.5.0-alpha.12",
|
|
4
4
|
"description": "Full-stack signals ORM with multiplayer",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -134,5 +134,5 @@
|
|
|
134
134
|
]
|
|
135
135
|
},
|
|
136
136
|
"license": "MIT",
|
|
137
|
-
"gitHead": "
|
|
137
|
+
"gitHead": "13e07eda7f31ee883390b3ad04bdc6ecfbcb7f4c"
|
|
138
138
|
}
|