Skip to content

Commit d5d5c9d

Browse files
committed
add collectionId()
1 parent 62434c1 commit d5d5c9d

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

Firestore/Swift/Source/ExpressionImplementation.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,10 @@ public extension Expression {
587587
return FunctionExpression("document_id", [self])
588588
}
589589

590+
func collectionId() -> FunctionExpression {
591+
return FunctionExpression("collection_id", [self])
592+
}
593+
590594
func ifError(_ catchExpression: Expression) -> FunctionExpression {
591595
return FunctionExpression("if_error", [self, catchExpression])
592596
}

Firestore/Swift/Source/SwiftAPI/Pipeline/Expressions/Expression.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1644,8 +1644,23 @@ public protocol Expression: Sendable {
16441644
/// - Returns: A new "FunctionExpression" representing the bitwise right shift operation.
16451645
func bitRightShift(_ numberExpression: Expression) -> FunctionExpression
16461646

1647+
/// Creates an expression that returns the document ID from a path.
1648+
///
1649+
/// - Note: This API is in beta.
1650+
///
1651+
/// ```swift
1652+
/// // Get the document ID from a path.
1653+
/// Field(FieldPath.documentID()).documentId()
1654+
/// ```
1655+
///
1656+
/// - Returns: A new `FunctionExpression` representing the documentId operation.
16471657
func documentId() -> FunctionExpression
16481658

1659+
/// Gets the collection id (kind) of a given document (either an absolute or
1660+
/// namespace relative reference). Throw error if the input is the
1661+
/// root itself.
1662+
func collectionId() -> FunctionExpression
1663+
16491664
/// Creates an expression that returns the result of `catchExpr` if this expression produces an
16501665
/// error during evaluation,
16511666
/// otherwise returns the result of this expression.

Firestore/Swift/Tests/Integration/PipelineTests.swift

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2027,6 +2027,42 @@ class PipelineIntegrationTests: FSTIntegrationTestCase {
20272027
TestHelper.compare(pipelineSnapshot: snapshot, expected: expectedResults, enforceOrder: true)
20282028
}
20292029

2030+
func testCollectionIdWorks() async throws {
2031+
let collRef = collectionRef()
2032+
let docRef = collRef.document("doc")
2033+
try await docRef.setData(["foo": "bar"])
2034+
2035+
let pipeline = db.pipeline()
2036+
.collection(collRef.path)
2037+
.select([
2038+
Field(FieldPath.documentID()).collectionId().as("collectionId"),
2039+
])
2040+
2041+
let snapshot = try await pipeline.execute()
2042+
2043+
let expectedResults: [[String: Sendable]] = [
2044+
["collectionId": collRef.collectionID],
2045+
]
2046+
2047+
TestHelper.compare(pipelineSnapshot: snapshot, expected: expectedResults, enforceOrder: true)
2048+
}
2049+
2050+
// func testCollectionIdOnRootThrowsError() async throws {
2051+
// let db = firestore()
2052+
// let pipeline = db.pipeline()
2053+
// .database()
2054+
// .select([
2055+
// Field(FieldPath.documentID()).collectionId().as("collectionId"),
2056+
// ])
2057+
//
2058+
// do {
2059+
// _ = try await pipeline.execute()
2060+
// XCTFail("Should have thrown an error")
2061+
// } catch {
2062+
// // Expected error
2063+
// }
2064+
// }
2065+
20302066
func testComparisonOperators() async throws {
20312067
let collRef = collectionRef(withDocuments: bookDocs)
20322068
let db = collRef.firestore

0 commit comments

Comments
 (0)