-
Notifications
You must be signed in to change notification settings - Fork 0
/
NullableAttribute.ts
49 lines (46 loc) · 2.16 KB
/
NullableAttribute.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
import type DynaRecord from "../../DynaRecord";
import Metadata from "../../metadata";
import { type Optional } from "../../types";
import type { AttributeOptions, NotForeignKey } from "../types";
import { type NativeScalarAttributeValue } from "@aws-sdk/util-dynamodb";
/**
* Similar to '@Attribute' but specific to Dates since Dates are not native types to dynamo
*
* IMPORTANT - For optimal type safety mark the class field property as optional
*
* @template T The class type that the decorator is applied to, ensuring type safety and integration within specific class instances.
* @template K A type constraint extending `Date`, ensuring that the decorator is only applied to class fields specifically intended to represent dates.
* @param props An {@link AttributeOptions} object providing configuration options for the attribute, such as its `alias` which allows the attribute to be referred to by an alternative name in the database context. The `nullable` property is also set to `true` by default, indicating that the date attribute can be empty.
* @returns A class field decorator function that operates within the class field's context. It configures the field as a date attribute and defines how it should be serialized and deserialized to/from DynamoDB.
*
* Usage example:
* ```typescript
* class MyEntity extends MyTable {
* @NullableAttribute({ alias: 'MyField' })
* public myField?: string; // Set to Optional
* }
* ```
*
* Here, `@Attribute` decorates `myField` of `MyEntity`, marking it as an entity attribute with an alias 'MyField' for ORM purposes.
*/
function NullableAttribute<
T extends DynaRecord,
K extends NativeScalarAttributeValue
>(props?: AttributeOptions) {
return function (
_value: undefined,
context: ClassFieldDecoratorContext<T, Optional<NotForeignKey<K>>>
) {
if (context.kind === "field") {
context.addInitializer(function () {
const entity: DynaRecord = Object.getPrototypeOf(this);
Metadata.addEntityAttribute(entity.constructor.name, {
attributeName: context.name.toString(),
nullable: true,
...props
});
});
}
};
}
export default NullableAttribute;