Home > Software engineering >  How to pass sequelize through express routes to controller in MVC model in node when using ES6 impor
How to pass sequelize through express routes to controller in MVC model in node when using ES6 impor

Time:01-06

I'm trying to refactor some existing code into an MVC model, and am not sure if I'm messing up the structure, or if I just can't figure out how to pass a variable, but, assuming my structure is good, how do I pass a sequelize instance through an Express route to a controller? Here's my code, hopefully simplified for clarity:

Structure:

src/
  db.js
  routes.js
  server.js
  controllers/mycontroller.js
  models/mymodel.js

server.js:

'use strict';

import express from 'express';
import Sequelize from 'sequelize';

import { router as routes } from './routes';
import db from './db';

const app = express();

try {
  await db.authenticate();
  console.log('Connection has been established successfully.');
} catch (error) {
  console.error('Unable to connect to the database:', error);
}

db.myTable.sync(); // this works fine

app.use(express.urlencoded({ extended: false }));
app.use(routes); // this, I think, needs to pass db.myTable

app.listen( 3300, () => {
  console.log('Listening on 3300');
});

db.js:

'use strict';

import Sequelize from 'sequelize';

import myTableModel from './models/mymodel';

const sequelize = new Sequelize('sqlite::memory');
const db = {};

db.authenticate = () => sequelize.authenticate();
db.myTable = myTableModel(sequelize);

export default db;

routes.js:

import express from 'express';
export const router = express.Router();

import MyController from './controllers/mycontroller';

const myController = new MyController();

... // other routes elided for brevity

router.post('/test', myController.store); // that db.myTable I thought I needed to pass above,
// I think I need to pass again here. Or, alternatively, I could put a constructor into
// MyController and pass it as an arg above when I call 'new MyController', but I still have to
// get it down here into this routes file.

mycontroller.js:

'use strict';

import MyTableModel from '../models/mymodel'; // This was an experiment I tried, but in retrospect,
// it of course makes no sense. I don't need to import the model, I need to have passed the
// instantiated model down here somehow

export default class MyController {
  store = async (req, res, next) => {
    await MyTable.create({ full: req.body.fullUrl}); // This fails (of course), because
    // MyTable.create doesn't exist here.

    res.redirect('/');
  }
}

So, back to the question: assuming this structure looks correct (feel free to comment on that as well), how do I get that MyTable sequelize object passed all the way through to the controller, so it can do its thing?

CodePudding user response:

Maybe in calling directly the model ?

'use strict';
import { myTable } from '../db';

export default class MyController {
  store = async (req, res, next) => {
    await MyTable.create({ full: req.body.fullUrl}); 

    res.redirect('/');
  }
}
  •  Tags:  
  • Related