baopham / laravel-dynamodb

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

Unable to get records as per sort_key #235

Closed connect-javed001 closed 3 years ago

connect-javed001 commented 3 years ago

Trying to get records in descending order, but it's always returns same result, I have sort_key (as current timestamp), can u please help me ?

Also provide solution when we want to search records based on date filter using sort key

zoe-edwards commented 3 years ago

What code are you using? Have you delcared the index in your model?

connect-javed001 commented 3 years ago

No, Defined primaryKey & compositeKey only

zoe-edwards commented 3 years ago

Can you show us your model?

connect-javed001 commented 3 years ago
namespace App\Models;

use BaoPham\DynamoDb\DynamoDbModel;

class Activity extends DynamoDbModel
{
    protected $table;
    protected $primaryKey;
    protected $compositeKey;

    public function __construct()
    {
        $this->primaryKey = config('dynamodb.connections.aws.hash_key');
        $this->compositeKey = [
            config('dynamodb.connections.aws.hash_key'),
            config('dynamodb.connections.aws.sort_key')
        ];
        $this->table = "Activity";
    }
}
zoe-edwards commented 3 years ago

Could you try using the index?

/**
 * Indexes.
 * [
 *     '<simple_index_name>' => [
 *          'hash' => '<index_key>'
 *     ],
 *     '<composite_index_name>' => [
 *          'hash' => '<index_hash_key>',
 *          'range' => '<index_range_key>'
 *     ],
 * ]
 *
 * @var array
 */
protected $dynamoDbIndexKeys = [
    'count_index' => [
        'hash' => 'count'
    ],
];
connect-javed001 commented 3 years ago

where we need to define above index ?

zoe-edwards commented 3 years ago

In the model?

connect-javed001 commented 3 years ago

Yes

zoe-edwards commented 3 years ago

Did it work?

connect-javed001 commented 3 years ago

No. Still getting same issue

zoe-edwards commented 3 years ago

What does your model look like now?

connect-javed001 commented 3 years ago
namespace App\Models;

use BaoPham\DynamoDb\DynamoDbModel;

class ActivityTracking extends DynamoDbModel
{
    protected $table;
    protected $primaryKey;
    protected $compositeKey;

    /**
     * Indexes.
     * [
     *     '<simple_index_name>' => [
     *          'hash' => '<index_key>'
     *     ],
     *     '<composite_index_name>' => [
     *          'hash' => '<index_hash_key>',
     *          'range' => '<index_range_key>'
     *     ],
     * ]
     *
     * @var array
     */
    protected $dynamoDbIndexKeys = [
        'count_index' => [
            'hash' => 'count'
        ],
    ];

    public function __construct()
    {
        $this->primaryKey = config('aws.hash_key');
        $this->compositeKey = [
            config('aws.hash_key'),
            config('aws.sort_key')
        ];
        $this->table = "ActivityTracking";
    }
}
zoe-edwards commented 3 years ago

Are you using withIndex('count_index') ?

connect-javed001 commented 3 years ago

Yes, But at server end, Index not defined

connect-javed001 commented 3 years ago

Is it compulsory to create index for sorting ?

zoe-edwards commented 3 years ago

You’ll need to create the index before you can use it.