Created
March 23, 2017 10:59
-
-
Save 8parth/4fac3ea278c45feddeef3a965c001ff3 to your computer and use it in GitHub Desktop.
Example of has_many with dynamodb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # fetch a movie | |
| m = Movie.first | |
| # fetch songs | |
| songs = m.get_songs |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Movie | |
| include ActiveModel::Model, ActiveModel::Serialization | |
| attr_accessor :name | |
| attr_accessor :year | |
| attr_accessor :released | |
| attr_accessor :created_at | |
| attr_accessor :actor | |
| attr_accessor :actress | |
| attr_accessor :songs_ids | |
| attr_accessor :updated_at | |
| def self.migrate_table | |
| migration = Aws::Record::TableMigration.new(self) | |
| migration.create!( | |
| provisioned_throughput: { | |
| read_capacity_units: 1, | |
| write_capacity_units: 1 | |
| }, | |
| global_secondary_index_throughput: { | |
| actor_actress_index: { | |
| read_capacity_units: 1, | |
| write_capacity_units: 1 | |
| }, | |
| } | |
| ) | |
| migration.wait_until_available | |
| end | |
| def self.first | |
| movie = $ddb.scan({ | |
| table_name: "movies", | |
| limit: 1 | |
| }).items.first | |
| Movie.new(movie) | |
| end | |
| def get_songs | |
| song_names = self.songs_ids.to_a | |
| expression_attrs = [] | |
| song_names.each do |name| | |
| expression_attrs << { "name" => name } | |
| end | |
| batch_get_query = { | |
| request_items: { | |
| "songs" => { | |
| keys: expression_attrs | |
| } | |
| } | |
| } | |
| songs_array = $ddb.batch_get_item(batch_get_query).responses["songs"] | |
| songs_array.map { |song| Song.new(song) } | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Song | |
| include ActiveModel::Model, ActiveModel::Serialization | |
| attr_accessor :name | |
| attr_accessor :movie_ids | |
| attr_accessor :created_at | |
| attr_accessor :updated_at | |
| def self.migrate_table | |
| migration = Aws::Record::TableMigration.new(self) | |
| migration.create!( | |
| provisioned_throughput: { | |
| read_capacity_units: 1, | |
| write_capacity_units: 1 | |
| }, | |
| global_secondary_index_throughput: { | |
| actor_actress_index: { | |
| read_capacity_units: 1, | |
| write_capacity_units: 1 | |
| }, | |
| } | |
| ) | |
| migration.wait_until_available | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment