@jaypie/constructs 1.2.42 → 1.2.43

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.
@@ -6,25 +6,23 @@ export interface JaypieDynamoDbProps extends Omit<dynamodb.TablePropsV2, "global
6
6
  /**
7
7
  * Configure GSIs for the table using @jaypie/fabric IndexDefinition format.
8
8
  * - `undefined`: No GSIs (default)
9
- * - Array of IndexDefinition: Use the specified indexes
9
+ * - Array of IndexDefinition: Use the specified indexes (prefer fabricIndex())
10
10
  *
11
11
  * @example
12
12
  * // No GSIs (default)
13
13
  * new JaypieDynamoDb(this, "myTable");
14
14
  *
15
15
  * @example
16
- * // With custom indexes
16
+ * // With fabricIndex-shaped indexes
17
+ * import { fabricIndex } from "@jaypie/fabric";
17
18
  * new JaypieDynamoDb(this, "myTable", {
18
- * indexes: [
19
- * { pk: ["scope", "model"], sk: ["sequence"] },
20
- * { pk: ["scope", "model", "type"], sk: ["sequence"], sparse: true },
21
- * ],
19
+ * indexes: [fabricIndex(), fabricIndex("alias"), fabricIndex("xid")],
22
20
  * });
23
21
  */
24
22
  indexes?: IndexDefinition[];
25
23
  /**
26
24
  * Partition key attribute definition.
27
- * @default { name: "model", type: AttributeType.STRING }
25
+ * @default { name: "id", type: AttributeType.STRING }
28
26
  */
29
27
  partitionKey?: dynamodb.Attribute;
30
28
  /**
@@ -40,8 +38,8 @@ export interface JaypieDynamoDbProps extends Omit<dynamodb.TablePropsV2, "global
40
38
  */
41
39
  service?: string;
42
40
  /**
43
- * Sort key attribute definition.
44
- * @default { name: "id", type: AttributeType.STRING }
41
+ * Sort key attribute definition. Defaults to `undefined` (no sort key) —
42
+ * the Jaypie single-table pattern uses `id` as a unique partition key.
45
43
  */
46
44
  sortKey?: dynamodb.Attribute;
47
45
  /**
@@ -53,8 +51,7 @@ export interface JaypieDynamoDbProps extends Omit<dynamodb.TablePropsV2, "global
53
51
  * DynamoDB table with Jaypie single-table design patterns.
54
52
  *
55
53
  * Creates a table with:
56
- * - Partition key: `model` (String)
57
- * - Sort key: `id` (String)
54
+ * - Partition key: `id` (String), no sort key
58
55
  * - Billing: PAY_PER_REQUEST (on-demand)
59
56
  * - Removal policy: RETAIN in production, DESTROY otherwise
60
57
  * - No GSIs by default (use `indexes` prop to add them)
@@ -65,13 +62,11 @@ export interface JaypieDynamoDbProps extends Omit<dynamodb.TablePropsV2, "global
65
62
  * const table = new JaypieDynamoDb(this, "myApp");
66
63
  *
67
64
  * @example
68
- * // With explicit table name (overrides CDK-generated name)
65
+ * // With fabricIndex() for GSIs
66
+ * import { fabricIndex } from "@jaypie/fabric";
69
67
  * const table = new JaypieDynamoDb(this, "MyTable", {
70
68
  * tableName: "custom-table-name",
71
- * indexes: [
72
- * { pk: ["scope", "model"] },
73
- * { pk: ["scope", "model", "type"], sparse: true },
74
- * ],
69
+ * indexes: [fabricIndex(), fabricIndex("alias"), fabricIndex("xid")],
75
70
  * });
76
71
  */
77
72
  export declare class JaypieDynamoDb extends Construct implements dynamodb.ITableV2 {
package/dist/esm/index.js CHANGED
@@ -26,7 +26,7 @@ import * as cloudfront from 'aws-cdk-lib/aws-cloudfront';
26
26
  import * as origins from 'aws-cdk-lib/aws-cloudfront-origins';
27
27
  import * as wafv2 from 'aws-cdk-lib/aws-wafv2';
28
28
  import * as dynamodb from 'aws-cdk-lib/aws-dynamodb';
29
- import { generateIndexName, DEFAULT_SORT_KEY } from '@jaypie/fabric';
29
+ import { getGsiAttributeNames } from '@jaypie/fabric';
30
30
  import * as cr from 'aws-cdk-lib/custom-resources';
31
31
  import { Nextjs } from 'cdk-nextjs-standalone';
32
32
  import * as path from 'path';
@@ -2895,41 +2895,39 @@ class JaypieDnsRecord extends Construct {
2895
2895
  }
2896
2896
  }
2897
2897
 
2898
- //
2899
- //
2900
- // Constants
2901
- //
2902
- /** Composite key separator used in GSI partition keys */
2903
- const SEPARATOR = "#";
2904
2898
  //
2905
2899
  //
2906
2900
  // Helper Functions
2907
2901
  //
2908
2902
  /**
2909
- * Convert IndexDefinition[] from @jaypie/fabric to CDK GlobalSecondaryIndexPropsV2[]
2903
+ * Convert IndexDefinition[] from @jaypie/fabric to CDK GlobalSecondaryIndexPropsV2[].
2910
2904
  *
2911
- * @param indexes - Array of IndexDefinition from @jaypie/fabric
2912
- * @returns Array of CDK GlobalSecondaryIndexPropsV2
2905
+ * Uses `getGsiAttributeNames` as the single source of truth so runtime writes
2906
+ * and CDK provisioning agree on attribute names. Composite sk indexes
2907
+ * (sk.length > 1) get a dedicated STRING `{indexName}Sk` attribute; single-field
2908
+ * sk indexes reference the field directly (STRING in the general case, NUMBER
2909
+ * for the legacy `sequence` name).
2913
2910
  */
2914
2911
  function indexesToGsi(indexes) {
2915
2912
  return indexes.map((index) => {
2916
- // Generate index name from pk fields if not provided
2917
- const indexName = index.name ?? generateIndexName(index.pk);
2918
- // Sort key defaults to ["sequence"] if not provided
2919
- const skFields = index.sk ?? DEFAULT_SORT_KEY;
2913
+ const { pk, sk } = getGsiAttributeNames(index);
2914
+ let sortKey;
2915
+ if (sk) {
2916
+ if (sk === "sequence") {
2917
+ sortKey = { name: "sequence", type: dynamodb.AttributeType.NUMBER };
2918
+ }
2919
+ else {
2920
+ sortKey = { name: sk, type: dynamodb.AttributeType.STRING };
2921
+ }
2922
+ }
2920
2923
  return {
2921
- indexName,
2924
+ indexName: pk,
2922
2925
  partitionKey: {
2923
- name: indexName,
2926
+ name: pk,
2924
2927
  type: dynamodb.AttributeType.STRING,
2925
2928
  },
2926
2929
  projectionType: dynamodb.ProjectionType.ALL,
2927
- sortKey: skFields.length === 1 && skFields[0] === "sequence"
2928
- ? { name: "sequence", type: dynamodb.AttributeType.NUMBER }
2929
- : {
2930
- name: skFields.join(SEPARATOR),
2931
- type: dynamodb.AttributeType.STRING,
2932
- },
2930
+ ...(sortKey && { sortKey }),
2933
2931
  };
2934
2932
  });
2935
2933
  }
@@ -2941,8 +2939,7 @@ function indexesToGsi(indexes) {
2941
2939
  * DynamoDB table with Jaypie single-table design patterns.
2942
2940
  *
2943
2941
  * Creates a table with:
2944
- * - Partition key: `model` (String)
2945
- * - Sort key: `id` (String)
2942
+ * - Partition key: `id` (String), no sort key
2946
2943
  * - Billing: PAY_PER_REQUEST (on-demand)
2947
2944
  * - Removal policy: RETAIN in production, DESTROY otherwise
2948
2945
  * - No GSIs by default (use `indexes` prop to add them)
@@ -2953,13 +2950,11 @@ function indexesToGsi(indexes) {
2953
2950
  * const table = new JaypieDynamoDb(this, "myApp");
2954
2951
  *
2955
2952
  * @example
2956
- * // With explicit table name (overrides CDK-generated name)
2953
+ * // With fabricIndex() for GSIs
2954
+ * import { fabricIndex } from "@jaypie/fabric";
2957
2955
  * const table = new JaypieDynamoDb(this, "MyTable", {
2958
2956
  * tableName: "custom-table-name",
2959
- * indexes: [
2960
- * { pk: ["scope", "model"] },
2961
- * { pk: ["scope", "model", "type"], sparse: true },
2962
- * ],
2957
+ * indexes: [fabricIndex(), fabricIndex("alias"), fabricIndex("xid")],
2963
2958
  * });
2964
2959
  */
2965
2960
  class JaypieDynamoDb extends Construct {
@@ -2971,11 +2966,11 @@ class JaypieDynamoDb extends Construct {
2971
2966
  const constructId = isShorthand ? `JaypieDynamoDb-${id}` : id;
2972
2967
  super(scope, constructId);
2973
2968
  const { billing = dynamodb.Billing.onDemand(), indexes, partitionKey = {
2974
- name: "model",
2969
+ name: "id",
2975
2970
  type: dynamodb.AttributeType.STRING,
2976
2971
  }, project, removalPolicy = isProductionEnv()
2977
2972
  ? RemovalPolicy.RETAIN
2978
- : RemovalPolicy.DESTROY, roleTag = CDK$2.ROLE.STORAGE, service, sortKey = { name: "id", type: dynamodb.AttributeType.STRING }, vendorTag, ...restProps } = props;
2973
+ : RemovalPolicy.DESTROY, roleTag = CDK$2.ROLE.STORAGE, service, sortKey, vendorTag, ...restProps } = props;
2979
2974
  // Convert IndexDefinition[] to CDK GSI props
2980
2975
  const globalSecondaryIndexes = indexes ? indexesToGsi(indexes) : undefined;
2981
2976
  this._table = new dynamodb.TableV2(this, "Table", {
@@ -2983,7 +2978,7 @@ class JaypieDynamoDb extends Construct {
2983
2978
  globalSecondaryIndexes,
2984
2979
  partitionKey,
2985
2980
  removalPolicy,
2986
- sortKey,
2981
+ ...(sortKey && { sortKey }),
2987
2982
  ...restProps,
2988
2983
  });
2989
2984
  // Apply tags