Firestore client library - Batched Writes Expecting Document Strings
I am writing PHP sample code for the Firestore library and am having issues doing a batch write with set/update/delete.
The set/update/delete functions (documented at https://googlecloudplatform.github.io/google-cloud-php/#/docs/google-cloud/v0.53.0/firestore/writebatch) seem to expect a string parameter for documentName, though I'd expect them to want a DocumentReference instead (which is what the Transactions functions expect https://googlecloudplatform.github.io/google-cloud-php/#/docs/google-cloud/v0.53.0/firestore/transaction).
First, is it okay that batch writes expects a string rather than a DocumentReference for the documentName? If not, how would I format my documentName strings?
Here is the code I'm trying to run:
// Create the Cloud Firestore client
$db = new FirestoreClient();
# [START fs_return_info_transaction]
$batch = $db->batch();
# Set the data for NYC
$nycRef = $db->collection('cities')->document('NYC');
$batch->set($nycRef, [
'name' => 'New York City'
]);
# Update the population for SF
$sfRef = $db->collection('cities')->document('SF');
$batch->update($sfRef, [
['path' => 'population', 'value' => 1000000]
]);
# Delete LA
$laRef = $db->collection('cities')->document('LA');
$batch->delete($laRef);
# Commit the batch
$batch->commit();
# [END fs_return_info_transaction]
printf('Batch write successfully completed.' . PHP_EOL);
You can use DocumentReference::name() to get a string name compatible with WriteBatch. This feature is a divergence from the other Firestore clients, so I'm planning on updating WriteBatch to accept a string or a DocumentReference.
@ryanmats, could you explain a bit more the reasons you'd use WriteBatch over DocumentReference or a transaction? I had envisioned the WriteBatch class as a lower-level API which wouldn't get used as often outside the library, but it seems that many people have been using it.