5

This is my managedb.js which manages all the database models:

var Sequelize = require('sequelize-postgres').sequelize
var postgres  = require('sequelize-postgres').postgres

 var db = new Sequelize('testdb', 'postgres', 'postgres', {
  dialect: 'postgres'
})


var models = [
'user'
];

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



exports.db = db;

This is my user.js

var sequelize = require("sequelize");
var seq = require("./managedb");
var db = seq.db;

var Project = db.define('Project', {
  title: sequelize.STRING,
  description: sequelize.TEXT
});

In my app.js

var seq = require('./models/managedb');
seq.db.sync();

Error I get is this:

var Project = sequelize.define('Project', {
                        ^
TypeError: Object function (database, username, password, options) {
    var urlParts
    options = options || {}

    if (arguments.length === 1 || (arguments.length === 2 && typeof username === 'object')) {
      options = username || {}
      urlParts = url.parse(arguments[0])
      database = urlParts.path.replace(/^\//,  '')
      dialect = urlParts.protocol
      options.dialect = urlParts.protocol.replace(/:$/, '')
      options.host = urlParts.hostname

      if (urlParts.port) {
        options.port = urlParts.port
      }

      if (urlParts.auth) {
        username = urlParts.auth.split(':')[0]
        password = urlParts.auth.split(':')[1]
      }
    }

    this.options = Utils._.extend({
      dialect: 'mysql',
      host: 'localhost',
      port: 3306,
      protocol: 'tcp',
      define: {},
      query: {},
      sync: {},
      logging: console.log,
      omitNull: false,
      queue: true,
      native: false,
      replication: false,
      pool: {},
      quoteIdentifiers: true
    }, options || {})

    if (this.options.logging === true) {
      console.log('DEPRECATION WARNING: The logging-option should be either a function or false. Default: console.log')
      this.options.logging = console.log
    }

    this.config = {
      database: database,
      username: username,
      password: (( (["", null, false].indexOf(password) > -1) || (typeof password == 'undefined')) ? null : password),
      host    : this.options.host,
      port    : this.options.port,
      pool    : this.options.pool,
      protocol: this.options.protocol,
      queue   : this.options.queue,
      native  : this.options.native,
      replication: this.options.replication,
      maxConcurrentQueries: this.options.maxConcurrentQueries
    }

    var ConnectorManager = require("./dialects/" + this.options.dialect + "/connector-manager")

    this.daoFactoryManager = new DAOFactoryManager(this)
    this.connectorManager  = new ConnectorManager(this, this.config)

    this.importCache = {}
  } has no method 'define'

1 Answer 1

7

This is because you have to instantiate Sequelize.

var Sequelize = require("sequelize");

var sequelize = new Sequelize('database', 'username');

var Project = sequelize.define('Project', {
  title: sequelize.STRING,
  description: sequelize.TEXT
});
Sign up to request clarification or add additional context in comments.

6 Comments

Do I've to instantiate sequelize in every model then? I can just import it from the manage db, right?
Yes, generally you should use the same instance for your models. Edit: You can module.exports your instance and use it to define your models in other files.
The error still persists. I edited the question to show the change in code. Did I export/import it wrong?
The error that I'm currently getting it this: TypeError: Cannot call method 'define' of undefined
Does it works if you use module.exports.db = db instead of exports.db = db?
|

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.