-
Notifications
You must be signed in to change notification settings - Fork 0
/
Decorator.ts
127 lines (109 loc) · 4.5 KB
/
Decorator.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// *
// * Decorator functions for FlashData - Realtime Database Library
// * https://github.com/phamngocduy98/node_flashdata_library
// *
import "reflect-metadata";
import {DocumentData, DocumentDataConstructor, IsCollectionParent} from "./internal";
const CollectionRegistrationMetadataKey = Symbol("CollectionRegistrationMetadataKey");
export type MapArrayTypes = "string" | "number" | "boolean";
export interface ICollectionRegistrationMetadata {
collectionName: string;
dataConstructor?: {new (...args: any): DocumentData};
type?: MapArrayTypes;
}
export function getRegisteredCollections(target: DocumentData | IsCollectionParent): ICollectionRegistrationMetadata[] {
return Reflect.getMetadata(CollectionRegistrationMetadataKey, target) || [];
}
// @Collection
export function Collection(constructor: DocumentDataConstructor<any>) {
return function (target: DocumentData | IsCollectionParent, propertyKey: string): any {
let registeredCollections = getRegisteredCollections(target);
let metaData: ICollectionRegistrationMetadata = {
collectionName: propertyKey,
dataConstructor: constructor
};
registeredCollections.push(metaData);
Reflect.defineMetadata(CollectionRegistrationMetadataKey, registeredCollections, target);
};
}
// @MapArray
export function MapArray(type: MapArrayTypes) {
return function (target: DocumentData, propertyKey: string): any {
let registeredCollections = getRegisteredCollections(target);
let metaData: ICollectionRegistrationMetadata = {
collectionName: propertyKey,
dataConstructor: undefined,
type: type
};
registeredCollections.push(metaData);
Reflect.defineMetadata(CollectionRegistrationMetadataKey, registeredCollections, target);
};
}
/*************************
* PROPERTY REGISTRATION
*************************/
const PropertyMetadataKey = Symbol("PropertyMetadataKey");
export function getRegisteredProperty(target: DocumentData): string[] {
return Reflect.getMetadata(PropertyMetadataKey, target) || [];
}
// @Property
export function Property() {
return function (target: DocumentData, propertyKey: string): any {
let registeredProperty = getRegisteredProperty(target);
registeredProperty.push(propertyKey);
Reflect.defineMetadata(CollectionRegistrationMetadataKey, registeredProperty, target);
};
}
/*******************
* LINKING FEATURE:
*******************/
const LinkingMetadataKey = Symbol("LinkingMetadataKey");
export interface ILinkingMetadata {
propertyKey: string;
targetRefPath?: string;
constructor?: DocumentDataConstructor<any>;
type: "nested" | "viaId";
}
export function getRegisteredNestedAndLinkingMetadata(target: DocumentData): ILinkingMetadata[] {
return Reflect.getMetadata(LinkingMetadataKey, target) || [];
}
// @NestedDocument
export function NestedDocument(constructor: DocumentDataConstructor<any>) {
return function (target: DocumentData, propertyKey: string): any {
let prevLinkedMetadata = getRegisteredNestedAndLinkingMetadata(target);
let metaData: ILinkingMetadata = {
propertyKey: propertyKey,
constructor: constructor,
type: "nested"
};
prevLinkedMetadata.push(metaData);
Reflect.defineMetadata(LinkingMetadataKey, prevLinkedMetadata, target);
};
}
// // @LinkDocumentReferenceArray
// export function LinkDocumentReferenceArray(refArrayPropertyName: string) {
// return function (target: DatabaseDocument<any>, propertyKey: string): any {
// let prevLinkedMetadata = getRegisteredLinkingMetadata(target);
// let metaData: ILinkingMetadata = {
// propertyKey: propertyKey,
// targetRefPath: refArrayPropertyName,
// linkVia: "docRef[]"
// };
// prevLinkedMetadata.push(metaData);
// Reflect.defineMetadata(LinkingMetadataKey, prevLinkedMetadata, target);
// };
// }
// @LinkDocument
export function LinkDocument(refPath: string) {
return function (target: DocumentData, propertyKey: string): any {
let prevLinkedMetadata = getRegisteredNestedAndLinkingMetadata(target);
let metaData: ILinkingMetadata = {
propertyKey: propertyKey,
targetRefPath: refPath,
constructor: undefined,
type: "viaId"
};
prevLinkedMetadata.push(metaData);
Reflect.defineMetadata(LinkingMetadataKey, prevLinkedMetadata, target);
};
}