Created
August 28, 2019 20:04
-
-
Save rusenask/f877d2e82a5c8095a344bc89a98250b7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
unc (m *FirestoreBinManager) BinDelete(ctx context.Context, binID string) error { | |
_, err := m.client.Collection(m.binsCollection).Doc(binID).Delete(ctx) | |
if err != nil { | |
m.logger.Errorw("failed to delete bin doc by ref", | |
"error", err, | |
) | |
} | |
// Now, get all the requests and delete them in a batch request | |
iter := m.client.Collection(m.reqsCollection).Where("Bin", "==", binID).Documents(ctx) | |
numDeleted := 0 | |
batch := m.client.Batch() | |
for { | |
doc, err := iter.Next() | |
if err == iterator.Done { | |
break | |
} | |
if err != nil { | |
return fmt.Errorf("Failed to iterate: %v", err) | |
} | |
batch.Delete(doc.Ref) | |
numDeleted++ | |
} | |
// If there are no documents to delete, | |
// the process is over. | |
if numDeleted == 0 { | |
return nil | |
} | |
_, err = batch.Commit(ctx) | |
return err | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment