Coder Social home page Coder Social logo

Comments (9)

baopham avatar baopham commented on May 20, 2024 3

Pagination isn't supported right now. But try chunk - it's the closest thing to pagination depending on your needs?

from laravel-dynamodb.

baopham avatar baopham commented on May 20, 2024 2

Oops, closing to soon. I will mark this as a feature request.

Pagination isn't supported but I would love to have it implemented. PR is welcome of course.

Related issue: #15

from laravel-dynamodb.

asantibanez avatar asantibanez commented on May 20, 2024

Pagination is supported in DynamoDB in the form of LastEvaluatedKey in the result set and ExclusiveStartKey when making a query or scan. See http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/QueryAndScan.html#Pagination

from laravel-dynamodb.

baopham avatar baopham commented on May 20, 2024

Yep. We just need to get it in here :)

from laravel-dynamodb.

zoul0813 avatar zoul0813 commented on May 20, 2024

A step forward for pagination?

https://github.com/baopham/laravel-dynamodb/compare/master...zoul0813:feature/skip-offset?expand=1

I added support for skip/offset and introduced setLastEvaluatedKey to the QueryBuilder. I also refactored the getCompositeKey method and added getCompositeKeyName to match the Eloquent getKey and getKeyName methods.

This doesn't introduce pagination, but allows you to use the primaryKey/compositeKey of a model to "skip".

For example, when calling chunk to walk through large datasets ... in the event of an error (ie; provisioned capacity issues) you can use the primaryKey/compositeKey of the last model evaluated as the lastEvaluatedKey and call chunk again, skipping anything you've already gone through.

I'm using this in a custom artisan command to reindex my DynamoDB Items in ElasticSearch. I occasionally run into provisioned capacity errors, and introducing this skip/offset modification has allowed me to "resume" after an error without having to go through all the items I've already processed.

Example of my "elasticsearch:reindex" command, taking advantage of "skip". I output the current models primaryKey/compositeKey values to the console so I can copy/paste them and pass them to the "--skip" parameter of the command to "resume".

<?php
namespace App\Console\Commands;

use Illuminate\Support\Facades\App;
use Illuminate\Console\Command;

use Exception;

class ElasticSearchReindex extends Command
{
  protected $signature = 'elasticsearch:reindex {model} {--size=100} {--rebuild} {--skip=*}';
  protected $description = 'DynamoDB Table Clear';

  public function handle()
  {
    $model = 'App\\' . $this->argument('model');
    $pageSize = (int)$this->option('size');
    $skip = $this->option('skip', null);

    $rebuild = filter_var($this->option('rebuild', false), FILTER_VALIDATE_BOOLEAN);

    if(!class_exists($model)) {
      $this->error('Invalid Class: ' . $model);
      die();
    }

    if($rebuild) {
      try {
        $this->warn('Deleting and rebuilding index');
        $model::deleteIndex();
        $model::createIndex();
        $model::putMapping();
        $this->info("Index Rebuilt (delete, create, put)");
      } catch(Exception $e) {
        $this->error($e->getMessage());
        die();
      }
    }

    // if $skip is empty, then lastEvaluatedKey is set to null
    $query = $model::skip($skip);

    $this->warn('pageSize: ' . $pageSize . ', skipping: ' . print_r($skip, true) . ', lastEvaluatedKey: ' . print_r($query->getLastEvaluatedKey(), true));

    try {
      $query->chunk($pageSize, function($models) {
        static $total = 0;
        $this->info('Total: ' . $total . ', Found: ' . count($models));
        foreach($models as $item) {
          try {
            $item->addToIndex();
            $this->line('Indexed: ' . $item->getKey() . ', ' . json_encode($item->getCompositeKey()) . ', ' . json_encode($item->getCompositeKeyValue()));
          } catch(Exception $e) {
            $this->error("EXCEPTION: " . $item->getKey() . ', ' . $e->getMessage() . ")");
          }
        }
        $total += count($models);
      });
    } catch(Exception $e) {
      $this->error("EXCEPTION: " . $e->getMessage());
    }
  }
}

@baopham I can create a PR if you'd like ...

from laravel-dynamodb.

baopham avatar baopham commented on May 20, 2024

yep please go ahead.

from laravel-dynamodb.

Saracevas avatar Saracevas commented on May 20, 2024

Any progress on this?

from laravel-dynamodb.

baopham avatar baopham commented on May 20, 2024

No, no update yet. During the holidays, I should have more time to look into it.

from laravel-dynamodb.

baopham avatar baopham commented on May 20, 2024

@Saracevas pagination is now added. You can try v4.0.0. Please check README, since it's DynamoDB so we cannot match exactly with the same concept as relational DB.

from laravel-dynamodb.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.