Comments (3)
Here's an example of setting things up in server.js
// Inside server.js
const HapiSequelize = require('hapi-sequelize');
const options = {
sequelize: {
name: 'yourDatabaseName',
models: ['./lib/modules/people/person.js'],
sequelize: new Sequelize(process.env.DATABASE_URL),
sync: true, // sync models - default false
forceSync: false, // force sync (drops tables) - default false
// onConnect: function (database) { // Optional
// console.log("Connected to db");
// // migrations, seeders, etc.
// }
},
};
server.register([
{ register: HapiSequelize, options: options.sequelize },
], (err) => {
if (err) {
server.log('error', 'Failed to load a plugin: ', err);
throw err;
}
// Start server
server.start((err) => {
if (err) {
server.log('error', 'Failed to start server: ', err);
throw err;
}
server.log('info', 'Server running at: ' + server.info.uri);
});
});
and your model would look like
module.exports = function(sequelize, DataTypes) {
const Person = sequelize.define("Person", {
uid: {
type: DataTypes.BIGINT,
primaryKey: true
},
firstName: {
type: DataTypes.STRING,
allowNull: false
},
lastName: {
type: DataTypes.STRING,
allowNull: false
},
email: {
type: DataTypes.STRING,
allowNull: false
}
}, {
classMethods: {
},
instanceMethods: {
}
});
return Person;
};
then you'd query in your controller / server layer like:
const list = function*(request, reply) {
try {
return reply(yield Person(request).all());
} catch (err) {
return reply(err);
}
};
from hapi-sequelize.
any api for how to use a model? @iwasrobbed
from hapi-sequelize.
@IAIAE The example above shows how to use the model. Sequelize itself gives you all the APIs http://docs.sequelizejs.com/manual/tutorial/models-usage.html ; this lib is just a wrapper around Sequelize that gives you access to it via hapi
If you're accessing a model from a controller, you can do something like this to get any arbitrary model:
const Collection = function(request) {
return request.getDb().getModel('Collection');
};
and then something like this in your controller or service layer function
Collection(request).create(request.payload));
from hapi-sequelize.
Related Issues (20)
- unit test$
- documentation HOT 2
- Add test coverage HOT 5
- Migration support HOT 1
- confidence/glue support HOT 5
- Loading fixtures HOT 1
- Does not register as a plugin HOT 3
- Sequelize authenticate failure silenced HOT 5
- Reduce npm module size by removing devDependencies HOT 2
- RangeError: Maximum call stack size exceeded HOT 1
- "onConnect" is not allowed HOT 6
- Handle 'No space left on device' error.
- Example of running migrations HOT 1
- Plugin not detecting models HOT 3
- Associations HOT 1
- Accessing the server instance from a model hook?
- Adding ignored models paths
- I can't register plugin in Hapi 17 HOT 1
- How about update hapi-sequelize to hapi 17 ? HOT 5
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-sequelize.