Demo Data
Faker.js is a great library for generating random values for various types of data. Full documentation for its generators can be found here: https://fakerjs.dev/api/.
Animal Data Example
interface Animal {
animalId: string;
owner?: string;
animalType?: string;
animalBreed?: string[];
animalName?: string;
animalWeight: number;
animalSex: string;
temperament: string[];
about: string;
images: string[];
primaryImage: number;
location: string;
zipCode: number;
adoptionStatus: number;
dateOfBirth?: Date;
color?: string;
vaccinationStatus?: boolean;
}
export default Animal;
Above is an interface for our Animal data type corresponding to the entires for the Animal collection in our Firestore Cloud database. You can see that all fields are not necessarily required (indicated by the ? preceding the assignment of the variable's type), but for the sake of this example, we will be generating values for all of these fields according to their types.
// BehaviorSubject that holds an array of Animal objects
// Initialized with an empty array
private animalsSubject = new BehaviorSubject<Animal[]>([]);
// BehaviorSubject that holds an Animal object or null
// Initialized with null
private selectedAnimalSource = new BehaviorSubject<Animal | null>(null);
...
// Function to create an animal document in Firestore collection named 'Animal'
createAnimal(animal: Animal): Promise<DocumentReference<Animal>> {
// Add the animal object to the 'Animal' collection in Firestore
return this.firestore.collection<Animal>('Animal').add(animal);
// NOTE: <Animal> here is an interface denoting the type of
}
// Async function to seed animals into Firestore
async seedAnimals(): Promise<void> {
// Loop 30 times to create 30 animal documents
for (let i = 0; i < 30; i++) {
// Define an animal object with randomly generated data
const animal: Animal = {
animalId: faker.datatype.uuid(), // Unique ID for the animal
owner: faker.name.firstName() + faker.name.lastName(), // Owner's full name
animalType: faker.animal.type(), // Random animal type
animalBreed: faker.random.words(3).split(' '), // Random animal breed
animalName: faker.name.lastName(), // Random animal name
animalWeight: faker.datatype.number(), // Random weight
animalSex: faker.name.gender(), // Random gender
temperament: faker.random.words(3).split(' '), // Random temperament
about: faker.lorem.paragraph(), // Random description
images: [faker.image.avatar(), faker.image.avatar(), faker.image.avatar()], // Array of avatar URLs
primaryImage: 0, // Index of primary image
location: faker.address.city(), // Random city
zipCode: parseInt(faker.address.zipCode()), // Random ZIP code
adoptionStatus: faker.datatype.number(), // Random adoption status
dateOfBirth: faker.date.past(), // Random past date
color: faker.color.human(), // Random human-readable color
vaccinationStatus: faker.datatype.boolean(), // Random boolean value indicating vaccination status
};
// Create the animal document in Firestore
await this.createAnimal(animal);
}
}
Here, we're creating 30 new animal objects of type Animal. After the creation of each new animal object, we call the asynchronous createAnimal funtion (it returns a promise we await in the seeding function). There, we use attempt to add to the Animal collection our animal object, which is typecast to the <Animal> interface again (for consistency).
Last updated
Was this helpful?