Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial ref syntax #270

Merged
merged 12 commits into from
Jan 6, 2017
Merged

Initial ref syntax #270

merged 12 commits into from
Jan 6, 2017

Conversation

elhigu
Copy link
Contributor

@elhigu elhigu commented Jan 3, 2017

Adds support to easy places to be able to use ref syntax instead of knex.raw... check test cases which parts are now supported.

Stuff that should be done nicer and implemented later on:

  • Documentation!!
  • JoinBuilder is currently inherited from BaseQueryBuilder so there are way too many methods available
  • Typings at least for ReferenceBuilder
  • Add also shorthand wrappers for lit (literals with casting info and e.g. which sql-array/list syntax certain passed array value is) and raw (convenience wrapper for knex.raw, which will be converted to knex.raw by objection APIs like ref)
  • Implementations for mysql and sqlite json syntax

@elhigu
Copy link
Contributor Author

elhigu commented Jan 3, 2017

This stuff requires psql >= 9.5 gotta set travis to use it also... tests might have some problems with node < 4 . I'll check those a bit later...

@elhigu elhigu force-pushed the initial-ref-syntax branch 5 times, most recently from 2604726 to 8478fe1 Compare January 3, 2017 23:19
@coveralls
Copy link

coveralls commented Jan 3, 2017

Coverage Status

Coverage increased (+0.04%) to 97.026% when pulling 543179d on elhigu:initial-ref-syntax into 6a425e0 on Vincit:master.

@kapouer
Copy link
Contributor

kapouer commented Jan 4, 2017

That looks fantastic but is missing documentation ?

@elhigu
Copy link
Contributor Author

elhigu commented Jan 4, 2017

@kapouer yes it is, I wanted sami to check this out before writing it to have kind of final code ready first.

.select(['id', ref('jsonObject:attr').as('foo')])
.groupBy([ref('jsonObject:attr'), 'id'])
.having('id', '>=', ref('jsonObject:attr').castInt())
// knex doesnt seem to support nested having
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

outdated comment, it just didn't support raw as a first parameter for having.

});
});

it('should update nicely', function () {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need to update more descriptive test case name

});
});

it('should patch nicely', function () {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bad name here as well

Copy link
Collaborator

@koskimas koskimas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really nice stuff in general!

const ret = super.call(builder, args);
// treat select(['1','2','3']) and select('1','2','3') the same way
const normalizedArgs = (args.length === 1 && Array.isArray(args[0])) ? args[0] : args;
const ret = super.call(builder, normalizedArgs);
const selections = _.flatten(this.args);

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_.flatten does exacly the same normalization that you have added above. You could simply move the _.flatten call before the super call.

const joinClauseBuilder = new JoinBuilder(knex);
func.call(joinClauseBuilder, joinClauseBuilder);
joinClauseBuilder.buildInto(knexQueryBuilder);

} else {
// This case is for function argument `join` operation and other methods that
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update the comment

json[attr] = JSON.parse(value);
} catch (err) {
// json column might contain plain single string which is not wrapped to array / object
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need this try-catch?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like it was necessary, because pg driver converts json-string to non quoted string.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shitballs

@@ -261,7 +267,7 @@ export default class Model extends ModelBase {
const attr = jsonAttr[i];
const value = json[attr];

if (_.isObject(value)) {
if (_.isObject(value) && !(value instanceof KnexRaw)) {
json[attr] = JSON.stringify(value);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this needed after the UpdateOperation stuff is moved to onBuild?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. I added test case, where json column value is updated with knex.raw and it fails even without reference builder stuff.

This failed because circular data structure:

        return BoundModel.query()
          .update({
            jsonArray: BoundModel.knex().raw('to_jsonb(??)', ['name'])
          }).then(function (result) {
            expect(result).to.be(4);
          });

args[0][key] = loweredValue;
}
});

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be moved to onBuild and the references should be stashed in splitQueryProps function as discussed.

// convert reference builders to knex.raw
args[i] = args[i].map(arg => {
return arg instanceof ReferenceBuilder ? knex.raw(...arg.toRawArgs()) : arg;
})
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this needed since you normalize the select args into a flat array?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.groupBy() with multiple arguments will fail without this.

@elhigu
Copy link
Contributor Author

elhigu commented Jan 6, 2017

Fixed everything!

@coveralls
Copy link

coveralls commented Jan 6, 2017

Coverage Status

Coverage increased (+0.05%) to 97.039% when pulling 44859de on elhigu:initial-ref-syntax into 6a425e0 on Vincit:master.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants