Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
namdv-1375 opened this issue Jul 10, 2024 · 5 comments
Open

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

namdv-1375 opened this issue Jul 10, 2024 · 5 comments

Comments

@namdv-1375
Copy link

namdv-1375 commented Jul 10, 2024

  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
Copy link
Member

andrykonchin commented Jul 10, 2024

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
Copy link
Author

@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
Copy link
Member

andrykonchin commented Jul 11, 2024

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
Copy link
Author

namdv-1375 commented Jul 15, 2024

@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
Copy link
Member

andrykonchin commented Jul 15, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants