aptivator / mongo-query-compiler

Transpiler that converts mongodb-like object queries into JavaScript filtering functions
11 stars 3 forks source link

$regex vs $regexp #4

Closed ianengelbrecht closed 1 year ago

ianengelbrecht commented 1 year ago

This library has $regexp operator, but Mongo has $regex. It would be nice if these were consistent so that query objects could be used in different contexts (filtering local lists vs fetching records from a db using something like RxDB, for example). It would also be great if the $regex operator could take strings as well as RegExp objects, also to be consistent with the Mongo API.

aptivator commented 1 year ago

How critical is it? The library is not meant to be 100% Mongo-compatible. It is more Mongo-inspired. For example, I've added $ref operator when I was first writing it and as of version 5.0 MongoDB supports $expr operator. The purpose for the two I think is the same, but Mongo's implementation is slightly different. And, I am not going to worry about it, especially since I've added $ref before mongo added $expr. There are other differences as well. Mongo supports dot-delimited nested queries, but it does not support unwinding the way the library does. Mongo has bit query operators and mongo-query-compiler does not. Etc.

ianengelbrecht commented 1 year ago

Thank you for the feedback @aptivator, yes I can understand that the two APIs would not be identical. My use case is that my data access layer uses these mongo style queries, and I'm mocking the database using faker.js to make arrays of data and mongo-query-compiler to 'query' those arrays during testing. The real database is rxDB, which also uses mongo query objects (called mango, built for CouchDB). The use case is that the user selects the fields, operators, and query terms in the ui, and the app builds a mongo query object based on that input which then gets passed to the data access layer. It would be great to be able to pass the same objects to the mock and production databases, and I think it would widen the applicability of mongo-query-compiler as a testing tool. I can't say how critical this is as it just my particular application, but perhaps you might want to consider it in terms of cross compatibility with these other database systems.

** I see PouchDB also uses mango queries, so that's rxDB, CouchDB, PouchDB, and of course Mongo itself.

aptivator commented 1 year ago

I am fine with the library having mongo-compatible query api. Adding an alias to $regexp and converting string regexes to RegExp objects is simple to do. Adding other operators should be straightforward as well.

You can add it and just do a pull request. Don't forget to add tests so that coverage remains at 100%. There is also a documentation repo that would need updating.

aptivator commented 1 year ago

I've added code and documentation for the $regex alias and also $options operators. Feel free to contribute next time.