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

search by parameter 'q' does not work #220

Open
JSProto opened this issue Oct 3, 2017 · 12 comments
Open

search by parameter 'q' does not work #220

JSProto opened this issue Oct 3, 2017 · 12 comments

Comments

@JSProto
Copy link

JSProto commented Oct 3, 2017

i have model and resource

const User = sequelize.define("User", {
      username: DataTypes.STRING
});

let userResource = epilogue.resource({
    model: db.User,
    endpoints: ['/users', '/users/:id']
});

I try to make a request GET /users?q=testuser to get one item. but I get the whole list.

think the mistake is here:
https://github.com/dchester/epilogue/blob/master/lib/Controllers/list.js#L145

variable criteria has one key {[Symbol (or)]: [{username: [Object]}]}
but Object.keys(criteria).length is 0
and the condition is not met and options.where is not written.

test example:

var s = Symbol.for('s'); 
var o = {}; o[s] = true;
console.log(Object.keys(o).length) // 0
@GabeAtWork
Copy link

GabeAtWork commented Oct 17, 2017

I stumbled accross the same problem. It seems the whole search block is broken with Sequelize 4 (I'm on 4.13.4).

I've tracked the bug down to these lines:

if (Object.keys(criteria).length)
criteria = Sequelize.and(criteria, Sequelize.or.apply(null, search));
else
criteria = Sequelize.or.apply(null, search);

It appears than nothing is returned by Sequelize.or.apply our Sequelize.and.apply anymore. I'm looking for ways to fix it.

Edit

(an hour later)

Alright, so @JSProto's assertion was right: symbols don't show up in object keys, meaning the Object.keys(criteria).length test in insufficient. I propose to test criteria presence like so :

Object.keys(criteria).length || (Object.getOwnPropertySymbols && Object.getOwnPropertySymbols(criteria).length)

I'll submit a PR.

@micksatana
Copy link

Similar fix may need to appy at this line?

if (Object.keys(criteria).length) {

@JSProto
Copy link
Author

JSProto commented Oct 18, 2017

and change this line.

Object.keys(criteria).forEach(function(attr) { delete req.params[attr]; });

I think you need to add a method to util.keys as a replacement for Object.keys, which will return all the object keys as an array. to get the correct size of the object and to iterate these keys.

@GabeAtWork
Copy link

Would I just make a new util file in lib/ then? With only that function?
I'm afraid the iteration won't work as expected, because of the nature of symbols. I'll test it.

@GabeAtWork
Copy link

I updated the PR. I replaced every call to Object.keys() with a call to lib/util getKeys(), which takes symbols into account. Tell me if I should roll that back and only use it on the few instances that were mentioned in the issue.

I ran tests/util.test.js on a system which knows Symbols, it would crash otherwise. The getKeys() function itself checks for the presence of Object.getOwnPropertySymbols though, so it should be safe on systems which don't support Symbols.

@JSProto
Copy link
Author

JSProto commented Oct 18, 2017

You can use Reflect.ownKeys if exists to get all keys.

let obj = {
    enum: 1,
    'non-enum': 2,
    [Symbol('symbol_key')]: 3
};
Object.defineProperty(obj, 'non-enum', { enumerable: false });

Object.getOwnPropertyNames(obj); // ['enum', 'non-enum']
Object.getOwnPropertySymbols(obj); // [Symbol(symbol_key)]
Reflect.ownKeys(obj); //[ 'enum', 'non-enum', Symbol(symbol_key)]

you can use this example or similar method in lib/util.js or other place:

exports.keys = function(obj){
    if (typeof Reflect === 'object') {
        return Reflect.ownKeys(obj);
    }
    else {
        let getOwnExists = typeof Object.getOwnPropertySymbols === 'function' && typeof Object.getOwnPropertyNames === 'function';
        return getOwnExists ? Object.getOwnPropertyNames(obj).concat(Object.getOwnPropertySymbols(obj)) : Object.keys(obj);
    }
};

@JSProto
Copy link
Author

JSProto commented Oct 18, 2017

sorry, I can check test in the evening when I get home.

@GabeAtWork
Copy link

GabeAtWork commented Oct 18, 2017

I updated the PR with the use of Reflect

@GabeAtWork
Copy link

Okay, so we now have 2 PRs:

  • The original fix is now here : Fix search with sequelize 3 #224
  • A new version with symbols worked on by @michailpanagiotis, with a dependency bump to Sequelize 4, integrating the new Symbols. Some more work is required, tests have broken with Sequelize 4.

@tommybananas
Copy link

tommybananas commented Nov 10, 2017

Hi everyone, I am maintaining a new project based off of Epilogue that has full test coverage for Sequelize 4.x.x. I have already merged this and other relevant pull requests from this project. Contributions welcome!

https://github.com/tommybananas/finale

@omidn
Copy link

omidn commented Dec 18, 2017

@tommybananas Will you create a PR?

@tommybananas
Copy link

tommybananas commented Dec 18, 2017

Not here, I am already maintaining a Sequelize 4.x compatible version with this fix implemented:

https://github.com/tommybananas/finale

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

5 participants