Comments (3)
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.
Yea, agree
from hapi-mongo-models.
// 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)
- Can't use Mongo Native method Aggregate HOT 3
- Not authorized on dbname to execute command HOT 1
- QUESTION: Does this plug-in support associations? HOT 2
- Issue with updateOne & update Many HOT 4
- server.start() to include Mongo models? HOT 3
- v6.0.0 release notes
- Add a modelsDirectory plugin option
- using aggregate function HOT 2
- Not expected results in v6.0.0 responses HOT 3
- Multiple db support HOT 1
- MongoDB custom logger HOT 1
- Unable to get the instantiated db reference. HOT 1
- Hapi v17 support HOT 1
- Migrate callbacks pattern to async/await (hapi v17)
- Support for ssh tunnel ? HOT 6
- models "path/to/customer" might be off? HOT 2
- Migration help HOT 1
- $elemMatch returns all subjects HOT 1
- If connection.db is optional, why is there an assertion error if it isn't provided? HOT 1
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.
from hapi-mongo-models.