-
Notifications
You must be signed in to change notification settings - Fork 0
/
SortKeyAttribute.ts
44 lines (41 loc) · 1.47 KB
/
SortKeyAttribute.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 SortKey } from "../../types";
import type { AttributeOptions } from "../types";
/**
* A decorator for designating the field for the sort 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 sort key attribute, such as custom metadata.
* @returns A class field decorator function that targets and initializes the class's prototype to register the sort key with the ORM's metadata system.
*
* Usage example:
* ```typescript
* class User extends BaseEntity {
* @SortKeyAttribute()
* public sk: SortKey;
* }
* ```
*
* In this example, `@SortKeyAttribute` decorates the `sk` field of `User`, marking it as the entity's sort key.
*/
function SortKeyAttribute<T extends DynaRecord, K extends SortKey>(
props?: AttributeOptions
) {
return function (
_value: undefined,
context: ClassFieldDecoratorContext<T, K>
) {
if (context.kind === "field") {
context.addInitializer(function () {
const entity: DynaRecord = Object.getPrototypeOf(this);
Metadata.addSortKeyAttribute(entity, {
attributeName: context.name.toString(),
...props
});
});
}
};
}
export default SortKeyAttribute;