Skip to content

Commit

Permalink
fixes 2630 (#2642)
Browse files Browse the repository at this point in the history
  • Loading branch information
koskimas authored and elhigu committed Jun 5, 2018
1 parent d9cd54b commit 1c65e69
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/dialects/postgres/index.js
Expand Up @@ -246,7 +246,8 @@ assign(Client_PG.prototype, {
if (returning === '*' || Array.isArray(returning)) {
returns[i] = row;
} else {
returns[i] = row[returning];
// Pluck the only column in the row.
returns[i] = row[Object.keys(row)[0]];
}
}
return returns;
Expand Down
55 changes: 55 additions & 0 deletions test/integration/builder/additional.js
Expand Up @@ -112,6 +112,61 @@ module.exports = function(knex) {

});

// TODO: This doesn't work on oracle yet.
if (['postgresql', 'mssql'].includes(knex.client.dialect)) {
describe('returning with wrapIdentifier and postProcessResponse`', () => {
let origHooks = {};

before('setup custom hooks', () => {
origHooks.postProcessResponse = knex.client.config.postProcessResponse;
origHooks.wrapIdentifier = knex.client.config.wrapIdentifier;

// Add `_foo` to each identifier.
knex.client.config.postProcessResponse = (res) => {
if (Array.isArray(res)) {
return res.map(it => {
if (typeof it === 'object') {
return _.mapKeys(it, (value, key) => {
return key + '_foo';
});
} else {
return it;
}
})
} else {
return res;
}
};

// Remove `_foo` from the end of each identifier.
knex.client.config.wrapIdentifier = (id) => {
return id.substring(0, id.length - 4);
};
});

after('restore hooks', () => {
knex.client.config.postProcessResponse = origHooks.postProcessResponse;
knex.client.config.wrapIdentifier = origHooks.wrapIdentifier;
});

it('should return the correct column when a single property is given to returning', () => {
return knex('accounts_foo')
.insert({ balance_foo: 123 })
.returning('balance_foo').then(res => {
expect(res).to.eql([123]);
});
});

it('should return the correct columns when multiple properties are given to returning', () => {
return knex('accounts_foo')
.insert({ balance_foo: 123, email_foo: 'foo@bar.com' })
.returning(['balance_foo', 'email_foo']).then(res => {
expect(res).to.eql([{ balance_foo: 123, email_foo: 'foo@bar.com' }]);;
});
});
});
}

it('should forward the .get() function from bluebird', function() {
return knex('accounts').select().limit(1).then(function(accounts){
var firstAccount = accounts[0];
Expand Down

0 comments on commit 1c65e69

Please sign in to comment.