Dynamoid / dynamoid

Ruby ORM for Amazon's DynamoDB.
MIT License
582 stars 195 forks source link

If I want to update table, how should I do it? #776

Open namdv-1375 opened 4 months ago

namdv-1375 commented 4 months ago
  1. If I want to update the table, how should I do it?
  2. If I want to query using the or condition, how should I write it? it will query like this, example:
    Post.where('id.in': ['id1', 'id2'] or 'user_id.in': ['uid1', 'uid2', 'uid3'])

    Pls help me, thanksss you ! @ckhsponge

andrykonchin commented 4 months ago

If I want to update the table, how should I do it?

Could you clarify what "update the table" mean? A table schema change or, for instance, changing some item's attribute value?

If I want to query using the or condition, how should I write it?

Right now the "or" operator isn't supported. It's planned to add in some of upcoming releases.

Dynamoid ~uses~ used to use legacy parameters QueryFilter and KeyConditions instead of FilterExpression and KeyConditionExpression (the first one does support OR operator)

https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_Query.html

namdv-1375 commented 4 months ago

@andrykonchin

Could you clarify what "update the table" mean? A table schema change or, for instance, changing some item's attribute value?

For example, if I want to add a new field, or add a new global_secondary_index to an existing table, what should I do? :((

andrykonchin commented 4 months ago

There are no any means to administer DynamoDB in Dynamoid, mostly because it's ORM and is focused on data manipulation.

As far as DynamoDB is a schemaless storage - you don't need to add/delete/change attributes explicitly, like you need to do in a relational database. A schema (a primary key) AFAIK cannot be changed for existing table. You mentioned indices - it's the only schema-related thing that probably makes sense to be supported, like ActiveRecord in Rails supports migrations.

TBH it isn't clear to me whether adding migration mechanism to Dynamoid makes sense at all (but I am still considering it) as far as I don't understand clearly the use case. The purpose of the migration mechanism is to be able to create identical table structure in different environments - e.g. test, development, on CI, on staging etc... But the SaaS nature of DynamoDB adds some issues and makes setting up a new environment more difficult so it's probably is being performed manually.

namdv-1375 commented 4 months ago

@andrykonchin Let me ask, I tried the following query but got an error. How should I correct the syntax? Please help me! Thanks

Dynamoid::Adapter.new.query("posts", {key_condition_expression: "user_id = :user_id", filter_expression: "id = :id OR category_id = :category_id", expression_attribute_values: {:user_id => "uid1", :id => "postid1", :category_id => "cateid2"}}).to_h
NoMethodError: undefined method `map' for an instance of String
andrykonchin commented 4 months ago

The adapter's query method returns Enumerator that contains pages (DynamoDB always return paged result as far as a page size is limited to 1 MB). A page is represented as an items Array + Hash with an optional key :last_evaluated_key.

So you can just either materialise Enumerator into Array or iterate it lazily with each:

enum = Dynamoid::Adapter.new.query(...)

# option 1
pages = enum.to_a

# option 2
enum.each do |items, options|
end