Skip to content

Commit 18641a8

Browse files
committed
Enable adding records to the database
1 parent 8c8afa3 commit 18641a8

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed

src/PHPCouchDB/Database.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,49 @@ public function getAllDocs($options = []) : array
7979
}
8080
}
8181
}
82+
83+
/**
84+
* Create a new document in the database
85+
*
86+
* @param array $doc An array representing the document's keys and values.
87+
* The data can be nested arrays, lists, anything
88+
* - if you include an "id" key, this will become the document ID,
89+
* otherwise it'll be autogenerated
90+
* @return PHPCouchDB\Document a document object of your new doc
91+
*/
92+
public function create($doc)
93+
{
94+
if (!is_array($doc)) {
95+
throw new \PHPCouchDB\Exception\DatabaseException('A document is required, in array format');
96+
}
97+
98+
// do we have the ID?
99+
if (isset($doc['id']) && !empty($doc['id'])) {
100+
// remove the ID from the array, then use it in the URL with PUT
101+
$id = $doc['id'];
102+
unset($doc['id']);
103+
$endpoint = "/" . $this->db_name . "/" . $id;
104+
$verb = 'PUT';
105+
} else {
106+
// no ID, make a POST request to create the record
107+
$endpoint = "/" . $this->db_name . "/";
108+
$verb = 'POST';
109+
}
110+
111+
try {
112+
$response = $this->client->request($verb, $endpoint, ['json' => $doc]);
113+
if ($response->getStatusCode() == 201 && $response_data = json_decode($response->getBody(), true)) {
114+
$id = $response_data['id'];
115+
// all good. Let's fetch the doc and return it
116+
$fetched_data = json_decode($this->client->get('/' . $this->db_name . '/' . $id)->getBody());
117+
return new \PHPCouchDB\Document($fetched_data);
118+
}
119+
} catch (\GuzzleHttp\Exception\ConnectException $e) {
120+
throw new \PHPCouchDB\Exception\ServerException(
121+
"Could not create record. Error: " . $e->getMessage(),
122+
0,
123+
$e
124+
);
125+
}
126+
}
82127
}

tests/PHPCouchDB/DatabaseTest.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,47 @@ public function testGetAllDocsWithNoDocs() {
6262
$this->assertInternalType('array', $docs);
6363
$this->assertEmpty($docs);
6464
}
65+
66+
public function testCreateWithID() {
67+
$egdb1 = '{"db_name":"egdb","update_seq":"0-g1AAAABXeJzLYWBgYMpgTmEQTM4vTc5ISXLIyU9OzMnILy7JAUklMiTV____PyuRAY-iPBYgydAApP5D1GYBAJmvHGw","sizes":{"file":8488,"external":0,"active":0},"purge_seq":0,"other":{"data_size":0},"doc_del_count":0,"doc_count":0,"disk_size":8488,"disk_format_version":6,"data_size":0,"compact_running":false,"instance_start_time":"0"}';
68+
$use_response = new Response(200, [], $egdb1);
69+
$create = '{"ok":true,"id":"abcde12345","rev":"1-928ec193918889e122e7ad45cfd88e47"}';
70+
$create_response = new Response(201, [], $create);
71+
$fetch = '{"_id":"abcde12345","_rev":"1-928ec193918889e122e7ad45cfd88e47","noise":"howl"}';
72+
$fetch_response = new Response(200, [], $fetch);
73+
74+
$mock = new MockHandler([ $this->db_response, $use_response, $create_response, $fetch_response ]);
75+
$handler = HandlerStack::create($mock);
76+
$client = new Client(['handler' => $handler]);
77+
78+
// userland code starts
79+
$server = new \PHPCouchDB\Server(["client" => $client]);
80+
$database = $server->useDB(["name" => "egdb"]);
81+
$doc = $database->create(["noise" => "howl", "id" => "abcde12345"]);
82+
83+
$this->assertInstanceOf('PHPCouchDB\Document', $doc);
84+
$this->assertObjectHasAttribute('id', $doc);
85+
$this->assertEquals("abcde12345", $doc->id);
86+
}
87+
88+
public function testCreateWithoutID() {
89+
$egdb1 = '{"db_name":"egdb","update_seq":"0-g1AAAABXeJzLYWBgYMpgTmEQTM4vTc5ISXLIyU9OzMnILy7JAUklMiTV____PyuRAY-iPBYgydAApP5D1GYBAJmvHGw","sizes":{"file":8488,"external":0,"active":0},"purge_seq":0,"other":{"data_size":0},"doc_del_count":0,"doc_count":0,"disk_size":8488,"disk_format_version":6,"data_size":0,"compact_running":false,"instance_start_time":"0"}';
90+
$use_response = new Response(200, [], $egdb1);
91+
$create = '{"ok":true,"id":"95613816b3a7490727388ebb47002c0f","rev":"1-928ec193918889e122e7ad45cfd88e47"}';
92+
$create_response = new Response(201, [], $create);
93+
$fetch = '{"_id":"95613816b3a7490727388ebb47002c0f","_rev":"1-928ec193918889e122e7ad45cfd88e47","noise":"howl"}';
94+
$fetch_response = new Response(200, [], $fetch);
95+
96+
$mock = new MockHandler([ $this->db_response, $use_response, $create_response, $fetch_response ]);
97+
$handler = HandlerStack::create($mock);
98+
$client = new Client(['handler' => $handler]);
99+
100+
// userland code starts
101+
$server = new \PHPCouchDB\Server(["client" => $client]);
102+
$database = $server->useDB(["name" => "egdb"]);
103+
$doc = $database->create(["noise" => "howl"]);
104+
105+
$this->assertInstanceOf('PHPCouchDB\Document', $doc);
106+
$this->assertObjectHasAttribute('id', $doc);
107+
}
65108
}

0 commit comments

Comments
 (0)