nexabase-console 2.1.9 → 2.2.0
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.
|
@@ -2,10 +2,16 @@ export declare const FIELD_VALUE_BRAND: unique symbol;
|
|
|
2
2
|
export type FieldValueMethod = 'serverTimestamp' | 'increment' | 'arrayUnion' | 'arrayRemove' | 'deleteField';
|
|
3
3
|
export declare class FieldValue {
|
|
4
4
|
readonly [FIELD_VALUE_BRAND] = true;
|
|
5
|
+
readonly _isFieldValue: boolean;
|
|
5
6
|
readonly method: FieldValueMethod;
|
|
6
7
|
readonly value?: any;
|
|
7
8
|
constructor(method: FieldValueMethod, value?: any);
|
|
8
9
|
static isFieldValue(val: any): val is FieldValue;
|
|
10
|
+
toJSON(): {
|
|
11
|
+
_isFieldValue: boolean;
|
|
12
|
+
method: FieldValueMethod;
|
|
13
|
+
value: any;
|
|
14
|
+
};
|
|
9
15
|
isEqual(other: any): boolean;
|
|
10
16
|
}
|
|
11
17
|
export declare const serverTimestamp: () => FieldValue;
|
|
@@ -7,14 +7,27 @@ exports.FIELD_VALUE_BRAND = Symbol.for('nexabase.fieldValue');
|
|
|
7
7
|
class FieldValue {
|
|
8
8
|
constructor(method, value) {
|
|
9
9
|
this[_a] = true;
|
|
10
|
+
this._isFieldValue = true;
|
|
10
11
|
this.method = method;
|
|
11
12
|
this.value = value;
|
|
13
|
+
this._isFieldValue = true;
|
|
12
14
|
Object.freeze(this);
|
|
13
15
|
}
|
|
14
16
|
static isFieldValue(val) {
|
|
15
17
|
return (val !== null &&
|
|
16
18
|
typeof val === 'object' &&
|
|
17
|
-
(val instanceof FieldValue ||
|
|
19
|
+
(val instanceof FieldValue ||
|
|
20
|
+
val[exports.FIELD_VALUE_BRAND] === true ||
|
|
21
|
+
val._isFieldValue === true ||
|
|
22
|
+
(typeof val.method === 'string' &&
|
|
23
|
+
['serverTimestamp', 'increment', 'arrayUnion', 'arrayRemove', 'deleteField'].includes(val.method))));
|
|
24
|
+
}
|
|
25
|
+
toJSON() {
|
|
26
|
+
return {
|
|
27
|
+
_isFieldValue: true,
|
|
28
|
+
method: this.method,
|
|
29
|
+
value: this.value !== undefined ? this.value : undefined
|
|
30
|
+
};
|
|
18
31
|
}
|
|
19
32
|
isEqual(other) {
|
|
20
33
|
if (!FieldValue.isFieldValue(other))
|
package/dist/utils/helpers.js
CHANGED
|
@@ -5,6 +5,7 @@ exports.deepEqual = deepEqual;
|
|
|
5
5
|
exports.applyDataTransforms = applyDataTransforms;
|
|
6
6
|
exports.calculateDocChanges = calculateDocChanges;
|
|
7
7
|
exports.generateIdempotencyKey = generateIdempotencyKey;
|
|
8
|
+
const FieldValue_1 = require("../firestore/FieldValue");
|
|
8
9
|
const Timestamp_1 = require("../firestore/Timestamp");
|
|
9
10
|
function getNestedValue(obj, path, docId) {
|
|
10
11
|
if (!obj || !path)
|
|
@@ -46,34 +47,61 @@ function deepEqual(a, b) {
|
|
|
46
47
|
return true;
|
|
47
48
|
}
|
|
48
49
|
function applyDataTransforms(existingData, updateData) {
|
|
49
|
-
|
|
50
|
+
if (!updateData || typeof updateData !== 'object')
|
|
51
|
+
return existingData || {};
|
|
52
|
+
const result = { ...(existingData || {}) };
|
|
50
53
|
const processNode = (obj, keyPath, value) => {
|
|
54
|
+
if (!obj || typeof obj !== 'object')
|
|
55
|
+
return;
|
|
51
56
|
const current = keyPath[0];
|
|
52
57
|
if (keyPath.length === 1) {
|
|
53
|
-
if (value
|
|
54
|
-
|
|
55
|
-
|
|
58
|
+
if (FieldValue_1.FieldValue.isFieldValue(value) ||
|
|
59
|
+
(value &&
|
|
60
|
+
typeof value === 'object' &&
|
|
61
|
+
(value._isFieldValue ||
|
|
62
|
+
['deleteField', 'serverTimestamp', 'increment', 'arrayUnion', 'arrayRemove'].includes(value.method)))) {
|
|
63
|
+
const method = value.method;
|
|
64
|
+
const val = value.value;
|
|
65
|
+
if (method === 'serverTimestamp') {
|
|
56
66
|
obj[current] = Timestamp_1.Timestamp.now();
|
|
57
67
|
}
|
|
58
|
-
else if (
|
|
59
|
-
obj[current] = (typeof obj[current] === 'number' ? obj[current] : 0) + (
|
|
68
|
+
else if (method === 'increment') {
|
|
69
|
+
obj[current] = (typeof obj[current] === 'number' ? obj[current] : 0) + (typeof val === 'number' ? val : 1);
|
|
60
70
|
}
|
|
61
|
-
else if (
|
|
71
|
+
else if (method === 'arrayUnion') {
|
|
62
72
|
const arr = Array.isArray(obj[current]) ? obj[current] : [];
|
|
63
|
-
|
|
73
|
+
const toAdd = Array.isArray(val) ? val : (val !== undefined ? [val] : []);
|
|
74
|
+
const existingSet = new Set(arr.map((item) => (typeof item === 'object' ? JSON.stringify(item) : item)));
|
|
75
|
+
for (const item of toAdd) {
|
|
76
|
+
const itemKey = typeof item === 'object' ? JSON.stringify(item) : item;
|
|
77
|
+
if (!existingSet.has(itemKey)) {
|
|
78
|
+
existingSet.add(itemKey);
|
|
79
|
+
arr.push(item);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
obj[current] = arr;
|
|
64
83
|
}
|
|
65
|
-
else if (
|
|
84
|
+
else if (method === 'arrayRemove') {
|
|
66
85
|
const arr = Array.isArray(obj[current]) ? obj[current] : [];
|
|
67
|
-
const toRemove =
|
|
68
|
-
|
|
86
|
+
const toRemove = Array.isArray(val) ? val : (val !== undefined ? [val] : []);
|
|
87
|
+
const removeSet = new Set(toRemove.map((item) => (typeof item === 'object' ? JSON.stringify(item) : item)));
|
|
88
|
+
obj[current] = arr.filter((x) => {
|
|
89
|
+
const itemKey = typeof x === 'object' ? JSON.stringify(x) : x;
|
|
90
|
+
return !removeSet.has(itemKey);
|
|
91
|
+
});
|
|
69
92
|
}
|
|
70
|
-
else if (
|
|
93
|
+
else if (method === 'deleteField') {
|
|
71
94
|
delete obj[current];
|
|
72
95
|
}
|
|
73
96
|
}
|
|
74
|
-
else if (value &&
|
|
75
|
-
|
|
97
|
+
else if (value &&
|
|
98
|
+
typeof value === 'object' &&
|
|
99
|
+
!Array.isArray(value) &&
|
|
100
|
+
typeof value.isEqual !== 'function' &&
|
|
101
|
+
!(value instanceof Timestamp_1.Timestamp)) {
|
|
102
|
+
if (!obj[current] || typeof obj[current] !== 'object' || Array.isArray(obj[current])) {
|
|
76
103
|
obj[current] = {};
|
|
104
|
+
}
|
|
77
105
|
for (const [k, v] of Object.entries(value)) {
|
|
78
106
|
processNode(obj[current], [k], v);
|
|
79
107
|
}
|
|
@@ -83,8 +111,13 @@ function applyDataTransforms(existingData, updateData) {
|
|
|
83
111
|
}
|
|
84
112
|
}
|
|
85
113
|
else {
|
|
86
|
-
if (!obj[current] || typeof obj[current] !== 'object')
|
|
114
|
+
if (!obj[current] || typeof obj[current] !== 'object' || Array.isArray(obj[current])) {
|
|
115
|
+
const isDelete = FieldValue_1.FieldValue.isFieldValue(value) ||
|
|
116
|
+
(value && typeof value === 'object' && value.method === 'deleteField');
|
|
117
|
+
if (isDelete)
|
|
118
|
+
return;
|
|
87
119
|
obj[current] = {};
|
|
120
|
+
}
|
|
88
121
|
processNode(obj[current], keyPath.slice(1), value);
|
|
89
122
|
}
|
|
90
123
|
};
|
package/package.json
CHANGED