Skip to content
This repository was archived by the owner on Jul 3, 2024. It is now read-only.

Commit 599851b

Browse files
committed
add NdbDocument type restrictions
1 parent 7daf6c6 commit 599851b

File tree

4 files changed

+39
-35
lines changed

4 files changed

+39
-35
lines changed

src/denormalizer-interface.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
import { Depth, ValidKey } from '@normalized-db/core';
1+
import { Depth, NdbDocument, ValidKey } from '@normalized-db/core';
22

33
export interface IDenormalizer {
4-
applyAll<T>(type: string, data: T[], depth?: number | Depth): Promise<T[]>;
4+
applyAll<T extends NdbDocument>(type: string, data: T[], depth?: number | Depth): Promise<T[]>;
55

6-
applyAllKeys<Key extends ValidKey, T>(type: string, keys: Key[], depth?: number | Depth): Promise<T[]>;
6+
applyAllKeys<Key extends ValidKey, T extends NdbDocument>(type: string,
7+
keys: Key[],
8+
depth?: number | Depth): Promise<T[]>;
79

8-
apply<T>(type: string, data: T, depth?: number | Depth): Promise<T>;
10+
apply<T extends NdbDocument>(type: string, data: T, depth?: number | Depth): Promise<T>;
911

10-
applyKey<Key extends ValidKey, T>(type: string, key: Key, depth?: number | Depth): Promise<T>;
12+
applyKey<Key extends ValidKey, T extends NdbDocument>(type: string, key: Key, depth?: number | Depth): Promise<T>;
1113
}

src/denormalizer.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import { Depth, FetchCallback, ISchema, isNull, KeyMap, NormalizedData, ValidKey } from '@normalized-db/core';
1+
import {
2+
Depth, FetchCallback, ISchema, isNull, KeyMap, NdbDocument, NormalizedData,
3+
ValidKey
4+
} from '@normalized-db/core';
25
import { IDenormalizer } from './denormalizer-interface';
36
import { BasicDenormalizer } from './implementation/basic-denormalizer';
47
import { ReverseReferenceDenormalizer } from './implementation/reverse-reference-denormalizer';
@@ -19,19 +22,23 @@ export class Denormalizer implements IDenormalizer {
1922
this._implementation = this.buildDenormalizer();
2023
}
2124

