-
Notifications
You must be signed in to change notification settings - Fork 0
/
PartitionKeyAttribute.ts
44 lines (41 loc) · 1.52 KB
/
PartitionKeyAttribute.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
import type DynaRecord from "../../DynaRecord";
import Metadata from "../../metadata";
import { type PartitionKey } from "../../types";
import type { AttributeOptions } from "../types";
/**
* A decorator for designating the field for the partition key on the dynamo table.
*
* @template T The entity to which the decorator is applied.
* @template K The type constraint ensuring the field is suitable to be a partition key.
* @param props An optional object of {@link AttributeOptions}, providing additional configuration for the partition key attribute, such as custom metadata.
* @returns A class field decorator function that targets and initializes the class's prototype to register the partition key with the ORM's metadata system.
*
* Usage example:
* ```typescript
* class User extends BaseEntity {
* @PartitionKeyAttribute()
* public pk: PartitionKey;
* }
* ```
*
* In this example, `@PartitionKeyAttribute` decorates the `pk` field of `User`, marking it as the entity's partition key.
*/
function PartitionKeyAttribute<T extends DynaRecord, K extends PartitionKey>(
props?: AttributeOptions
) {
return function (
_value: undefined,
context: ClassFieldDecoratorContext<T, K>
) {
if (context.kind === "field") {
context.addInitializer(function () {
const entity: DynaRecord = Object.getPrototypeOf(this);
Metadata.addPartitionKeyAttribute(entity, {
attributeName: context.name.toString(),
...props
});
});
}
};
}
export default PartitionKeyAttribute;