baopham / laravel-dynamodb

Eloquent syntax for DynamoDB
https://packagist.org/packages/baopham/dynamodb
MIT License
490 stars 127 forks source link

I can't sort the items on a query #248

Closed jlroserKwerty closed 2 years ago

jlroserKwerty commented 2 years ago

I'm follow the instructions and check others issues. I follow the steps but I couldn't get the results sorts. I'm suing lumen

My Dynamo has a primary key, called oid and a secondary global index, with the oid like the partion key and priority sort key.

To obtain the list of emails I use, where I get all the emails from one source, I try the withIndex also, but no work for me.

                $mails = new DynamoMail();
                $count = $mails->where('source', $requestMail)->count();
                $list_emails = $mails->where('source', '=', $requestMail)
                    ->where('priority', '>', 0)
                    ->limit($limit)
                    ->all();

The declaration of the model is

          use Illuminate\Database\Eloquent\Factories\HasFactory;
          use BaoPham\DynamoDb\DynamoDbModel as DynamoDbModel;

          class DynamoMail extends DynamoDbModel
          {
              use HasFactory;

              protected $table = 'kwermail';
              protected $primaryKey = 'oid';
              protected $dynamoDbIndexKeys = [
                  'oid-priority-index' => [
                      'hash' => 'oid',
                      'range' => 'priority'
                  ],
              ];
              protected $fillable = ['oid', 'priority', 'subject', 'html_text', 'source', 'text', 'from',
                  'date_received', 'attachment', 'resume', 'feel', 'attachments_url', 'mailbox', 'to', 'read'];
}

Version info

jlroserKwerty commented 2 years ago

I forgot to say that I also use the decorate option

    ->decorate(function (RawDynamoDbQuery $raw) {
        // desc order
        $raw->query['ScanIndexForward'] = false;
    })
zoe-edwards commented 2 years ago

You would need to have your source attribute indexed in order to use a where on it sensibly.

jlroserKwerty commented 2 years ago

Thanks for the response, when I use the source field into the index return the results sort. Thanks