hapipal / boilerplate

A friendly, proven starting place for your next hapi plugin or deployment
https://hapipal.com
183 stars 27 forks source link

How to use mongodb with hapipal structure #69

Closed vvarune closed 5 years ago

vvarune commented 5 years ago

I want to use mongodb with hapipal,Can you please advice is there any plugin available for mongodb like knex.

devinivy commented 5 years ago

There isn't an "official" plugin for using mongo, but hapi pal also shouldn't get in your way if you choose to use some other hapi plugin that provides the functionality.

We do have an example of a simple setup using Mongoose ORM which allows you to place your mongo models in lib/models/ and use hpal make models ModelName to scaffold a new model. You can find that here: https://github.com/hapipal/haute-couture/blob/master/API.md#amendment-example

vvarune commented 5 years ago

Thank you for your suggestion. Looks like suitable for my requirement. I'm facing challenges in registering this plugin in server/manifest.js.

devinivy commented 5 years ago

Which plugin are you having trouble registering? If you're talking about haute-couture, that isn't a plugin at all. In this boilerplate you will find that lib/index.js uses haute-couture in order to turn your project's files under lib/ into a hapi plugin.

vvarune commented 5 years ago

lib/models/index.js

'use strict';

const HauteCouture = require('haute-couture'); const Mongoose = require('mongoose');

module.exports = { name: 'hapi-mongo', register: async (server, options) => { server.app.connection = Mongoose.createConnection('mongodb://127.0.0.1/test'); await HauteCouture.using()(server, options); } };

lib/.hc.js

'use strict';

module.exports = { add: [{ place: 'models', list: true, signature: ['name', 'schema'], method: (server, options, name, schema) => { const { connection } = server.app; server.app.models = server.app.models || {}; server.app.models[name] = connection.model(name, schema); // error occuring }, }] };

server/manifest.js

register: {
    plugins: [
        {
            plugin: '../lib', 
            options: {}
        },
        {
            plugin: {
                $filter: { $env: 'NODE_ENV' },
                $default: 'hpal-debug',
                production: Toys.noop
            }
        }
    ]
}

But I'm getting the following error,

TypeError: Custom "models" method called by haute using /home/arul/projects/hapijs/test/project-mongo/lib/models/index.js: Cannot read property 'model' of undefined
    at method (/home/arul/projects/hapijs/test/project-mongo/lib/.hc.js:12:50)

It seems that connection is not establishing.

devinivy commented 5 years ago

I think it's a simple error with some of your code,

--const { connection } = server;
++const { connection } = server.app;
vvarune commented 5 years ago

No.. Still the same error.

devinivy commented 5 years ago

Sorry, I don't think I'm in a good position to help you debug the rest of your issue without quite a bit more information. I tried the example in the haute-couture docs and it works alright for me. I suggest following the stack traces and doing some standard debugging to figure out where things are going awry.

Ba7er commented 3 years ago

@vvarune , maybe you can try this in .hc.js module.exports = { recursive: true, add: [{ place: 'models', list: true, signature: ['name', 'schema'], method: (server, options, name, schema) => { const { connection } = server.app; server.app.models = server.app.models || {}; server.app.models[name] = connection.model(name, schema); }, }] };