rajt 0.0.14 → 0.0.15
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/dynamodb/decorators.ts +75 -35
package/package.json
CHANGED
|
@@ -14,6 +14,9 @@ export type ModelOpts = string | {
|
|
|
14
14
|
}
|
|
15
15
|
|
|
16
16
|
export function getModelMetadata(target: Function | any): ModelMetadata {
|
|
17
|
+
if (!target?.m)
|
|
18
|
+
throw Error(`Entity "${target?.name}" not registred, Use @Entity or @Model.`)
|
|
19
|
+
|
|
17
20
|
const typeKeys = typeof target.m[1]
|
|
18
21
|
return {
|
|
19
22
|
table: target.m[0],
|
|
@@ -42,25 +45,61 @@ function _key(target: Function | any, pk: string, sk?: string) {
|
|
|
42
45
|
target.m[1] = pk && sk? [pk, sk] : [pk]
|
|
43
46
|
}
|
|
44
47
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
+
function _model(target: any, opt?: ModelOpts) {
|
|
49
|
+
_table(target, opt)
|
|
50
|
+
const notStr = typeof opt !== 'string'
|
|
48
51
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
_table(target, opt)
|
|
52
|
-
const notStr = typeof opt !== 'string'
|
|
52
|
+
if (!opt || notStr && opt?.zip)
|
|
53
|
+
_zip(target)
|
|
53
54
|
|
|
54
|
-
|
|
55
|
-
|
|
55
|
+
const pk = opt && notStr ? opt?.partitionKey : undefined
|
|
56
|
+
const sk = opt && notStr ? opt?.sortKey : undefined
|
|
57
|
+
_key(target, pk || 'PK', sk || 'SK')
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function _pk(target: any, prop: string) {
|
|
61
|
+
if (!target?.m) target.m = []
|
|
62
|
+
if (['string', 'undefined'].includes(typeof target.m[1])) {
|
|
63
|
+
target.m[1] = prop
|
|
64
|
+
} else {
|
|
65
|
+
target.m[1][0] = prop
|
|
66
|
+
}
|
|
67
|
+
}
|
|
56
68
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
69
|
+
function _sk(target: any, prop: string) {
|
|
70
|
+
if (!target?.m) target.m = []
|
|
71
|
+
if (['string', 'undefined'].includes(typeof target.m[1])) {
|
|
72
|
+
target.m[1] = []
|
|
73
|
+
target.m[1][1] = prop
|
|
74
|
+
} else {
|
|
75
|
+
target.m[1][0] = prop
|
|
60
76
|
}
|
|
61
77
|
}
|
|
62
78
|
|
|
63
|
-
export function
|
|
79
|
+
export function Entity(target: Function): void
|
|
80
|
+
export function Entity(opt?: ModelOpts): ClassDecorator
|
|
81
|
+
export function Entity(...args: any[]): void | ClassDecorator {
|
|
82
|
+
if (args.length === 1 && typeof args[0] === 'function')
|
|
83
|
+
return _table(args[0])
|
|
84
|
+
|
|
85
|
+
return (target: any) => _table(target, ...args)
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export function Model(target: Function): void
|
|
89
|
+
export function Model(opt?: ModelOpts): ClassDecorator
|
|
90
|
+
export function Model(...args: any[]): void | ClassDecorator {
|
|
91
|
+
if (args.length === 1 && typeof args[0] === 'function')
|
|
92
|
+
return _model(args[0])
|
|
93
|
+
|
|
94
|
+
return (target: any) => _model(target, ...args)
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export function Zip(target: Function): void
|
|
98
|
+
export function Zip(): ClassDecorator
|
|
99
|
+
export function Zip(...args: any[]): void | ClassDecorator {
|
|
100
|
+
if (args.length === 1 && typeof args[0] === 'function')
|
|
101
|
+
return _zip(args[0])
|
|
102
|
+
|
|
64
103
|
return (target: any) => _zip(target)
|
|
65
104
|
}
|
|
66
105
|
|
|
@@ -69,29 +108,30 @@ export function Key(pk: string, sk?: string) {
|
|
|
69
108
|
_key(target, pk, sk)
|
|
70
109
|
}
|
|
71
110
|
}
|
|
72
|
-
export
|
|
73
|
-
return Key(pk, sk)
|
|
74
|
-
}
|
|
111
|
+
export const Keys = Key
|
|
75
112
|
|
|
76
|
-
export function PartitionKey(attrName
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
113
|
+
export function PartitionKey(attrName: string): PropertyDecorator
|
|
114
|
+
export function PartitionKey(target: any, propertyKey: string): void
|
|
115
|
+
export function PartitionKey(target: any, propertyKey: string | undefined, parameterIndex: number): void
|
|
116
|
+
export function PartitionKey(...args: any[]): void | PropertyDecorator {
|
|
117
|
+
if (!args.length) return
|
|
118
|
+
|
|
119
|
+
if (typeof args[0] === 'function' && typeof args[1] === 'string' && args[1])
|
|
120
|
+
return _pk(args[0], args[1])
|
|
121
|
+
|
|
122
|
+
if (args.length === 1 && args[0])
|
|
123
|
+
return (target: any) => _pk(target, args[0])
|
|
85
124
|
}
|
|
86
125
|
|
|
87
|
-
export function SortKey(attrName
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
126
|
+
export function SortKey(attrName: string): PropertyDecorator
|
|
127
|
+
export function SortKey(target: any, propertyKey: string): void
|
|
128
|
+
export function SortKey(target: any, propertyKey: string | undefined, parameterIndex: number): void
|
|
129
|
+
export function SortKey(...args: any[]): void | PropertyDecorator {
|
|
130
|
+
if (!args.length) return
|
|
131
|
+
|
|
132
|
+
if (typeof args[0] === 'function' && typeof args[1] === 'string' && args[1])
|
|
133
|
+
return _sk(args[0], args[1])
|
|
134
|
+
|
|
135
|
+
if (args.length === 1 && args[0])
|
|
136
|
+
return (target: any) => _sk(target, args[0])
|
|
97
137
|
}
|