Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public class CollectionReference extends Query {
*/
@Nonnull
public String getId() {
return path.getId();
return options.getCollectionId();
}

/**
Expand All @@ -69,7 +69,7 @@ public String getId() {
*/
@Nullable
public DocumentReference getParent() {
ResourcePath parent = path.getParent();
ResourcePath parent = options.getParentPath();
return parent.isDocument() ? new DocumentReference(firestore, parent) : null;
}

Expand All @@ -81,7 +81,7 @@ public DocumentReference getParent() {
*/
@Nonnull
public String getPath() {
return path.getPath();
return getResourcePath().getPath();
}

/**
Expand All @@ -104,10 +104,10 @@ public DocumentReference document() {
*/
@Nonnull
public DocumentReference document(@Nonnull String childPath) {
ResourcePath documentPath = path.append(childPath);
ResourcePath documentPath = getResourcePath().append(childPath);
Preconditions.checkArgument(
documentPath.isDocument(),
String.format("Path should point to a Document Reference: %s", path));
String.format("Path should point to a Document Reference: %s", getPath()));
return new DocumentReference(firestore, documentPath);
}

Expand All @@ -124,8 +124,8 @@ public DocumentReference document(@Nonnull String childPath) {
@Nonnull
public Iterable<DocumentReference> listDocuments() {
ListDocumentsRequest.Builder request = ListDocumentsRequest.newBuilder();
request.setParent(path.getParent().toString());
request.setCollectionId(this.getId());
request.setParent(options.getParentPath().toString());
request.setCollectionId(options.getCollectionId());
request.setMask(DocumentMask.getDefaultInstance());
request.setShowMissing(true);

Expand Down Expand Up @@ -206,4 +206,9 @@ public ApiFuture<DocumentReference> add(Object pojo) {
}
return add((Map<String, Object>) converted);
}

/** Returns a resource path pointing to this collection. */
ResourcePath getResourcePath() {
return options.getParentPath().append(options.getCollectionId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@ public interface Firestore extends Service<FirestoreOptions>, AutoCloseable {
@Nonnull
Iterable<CollectionReference> getCollections();

/**
* Creates and returns a new @link{Query} that includes all documents in the database that are
* contained in a collection or subcollection with the given @code{collectionId}.
*
* @param collectionId Identifies the collections to query over. Every collection or subcollection
* with this ID as the last segment of its path will be included. Cannot contain a slash.
* @return The created Query.
*/
Query collectionGroup(@Nonnull String collectionId);

/**
* Executes the given updateFunction and then attempts to commit the changes applied within the
* transaction. If any document read within the transaction has changed, the updateFunction will
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,10 @@ public WriteBatch batch() {
@Nonnull
@Override
public CollectionReference collection(@Nonnull String collectionPath) {
return new CollectionReference(this, databasePath.append(collectionPath));
ResourcePath path = databasePath.append(collectionPath);
Preconditions.checkArgument(
path.isCollection(), "Invalid path specified. Path should point to a collection");
return new CollectionReference(this, path);
}

@Nonnull
Expand Down Expand Up @@ -244,6 +247,16 @@ public void onCompleted() {
return futureList;
}

@Nonnull
@Override
public Query collectionGroup(@Nonnull final String collectionId) {
Preconditions.checkArgument(
!collectionId.contains("/"),
String.format(
"Invalid collectionId '%s'. Collection IDs must not contain '/'.", collectionId));
return new Query(this, collectionId);
}

@Nonnull
@Override
public <T> ApiFuture<T> runTransaction(@Nonnull final Transaction.Function<T> updateFunction) {
Expand Down Expand Up @@ -401,6 +414,11 @@ String getDatabaseName() {
return databasePath.getDatabaseName().toString();
}

/** Returns a path to the Firestore project associated with this client. */
ResourcePath getResourcePath() {
return databasePath;
}

/** Returns the underlying RPC client. */
FirestoreRpc getClient() {
return firestoreClient;
Expand Down
Loading