diff --git a/src/Plugin/Insert/Payload.php b/src/Plugin/Insert/Payload.php index cdd9f1c5..72044a9f 100644 --- a/src/Plugin/Insert/Payload.php +++ b/src/Plugin/Insert/Payload.php @@ -54,10 +54,33 @@ public static function fromRequest(Request $request): static { $request->payload = trim($request->payload) . "\n"; } $self->queries[] = $self->buildCreateTableQuery(...$parser->parse($request->payload)); - $self->queries[] = $request->payload; + $self->queries[] = (str_contains($self->path, '_doc') || str_contains($self->path, '_create')) + ? self::preprocessElasticLikeRequest($self->path, $request->payload) + : $request->payload; + return $self; } + /** + * @param string $path + * @param string $payload + * @return string + */ + protected static function preprocessElasticLikeRequest(string &$path, string $payload): string { + $pathParts = explode('/', $path); + $table = $pathParts[0]; + $path = 'insert'; + $query = [ + 'table' => $table, + 'doc' => (array)json_decode($payload, true), + ]; + if (isset($pathParts[2]) && $pathParts[2]) { + $query['id'] = (int)$pathParts[2]; + } + + return (string)json_encode($query); + } + /** * @param string $name * @param array $cols diff --git a/test/Buddy/functional/InsertQueryTest.php b/test/Buddy/functional/InsertQueryTest.php index 5b16ee2d..3100a86b 100644 --- a/test/Buddy/functional/InsertQueryTest.php +++ b/test/Buddy/functional/InsertQueryTest.php @@ -65,31 +65,31 @@ public function testHTTPElasticInsertQueryOk(): void { /** @var array>,total?:string,columns?:string}> $out */ $this->assertArrayHasKey(0, $out); $outData = $out[0]['data'][0]; - if (!isset($outData['_id'], $outData['_index'], $outData['result'])) { + if (!isset($outData['_id'], $outData['table'], $outData['result'])) { $this->fail(); } $result = [1, $this->testTable, 'created']; - $this->assertEquals($result, [$outData['_id'], $outData['_index'], $outData['result']]); + $this->assertEquals($result, [$outData['_id'], $outData['table'], $outData['result']]); $out = static::runHttpQuery($query, true, "{$this->testTable}/_doc"); /** @var array>,total?:string,columns?:string}> $out */ $this->assertArrayHasKey(0, $out); $outData = $out[0]['data'][0]; - if (!isset($outData['_index'], $outData['result'])) { + if (!isset($outData['table'], $outData['result'])) { $this->fail(); } $result = [$this->testTable, 'created']; - $this->assertEquals($result, [$outData['_index'], $outData['result']]); + $this->assertEquals($result, [$outData['table'], $outData['result']]); $out = static::runHttpQuery($query, true, "{$this->testTable}/_doc/2"); /** @var array>,total?:string,columns?:string}> $out */ $this->assertArrayHasKey(0, $out); $outData = $out[0]['data'][0]; - if (!isset($outData['_id'], $outData['_index'], $outData['result'])) { + if (!isset($outData['_id'], $outData['table'], $outData['result'])) { $this->fail(); } - $result = [2, $this->testTable, 'updated']; - $this->assertEquals($result, [$outData['_id'], $outData['_index'], $outData['result']]); + $result = [2, $this->testTable, 'created']; + $this->assertEquals($result, [$outData['_id'], $outData['table'], $outData['result']]); } public function testHTTPElasticInsertQueryFail(): void { @@ -102,11 +102,7 @@ public function testHTTPElasticInsertQueryFail(): void { if (!isset($outData['error'])) { $this->fail(); } - $result = [ - 'type' => 'index_not_found_exception', - 'reason' => 'no such index [test]', - 'table' => 'test', - ]; + $result = '/test/_create - unsupported endpoint'; $this->assertEquals($result, $outData['error']); }