-
-
Save tongueroo/bd3d2918d472aeb3cc4db68f49e58bcd to your computer and use it in GitHub Desktop.
dynomite product more low-level examples
This file contains 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 Product < ApplicationItem | |
# field :category, | |
# :product_id, | |
# :product_name, | |
# :price, | |
# :stock_quantity | |
field :stock_quantity | |
validates :price, presence: true | |
before_save :set_stock_quantity | |
def set_stock_quantity | |
self.stock_quantity ||= 1 | |
end | |
class << self | |
# if attribute has index, it must be in **key_condition_expression** | |
# works | |
def query_gsi_category_and_product_name | |
primary_index = indexes.first # category-product_name-index | |
query( | |
index_name: primary_index.index_name, | |
expression_attribute_names: {"#category"=>"category", "#product_name"=>"product_name"}, | |
expression_attribute_values: {":category"=>"Books", ":product_name"=>"Novel"}, | |
key_condition_expression: "#category = :category AND #product_name = :product_name", | |
).map(&:product_name) | |
end | |
# if attribute is primary index, it must be in **key_condition_expression** and no index_name is specified | |
# works | |
def query_primary_composite_key | |
query( | |
# index_name: primary_index.index_name, # DO NOT SPECIFY INDEX_NAME for primary key | |
expression_attribute_names: {"#category"=>"category", "#product_id"=>"product_id"}, | |
expression_attribute_values: {":category"=>"Books", ":product_id"=>30}, | |
key_condition_expression: "#category = :category AND #product_id >= :product_id", | |
).map(&:product_name) | |
end | |
# Product.where(category: "Books").where("stock_quantity > 30").to_a | |
# Product.where(category: "Books").filter_expression("stock_quantity > 30").to_a | |
# works | |
def query_greather_than | |
primary_index = indexes.first | |
puts "index_name: #{primary_index.index_name}" # category-product_name-index | |
query( | |
index_name: primary_index.index_name, | |
filter_expression: "#stock_quantity >= :stock_quantity", | |
expression_attribute_names: {"#category"=>"category", "#stock_quantity"=>"stock_quantity"}, | |
expression_attribute_values: {":category"=>"Books", ":stock_quantity"=>30}, | |
key_condition_expression: "#category = :category", | |
).map(&:product_name) | |
end | |
# Product.where(product_name: ["Smartphone", "Laptop"]).to_a | |
# Product.filter_expression(product_name: ["Smartphone", "Laptop"]).to_a | |
# works | |
def scan_multiple_product_names | |
scan( | |
filter_expression: "#product_name IN (:product_name1, :product_name2)", | |
expression_attribute_names: {"#product_name"=>"product_name"}, | |
expression_attribute_values: {":product_name1"=>"Smartphone",":product_name2"=>"Laptop"}, | |
).map(&:product_name) | |
end | |
def query_first | |
primary_index = indexes.first | |
query( | |
index_name: primary_index.index_name, | |
:expression_attribute_names=>{"#category"=>"category"}, | |
:expression_attribute_values=>{":category"=>"Demo"}, | |
:key_condition_expression=>"#category = :category", | |
:table_name=>"demo-dev_products", | |
scan_index_forward: true, | |
).first | |
end | |
def query_last | |
primary_index = indexes.first | |
query( | |
index_name: primary_index.index_name, | |
:expression_attribute_names=>{"#category"=>"category"}, | |
:expression_attribute_values=>{":category"=>"Demo"}, | |
:key_condition_expression=>"#category = :category", | |
:table_name=>"demo-dev_products", | |
scan_index_forward: false, | |
).first | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment