In the official documentation of Sequelize (http://docs.sequelizejs.com/en/v3/docs/raw-queries/), it mentions:
If you pass a model, the returned data will be instances of that model.
// The caller is the model definition. This allows easy mapping of a query to a predefined model
sequelize.query('SELECT * FROM projects', { model: Projects }).then(function(projects){
// Each record will now be an instance of Project
})
A model has been defined for a resource named agent
.
module.exports = function(sequelize, DataTypes) {
let Agent = sequelize.define('Agent', {
responseStatus: DataTypes.STRING,
agentnum: {
type: DataTypes.STRING,
primaryKey: true,
allowNull: false,
field : 'agentno'
},
fname : {
type: DataTypes.STRING,
allowNull : false,
field: 'fname'
},
lname : {
type: DataTypes.STRING,
allowNull: false,
field : 'lname'
},
fullname : {
type: DataTypes.STRING,
allowNull : false,
field: 'full_name'
},
status : {
type: DataTypes.STRING,
allowNull: false,
field: 'business_status'
},
loginDate: DataTypes.DATE
}, {
freezeTableName: false,
timestamps: false
});
return Agent;
};
However, when using sequelize.query
with my query and specifying the model as Agent, I encounter an error thrown from sequelize:
TypeError: this.model.bulkBuild is not a function
The stack trace points to
sequelize\lib\dialects\abstract\query.js:675
.
This issue persists until I apply a QueryType of sequelize.QueryTypes.RAW
. At this point, the query executes but returns a JSON response that is not an instance of my Agent model. The JSON response contains field names that should have been mapped to.
I have imported my model according to the instructions in their express sample (https://github.com/sequelize/express-example/blob/master/models/index.js). The models collection confirms that my Agent model is included.
import Sequelize from 'sequelize';
import config from './config';
export default callback => {
const sequelize = new Sequelize(config.database, config.username, config.password, config.params);
sequelize.sync().then(function() {
let db = { }
let agentModel = sequelize.import('model/agent.js');
db[agentModel.name] = agentModel;
db.sequelize = sequelize;
db.Sequelize = Sequelize;
db.sequelize.authenticate().then(function() {
console.log('CONNECTION OK');
});
callback(db);
}).catch(function(err) {
console.log('FAILED TO CONNECT: ', err.message);
});
}
I expect the query to return an instance of Agent when invoked (e.g., through a POST request to my API). I am utilizing MS SQL Server 2008 R2.
Any suggestions are welcomed. Thank you.
EDIT 1/30 Here is the code generating the sequelize object and passing in the model. Although the model collection shows that my item is added, it does not have any properties.
connectDb: (function () {
var sequelize;
function createInstance() {
var sequelizeInstance, connectedAndAuthenticated;
sequelizeInstance = new Sequelize(config.database, config.username, config.password, config.params);
connectedAndAuthenticated = sequelizeInstance.authenticate();
connectedAndAuthenticated.sequelize = sequelizeInstance;
connectedAndAuthenticated.Sequelize = Sequelize;
var model = sequelizeInstance.import('../src/model/agent.js');
return connectedAndAuthenticated;
}
return {
getInstance : function () {
if (!sequelize) {
sequelize = createInstance();
}
return sequelize;
}
};
}())
EDIT 1/26 After modifying the QueryTypes
, I realized two things - inadvertently creating a table in the database with the name of the model (Agent) and having the object returned with an empty value for tablename
property. Even though the schema
and tablename
are specified by me, since the query is a stored procedure involving multiple queries and tables, it doesn't directly map to an object named Agent
in my database. However, based on the documentation, it seems that this shouldn't matter, as I am creating my own model tied to the query result.