How to use mongodb with hapipal structure
vvarune opened this issue · 8 comments
I want to use mongodb with hapipal,Can you please advice is there any plugin available for mongodb like knex.
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
Thank you for your suggestion. Looks like suitable for my requirement. I'm facing challenges in registering this plugin in server/manifest.js
.
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.
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.
I think it's a simple error with some of your code,
--const { connection } = server;
++const { connection } = server.app;
No.. Still the same error.
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.
@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); }, }] };