@@ -7,6 +7,7 @@ import { ModelMetadata } from '../../metadata/model-metadata.model'
77import { PropertyMetadata } from '../../metadata/property-metadata.model'
88import { SecondaryIndex } from '../index/secondary-index'
99import { KEY_PROPERTY } from '../property/key-property.const'
10+ import { modelErrors } from './errors.const'
1011import { KEY_MODEL } from './key-model.const'
1112import { ModelData } from './model-data.model'
1213
@@ -21,27 +22,26 @@ export function Model(opts: ModelData = {}): ClassDecorator {
2122 // get all the properties with @Property () annotation (or @PartitionKey(),...)
2223 // if given class has own properties, all inherited properties are already set and we can get the properties with 'getOwnMetadata'.
2324 // otherwise when the given class does not have own properties, there's no 'ownMetadata' but we need to get them from the class it extends with 'getMetadata'
24- const properties : Array < PropertyMetadata < any > > = Reflect . hasOwnMetadata ( KEY_PROPERTY , constructor )
25- ? Reflect . getOwnMetadata ( KEY_PROPERTY , constructor )
26- : Reflect . getMetadata ( KEY_PROPERTY , constructor )
25+ const properties : Array < PropertyMetadata < any > > =
26+ ( Reflect . hasOwnMetadata ( KEY_PROPERTY , constructor )
27+ ? Reflect . getOwnMetadata ( KEY_PROPERTY , constructor )
28+ : Reflect . getMetadata ( KEY_PROPERTY , constructor ) ) || [ ]
2729
2830 // get partition key
29- const partitionKeys = properties
30- ? properties . filter ( property => property . key && property . key . type === 'HASH' )
31- : null
32- const partitionKeyName : string | null = partitionKeys && partitionKeys . length ? partitionKeys [ 0 ] . nameDb : null
31+ const partitionKeys = properties . filter ( property => property . key && property . key . type === 'HASH' )
32+
33+ const partitionKeyName : string | null = partitionKeys . length ? partitionKeys [ 0 ] . nameDb : null
3334
3435 /*
3536 * get the local and global secondary indexes
3637 */
37- const globalSecondaryIndexes : any = getGlobalSecondaryIndexes ( properties ) || [ ]
38- const localSecondaryIndexes : any = getLocalSecondaryIndexes ( partitionKeyName , properties ) || [ ]
38+ const globalSecondaryIndexes : any = getGlobalSecondaryIndexes ( properties )
39+ const localSecondaryIndexes : any = getLocalSecondaryIndexes ( partitionKeyName , properties )
3940 const indexes : Map < string , SecondaryIndex < any > > = new Map ( [ ...globalSecondaryIndexes , ...localSecondaryIndexes ] )
4041
41- const transientProperties =
42- properties && properties . length
43- ? properties . filter ( property => property . transient === true ) . map ( property => property . name )
44- : [ ]
42+ const transientProperties = properties . length
43+ ? properties . filter ( property => property . transient === true ) . map ( property => property . name )
44+ : [ ]
4545
4646 const metaData : ModelMetadata < any > = {
4747 clazz : constructor ,
@@ -77,46 +77,36 @@ function testForLSI<T>(property: PropertyMetadata<T>): property is PropertyMetad
7777/**
7878 * @hidden
7979 */
80- function getGlobalSecondaryIndexes ( properties : Array < PropertyMetadata < any > > ) : Map < string , SecondaryIndex < any > > | null {
81- if ( properties && properties . length ) {
82- return properties . filter ( testForGSI ) . reduce ( ( map , property ) : Map < string , SecondaryIndex < any > > => {
83- let gsi : SecondaryIndex < any >
84- Object . keys ( property . keyForGSI ) . forEach ( indexName => {
85- if ( map . has ( indexName ) ) {
86- gsi = map . get ( indexName )
87- } else {
88- gsi = < SecondaryIndex < any > > { }
89- }
90-
91- switch ( property . keyForGSI [ indexName ] ) {
92- case 'HASH' :
93- if ( gsi . partitionKey ) {
94- throw new Error (
95- `there is already a partition key defined for global secondary index ${ indexName } (property name: ${ property . nameDb } )` ,
96- )
97- }
98-
99- gsi . partitionKey = property . nameDb
100- break
101- case 'RANGE' :
102- if ( gsi . sortKey ) {
103- throw new Error (
104- `there is already a sort key defined for global secondary index ${ indexName } (property name: ${ property . nameDb } )` ,
105- )
106- }
107-
108- gsi . sortKey = property . nameDb
109- break
110- }
111-
112- map . set ( indexName , gsi )
113- } )
114-
115- return map
116- } , new Map ( ) )
117- } else {
118- return null
119- }
80+ function getGlobalSecondaryIndexes ( properties : Array < PropertyMetadata < any > > ) : Map < string , SecondaryIndex < any > > {
81+ return properties . filter ( testForGSI ) . reduce ( ( map , property ) : Map < string , SecondaryIndex < any > > => {
82+ let gsi : SecondaryIndex < any >
83+ Object . keys ( property . keyForGSI ) . forEach ( indexName => {
84+ if ( map . has ( indexName ) ) {
85+ gsi = map . get ( indexName )
86+ } else {
87+ gsi = < SecondaryIndex < any > > { }
88+ }
89+
90+ switch ( property . keyForGSI [ indexName ] ) {
91+ case 'HASH' :
92+ if ( gsi . partitionKey ) {
93+ throw new Error ( modelErrors . gsiMultiplePk ( indexName , property . nameDb ) )
94+ }
95+ gsi . partitionKey = property . nameDb
96+ break
97+ case 'RANGE' :
98+ if ( gsi . sortKey ) {
99+ throw new Error ( modelErrors . gsiMultipleSk ( indexName , property . nameDb ) )
100+ }
101+ gsi . sortKey = property . nameDb
102+ break
103+ }
104+
105+ map . set ( indexName , gsi )
106+ } )
107+
108+ return map
109+ } , new Map ( ) )
120110}
121111
122112/**
@@ -125,35 +115,27 @@ function getGlobalSecondaryIndexes(properties: Array<PropertyMetadata<any>>): Ma
125115function getLocalSecondaryIndexes (
126116 basePartitionKey : string | null ,
127117 properties : Array < PropertyMetadata < any > > ,
128- ) : Map < string , SecondaryIndex < any > > | null {
129- if ( properties && properties . length ) {
130- return properties . filter ( testForLSI ) . reduce ( ( map , property ) : Map < string , SecondaryIndex < any > > => {
131- let lsi : SecondaryIndex < any >
132-
133- property . sortKeyForLSI . forEach ( indexName => {
134- if ( map . has ( indexName ) ) {
135- throw new Error (
136- `only one sort key can be defined for the same local secondary index, ${ property . nameDb } is already defined as sort key for index ${ indexName } ` ,
137- )
138- }
139-
140- if ( ! basePartitionKey ) {
141- throw new Error (
142- 'a local secondary index requires the partition key to be defined, use the @PartitionKey decorator' ,
143- )
144- }
145-
146- lsi = {
147- partitionKey : basePartitionKey ,
148- sortKey : property . nameDb ,
149- }
150-
151- map . set ( indexName , lsi )
152- } )
153-
154- return map
155- } , new Map ( ) )
156- } else {
157- return null
158- }
118+ ) : Map < string , SecondaryIndex < any > > {
119+ return properties . filter ( testForLSI ) . reduce ( ( map , property ) : Map < string , SecondaryIndex < any > > => {
120+ let lsi : SecondaryIndex < any >
121+
122+ property . sortKeyForLSI . forEach ( indexName => {
123+ if ( map . has ( indexName ) ) {
124+ throw new Error ( modelErrors . lsiMultipleSk ( indexName , property . nameDb ) )
125+ }
126+
127+ if ( ! basePartitionKey ) {
128+ throw new Error ( modelErrors . lsiRequiresPk ( indexName , property . nameDb ) )
129+ }
130+
131+ lsi = {
132+ partitionKey : basePartitionKey ,
133+ sortKey : property . nameDb ,
134+ }
135+
136+ map . set ( indexName , lsi )
137+ } )
138+
139+ return map
140+ } , new Map ( ) )
159141}
0 commit comments