Skip to content

Code preview

DSL to express Firestore collection & document path

val users = rootCollection<User, UserId>("users")
val messages = users.subCollection<Message, MessageId>("messages")

// /users
users

// /users/user1
users / UserId("user1")

// /users/user1/message
users / UserId("user1") / messages

// /users/user1/message/message1
users / UserId("user1") / messages / MessageId("message1")
val users = collection("users")
val messages = collection("messages")

// /users
users

// /users/user1
users / "user1"

// /users/user1/message
users / "user1" / messages

// /users/user1/message/message1
users / "user1" / messages / "message1"

Use collection & document path for operations.

// add (ID auto generated by Firestore)
val userId: String = add(users, User())

// set
put(users / UserId("user1"), User())

// get
val user = get<User>(users / UserId("user1"))

// get all
val messages = getAll<Message>(users / UserId("user1") / messages)
// add (ID auto generated by Firestore)
val userId: String = add(users, User())

// set
put(users / "user1", User())

// get
val user = get<User>(users / "user1")

// get all
val messages = getAll<Message>(users / "user1" / messages)

Define flexible dynamic or strict + type-inference typed collection hierarchy.

// Using `dynamic` API
val users = collection("users")
val messages = collection("messages")

//       OR

// Using `typed` API
val users = rootCollection<User, UserId>("users")
val messages = users.subCollection<Message, MessageId>("messages")