-
-
Notifications
You must be signed in to change notification settings - Fork 197
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Adapter#execute method to run PartiQL queries
- Loading branch information
1 parent
a0b900e
commit e015d51
Showing
6 changed files
with
194 additions
and
1 deletion.
There are no files selected for viewing
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
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
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
62 changes: 62 additions & 0 deletions
62
lib/dynamoid/adapter_plugin/aws_sdk_v3/execute_statement.rb
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# frozen_string_literal: true | ||
|
||
module Dynamoid | ||
# @private | ||
module AdapterPlugin | ||
class AwsSdkV3 | ||
# Excecute a PartiQL query | ||
# | ||
# Documentation: | ||
# - https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_ExecuteStatement.html | ||
# - https://docs.aws.amazon.com/sdk-for-ruby/v3/api/Aws/DynamoDB/Client.html#execute_statement-instance_method | ||
# | ||
# NOTE: For reads result may be paginated. Only pagination with NextToken | ||
# is implemented. Currently LastEvaluatedKey in response cannot be fed to | ||
# ExecuteStatement to get the next page. | ||
# | ||
# See also: | ||
# - https://repost.aws/questions/QUgNPbBYWiRoOlMsJv-XzrWg/how-to-use-last-evaluated-key-in-execute-statement-request | ||
# - https://stackoverflow.com/questions/71438439/aws-dynamodb-executestatement-pagination | ||
class ExecuteStatement | ||
attr_reader :client, :statement, :parameters, :options | ||
|
||
def initialize(client, statement, parameters, options) | ||
@client = client | ||
@statement = statement | ||
@parameters = parameters | ||
@options = options.symbolize_keys.slice(:consistent_read) | ||
end | ||
|
||
def call | ||
request = { | ||
statement: @statement, | ||
parameters: @parameters, | ||
consistent_read: @options[:consistent_read], | ||
} | ||
|
||
response = client.execute_statement(request) | ||
|
||
if !response.next_token | ||
return response_to_items(response) | ||
end | ||
|
||
Enumerator.new do |yielder| | ||
yielder.yield(response_to_items(response)) | ||
|
||
while response.next_token | ||
request[:next_token] = response.next_token | ||
response = client.execute_statement(request) | ||
yielder.yield(response_to_items(response)) | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def response_to_items(response) | ||
response.items.map(&:symbolize_keys) | ||
end | ||
end | ||
end | ||
end | ||
end |
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
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