Module: Dynamoid::CDK::Schema::TableBuilder

Defined in:
lib/dynamoid/cdk/schema/table_builder.rb

Overview

Turns a Descriptor into an AWS CDK TableV2. AWS CDK is the caller's dependency (the Ruby CDK, from its preview feed), so it is referenced lazily at build time — the Introspector half of the gem works without it, and a clear error is raised if .table is called without it loaded.

Class Method Summary collapse

Class Method Details

.build(scope, id, descriptor, **table_props) ⇒ Object



13
14
15
16
# File 'lib/dynamoid/cdk/schema/table_builder.rb', line 13

def build(scope, id, descriptor, **table_props)
  props = table_props.merge(key_props(descriptor), index_props(descriptor))
  dynamodb::TableV2.new(scope, id, props)
end

.cdk_attribute(attr) ⇒ Object



33
34
35
# File 'lib/dynamoid/cdk/schema/table_builder.rb', line 33

def cdk_attribute(attr)
  { name: attr.name, type: cdk_attribute_type(attr.type) }
end

.cdk_attribute_type(type) ⇒ Object



58
59
60
61
62
63
64
65
# File 'lib/dynamoid/cdk/schema/table_builder.rb', line 58

def cdk_attribute_type(type)
  case type
  when :string then dynamodb::AttributeType::STRING
  when :number then dynamodb::AttributeType::NUMBER
  when :binary then dynamodb::AttributeType::BINARY
  else raise Error, "unsupported key attribute type #{type.inspect}"
  end
end

.cdk_gsi(index) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/dynamoid/cdk/schema/table_builder.rb', line 37

def cdk_gsi(index)
  props = {
    index_name: index.name,
    partition_key: cdk_attribute(index.partition_key),
    projection_type: cdk_projection_type(index.projection_type)
  }
  props[:sort_key] = cdk_attribute(index.sort_key) if index.sort_key
  props[:non_key_attributes] = index.non_key_attributes if index.non_key_attributes
  props
end

.cdk_lsi(index) ⇒ Object



48
49
50
51
52
53
54
55
56
# File 'lib/dynamoid/cdk/schema/table_builder.rb', line 48

def cdk_lsi(index)
  props = {
    index_name: index.name,
    sort_key: cdk_attribute(index.sort_key),
    projection_type: cdk_projection_type(index.projection_type)
  }
  props[:non_key_attributes] = index.non_key_attributes if index.non_key_attributes
  props
end

.cdk_projection_type(type) ⇒ Object



67
68
69
70
71
72
73
74
# File 'lib/dynamoid/cdk/schema/table_builder.rb', line 67

def cdk_projection_type(type)
  case type
  when :all then dynamodb::ProjectionType::ALL
  when :keys_only then dynamodb::ProjectionType::KEYS_ONLY
  when :include then dynamodb::ProjectionType::INCLUDE
  else raise Error, "unsupported projection type #{type.inspect}"
  end
end

.dynamodbObject

Raises:



76
77
78
79
80
81
# File 'lib/dynamoid/cdk/schema/table_builder.rb', line 76

def dynamodb
  return AWSCDK::DynamoDB if defined?(AWSCDK::DynamoDB)

  raise Error,
        "aws-cdk-lib must be loaded to build tables — add it to your CDK app and `require \"aws-cdk-lib\"`"
end

.index_props(descriptor) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/dynamoid/cdk/schema/table_builder.rb', line 24

def index_props(descriptor)
  props = {}
  gsis = descriptor.global_secondary_indexes
  props[:global_secondary_indexes] = gsis.map { |i| cdk_gsi(i) } if gsis.any?
  lsis = descriptor.local_secondary_indexes
  props[:local_secondary_indexes] = lsis.map { |i| cdk_lsi(i) } if lsis.any?
  props
end

.key_props(descriptor) ⇒ Object



18
19
20
21
22
# File 'lib/dynamoid/cdk/schema/table_builder.rb', line 18

def key_props(descriptor)
  props = { partition_key: cdk_attribute(descriptor.partition_key) }
  props[:sort_key] = cdk_attribute(descriptor.sort_key) if descriptor.sort_key
  props
end