@pristine-ts/data-mapping-common 2.0.7 → 2.0.9
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/dist/lib/cjs/builders/auto-data-mapping.builder.js +36 -1
- package/dist/lib/cjs/builders/auto-data-mapping.builder.js.map +1 -1
- package/dist/lib/cjs/builders/data-mapping.builder.js +7 -6
- package/dist/lib/cjs/builders/data-mapping.builder.js.map +1 -1
- package/dist/lib/cjs/errors/data-normalizer-not-found.error.js +15 -0
- package/dist/lib/cjs/errors/data-normalizer-not-found.error.js.map +1 -0
- package/dist/lib/cjs/errors/errors.js +3 -0
- package/dist/lib/cjs/errors/errors.js.map +1 -1
- package/dist/lib/cjs/errors/undefined-destination-property.error.js +14 -0
- package/dist/lib/cjs/errors/undefined-destination-property.error.js.map +1 -0
- package/dist/lib/cjs/mappers/data.mapper.js +3 -5
- package/dist/lib/cjs/mappers/data.mapper.js.map +1 -1
- package/dist/lib/cjs/nodes/base-data-mapping.node.js +4 -0
- package/dist/lib/cjs/nodes/base-data-mapping.node.js.map +1 -1
- package/dist/lib/cjs/nodes/data-mapping.leaf.js +36 -11
- package/dist/lib/cjs/nodes/data-mapping.leaf.js.map +1 -1
- package/dist/lib/cjs/nodes/data-mapping.node.js +119 -49
- package/dist/lib/cjs/nodes/data-mapping.node.js.map +1 -1
- package/dist/lib/cjs/options/auto-data-mapping-builder.options.js +2 -1
- package/dist/lib/cjs/options/auto-data-mapping-builder.options.js.map +1 -1
- package/dist/lib/cjs/tsconfig.cjs.tsbuildinfo +1 -1
- package/dist/lib/esm/builders/auto-data-mapping.builder.js +36 -1
- package/dist/lib/esm/builders/auto-data-mapping.builder.js.map +1 -1
- package/dist/lib/esm/builders/data-mapping.builder.js +7 -6
- package/dist/lib/esm/builders/data-mapping.builder.js.map +1 -1
- package/dist/lib/esm/errors/data-normalizer-not-found.error.js +11 -0
- package/dist/lib/esm/errors/data-normalizer-not-found.error.js.map +1 -0
- package/dist/lib/esm/errors/errors.js +3 -0
- package/dist/lib/esm/errors/errors.js.map +1 -1
- package/dist/lib/esm/errors/undefined-destination-property.error.js +10 -0
- package/dist/lib/esm/errors/undefined-destination-property.error.js.map +1 -0
- package/dist/lib/esm/mappers/data.mapper.js +3 -5
- package/dist/lib/esm/mappers/data.mapper.js.map +1 -1
- package/dist/lib/esm/nodes/base-data-mapping.node.js +4 -0
- package/dist/lib/esm/nodes/base-data-mapping.node.js.map +1 -1
- package/dist/lib/esm/nodes/data-mapping.leaf.js +36 -11
- package/dist/lib/esm/nodes/data-mapping.leaf.js.map +1 -1
- package/dist/lib/esm/nodes/data-mapping.node.js +119 -49
- package/dist/lib/esm/nodes/data-mapping.node.js.map +1 -1
- package/dist/lib/esm/options/auto-data-mapping-builder.options.js +2 -1
- package/dist/lib/esm/options/auto-data-mapping-builder.options.js.map +1 -1
- package/dist/lib/esm/tsconfig.tsbuildinfo +1 -1
- package/dist/types/builders/auto-data-mapping.builder.d.ts +16 -0
- package/dist/types/builders/data-mapping.builder.d.ts +3 -2
- package/dist/types/errors/data-normalizer-not-found.error.d.ts +8 -0
- package/dist/types/errors/errors.d.ts +3 -0
- package/dist/types/errors/undefined-destination-property.error.d.ts +8 -0
- package/dist/types/interfaces/data-mapping-interceptor.interface.d.ts +6 -2
- package/dist/types/nodes/data-mapping.leaf.d.ts +11 -0
- package/dist/types/nodes/data-mapping.node.d.ts +54 -2
- package/dist/types/options/auto-data-mapping-builder.options.d.ts +12 -0
- package/package.json +3 -3
|
@@ -2,6 +2,17 @@ import { ClassConstructor } from "class-transformer";
|
|
|
2
2
|
import { DataMappingBuilder } from "./data-mapping.builder";
|
|
3
3
|
import { AutoDataMappingBuilderOptions } from "../options/auto-data-mapping-builder.options";
|
|
4
4
|
export declare class AutoDataMappingBuilder {
|
|
5
|
+
/**
|
|
6
|
+
* Schema cache keyed by destination class. The auto-built schema is deterministic in `destinationType`
|
|
7
|
+
* + the *shape* of the source (which decoration metadata describes), so for a stable class definition
|
|
8
|
+
* the same builder can be reused across calls. We key only by destinationType to keep things simple;
|
|
9
|
+
* if the source shape varies and the auto-inference depends on it (e.g. inferring scalar-array member
|
|
10
|
+
* type from `source[propertyKey][0]`), callers needing fresh inference can bypass the cache via the
|
|
11
|
+
* `disableCache` option.
|
|
12
|
+
*
|
|
13
|
+
* Stored under a WeakMap so unused destination classes can be garbage-collected.
|
|
14
|
+
*/
|
|
15
|
+
private readonly cache;
|
|
5
16
|
/**
|
|
6
17
|
* This method receives a source object and a destinationType that corresponds to the type of the class
|
|
7
18
|
* that the source should be mapped to. It then creates a DataMappingBuilder object that contains the schema
|
|
@@ -11,6 +22,11 @@ export declare class AutoDataMappingBuilder {
|
|
|
11
22
|
* @param options
|
|
12
23
|
*/
|
|
13
24
|
build(source: any, destinationType: ClassConstructor<any>, options?: AutoDataMappingBuilderOptions): DataMappingBuilder;
|
|
25
|
+
/**
|
|
26
|
+
* Clear the cached schema for a destinationType (or the whole cache when no argument is passed).
|
|
27
|
+
* Useful in tests, or when class metadata changes at runtime (rare).
|
|
28
|
+
*/
|
|
29
|
+
clearCache(destinationType?: ClassConstructor<any>): void;
|
|
14
30
|
/**
|
|
15
31
|
* This method is the internal method that is called recursively to build the schema.
|
|
16
32
|
*
|
|
@@ -88,11 +88,12 @@ export declare class DataMappingBuilder extends BaseDataMappingNode {
|
|
|
88
88
|
*/
|
|
89
89
|
import(schema: any): void;
|
|
90
90
|
/**
|
|
91
|
-
* This method exports
|
|
91
|
+
* This method exports the schema as a plain object. The export does not mutate the live tree —
|
|
92
|
+
* the builder remains usable for mapping after this call returns.
|
|
92
93
|
*/
|
|
93
94
|
export(): {
|
|
94
95
|
nodes: {
|
|
95
|
-
[
|
|
96
|
+
[key: string]: any;
|
|
96
97
|
};
|
|
97
98
|
normalizers: {
|
|
98
99
|
key: DataNormalizerUniqueKey;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { DataNormalizerUniqueKey } from "../types/data-normalizer-unique-key.type";
|
|
2
|
+
/**
|
|
3
|
+
* Thrown when a leaf references a normalizer key that hasn't been registered with the DataMapper.
|
|
4
|
+
*/
|
|
5
|
+
export declare class DataNormalizerNotFoundError extends Error {
|
|
6
|
+
readonly normalizerUniqueKey: DataNormalizerUniqueKey;
|
|
7
|
+
constructor(message: string, normalizerUniqueKey: DataNormalizerUniqueKey);
|
|
8
|
+
}
|
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
export * from "./array-data-mapping-node-invalid-source-property-type.error";
|
|
2
|
+
export * from "./auto-map-primitive-type-normalizer-not-found.error";
|
|
2
3
|
export * from "./data-after-mapping-interceptor-already-added.error";
|
|
3
4
|
export * from "./data-before-mapping-interceptor-already-added.error";
|
|
4
5
|
export * from "./data-normalizer-already-added.error";
|
|
6
|
+
export * from "./data-normalizer-not-found.error";
|
|
5
7
|
export * from "./data-mapping-interceptor-not-found.error";
|
|
6
8
|
export * from "./data-mapping-source-property-not-found.error";
|
|
7
9
|
export * from "./normalizer-invalid-source-type.error";
|
|
10
|
+
export * from "./undefined-destination-property.error";
|
|
8
11
|
export * from "./undefined-source-property.error";
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { DataMappingLeaf } from "../nodes/data-mapping.leaf";
|
|
2
|
+
import { DataMappingNode } from "../nodes/data-mapping.node";
|
|
3
|
+
/**
|
|
4
|
+
* Thrown when a Node is being committed to its parent without a `destinationProperty` set.
|
|
5
|
+
*/
|
|
6
|
+
export declare class UndefinedDestinationPropertyError extends Error {
|
|
7
|
+
constructor(node: DataMappingLeaf | DataMappingNode);
|
|
8
|
+
}
|
|
@@ -7,13 +7,17 @@ export interface DataMappingInterceptorInterface {
|
|
|
7
7
|
getUniqueKey(): DataMappingInterceptorUniqueKeyType;
|
|
8
8
|
/**
|
|
9
9
|
* This method is called before the row is being mapped and normalized. It allows you to combine fields for example if that's what you want.
|
|
10
|
+
*
|
|
10
11
|
* @param row
|
|
12
|
+
* @param options The options associated with this interceptor entry on the builder.
|
|
11
13
|
*/
|
|
12
|
-
beforeMapping(row: any): Promise<any>;
|
|
14
|
+
beforeMapping(row: any, options?: any): Promise<any>;
|
|
13
15
|
/**
|
|
14
16
|
* This method is called after the row is being mapped and normalized. It can allow you to apply operations on each
|
|
15
17
|
* field or combine fields for example.
|
|
18
|
+
*
|
|
16
19
|
* @param row
|
|
20
|
+
* @param options The options associated with this interceptor entry on the builder.
|
|
17
21
|
*/
|
|
18
|
-
afterMapping(row: any): Promise<any>;
|
|
22
|
+
afterMapping(row: any, options?: any): Promise<any>;
|
|
19
23
|
}
|
|
@@ -27,6 +27,11 @@ export declare class DataMappingLeaf {
|
|
|
27
27
|
* This property contains an array of Normalizers that must be excluded from normalizers defined by parents.
|
|
28
28
|
*/
|
|
29
29
|
excludedNormalizers: Set<DataNormalizerUniqueKey>;
|
|
30
|
+
/**
|
|
31
|
+
* Memoized merge of inherited root normalizers (minus excluded) and leaf-local normalizers.
|
|
32
|
+
* Cleared whenever the leaf's normalizer configuration changes.
|
|
33
|
+
*/
|
|
34
|
+
private effectiveNormalizersCache?;
|
|
30
35
|
/**
|
|
31
36
|
* This method specified whether it's possible that this element not be present in the `source` object.
|
|
32
37
|
*/
|
|
@@ -65,6 +70,12 @@ export declare class DataMappingLeaf {
|
|
|
65
70
|
* @param normalizerUniqueKey
|
|
66
71
|
*/
|
|
67
72
|
excludeNormalizer(normalizerUniqueKey: DataNormalizerUniqueKey): DataMappingLeaf;
|
|
73
|
+
/**
|
|
74
|
+
* Returns the merged list of normalizers applied to this leaf: inherited root normalizers minus the
|
|
75
|
+
* ones explicitly excluded here, followed by leaf-local normalizers. Memoized — the cache is cleared
|
|
76
|
+
* by `addNormalizer`/`excludeNormalizer`.
|
|
77
|
+
*/
|
|
78
|
+
private getEffectiveNormalizers;
|
|
68
79
|
/**
|
|
69
80
|
* This method adds this node to its parent and returns the parent.
|
|
70
81
|
*/
|
|
@@ -85,6 +85,52 @@ export declare class DataMappingNode extends BaseDataMappingNode {
|
|
|
85
85
|
map(source: any, destination: any, normalizersMap: {
|
|
86
86
|
[key in DataNormalizerUniqueKey]: DataNormalizerInterface<any, any>;
|
|
87
87
|
}, options?: DataMapperOptions): Promise<void>;
|
|
88
|
+
/**
|
|
89
|
+
* Build the destination object for the single-object case.
|
|
90
|
+
*
|
|
91
|
+
* If a `destinationType` (class constructor) is set, instantiate the class. Otherwise return
|
|
92
|
+
* a plain object. When `includeSourceKeys` is true, the source's own enumerable properties are
|
|
93
|
+
* seeded onto the result so they survive the mapping; sub-nodes then overlay renamed keys on top.
|
|
94
|
+
*
|
|
95
|
+
* Note: the single-object case treats `destinationType` strictly as a class constructor.
|
|
96
|
+
* Factory callbacks only make sense for the per-element ObjectArray case below.
|
|
97
|
+
*/
|
|
98
|
+
private buildDestinationObject;
|
|
99
|
+
/**
|
|
100
|
+
* Build the destination object for a single element of an ObjectArray. Mirrors
|
|
101
|
+
* `buildDestinationObject`, with two extra wrinkles unique to arrays:
|
|
102
|
+
*
|
|
103
|
+
* 1. `destinationType` can be a *factory callback* instead of a fixed class. The callback
|
|
104
|
+
* receives the source array's parent + the element's index and returns an instance of
|
|
105
|
+
* the concrete class to use. This is how polymorphic arrays work (e.g. some elements
|
|
106
|
+
* become `Cat`, others `Dog`).
|
|
107
|
+
* 2. The seed value is the per-element object, not the whole array.
|
|
108
|
+
*/
|
|
109
|
+
private buildArrayMemberDestination;
|
|
110
|
+
/**
|
|
111
|
+
* Resolve the concrete class constructor for a single element of an ObjectArray.
|
|
112
|
+
*
|
|
113
|
+
* `destinationType` here is one of two things:
|
|
114
|
+
*
|
|
115
|
+
* - a **class constructor** (uniform array — every element maps to the same class), or
|
|
116
|
+
* - a **factory callback** of shape `(source, sourceProperty, index) => instance`
|
|
117
|
+
* (polymorphic array — class is chosen per element).
|
|
118
|
+
*
|
|
119
|
+
* JavaScript gives us no clean way to distinguish a class constructor from a callback —
|
|
120
|
+
* both are `typeof === "function"`. The conventional discriminant: a class constructor has
|
|
121
|
+
* a `.prototype` object (where its instance methods live); an arrow function does not.
|
|
122
|
+
*
|
|
123
|
+
* Caveat: a regular `function() { ... }` expression used as a callback would also have a
|
|
124
|
+
* `.prototype` and would be mis-classified as a class. The public type only documents the
|
|
125
|
+
* arrow-function form (`ArrayMemberTypeFactoryCallbackType = (...) => any`), and in practice
|
|
126
|
+
* callers use arrow functions, so this is safe for documented usage.
|
|
127
|
+
*/
|
|
128
|
+
private resolveArrayMemberConstructor;
|
|
129
|
+
/**
|
|
130
|
+
* Run every direct child node against `(sourceElement, destinationElement)`. Each child
|
|
131
|
+
* writes its mapped destination property onto `destinationElement`.
|
|
132
|
+
*/
|
|
133
|
+
private runSubNodes;
|
|
88
134
|
/**
|
|
89
135
|
* This method imports a schema.
|
|
90
136
|
*
|
|
@@ -92,7 +138,13 @@ export declare class DataMappingNode extends BaseDataMappingNode {
|
|
|
92
138
|
*/
|
|
93
139
|
import(schema: any): void;
|
|
94
140
|
/**
|
|
95
|
-
* This method exports this node.
|
|
141
|
+
* This method exports this node. The exported representation is a plain object — it does not mutate
|
|
142
|
+
* the live tree, so the same builder can continue to be used for mapping after `export()` is called.
|
|
143
|
+
*
|
|
144
|
+
* `destinationType` is intentionally not serialized: class constructors aren't transferable, and
|
|
145
|
+
* factory callbacks (`ArrayMemberTypeFactoryCallbackType`) hold closures. To rehydrate a schema with
|
|
146
|
+
* the same destination instantiation behavior, decorate the destination class with class-transformer's
|
|
147
|
+
* `@Type()` and pass the destination class to `DataMapper.map()`.
|
|
96
148
|
*/
|
|
97
149
|
export(): {
|
|
98
150
|
_type: DataMappingNodeTypeEnum;
|
|
@@ -100,7 +152,7 @@ export declare class DataMappingNode extends BaseDataMappingNode {
|
|
|
100
152
|
destinationProperty: string;
|
|
101
153
|
isOptional: boolean;
|
|
102
154
|
nodes: {
|
|
103
|
-
[
|
|
155
|
+
[key: string]: any;
|
|
104
156
|
};
|
|
105
157
|
};
|
|
106
158
|
}
|
|
@@ -21,5 +21,17 @@ export declare class AutoDataMappingBuilderOptions {
|
|
|
21
21
|
* This property specifies if the errors should be logged.
|
|
22
22
|
*/
|
|
23
23
|
logErrors?: boolean;
|
|
24
|
+
/**
|
|
25
|
+
* When `true`, bypass the AutoDataMappingBuilder schema cache and always rebuild from metadata.
|
|
26
|
+
*
|
|
27
|
+
* The cache keys by destinationType only. If the auto-inference depends on the live source shape
|
|
28
|
+
* (e.g. inferring the element type of an untyped scalar array from `source[propertyKey][0]`),
|
|
29
|
+
* the first call wins and subsequent calls reuse that decision. Pass `true` when you need fresh
|
|
30
|
+
* inference per call.
|
|
31
|
+
*
|
|
32
|
+
* Default value is `false`. Optional in the type so existing consumers passing a plain object
|
|
33
|
+
* literal (without instantiating `AutoDataMappingBuilderOptions`) remain compatible.
|
|
34
|
+
*/
|
|
35
|
+
disableCache?: boolean;
|
|
24
36
|
constructor(options?: Partial<AutoDataMappingBuilderOptions>);
|
|
25
37
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pristine-ts/data-mapping-common",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.9",
|
|
4
4
|
"description": "",
|
|
5
5
|
"module": "dist/lib/esm/data-mapping-common.js",
|
|
6
6
|
"main": "dist/lib/cjs/data-mapping-common.js",
|
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
},
|
|
22
22
|
"dependencies": {
|
|
23
23
|
"@pristine-ts/class-validator": "^2.0.4",
|
|
24
|
-
"@pristine-ts/common": "^2.0.
|
|
24
|
+
"@pristine-ts/common": "^2.0.9",
|
|
25
25
|
"@pristine-ts/metadata": "^1.0.16",
|
|
26
26
|
"class-transformer": "^0.5.1",
|
|
27
27
|
"date-fns": "^3.3.1"
|
|
@@ -63,7 +63,7 @@
|
|
|
63
63
|
"jest-extended/all"
|
|
64
64
|
]
|
|
65
65
|
},
|
|
66
|
-
"gitHead": "
|
|
66
|
+
"gitHead": "760994a87a1207f1670d31c67c6b8cde4c5b09f4",
|
|
67
67
|
"repository": {
|
|
68
68
|
"type": "git",
|
|
69
69
|
"url": "https://github.com/magieno/pristine-ts.git",
|