Skip to content

Commit

Permalink
fix(sequelize/query): use hasOwnProperty for keys in query (#9100) (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
jharting authored and sushantdhiman committed Feb 25, 2018
1 parent f1c15df commit 222b13f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 7 deletions.
14 changes: 7 additions & 7 deletions lib/dialects/abstract/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ class AbstractQuery {
// Map each key to an include option
let previousPiece;
const buildIncludeMap = piece => {
if ($current.includeMap[piece]) {
if ($current.includeMap.hasOwnProperty(piece)) {
includeMap[key] = $current = $current.includeMap[piece];
if (previousPiece) {
previousPiece = previousPiece+'.'+piece;
Expand All @@ -432,15 +432,15 @@ class AbstractQuery {
// Calculate the string prefix of a key ('User.Results' for 'User.Results.id')
const keyPrefixStringMemo = {};
const keyPrefixString = (key, memo) => {
if (!memo[key]) {
if (!memo.hasOwnProperty(key)) {
memo[key] = key.substr(0, key.lastIndexOf('.'));
}
return memo[key];
};
// Removes the prefix from a key ('id' for 'User.Results.id')
const removeKeyPrefixMemo = {};
const removeKeyPrefix = key => {
if (!removeKeyPrefixMemo[key]) {
if (!removeKeyPrefixMemo.hasOwnProperty(key)) {
const index = key.lastIndexOf('.');
removeKeyPrefixMemo[key] = key.substr(index === -1 ? 0 : index + 1);
}
Expand All @@ -450,9 +450,9 @@ class AbstractQuery {
const keyPrefixMemo = {};
const keyPrefix = key => {
// We use a double memo and keyPrefixString so that different keys with the same prefix will receive the same array instead of differnet arrays with equal values
if (!keyPrefixMemo[key]) {
if (!keyPrefixMemo.hasOwnProperty(key)) {
const prefixString = keyPrefixString(key, keyPrefixStringMemo);
if (!keyPrefixMemo[prefixString]) {
if (!keyPrefixMemo.hasOwnProperty(prefixString)) {
keyPrefixMemo[prefixString] = prefixString ? prefixString.split('.') : [];
}
keyPrefixMemo[key] = keyPrefixMemo[prefixString];
Expand All @@ -462,7 +462,7 @@ class AbstractQuery {
// Calcuate the last item in the array prefix ('Results' for 'User.Results.id')
const lastKeyPrefixMemo = {};
const lastKeyPrefix = key => {
if (!lastKeyPrefixMemo[key]) {
if (!lastKeyPrefixMemo.hasOwnProperty(key)) {
const prefix = keyPrefix(key);
const length = prefix.length;

Expand Down Expand Up @@ -527,7 +527,7 @@ class AbstractQuery {
$keyPrefix = keyPrefix(key);

// On the first row we compute the includeMap
if (rowsI === 0 && includeMap[key] === undefined) {
if (rowsI === 0 && !includeMap.hasOwnProperty(key)) {
if (!$keyPrefix.length) {
includeMap[key] = includeMap[''] = includeOptions;
} else {
Expand Down
35 changes: 35 additions & 0 deletions test/integration/model/attributes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,41 @@ describe(Support.getTestDialectTeaser('Model'), () => {
expect(person.get('toString')).to.equal('Jozef');
});
});

it('allows for an attribute to be called "toString" with associations', function () {
const Person = this.sequelize.define('person', {
name: Sequelize.STRING,
nick: Sequelize.STRING
});

const Computer = this.sequelize.define('computer', {
hostname: Sequelize.STRING,
});

Person.hasMany(Computer);

return this.sequelize.sync({force: true})
.then(() => Person.create({name: 'Jozef', nick: 'Joe'}))
.then(person => person.createComputer({hostname: 'laptop'}))
.then(() => Person.findAll({
attributes: [
'nick',
['name', 'toString']
],
include: {
model: Computer
},
where: {
name: 'Jozef'
}
}))
.then(result => {
expect(result.length).to.equal(1);
expect(result[0].dataValues['toString']).to.equal('Jozef');
expect(result[0].get('toString')).to.equal('Jozef');
expect(result[0].get('computers')[0].hostname).to.equal('laptop');
});
});
});
});
});

0 comments on commit 222b13f

Please sign in to comment.