@trullock/firebase-common 1.2.0 → 1.3.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.
- package/package.json +1 -1
- package/src/automapper.js +2 -2
- package/tests/automapper.test.js +23 -3
package/package.json
CHANGED
package/src/automapper.js
CHANGED
|
@@ -4,7 +4,7 @@ let mappings = [];
|
|
|
4
4
|
/**
|
|
5
5
|
*
|
|
6
6
|
* @param {function} toFirestore A function of the form (key: string, value: object/primitive) => object/primitive. Return a mapped value or undefined to fallback to default mapping
|
|
7
|
-
* @param {function} fromFirestore A function of the form (key: string, value: object/primitive) => object/primitive. Return a mapped value or undefined to fallback to default mapping
|
|
7
|
+
* @param {function} fromFirestore A function of the form (key: string, value: object/primitive, dest: object) => object/primitive. Return a mapped value or undefined to fallback to default mapping
|
|
8
8
|
*/
|
|
9
9
|
export function registerMapping(toFirestore, fromFirestore)
|
|
10
10
|
{
|
|
@@ -237,7 +237,7 @@ export function autoMapFromFirestore(obj)
|
|
|
237
237
|
let handled = false;
|
|
238
238
|
for(let mapping of mappings)
|
|
239
239
|
{
|
|
240
|
-
let result = mapping.fromFirestore(key, value)
|
|
240
|
+
let result = mapping.fromFirestore(key, value, dest)
|
|
241
241
|
if(result !== undefined)
|
|
242
242
|
{
|
|
243
243
|
dest[key] = result;
|
package/tests/automapper.test.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import test from "node:test";
|
|
2
2
|
import assert from "node:assert";
|
|
3
|
-
import { autoMapFromFirestore, autoMapToFirestore, registerClassForPersistence } from "../src/automapper.js";
|
|
3
|
+
import { autoMapFromFirestore, autoMapToFirestore, registerClassForPersistence, registerMapping } from "../src/automapper.js";
|
|
4
4
|
import { Mixins } from "../src/utils.js";
|
|
5
5
|
|
|
6
6
|
class AggregateRoot
|
|
@@ -70,6 +70,10 @@ class Baz extends Foo {
|
|
|
70
70
|
|
|
71
71
|
class Academy extends Mixins([Baz, AggregateRoot])
|
|
72
72
|
{
|
|
73
|
+
static Test = class {}
|
|
74
|
+
|
|
75
|
+
uniqueList = new Set();
|
|
76
|
+
|
|
73
77
|
changeName(name)
|
|
74
78
|
{
|
|
75
79
|
this.apply('academyNameChanged', {
|
|
@@ -86,9 +90,11 @@ class Academy extends Mixins([Baz, AggregateRoot])
|
|
|
86
90
|
{
|
|
87
91
|
super();
|
|
88
92
|
this.bar = new Baz.Bar();
|
|
93
|
+
this.uniqueList.add('a')
|
|
94
|
+
this.uniqueList.add('b')
|
|
95
|
+
this.uniqueList.add('b')
|
|
89
96
|
}
|
|
90
97
|
|
|
91
|
-
static Test = class {}
|
|
92
98
|
}
|
|
93
99
|
|
|
94
100
|
test('test', t => {
|
|
@@ -107,6 +113,17 @@ test('test', t => {
|
|
|
107
113
|
test('automapping', t => {
|
|
108
114
|
registerClassForPersistence(Academy);
|
|
109
115
|
|
|
116
|
+
registerMapping((key, value) => {
|
|
117
|
+
if(value instanceof Set)
|
|
118
|
+
return Array.from(value)
|
|
119
|
+
return undefined;
|
|
120
|
+
}, (key, value, dest) => {
|
|
121
|
+
if(dest[key] instanceof Set)
|
|
122
|
+
return new Set(value);
|
|
123
|
+
return undefined;
|
|
124
|
+
})
|
|
125
|
+
|
|
126
|
+
|
|
110
127
|
let a = new Academy();
|
|
111
128
|
let mapped = autoMapToFirestore(a);
|
|
112
129
|
let unmapped = autoMapFromFirestore(mapped);
|
|
@@ -120,11 +137,14 @@ test('automapping', t => {
|
|
|
120
137
|
assert.equal(unmapped.bar.a, 1)
|
|
121
138
|
|
|
122
139
|
assert.equal(a.bar.barzy(), "barzy")
|
|
123
|
-
assert.equal(unmapped.bar.barzy(), "barzy")
|
|
140
|
+
assert.equal(unmapped.bar.barzy(), "barzy");
|
|
124
141
|
|
|
125
142
|
assert.equal(a.foozzy(), "foozzy")
|
|
126
143
|
assert.equal(unmapped.foozzy(), "foozzy")
|
|
127
144
|
|
|
128
145
|
assert.equal(a.bazzy(), "bazzy")
|
|
129
146
|
assert.equal(unmapped.bazzy(), "bazzy")
|
|
147
|
+
|
|
148
|
+
assert(a.uniqueList instanceof Set)
|
|
149
|
+
assert.equal(unmapped.uniqueList.size, 2)
|
|
130
150
|
})
|