22-
public applyAll<T>(type: string, data: T[], depth?: number | Depth): Promise<T[]> {
25+
public applyAll<T extends NdbDocument>(type: string, data: T[], depth?: number | Depth): Promise<T[]> {
2326
return this._implementation.applyAll<T>(type, data, depth);
2427
}
2528

26-
public applyAllKeys<Key extends ValidKey, T>(type: string, keys: Key[], depth?: number | Depth): Promise<T[]> {
29+
public applyAllKeys<Key extends ValidKey, T extends NdbDocument>(type: string,
30+
keys: Key[],
31+
depth?: number | Depth): Promise<T[]> {
2732
return this._implementation.applyAllKeys<Key, T>(type, keys, depth);
2833
}
2934

30-
public apply<T>(type: string, data: T, depth?: number | Depth): Promise<T> {
35+
public apply<T extends NdbDocument>(type: string, data: T, depth?: number | Depth): Promise<T> {
3136
return this._implementation.apply<T>(type, data, depth);
3237
}
3338

34-
public applyKey<Key extends ValidKey, T>(type: string, key: Key, depth?: number | Depth): Promise<T> {
39+
public applyKey<Key extends ValidKey, T extends NdbDocument>(type: string,
40+
key: Key,
41+
depth?: number | Depth): Promise<T> {
3542
return this._implementation.applyKey<Key, T>(type, key, depth);
3643
}
3744

src/implementation/basic-denormalizer.ts

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,6 @@
11
import {
2-
deepClone,
3-
Depth,
4-
FetchCallback,
5-
InvalidTypeError,
6-
ISchema,
7-
isNull,
8-
isObject,
9-
IStore,
10-
IStoreTargetItem,
11-
KeyMap,
12-
NormalizedData,
13-
NotFoundError,
14-
TypeMismatchError,
15-
ValidKey
2+
deepClone, Depth, FetchCallback, InvalidTypeError, ISchema, isNull, isObject, IStore, IStoreTargetItem, KeyMap,
3+
NdbDocument, NormalizedData, NotFoundError, TypeMismatchError, ValidKey
164
} from '@normalized-db/core';
175
import { IDenormalizer } from '../denormalizer-interface';
186

@@ -42,17 +30,19 @@ export class BasicDenormalizer implements IDenormalizer {
4230
}
4331
}
4432

45-
public async applyAll<T>(type: string, data: T[], depth?: number | Depth): Promise<T[]> {
33+
public async applyAll<T extends NdbDocument>(type: string, data: T[], depth?: number | Depth): Promise<T[]> {
4634
this.validateType(type);
4735
return await this.denormalizeArray(type, data, depth);
4836
}
4937

50-
public async applyAllKeys<Key extends ValidKey, T>(type: string, keys: Key[], depth?: number | Depth): Promise<T[]> {
38+
public async applyAllKeys<Key extends ValidKey, T extends NdbDocument>(type: string,
39+
keys: Key[],
40+
depth?: number | Depth): Promise<T[]> {
5141
this.validateType(type);
5242
return await Promise.all(keys.map(async key => await this.applyKey<Key, T>(type, key, depth)));
5343
}
5444

55-
public async apply<T>(type: string, data: T, depth?: number | Depth): Promise<T> {
45+
public async apply<T extends NdbDocument>(type: string, data: T, depth?: number | Depth): Promise<T> {
5646
this.validateType(type);
5747

5848
if (isObject(data)) {
@@ -62,7 +52,9 @@ export class BasicDenormalizer implements IDenormalizer {
6252
}
6353
}
6454

65-
public async applyKey<Key extends ValidKey, T>(type: string, key: Key, depth?: number | Depth): Promise<T> {
55+
public async applyKey<Key extends ValidKey, T extends NdbDocument>(type: string,
56+
key: Key,
57+
depth?: number | Depth): Promise<T> {
6658
this.validateType(type);
6759

6860
if (!this._keys[type].has(key)) {
@@ -91,12 +83,12 @@ export class BasicDenormalizer implements IDenormalizer {
9183
return await this.apply(type, deepClone(data), depth);
9284
}
9385

94-
protected async denormalizeArray(type: string, data: any[], depth: number | Depth): Promise<any> {
86+
protected async denormalizeArray(type: string, data: NdbDocument[], depth: number | Depth): Promise<any> {
9587
// TODO: check for arrays, empty objects
9688
return await Promise.all(data.map(item => this.denormalizeObject(type, item, depth)));
9789
}
9890

99-
protected async denormalizeObject(type: string, data: any, depth: number | Depth): Promise<any> {
91+
protected async denormalizeObject(type: string, data: NdbDocument, depth: number | Depth): Promise<any> {
10092
const config = this._schema.getConfig(type);
10193
if (!isNull(config.targets)) {
10294
await this.denormalizeTargets(type, data, config, depth);
@@ -105,7 +97,10 @@ export class BasicDenormalizer implements IDenormalizer {
10597
return data;
10698
}
10799

108-
protected async denormalizeTargets(type: string, data: any, config: IStore, depth: number | Depth): Promise<void> {
100+
protected async denormalizeTargets(type: string,
101+
data: NdbDocument,
102+
config: IStore,
103+
depth: number | Depth): Promise<void> {
109104
const promises = Object.keys(config.targets)
110105
.filter(field => field in data && !isNull(data[field]))
111106
.map(async field => {
@@ -167,7 +162,7 @@ export class BasicDenormalizer implements IDenormalizer {
167162
return isArray ? result : result[0];
168163
}
169164

170-
protected applyTarget(type: string, data: any, depth?: number | Depth): Promise<any> {
165+
protected applyTarget(type: string, data: NdbDocument, depth?: number | Depth): Promise<NdbDocument> {
171166
return this.apply(type, data, depth);
172167
}
173168

src/implementation/reverse-reference-denormalizer.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { Depth } from '@normalized-db/core';
1+
import { Depth, NdbDocument } from '@normalized-db/core';
22
import { BasicDenormalizer } from './basic-denormalizer';
33

44
export class ReverseReferenceDenormalizer extends BasicDenormalizer {
55

6-
protected async applyTarget(type: string, data: any | any[], depth?: number | Depth): Promise<any[]> {
7-
const denormalizedTarget = await super.applyTarget(type, data, depth);
6+
protected async applyTarget(type: string, data: NdbDocument, depth?: number | Depth): Promise<NdbDocument> {
7+
const denormalizedTarget: any = await super.applyTarget(type, data, depth);
88
if ('_refs' in denormalizedTarget) {
99
delete denormalizedTarget['_refs'];
1010
}

0 commit comments

Comments
 (0)