Giter VIP home page Giter VIP logo

svlearnmongo's Introduction

  1. Dependency:

    Insert mongodb as dependency, then installs it.

    Dotenv loads the .env eviroment file

    npm install --save mongodb
    npm install dotenv
    
  2. File: .env

    Hidden informations which should not be committed on git

    MONGO_URL="mongodb://SvLearnMongoUser:SvLearnMongoP0ss@localhost:27017/SvLearnMongo"
  3. Folder: src/db

  4. Alias: svelte.config.js

    Creating alias for folder

    kit: {
    		adapter: adapter(),
    		alias: {
    			'$db': "./src/db"
    		}
    	}
  5. 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()
  6. File: src/hooks.server.ts

    import {start_mongo} from '$db/mongo';
    
    start_mongo().then(()=> { console.log("Mongo started."); })
  7. 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');
  8. 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 }
    }
  9. 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 People

Contributors

gyurci08 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.