0

This is my managedb.js

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

 var db = new Sequelize('tesf3', 'postgres', 'postgres', {
  dialect: 'postgres',
  port: '5432',
  omitNull: true
});


module.exports.db = db;

var Link = db.import(__dirname + '/lib/link/models').Link;
var LinkUser = db.import(__dirname + '/lib/link/models').LinkUser;

module.exports.Link = Link;
module.exports.LinkUser = LinkUser;

This is my models.js in a library:

var sequelize = require('../../managedb').db;
var DataTypes = require('sequelize-postgres').sequelize;

var Link = sequelize.define('Link', {
    url: {
      type: DataTypes.STRING,
      validate:{
        isUrl: true,
        notEmpty: true,
        notNull: true
      }
    },
    context: {
      type: DataTypes.STRING,
      defaultValue: " "
    },
    previewImage: {
      type: DataTypes.STRING
    },
    source:{
      type: DataTypes.STRING
    },
    shortUrl:{
      type: DataTypes.STRING
    },
    viewed:{
      type: DataTypes.STRING
    },
    image:{
      type: DataTypes.STRING
    },
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true
    }
  },
    {
    instanceMethods: {
      countTasks: function() {
        // how to implement this method ?
      }
    }
  });


var LinkUser = sequelize.define('LinkUser', {
    linkId: {
      type: DataTypes.INTEGER
    },
    userId: {
      type: DataTypes.INTEGER
    },
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true
    }
  },
    {
    instanceMethods: {
      countTasks: function() {
        // how to implement this method ?
      }
    }
  });


module.exports.Link = Link;
module.exports.LinkUser = LinkUser;

Where am I going wrong? Can't I define more than one model in a single js file?

1
  • if you post the trace , it will be helpful Commented Aug 25, 2013 at 14:03

1 Answer 1

2

Sequelize.import expects a function to be returned from your model file(s). I have each model in a separate file. For example, link.js could be something like this:

module.exports = function(sequelize, DataTypes) {
    return sequelize.define('Link', {
        url: {
          type: DataTypes.STRING,
          validate:{
            isUrl: true,
            notEmpty: true,
            notNull: true
          }
        },
        context: {
          type: DataTypes.STRING,
          defaultValue: " "
        },

        ...

    });
}

Then in your managedb.js file, you can loop through the files:

// load models
var models = [
 'Link',
 'LinkUser'
];

models.forEach(function(model) {
 module.exports[model] = sequelize.import(__dirname + '/' + model);
});
Sign up to request clarification or add additional context in comments.

Comments

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.