This video is available to students only
Module 4 Summary
This lesson is a summary of the work done in Module 4.0.
In this module, we've established a connection between our Node server and the Mongo database with which we've created and set up in a database as a service known as MongoDB Atlas. Our database credentials are declared in a .env
file to decouple environment-specific configuration and application code as well as to avoid committing our database configuration as part of our source code.
In the temp/seed.ts
file, we've created a seed script that will help populate our database with three mock listings whenever the application seed
script is run.
1
require("dotenv").config();
2
3
import { ObjectId } from "mongodb";
4
import { connectDatabase } from "../src/database";
5
import { Listing } from "../src/lib/types";
6
7
const seed = async () => {
8
try {
9
console.log("[seed] : running...");
10
11
const db = await connectDatabase();
12
const listings: Listing[] = [
13
{
14
_id: new ObjectId(),
15
title: "Clean and fully furnished apartment. 5 min away from CN Tower",
16
image:
17
"https://res.cloudinary.com/tiny-house/image/upload/v1560641352/mock/Toronto/toronto-listing-1_exv0tf.jpg",
18
address: "3210 Scotchmere Dr W, Toronto, ON, CA",
19
price: 10000,
20
numOfGuests: 2,
21
numOfBeds: 1,
22
numOfBaths: 2,
23
rating: 5
24
},
25
{
26
_id: new ObjectId(),
27
title: "Luxurious home with private pool",
28
image:
29
"https://res.cloudinary.com/tiny-house/image/upload/v1560645376/mock/Los%20Angeles/los-angeles-listing-1_aikhx7.jpg",
30
address: "100 Hollywood Hills Dr, Los Angeles, California",
31
price: 15000,
32
numOfGuests: 2,
33
numOfBeds: 1,
34
numOfBaths: 1,
35
rating: 4
36
},
37
{
38
_id: new ObjectId(),
39
title: "Single bedroom located in the heart of downtown San Fransisco",
40
image:
41
"https://res.cloudinary.com/tiny-house/image/upload/v1560646219/mock/San%20Fransisco/san-fransisco-listing-1_qzntl4.jpg",
42
address: "200 Sunnyside Rd, San Fransisco, California",
43
price: 25000,
44
numOfGuests: 3,
45
numOfBeds: 2,
46
numOfBaths: 2,
47
rating: 3
48
}
49
];
50
51
for (const listing of listings) {
52
await db.listings.insertOne(listing);
53
}
54
55
console.log("[seed] : success");
56
} catch {
57
throw new Error("failed to seed database");
58
}
59
};
This page is a preview of TinyHouse: A Fullstack React Masterclass with TypeScript and GraphQL