Skip to content

Instantly share code, notes, and snippets.

@Tonnie-Dev
Last active January 27, 2026 07:07
Show Gist options
  • Select an option

  • Save Tonnie-Dev/59920ee330043293e848770ecf339a2c to your computer and use it in GitHub Desktop.

Select an option

Save Tonnie-Dev/59920ee330043293e848770ecf339a2c to your computer and use it in GitHub Desktop.
Seeding on Firestore
val firebaseModule = module {
single { FirebaseAuth.getInstance() }
single { FirebaseFirestore.getInstance() }
single { FirestoreSeeder(get()) }
}
data class Pizza(
override val id: Long,
override val name: String,
val ingredients: List<String>,
val price: Double,
val imageUrl: String,
override val category: Category
) : SearchItem
enum class Category(
val categoryName: String,
val folderPath: String
) {
PIZZA(categoryName = "Pizza", folderPath = "pizza"),
DRINKS(categoryName = "Drinks", folderPath = "drinks"),
SAUCE(categoryName = "Sauces", folderPath = "sauce"),
ICE_CREAM(categoryName = "Ice Cream", folderPath = "icecream")
}
fun Pizza.fullImageUrl(): String =
"https://pl-coding.com/wp-content/uploads/lazypizza/${category.folderPath}/$imageUrl"
class FirestoreSeeder(private val firestore: FirebaseFirestore) {
suspend fun seedPizzas(pizzas: List<Pizza>) {
val snapshot = firestore.collection("pizzas")
.limit(1)
.get()
.await()
if (!snapshot.isEmpty) return // already seeded
pizzas.forEach { pizza ->
firestore
.collection("pizzas")
.document(pizza.id.toString())
.set(pizza.toDto())
.await()
}
}
suspend fun seedAddOnItem(items: List<AddOnItem>, collectionPath: String) {
val snapshot = firestore.collection(collectionPath)
.limit(1)
.get()
.await()
if (!snapshot.isEmpty) return
items.forEach { item ->
firestore.collection(collectionPath)
.document("${item.id}")
.set(item.toDto())
.await()
}
}
suspend fun clearPizzas() {
val snapshot = firestore.collection("pizzas")
.get()
.await()
if (snapshot.isEmpty) return
snapshot.documents.forEach { doc ->
doc.reference.delete()
.await()
}
}
suspend fun clearAddOnItem(collectionPath: String) {
val snapshot = firestore.collection(collectionPath)
.get()
.await()
if (snapshot.isEmpty) return
snapshot.documents.forEach { doc ->
doc.reference.delete()
.await()
}
}
}
class MenuViewModel(
private val cartRepository: CartRepository,
private val authRepository: AuthRepository,
private val firestoreSeeder: FirestoreSeeder
) : HomeBaseViewModel() { ...
override fun onEvent(event: MenuUiEvent) {
when (event) { ...
MenuUiEvent.SeedFirestore -> seedFirestoreDevSwitch()
MenuUiEvent.ClearFirestore -> clearFirestoreDevSwitch()
}
private fun seedFirestoreDevSwitch() {
launch {
firestoreSeeder.seedPizzas(mockPizzas)
firestoreSeeder.seedAddOnItem(items = drinksMock, collectionPath = "drinks")
firestoreSeeder.seedAddOnItem(items = iceCreamsMock, collectionPath = "ice_creams")
firestoreSeeder.seedAddOnItem(items = saucesMock, collectionPath = "sauces")
}
}
private fun clearFirestoreDevSwitch() {
launch {
firestoreSeeder.clearPizzas()
firestoreSeeder.clearAddOnItem(collectionPath = "drinks")
firestoreSeeder.clearAddOnItem(collectionPath = "ice_creams")
firestoreSeeder.clearAddOnItem(collectionPath = "sauces")
}
}
}
data class PizzaDto(
val id: Long = 0L,
val name: String = "",
val ingredients: List<String> = emptyList(),
val price: Double = 0.0,
val imageUrl: String = "",
val category: String = ""
)
fun Pizza.toDto(): PizzaDto {
return PizzaDto(
id = id,
name = name,
ingredients = ingredients,
price = price,
imageUrl = fullImageUrl(),
category = category.name
)
}
fun PizzaDto.toDomain(): Pizza {
return Pizza(
id = id,
name = name,
ingredients = ingredients,
price = price,
imageUrl = imageUrl,
category = Category.valueOf(category)
)
}
val mockPizzas = listOf(
Pizza(
id = 1L,
name = "Margherita",
ingredients = listOf(
"Tomato sauce",
"Mozzarella",
"Fresh basil",
"Olive oil"
),
price = 8.99,
imageUrl = "margherita.png",
category = Category.PIZZA
),
Pizza(
id = 2L,
name = "Pepperoni",
ingredients = listOf(
"Tomato sauce",
"Mozzarella",
"Pepperoni"
),
price = 9.99,
imageUrl = "pepperoni.png",
category = Category.PIZZA
),
Pizza(
id = 3L,
name = "Hawaiian",
ingredients = listOf(
"Tomato sauce",
"Mozzarella",
"Ham",
"Pineapple"
),
price = 10.49,
imageUrl = "hawaiian.png",
category = Category.PIZZA
),
Pizza(
id = 4L,
name = "BBQ Chicken",
ingredients = listOf(
"BBQ sauce",
"Mozzarella",
"Grilled chicken",
"Onion",
"Corn"
),
price = 11.49,
imageUrl = "bbq-chicken.png",
category = Category.PIZZA
),
Pizza(
id = 5L,
name = "Four Cheese",
ingredients = listOf(
"Mozzarella",
"Gorgonzola",
"Parmesan",
"Ricotta"
),
price = 11.99,
imageUrl = "four-cheese.png",
category = Category.PIZZA
),
Pizza(
id = 6L,
name = "Veggie Delight",
ingredients = listOf(
"Tomato sauce",
"Mozzarella",
"Mushrooms",
"Olives",
"Bell pepper",
"Onion",
"Corn"
),
price = 9.79,
imageUrl = "veggie-delight.png",
category = Category.PIZZA
),
Pizza(
id = 7L,
name = "Meat Lovers",
ingredients = listOf(
"Tomato sauce",
"Mozzarella",
"Pepperoni",
"Ham",
"Bacon",
"Sausage"
),
price = 12.49,
imageUrl = "meat-lovers.png",
category = Category.PIZZA
),
Pizza(
id = 8L,
name = "Spicy Inferno",
ingredients = listOf(
"Tomato sauce",
"Mozzarella",
"Spicy salami",
"Jalapeños",
"Red chili pepper",
"Garlic"
),
price = 11.29,
imageUrl = "spicy-inferno.png",
category = Category.PIZZA
),
Pizza(
id = 9L,
name = "Seafood Special",
ingredients = listOf(
"Tomato sauce",
"Mozzarella",
"Shrimp",
"Mussels",
"Squid",
"Parsley"
),
price = 13.99,
imageUrl = "seafood-special.png",
category = Category.PIZZA
),
Pizza(
id = 10L,
name = "Truffle Mushroom",
ingredients = listOf(
"Cream sauce",
"Mozzarella",
"Mushrooms",
"Truffle oil",
"Parmesan"
),
price = 12.99,
imageUrl = "truffle-mushroom.png",
category = Category.PIZZA
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment