Skip to content

Commit 19580c4

Browse files
committed
enhance: ImmutableJS always denormalizes to normal JS
1 parent 78574b1 commit 19580c4

File tree

13 files changed

+136
-156
lines changed

13 files changed

+136
-156
lines changed

.changeset/brave-bats-itch.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
'@data-client/normalizr': minor
3+
---
4+
5+
BREAKING CHANGE: Denormalize always transforms immutablejs entities into the class
6+
7+
Previously using ImmutableJS structures when calling denormalize() would maintain
8+
nested schemas as immutablejs structures still. Now everything is converted to normal JS.
9+
This is how the types have always been specified.

docs/core/concepts/normalization.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -392,15 +392,15 @@ xychart-beta
392392
title "Denormalize Single Entity"
393393
x-axis [normalizr, "Data Client", "Data Client (cached)"]
394394
y-axis "Operations per second" 0 --> 5618500
395-
bar [212500, 1288500, 5618500]
395+
bar [212500, 1341000, 5618500]
396396
```
397397

398398
```mermaid
399399
xychart-beta
400400
title "Denormalize Large List"
401401
x-axis [normalizr, "Data Client", "Data Client (cached)"]
402402
y-axis "Operations per second" 0 --> 12962
403-
bar [1151, 1807, 13182]
403+
bar [1151, 1986, 13182]
404404
```
405405

406406
</Grid>

examples/benchmark/old-normalizr/normalizr.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,6 @@ function mergeWithStore({ entities, result }, storeState) {
4646

4747
let curState = state;
4848
export default function addNormlizrSuite(suite) {
49-
%OptimizeFunctionOnNextCall(normalize);
50-
%OptimizeFunctionOnNextCall(denormalize);
5149
return suite
5250
.add('normalizeLong', () => {
5351
return mergeWithStore(normalize(data, ProjectSchema), state);
@@ -63,7 +61,7 @@ export default function addNormlizrSuite(suite) {
6361
})
6462
.add('denormalizeShort donotcache 500x', () => {
6563
for (let i = 0; i < 500; ++i) {
66-
const user = denormalize('gnoff', User, githubState.entities);
64+
var user = denormalize('gnoff', User, githubState.entities);
6765
// legacy normalizr doesn't convert this for us, so we must do manually afterward.
6866
user.createdAt = new Date(user.createdAt);
6967
user.updatedAt = new Date(user.updatedAt);

packages/endpoint/src/schemas/__tests__/Entity.test.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -812,10 +812,8 @@ describe(`${Entity.name} denormalization`, () => {
812812
},
813813
};
814814

815-
expect(denormalize(Menu, '1', entities)).toMatchSnapshot();
816815
expect(denormalize(Menu, '1', fromJS(entities))).toMatchSnapshot();
817816

818-
expect(denormalize(Menu, '2', entities)).toMatchSnapshot();
819817
expect(denormalize(Menu, '2', fromJS(entities))).toMatchSnapshot();
820818
});
821819

packages/endpoint/src/schemas/__tests__/EntityMixin.test.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -948,10 +948,8 @@ describe(`${schema.Entity.name} denormalization`, () => {
948948
},
949949
};
950950

951-
expect(denormalize(Menu, '1', entities)).toMatchSnapshot();
952951
expect(denormalize(Menu, '1', fromJS(entities))).toMatchSnapshot();
953952

954-
expect(denormalize(Menu, '2', entities)).toMatchSnapshot();
955953
expect(denormalize(Menu, '2', fromJS(entities))).toMatchSnapshot();
956954
});
957955

packages/endpoint/src/schemas/__tests__/__snapshots__/Entity.test.ts.snap

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -60,22 +60,6 @@ Menu {
6060
`;
6161

