Module: Dynamoid::CDK::Schema::Serializer

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

Overview

Descriptor to and from plain data, so the two halves of this gem can run in different processes.

The usual arrangement loads the models at synth time and calls table. That is not always possible: a CDK app may deliberately not depend on the application it deploys — a separate infra bundle, a synth step with no database gems, an app whose models cannot load without config the deploy machine lacks. Then the app dumps its schema during build and the CDK app reads the result:

# in the app, where Dynamoid and the models are loaded
File.write("build/tables.json", JSON.dump(Schema.dump(Post)))

# in the CDK app, which needs neither
Schema.table_from(self, "Posts", JSON.parse(File.read("build/tables.json")))

Keys are strings and values are strings, arrays, hashes or nil, so the result survives a JSON round trip unchanged.

Class Method Summary collapse

Class Method Details

.dump(descriptor) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/dynamoid/cdk/schema/serializer.rb', line 27

def dump(descriptor)
  {
    "partition_key" => dump_attribute(descriptor.partition_key),
    "sort_key" => dump_attribute(descriptor.sort_key),
    "global_secondary_indexes" => descriptor.global_secondary_indexes.map { |i| dump_index(i) },
    "local_secondary_indexes" => descriptor.local_secondary_indexes.map { |i| dump_index(i) }
  }
end

.dump_attribute(attribute) ⇒ Object



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

def dump_attribute(attribute)
  return nil if attribute.nil?

  { "name" => attribute.name, "type" => attribute.type.to_s }
end

.dump_index(index) ⇒ Object



60
61
62
63
64
65
66
67
68
# File 'lib/dynamoid/cdk/schema/serializer.rb', line 60

def dump_index(index)
  {
    "name" => index.name,
    "partition_key" => dump_attribute(index.partition_key),
    "sort_key" => dump_attribute(index.sort_key),
    "projection_type" => index.projection_type.to_s,
    "non_key_attributes" => index.non_key_attributes
  }
end

.load(data) ⇒ Object



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

def load(data)
  data = stringify(data)

  Descriptor.new(
    partition_key: load_attribute(data["partition_key"]),
    sort_key: load_attribute(data["sort_key"]),
    global_secondary_indexes: Array(data["global_secondary_indexes"]).map { |i| load_index(i) },
    local_secondary_indexes: Array(data["local_secondary_indexes"]).map { |i| load_index(i) }
  )
end

.load_attribute(data) ⇒ Object



53
54
55
56
57
58
# File 'lib/dynamoid/cdk/schema/serializer.rb', line 53

def load_attribute(data)
  return nil if data.nil?

  data = stringify(data)
  Attribute.new(name: data.fetch("name"), type: data.fetch("type").to_sym)
end

.load_index(data) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/dynamoid/cdk/schema/serializer.rb', line 70

def load_index(data)
  data = stringify(data)

  Index.new(
    name: data.fetch("name"),
    partition_key: load_attribute(data["partition_key"]),
    sort_key: load_attribute(data["sort_key"]),
    projection_type: data.fetch("projection_type").to_sym,
    non_key_attributes: data["non_key_attributes"]
  )
end

.stringify(data) ⇒ Object

Accept symbol keys too: a descriptor that has not been through JSON is a reasonable thing to hand back, and failing on it would be a trap.



84
85
86
# File 'lib/dynamoid/cdk/schema/serializer.rb', line 84

def stringify(data)
  data.is_a?(Hash) ? data.transform_keys(&:to_s) : data
end