Main notes taken from How to create a SvelteKit and MongoDB app. However it is slightly modified to account for the new +page.server.js and +page.svelte naming conventions where routing is now stored in its corresponding folder naming structure, as found in their Docs here.
The only intended purpose is to simply connect to the DB, read the data and display what we have in there onto a page. Other CRUD elements will be considered as we explore MongoDB through Svelte.
Folder Structure
├── src/ │ ├── lib/ │ │ ├── db.ts │ │ │ ├── routes/ │ │ ├── cars/ │ │ ├── +page.server.ts │ │ ├── +page.svelte
lib/db.ts -- is responsible for the mongodb connection, handling the .env variables and general back-end functionality.
+page.server.ts -- takes the DB handle, from lib which then returns a load() function response to a const variable called data/prop cariable called 'cars'.
return {
status: 200,
cars: JSON.stringify(cars)
}
The resulting structure of that response is like:
{data: {
status: 200,
cars: '[{"_id":"6306cf64e003b011330d56e1","brand":"Audi","mileage":2000},...]'
}
}