Module: Dynamoid::CDK::Schema

Defined in:
lib/dynamoid/cdk/schema.rb,
lib/dynamoid/cdk/schema.rb,
lib/dynamoid/cdk/schema/version.rb,
lib/dynamoid/cdk/schema/descriptor.rb,
lib/dynamoid/cdk/schema/serializer.rb,
lib/dynamoid/cdk/schema/introspector.rb,
lib/dynamoid/cdk/schema/table_builder.rb,
sig/dynamoid/cdk/schema.rbs

Overview

Generate AWS CDK DynamoDB tables straight from your Dynamoid models, so the models are the single source of truth for keys and indexes and the two can't drift.

Defined Under Namespace

Modules: Introspector, Serializer, TableBuilder Classes: Attribute, Descriptor, Error, Index

Constant Summary collapse

VERSION =

Returns:

  • (String)
"0.2.0"

Class Method Summary collapse

Class Method Details

.describe(model) ⇒ Object

The model's table shape as plain data (a Descriptor). Needs only Dynamoid — handy for inspection, tests, or feeding another tool.



28
29
30
# File 'lib/dynamoid/cdk/schema.rb', line 28

def describe(model)
  Introspector.call(model)
end

.dump(model) ⇒ Object

The model's table shape as plain data that survives JSON — for when the CDK app cannot load the models, and the schema has to travel as a file from build time to synth time. Needs only Dynamoid.

File.write("build/tables.json", JSON.dump(Schema.dump(Post)))


48
49
50
# File 'lib/dynamoid/cdk/schema.rb', line 48

def dump(model)
  Serializer.dump(describe(model))
end

.load(data) ⇒ Object

The inverse: a Descriptor from whatever dump produced.



53
54
55
# File 'lib/dynamoid/cdk/schema.rb', line 53

def load(data)
  Serializer.load(data)
end

.table(scope, id, model, **table_props) ⇒ Object

Build and return a CDK TableV2 for model in scope under id. Any extra keyword arguments pass straight through to TableV2 (e.g. removal_policy:, billing:, point_in_time_recovery:), so the model owns keys and indexes while the caller owns table-level configuration. Requires aws-cdk-lib to be loaded.

Dynamoid::CDK::Schema.table(self, "Posts", Post, removal_policy: ...)


39
40
41
# File 'lib/dynamoid/cdk/schema.rb', line 39

def table(scope, id, model, **table_props)
  TableBuilder.build(scope, id, describe(model), **table_props)
end

.table_from(scope, id, data, **table_props) ⇒ Object

Build a CDK TableV2 from dumped data (or a Descriptor) instead of from a model, so the CDK app needs neither Dynamoid nor the app itself. Table-level props pass through exactly as in table.

Schema.table_from(self, "Posts", JSON.parse(File.read("build/tables.json")))


62
63
64
65
# File 'lib/dynamoid/cdk/schema.rb', line 62

def table_from(scope, id, data, **table_props)
  descriptor = data.is_a?(Descriptor) ? data : load(data)
  TableBuilder.build(scope, id, descriptor, **table_props)
end