Coder Social home page Coder Social logo

Using hapi-sequelize about hapi-sequelize HOT 3 CLOSED

danecando avatar danecando commented on June 15, 2024
Using hapi-sequelize

from hapi-sequelize.

Comments (3)

iwasrobbed avatar iwasrobbed commented on June 15, 2024

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.

IAIAE avatar IAIAE commented on June 15, 2024

any api for how to use a model? @iwasrobbed

from hapi-sequelize.

iwasrobbed avatar iwasrobbed commented on June 15, 2024

@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)

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.