aws / aws-sdk-ruby-record

Official repository for the aws-record gem, an abstraction for Amazon DynamoDB.
Apache License 2.0
318 stars 41 forks source link

"limit" on scan #99

Closed mohie93 closed 4 years ago

mohie93 commented 4 years ago

My code :

Model

class Project
  include Aws::Record
  string_attr     :id, hash_key: true
  string_attr     :title
  string_attr     :description
  string_attr     :status
end

Controller

class ProjectsController < ApplicationController
    def index
     options = {:limit => params[:limit]}
     projects = Project.scan(options)
    end
end

Output All records


Also tried

class ProjectsController < ApplicationController
    def index
     projects = Project.build_scan.limit(params[:limit]).complete!
    end
end

Output All records

But

class ProjectsController < ApplicationController
    def index
     projects = Project.build_scan.limit(params[:limit]).complete!
     projects.page # if i removed this line the Output gonna be all records
     projects
    end
end

Output All records as per limit. Can you help to explain why it works only when .page is used?

alextwoods commented 4 years ago

Appologies for the delay in responding to this! This is expected, but confusing behavior from the Limit parameter on DynamoDB's scan - this limits the maximum number of items returned per page (though the actual number per page may be less than limit if the size exceeds the 1MB limit). The ItemCollection that is returned by the scan in your code is an Enumerable which will keep fetching new pages as they are needed (and will eventually page through all of the items).

One way to get the behavior you want is to use the Enumerable#first method, eg:

class ProjectsController < ApplicationController
    def index
     # This will only request as many pages of results as are required to get params[:limit] items.
     projects = Project.scan.first(params[:limit]) 
    end
end