1

Working on a project for a complete rewrite of an RoR to Node/Sequelize/Express/Angular app.

I have executed the old schema on my local machine and can view the db, tables and data OK.

Within my Node code I need to connect the existing DB.

I am following this tutorial: http://redotheweb.com/2013/02/20/sequelize-the-javascript-orm-in-practice.html

My question arrises when using sequelize.import and pointing it a directory.

  1. Should this be my local MySQL directory where the schema was set up? (assuming yes)
  2. Does model refer to the table in the DB?

Advice on how to correctly access the existing relational mapping is helpful.

This if my first question on Stack, thank you in advance.

var express = require('express');
var http = require('http');
var path = require('path');
var config = require('./config/config');

var Sequelize = require('sequelize');

var app = express();

var db = new Sequelize('somedb', 'root', 'password');

var models = [
  'users'
];

models.forEach(function(model){
  module.exports[model] = sequelize.import(__dirname + '/' + )
})

1 Answer 1

1

Sequelize.import CANNOT import your current SQL schemas. You will need to write javascript code to define your models:

// in models/users.js
module.exports = function(sequelize, DataTypes) {
    return sequelize.define('User', {
        first_name: DataTypes.STRING,
        last_name: DataTypes.STRING,
    });
};

Assuming the script you posted is the root and there is a folder named models with a file users.js in the same directory, then the script will work. The path you pass to .import is the file system path of the javascript file that defines your model

Sign up to request clarification or add additional context in comments.

1 Comment

This is helpful. Ended up using a more basic pattern of exporting an object. My file is long, but it's quicker. You were right. A npm to do this would be cool. module.exports = { Users: Users, Races: Races, Race_Users: Race_Users };

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.