6262
exports[`Entity denormalization denormalizes deep entities with records 2`] = `
63-
Menu {
64-
"food": Food {
65-
"id": "1",
66-
},
67-
"id": "1",
68-
}
69-
`;
70-
71-
exports[`Entity denormalization denormalizes deep entities with records 3`] = `
72-
Menu {
73-
"food": null,
74-
"id": "2",
75-
}
76-
`;
77-
78-
exports[`Entity denormalization denormalizes deep entities with records 4`] = `
7963
Menu {
8064
"food": null,
8165
"id": "2",
@@ -107,15 +91,15 @@ exports[`Entity denormalization denormalizes recursive dependencies 2`] = `
10791
Report {
10892
"draftedBy": User {
10993
"id": "456",
110-
"reports": Immutable.List [
94+
"reports": [
11195
[Circular],
11296
],
11397
"role": "manager",
11498
},
11599
"id": "123",
116100
"publishedBy": User {
117101
"id": "456",
118-
"reports": Immutable.List [
102+
"reports": [
119103
[Circular],
120104
],
121105
"role": "manager",
@@ -142,7 +126,7 @@ User {
142126
exports[`Entity denormalization denormalizes recursive dependencies 4`] = `
143127
User {
144128
"id": "456",
145-
"reports": Immutable.List [
129+
"reports": [
146130
Report {
147131
"draftedBy": [Circular],
148132
"id": "123",

packages/endpoint/src/schemas/__tests__/__snapshots__/EntityMixin.test.ts.snap

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -75,22 +75,6 @@ Menu {
7575
`;
7676

7777
exports[`EntityMixin denormalization denormalizes deep entities with records 2`] = `
78-
Menu {
79-
"food": Food {
80-
"id": "1",
81-
},
82-
"id": "1",
83-
}
84-
`;
85-
86-
exports[`EntityMixin denormalization denormalizes deep entities with records 3`] = `
87-
Menu {
88-
"food": null,
89-
"id": "2",
90-
}
91-
`;
92-
93-
exports[`EntityMixin denormalization denormalizes deep entities with records 4`] = `
9478
Menu {
9579
"food": null,
9680
"id": "2",
@@ -148,15 +132,15 @@ exports[`EntityMixin denormalization nesting denormalizes recursive dependencies
148132
Report {
149133
"draftedBy": User {
150134
"id": "456",
151-
"reports": Immutable.List [
135+
"reports": [
152136
[Circular],
153137
],
154138
"role": "manager",
155139
},
156140
"id": "123",
157141
"publishedBy": User {
158142
"id": "456",
159-
"reports": Immutable.List [
143+
"reports": [
160144
[Circular],
161145
],
162146
"role": "manager",
@@ -183,7 +167,7 @@ User {
183167
exports[`EntityMixin denormalization nesting denormalizes recursive dependencies 4`] = `
184168
User {
185169
"id": "456",
186-
"reports": Immutable.List [
170+
"reports": [
187171
Report {
188172
"draftedBy": [Circular],
189173
"id": "123",

packages/endpoint/src/schemas/__tests__/__snapshots__/Values.test.js.snap

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -461,69 +461,69 @@ exports[`input (immutable) ValuesSchema denormalization (current) works on compl
461461
"data": {
462462
"estimates": {
463463
"BTC": Estimate {
464-
"coinbase_fees": Immutable.Map {
464+
"coinbase_fees": {
465465
"amount": "0.00002270",
466466
"currency": "BTC",
467467
},
468468
"confirmation_duration": 900,
469-
"exchange": Immutable.Map {
470-
"rate": "6820.07",
471-
"local": "USD",
469+
"exchange": {
472470
"crypto": "BTC",
473-
},
474-
"exchange_to_proceeds": Immutable.Map {
471+
"local": "USD",
475472
"rate": "6820.07",
476-
"local": "EUR",
473+
},
474+
"exchange_to_proceeds": {
477475
"crypto": "BTC",
476+
"local": "EUR",
477+
"rate": "6820.07",
478478
},
479-
"fee": Immutable.Map {
479+
"fee": {
480480
"amount": "0.00002270",
481481
"currency": "BTC",
482482
},
483-
"fee_per_kb": Immutable.Map {
483+
"fee_per_kb": {
484484
"amount": "0.00016566",
485485
"currency": "BTC",
486486
},
487-
"min_order_size": Immutable.Map {
487+
"min_order_size": {
488488
"amount": "0.001",
489489
"currency": "BTC",
490490
},
491491
"priority": "fast",
492-
"recipient_value": Immutable.Map {
492+
"recipient_value": {
493493
"amount": "0.00054147",
494494
"currency": "BTC",
495495
},
496496
},
497497
"ETH": Estimate {
498-
"coinbase_fees": Immutable.Map {
498+
"coinbase_fees": {
499499
"amount": "0.00002270",
500500
"currency": "BTC",
501501
},
502502
"confirmation_duration": 900,
503-
"exchange": Immutable.Map {
504-
"rate": "197.07",
505-
"local": "USD",
503+
"exchange": {
506504
"crypto": "ETH",
505+
"local": "USD",
506+
"rate": "197.07",
507507
},
508-
"exchange_to_proceeds": Immutable.Map {
509-
"rate": "6820.07",
510-
"local": "EUR",
508+
"exchange_to_proceeds": {
511509
"crypto": "BTC",
510+
"local": "EUR",
511+
"rate": "6820.07",
512512
},
513-
"fee": Immutable.Map {
513+
"fee": {
514514
"amount": "0.03795",
515515
"currency": "ETH",
516516
},
517-
"fee_per_kb": Immutable.Map {
517+
"fee_per_kb": {
518518
"amount": "0.00086",
519519
"currency": "ETH",
520520
},
521-
"min_order_size": Immutable.Map {
521+
"min_order_size": {
522522
"amount": "0.001",
523523
"currency": "BTC",
524524
},
525525
"priority": "fast",
526-
"recipient_value": Immutable.Map {
526+
"recipient_value": {
527527
"amount": "2.53",
528528
"currency": "ETH",
529529
},

packages/normalizr/src/denormalize/denormalize.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { getEntities } from './getEntities.js';
22
import LocalCache from './localCache.js';
33
import getUnvisit from './unvisit.js';
44
import type { Schema } from '../interface.js';
5+
import { isImmutable } from '../schemas/ImmutableUtils.js';
56
import type { DenormalizeNullable } from '../types.js';
67

78
export function denormalize<S extends Schema>(
@@ -19,5 +20,6 @@ export function denormalize<S extends Schema>(
1920
getEntities(entities),
2021
new LocalCache(),
2122
args,
23+
isImmutable(entities),
2224
)(schema, input).data;
2325
}

0 commit comments

Comments
 (0)