baopham / laravel-dynamodb

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

get() with a non-existent value as the primary key using where returns a collection with null instead of an empty collection #278

Open crhg opened 5 months ago

crhg commented 5 months ago

Describe the bug

Assume the name of the primary key in table foos is id.

Foo::where('id', '__non_existent__')->get()

Expected result: Empty collection

> Foo::where('id', '__non_existent__')->get()
= BaoPham\DynamoDb\DynamoDbCollection {#8448
    all: [],
  }

Actual result: Collection with null as an element.

> Foo::where('id', '__non_existent__')->get()
= BaoPham\DynamoDb\DynamoDbCollection {#8126
    all: [
      null,
    ],
  }

\BaoPhamDynamoDbQueryBuilder::getAll performs the following special logic when exact search is available. If item does not exist and null is returned, it should return an empty collection.

        if ($analyzer->isExactSearch()) {
            $item = $this->find($analyzer->identifierConditionValues(), $columns);

            return $this->getModel()->newCollection([$item]);
        }

Schema

Describe your table schema:

Debug info

> Foo::where('id', '__non_existent__')->toDynamoDbQuery()
= BaoPham\DynamoDb\RawDynamoDbQuery {#7193
    +op: "Query",
    +query: [
      "TableName" => "foos",
      "KeyConditionExpression" => "#id = :a1",
      "ExpressionAttributeNames" => [
        "#id" => "id",
      ],
      "ExpressionAttributeValues" => [
        ":a1" => [
          "S" => "__non_existent__",
        ],
      ],
    ],
  }

Version info