Code Monkey home page Code Monkey logo

Comments (3)

jedireza avatar jedireza commented on May 21, 2024

Yeah having an example in the readme would be good. Please consider creating a pull request.

In general though instead of getting to the model singleton via:

const Customer = request.server.plugins['hapi-mongo-models'].Customer;

I think it's better to just require them directly. This makes testing easier also:

const Customer = require('./path/to/models/customer.');

from hapi-mongo-models.

Miscellaneous avatar Miscellaneous commented on May 21, 2024

Yea, agree

from hapi-mongo-models.

Miscellaneous avatar Miscellaneous commented on May 21, 2024
// app.js

'use strict';

const Hapi = require('hapi');
const Path = require('path');
const HapiMongoModels = require('hapi-mongo-models');

const mongodbPlugin = {
    plugin: HapiMongoModels,
    options: {
        mongodbdb: {
            connection: {
                uri: 'mongodb://localhost:27017/',
                db: 'hapi-mongo-models-test'
            },
            options: {
                useNewUrlParser: true
            }
        },
        models: [
            Path.resolve(__dirname, 'models/customer'),
            Path.resolve(__dirname, 'models/stock')
        ],
        autoIndex: false
    }
};

const startServer = async function()
{
    try {
        await server.register( mongodbPlugin );
    }
    catch( err ){ return console.log( err ) }


    try {
        await server.start();
    }
    catch (err) {
        console.log(err);
        process.exit(1);
    }

    const startRouters = async function()
    {
        try {
            await server.register({
                plugin: require('./plugins/customer'),
                options: {
                    mongodb: 'mongodb://localhost:27017/',
                    autoIndex: false,
                    models: {
                        Customer: './models/customer'
                    }
                }
            });
        }
        catch( err ){ return console.log( err ) }

        try {
            await server.register({
                plugin: require('./plugins/stock'),
                options: {
                    mongodb: 'mongodb://localhost:27017/',
                    autoIndex: false,
                    models: {
                        Stock: './models/stock'
                    }
                }
            });
        }
        catch( err ){ return console.log( error: err ) }
    };

    startRouters();
}

startServer();

// plugins/customer.js
'use strict';

const Joi = require('joi');
const Stock = require('../models/stock'); //Optional if needing access to other models

exports.plugin = {
    name: 'customer',
    register:( server, options ) => {

        server.route({
            method: 'GET',
            path: '/customer',
            options: { auth: false },
            handler: async function( request, h )
            {
                var result = null;
                try{
                     result = await Customer.find( {}, { projection: { _id:0 }});
                }
                catch( err ){ return console.log( err )}

                return result;
            }
        });
    }
};



// plugins/stock.js
'use strict';

const Joi = require('joi');
const Customer = require('../models/customer'); //Optional if needing access to other models

exports.plugin = {
    name: 'stock',
    register:( server, options ) => {

        server.route({
            method: 'GET',
            path: '/stock',
            options: { auth: false },
            handler: async function( request, h )
            {
                var result = null;
                try{
                     result = await Stock.find( {}, { projection: { _id:0 }});
                }
                catch( err ){ return console.log( err )}

                return result;
            }
        });
    }
};

from hapi-mongo-models.

Related Issues (20)

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.