dyna-record 0.1.3 → 0.1.4

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/README.md CHANGED
@@ -142,6 +142,7 @@ Use the attribute decorators below to define attributes on a model. The decorato
142
142
  - [@NumberAttribute](https://dyna-record.com/functions/NumberAttribute.html)
143
143
  - [@BooleanAttribute](https://dyna-record.com/functions/BooleanAttribute.html)
144
144
  - [@DateAttribute](https://dyna-record.com/functions/DateAttribute.html)
145
+ - [@EnumAttribute](https://dyna-record.com/functions/EnumAttribute.html)
145
146
 
146
147
  - The [alias](https://dyna-record.com/interfaces/AttributeOptions.html#alias) option allows you to specify the attribute name as it appears in the DynamoDB table, different from your class property name.
147
148
  - Set nullable attributes as optional for optimal type safety
@@ -0,0 +1,32 @@
1
+ import type DynaRecord from "../../DynaRecord";
2
+ import type { AttributeDecoratorContext, AttributeOptions } from "../types";
3
+ /**
4
+ * Extends {@link AttributeOptions} but requires that the allowed values are set on `values`
5
+ */
6
+ export interface EnumAttributeOptions extends AttributeOptions {
7
+ values: [string, ...string[]];
8
+ }
9
+ /**
10
+ * A decorator for marking class fields as enum attributes within the context of a single-table design entity. Only the types specified in `values` are allowed via both the type system and runtime schema checks.
11
+ *
12
+ * This enforces a union type of the specified fields listed in `values`
13
+ *
14
+ * @template T The entity the decorator is applied to.
15
+ * @template K The type of the attribute, restricted to the values listed in `values` prop of EnumAttributeOptions
16
+ * @param props An optional object of {@link EnumAttributeOptions}, including the allowed values, configuration options such as metadata attributes and additional property characteristics.
17
+ * @returns A class field decorator function that targets and initializes the class's prototype to register the attribute with the ORM's metadata system, ensuring proper handling and validation of the entity's enum values.
18
+ *
19
+ * Usage example:
20
+ * ```typescript
21
+ * class Product extends BaseEntity {
22
+ * @EnumAttribute({ alias: 'SomeField', values: ["val-1", "val-2"] })
23
+ * public someField: "val-1" | "val-2"; // Attribute representing the union/enum types specified in `values`. In this case the only allowed values are "val-1" and "val-2"
24
+ *
25
+ * @EnumAttribute({ alias: 'MyNullableField', nullable: true, values: ["val-1", "val-2"] })
26
+ * public myNullableField?: "val-1" | "val-2"; // Set to Optional for nullable attributes
27
+ * }
28
+ * ```
29
+ */
30
+ declare function EnumAttribute<T extends DynaRecord, const K extends P["values"][number], const P extends EnumAttributeOptions>(props: P): (_value: undefined, context: AttributeDecoratorContext<T, K, P>) => void;
31
+ export default EnumAttribute;
32
+ //# sourceMappingURL=EnumAttribute.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"EnumAttribute.d.ts","sourceRoot":"","sources":["../../../../src/decorators/attributes/EnumAttribute.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,UAAU,MAAM,kBAAkB,CAAC;AAE/C,OAAO,KAAK,EAAE,yBAAyB,EAAE,gBAAgB,EAAE,MAAM,UAAU,CAAC;AAE5E;;GAEG;AACH,MAAM,WAAW,oBAAqB,SAAQ,gBAAgB;IAG5D,MAAM,EAAE,CAAC,MAAM,EAAE,GAAG,MAAM,EAAE,CAAC,CAAC;CAC/B;AAED;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,iBAAS,aAAa,CACpB,CAAC,SAAS,UAAU,EACpB,KAAK,CAAC,CAAC,SAAS,CAAC,CAAC,QAAQ,CAAC,CAAC,MAAM,CAAC,EACnC,KAAK,CAAC,CAAC,SAAS,oBAAoB,EACpC,KAAK,EAAE,CAAC,YAEE,SAAS,WACR,0BAA0B,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,UAe9C;AAED,eAAe,aAAa,CAAC"}
@@ -0,0 +1,44 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const zod_1 = require("zod");
7
+ const metadata_1 = __importDefault(require("../../metadata"));
8
+ /**
9
+ * A decorator for marking class fields as enum attributes within the context of a single-table design entity. Only the types specified in `values` are allowed via both the type system and runtime schema checks.
10
+ *
11
+ * This enforces a union type of the specified fields listed in `values`
12
+ *
13
+ * @template T The entity the decorator is applied to.
14
+ * @template K The type of the attribute, restricted to the values listed in `values` prop of EnumAttributeOptions
15
+ * @param props An optional object of {@link EnumAttributeOptions}, including the allowed values, configuration options such as metadata attributes and additional property characteristics.
16
+ * @returns A class field decorator function that targets and initializes the class's prototype to register the attribute with the ORM's metadata system, ensuring proper handling and validation of the entity's enum values.
17
+ *
18
+ * Usage example:
19
+ * ```typescript
20
+ * class Product extends BaseEntity {
21
+ * @EnumAttribute({ alias: 'SomeField', values: ["val-1", "val-2"] })
22
+ * public someField: "val-1" | "val-2"; // Attribute representing the union/enum types specified in `values`. In this case the only allowed values are "val-1" and "val-2"
23
+ *
24
+ * @EnumAttribute({ alias: 'MyNullableField', nullable: true, values: ["val-1", "val-2"] })
25
+ * public myNullableField?: "val-1" | "val-2"; // Set to Optional for nullable attributes
26
+ * }
27
+ * ```
28
+ */
29
+ function EnumAttribute(props) {
30
+ return function (_value, context) {
31
+ if (context.kind === "field") {
32
+ context.addInitializer(function () {
33
+ const entity = Object.getPrototypeOf(this);
34
+ metadata_1.default.addEntityAttribute(entity.constructor.name, {
35
+ attributeName: context.name.toString(),
36
+ nullable: props?.nullable,
37
+ type: zod_1.z.enum(props.values),
38
+ ...props
39
+ });
40
+ });
41
+ }
42
+ };
43
+ }
44
+ exports.default = EnumAttribute;
@@ -5,5 +5,6 @@ export { default as DateAttribute } from "./DateAttribute";
5
5
  export { default as StringAttribute } from "./StringAttribute";
6
6
  export { default as BooleanAttribute } from "./BooleanAttribute";
7
7
  export { default as NumberAttribute } from "./NumberAttribute";
8
+ export { default as EnumAttribute } from "./EnumAttribute";
8
9
  export * from "./serializers";
9
10
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/decorators/attributes/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,IAAI,qBAAqB,EAAE,MAAM,yBAAyB,CAAC;AAC3E,OAAO,EAAE,OAAO,IAAI,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AACjE,OAAO,EAAE,OAAO,IAAI,mBAAmB,EAAE,MAAM,uBAAuB,CAAC;AACvE,OAAO,EAAE,OAAO,IAAI,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAC3D,OAAO,EAAE,OAAO,IAAI,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAC/D,OAAO,EAAE,OAAO,IAAI,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AACjE,OAAO,EAAE,OAAO,IAAI,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAC/D,cAAc,eAAe,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/decorators/attributes/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,IAAI,qBAAqB,EAAE,MAAM,yBAAyB,CAAC;AAC3E,OAAO,EAAE,OAAO,IAAI,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AACjE,OAAO,EAAE,OAAO,IAAI,mBAAmB,EAAE,MAAM,uBAAuB,CAAC;AACvE,OAAO,EAAE,OAAO,IAAI,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAC3D,OAAO,EAAE,OAAO,IAAI,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAC/D,OAAO,EAAE,OAAO,IAAI,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AACjE,OAAO,EAAE,OAAO,IAAI,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAC/D,OAAO,EAAE,OAAO,IAAI,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAC3D,cAAc,eAAe,CAAC"}
@@ -17,7 +17,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
17
17
  return (mod && mod.__esModule) ? mod : { "default": mod };
18
18
  };
19
19
  Object.defineProperty(exports, "__esModule", { value: true });
20
- exports.NumberAttribute = exports.BooleanAttribute = exports.StringAttribute = exports.DateAttribute = exports.ForeignKeyAttribute = exports.SortKeyAttribute = exports.PartitionKeyAttribute = void 0;
20
+ exports.EnumAttribute = exports.NumberAttribute = exports.BooleanAttribute = exports.StringAttribute = exports.DateAttribute = exports.ForeignKeyAttribute = exports.SortKeyAttribute = exports.PartitionKeyAttribute = void 0;
21
21
  var PartitionKeyAttribute_1 = require("./PartitionKeyAttribute");
22
22
  Object.defineProperty(exports, "PartitionKeyAttribute", { enumerable: true, get: function () { return __importDefault(PartitionKeyAttribute_1).default; } });
23
23
  var SortKeyAttribute_1 = require("./SortKeyAttribute");
@@ -32,4 +32,6 @@ var BooleanAttribute_1 = require("./BooleanAttribute");
32
32
  Object.defineProperty(exports, "BooleanAttribute", { enumerable: true, get: function () { return __importDefault(BooleanAttribute_1).default; } });
33
33
  var NumberAttribute_1 = require("./NumberAttribute");
34
34
  Object.defineProperty(exports, "NumberAttribute", { enumerable: true, get: function () { return __importDefault(NumberAttribute_1).default; } });
35
+ var EnumAttribute_1 = require("./EnumAttribute");
36
+ Object.defineProperty(exports, "EnumAttribute", { enumerable: true, get: function () { return __importDefault(EnumAttribute_1).default; } });
35
37
  __exportStar(require("./serializers"), exports);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dyna-record",
3
- "version": "0.1.3",
3
+ "version": "0.1.4",
4
4
  "description": "Typescript Object Relational Mapper (ORM) for Dynamo",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",