-
Dependency:
Insert mongodb as dependency, then installs it.
Dotenv loads the .env eviroment file
npm install --save mongodb npm install dotenv
-
File: .env
Hidden informations which should not be committed on git
MONGO_URL="mongodb://SvLearnMongoUser:SvLearnMongoP0ss@localhost:27017/SvLearnMongo"
-
Folder: src/db
-
Alias: svelte.config.js
Creating alias for folder
kit: { adapter: adapter(), alias: { '$db': "./src/db" } }
-
File: src/db/mongo.ts
import {MongoClient} from "mongodb"; import {MONGO_URL} from "$env/static/private"; const client = new MongoClient(MONGO_URL); export function start_mongo(): Promise<MongoClient>{ console.log("Starting Mongo connection...") return client.connect(); } export default client.db()
-
File: src/hooks.server.ts
import {start_mongo} from '$db/mongo'; start_mongo().then(()=> { console.log("Mongo started."); })
-
File: src/db/sampleCollection.ts
This is the db schema
import db from "$db/mongo" // This is the database's table export const sampleCollection = db.collection('sampleCollection');
-
File: +page.server.ts
This is ".server." because it needs to run only on the server! Server side!
Projection is what should be seen in the query: 0 - false, 1 - true
import {documents} from "$db/documents"; import type {PageServerLoad} from "./$types"; export const load: PageServerLoad = async function() { const data = await documents.find({}, {limit: 50, projection: {'_id': 0,'name': 1}}).toArray(); console.log("Data: ", data); return { documents: data } }
-
File: +page.svelte
Client side!
<script lang="ts"> import type {PageData} from "./$types"; export let data: PageData; $:({documents} = data); </script> <h1>Welcome to SvelteKit</h1> <p>Visit <a href="https://kit.svelte.dev">kit.svelte.dev</a> to read the documentation</p> <section> {#each documents as document} <article> <p>{document.name}</p> </article> {/each } </section>
svlearnmongo's Introduction
svlearnmongo's People
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ๐๐๐
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.