How to configure mysql and Sequelize in Kraken.js

I am trying to learn how to work with KrakenJs and use mysql for my database. but I couldn’t find anything on the net, this is what I came up with. I created a folder called lib, and I want my db.js connection file to be in the database

'use strict';

var mysql = require('mysql');
var Sequelize = require('sequelize');

var sequelize = new Sequelize('site', 'root', 'root', {
  host: 'localhost',
  dialect: 'mysql',

  pool: {
    max: 5,
    min: 0,
    idle: 10000
},

});


module.exports = sequelize;

First of all, I'm not sure if I need to enable my main index.js file configuration option ...

'use strict';

var express = require('express');
var kraken = require('kraken-js');
var db = require('./lib/db');

var options, app;

/*
 * Create and configure application. Also exports application instance     for use by tests.
 * See https://github.com/krakenjs/kraken-js#options for additional configuration options.
 */
options = {
    onconfig: function (config, next) {
        /*
         * Add any additional config setup or overrides here. `config` is an initialized
         * `confit` (https://github.com/krakenjs/confit/) configuration object.
         */
           next(null, config);
      }
};

How to set up my model is just a simple model, I just want to get an idea of ​​how to use it ...

'use strict';

var db = require('../lib/db');

module.exports = function User() {

   var User = sequelize.define('user', {
    firstName: {
        type: Sequelize.STRING,
    },
    lastName: {
        type: Sequelize.STRING
    }
    }, {
    freezeTableName: true // Model tableName will be the same as the   model name
    });

};

And finally, how to use it my controller

'use strict';

var User = require('../models/users');


module.exports = function (router) {

// var model = new User();

router.get('/', function (req, res) {

    User.findOne().then(function (user) {
            res.render('index', user);

        });



    });

};
+4
source share

Source: https://habr.com/ru/post/1616669/


All Articles