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

Can't query jsonb column keys containing periods via http querystring #76

Closed
Aerlin opened this issue Dec 20, 2019 · 2 comments
Closed

Comments

@Aerlin
Copy link

Aerlin commented Dec 20, 2019

When querying a jsonb column by a key containing a period, Objection needs the key to have square brackets around it. .

Objection example of how it looks.

var results = await Item.query().where(ref('properties:[oai.iai.al]').castInt(), 80)

Now when querying via feather's service.find() it works.

var results = await app.service('items').find({
  query: {
    properties: { 
    	"[oai.iai.al]": 80
    }
  }
});

But when wrapping the key with brackets in a querystring it fails.

var res = await fetch("http://127.0.0.1:8471/items?" + qs.stringify({
	properties: {
		"[oai.iai.al]": 80
	}
}));

I'm thinking the express.urlencoded() middleware is parsing the brackets as a array. Feathers-Objection may need to check if the key contains periods and add the brackets itself.

Adding this to objectify() seems to help.

Object.keys(params || {}).forEach(key => {
	let value = params[key];

	// Wrap key with brackets if it contains a period.
	if (parentKey && key.includes(".") && !key.match(/\[(.*)\]/)) {
		key = "[" + key + "]";
	}

	if (_utils.default.isPlainObject(value)) {
		return this.objectify(query, value, key, parentKey);
	}
	...
@dekelev
Copy link
Member

dekelev commented Dec 27, 2019

Try this:

var res = await fetch("http://127.0.0.1:8471/items?" + qs.stringify({
	properties: {
		"(oai.iai.al)": 80
	}
}));

Querying with dot will cause Postgres to target a nested field and there are tests in feathers-objection to verify this functionality.

When querying a JSON object, you can wrap a prop name with () and it will be replaced with [].

I've added an example to the docs

@dekelev dekelev closed this as completed Dec 27, 2019
@Aerlin
Copy link
Author

Aerlin commented Dec 27, 2019

Thank you @dekelev!

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

No branches or pull requests

2 participants