Skip to content

Commit

Permalink
fixes Vincit#1089
Browse files Browse the repository at this point in the history
  • Loading branch information
koskimas authored and flipace committed May 7, 2019
1 parent fe5dd4a commit 1b4ecd8
Show file tree
Hide file tree
Showing 3 changed files with 179 additions and 46 deletions.
8 changes: 7 additions & 1 deletion lib/queryBuilder/operations/UpdateOperation.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class UpdateOperation extends QueryBuilderOperation {

convertFieldExpressionsToRaw(builder, json) {
const knex = builder.knex();
const modelClass = builder.modelClass();
const convertedJson = {};

for (const key of Object.keys(json)) {
Expand All @@ -61,7 +62,12 @@ class UpdateOperation extends QueryBuilderOperation {
// "col" : raw(`jsonb_set("col", '{attr}', to_jsonb("other"#>'{lol}'), true)`)

let parsed = ref(key);
let jsonRefs = '{' + parsed.parsedExpr.access.map(it => it.ref).join(',') + '}';
let jsonRefs =
'{' +
parsed.parsedExpr.access
.map(it => modelClass.columnNameToPropertyName(it.ref))
.join(',') +
'}';
let valuePlaceholder = '?';

if (isKnexQueryBuilder(val) || isKnexRaw(val)) {
Expand Down
109 changes: 87 additions & 22 deletions tests/integration/knexSnakeCase.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@ module.exports = session => {
return 'person';
}

static get jsonAttributes() {
return ['address'];
}

static get relationMappings() {
return {
parent: {
parentPerson: {
relation: Model.BelongsToOneRelation,
modelClass: Person,
join: {
Expand Down Expand Up @@ -72,6 +76,10 @@ module.exports = session => {
table.increments('id').primary();
table.string('first_name');
table.integer('parent_id');

if (session.isPostgres()) {
table.jsonb('person_address');
}
})
.createTable('animal', table => {
table.increments('id').primary();
Expand Down Expand Up @@ -174,15 +182,33 @@ module.exports = session => {

describe('queries', () => {
beforeEach(() => {
function maybeWithAddress(obj, address) {
if (session.isPostgres()) {
obj.personAddress = address;
}

return obj;
}

return Person.query(knex).insertGraph({
firstName: 'Seppo',

parent: {
parentPerson: {
firstName: 'Teppo',

parent: {
firstName: 'Matti'
}
parentPerson: maybeWithAddress(
{
firstName: 'Matti'
},
{
personCity: 'Jalasjärvi',

cityCoordinates: {
latitudeCoordinate: 61,
longitudeCoordinate: 23
}
}
)
},

pets: [
Expand Down Expand Up @@ -222,6 +248,41 @@ module.exports = session => {
});
}

it('joinRelation', () => {
return Person.query(knex)
.joinRelation('parentPerson.parentPerson')
.select('parentPerson:parentPerson.firstName as nestedRef')
.then(result => {
expect(result).to.eql([{ nestedRef: 'Matti' }]);
});
});

if (session.isPostgres()) {
it('update with json references', () => {
return Person.query(knex)
.where('firstName', 'Matti')
.patch({
'personAddress:cityCoordinates.latitudeCoordinate': 30
})
.returning('*')
.then(result => {
expect(result).to.containSubset([
{
firstName: 'Matti',
parentId: null,
personAddress: {
personCity: 'Jalasjärvi',
cityCoordinates: {
latitudeCoordinate: 30,
longitudeCoordinate: 23
}
}
}
]);
});
});
}

it('$relatedQuery', () => {
return Person.query(knex)
.findOne({ firstName: 'Seppo' })
Expand All @@ -242,12 +303,14 @@ module.exports = session => {

[Model.WhereInEagerAlgorithm, Model.JoinEagerAlgorithm, Model.NaiveEagerAlgorithm].forEach(
eagerAlgo => {
it(`eager (${eagerAlgo.name})`, () => {
it(`eager (${eagerAlgo})`, () => {
return Person.query(knex)
.select('person.firstName as rootFirstName')
.modifyEager('parent', qb => qb.select('firstName as parentFirstName'))
.modifyEager('parent.parent', qb => qb.select('firstName as grandParentFirstName'))
.eager('[parent.parent, pets, movies]')
.modifyEager('parentPerson', qb => qb.select('firstName as parentFirstName'))
.modifyEager('parentPerson.parentPerson', qb =>
qb.select('firstName as grandParentFirstName')
)
.eager('[parentPerson.parentPerson, pets, movies]')
.eagerAlgorithm(eagerAlgo)
.orderBy('person.firstName')
.then(people => {
Expand All @@ -256,10 +319,10 @@ module.exports = session => {
{
rootFirstName: 'Seppo',

parent: {
parentPerson: {
parentFirstName: 'Teppo',

parent: {
parentPerson: {
grandParentFirstName: 'Matti'
}
},
Expand All @@ -285,7 +348,7 @@ module.exports = session => {
{
rootFirstName: 'Teppo',

parent: {
parentPerson: {
parentFirstName: 'Matti'
}
},
Expand Down Expand Up @@ -322,7 +385,7 @@ module.exports = session => {

static get relationMappings() {
return {
parent: {
parentPerson: {
relation: Model.BelongsToOneRelation,
modelClass: Person,
join: {
Expand Down Expand Up @@ -489,10 +552,10 @@ module.exports = session => {
return Person.query(knex).insertGraph({
firstName: 'Seppo',

parent: {
parentPerson: {
firstName: 'Teppo',

parent: {
parentPerson: {
firstName: 'Matti'
}
},
Expand Down Expand Up @@ -543,12 +606,14 @@ module.exports = session => {

[Model.WhereInEagerAlgorithm, Model.JoinEagerAlgorithm, Model.NaiveEagerAlgorithm].forEach(
eagerAlgo => {
it(`eager (${eagerAlgo.name})`, () => {
it(`eager (${eagerAlgo})`, () => {
return Person.query(knex)
.select('person.firstName as rootFirstName')
.modifyEager('parent', qb => qb.select('firstName as parentFirstName'))
.modifyEager('parent.parent', qb => qb.select('firstName as grandParentFirstName'))
.eager('[parent.parent, pets, movies]')
.modifyEager('parentPerson', qb => qb.select('firstName as parentFirstName'))
.modifyEager('parentPerson.parentPerson', qb =>
qb.select('firstName as grandParentFirstName')
)
.eager('[parentPerson.parentPerson, pets, movies]')
.eagerAlgorithm(eagerAlgo)
.orderBy('person.firstName')
.then(people => {
Expand All @@ -557,10 +622,10 @@ module.exports = session => {
{
rootFirstName: 'Seppo',

parent: {
parentPerson: {
parentFirstName: 'Teppo',

parent: {
parentPerson: {
grandParentFirstName: 'Matti'
}
},
Expand All @@ -586,7 +651,7 @@ module.exports = session => {
{
rootFirstName: 'Teppo',

parent: {
parentPerson: {
parentFirstName: 'Matti'
}
},
Expand Down

0 comments on commit 1b4ecd8

Please sign in to comment.