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

Feature request: Query model tables and curry query functions #8

Closed
RoelWKramer opened this issue Aug 1, 2011 · 1 comment
Closed

Comments

@RoelWKramer
Copy link

In django's ORM it is possible to chain query functions and query model tables immediately. Example:

Entry.objects.filter(headline__startswith='What').exclude(pub_date__gte=datetime.now()).filter(pub_date__gte=datetime(2005, 1, 1))

Example 2:

    q1 = Entry.objects.filter(headline__startswith="What")
    q2 = q1.exclude(pub_date__gte=datetime.now())
    q3 = q1.filter(pub_date__gte=datetime.now())

Now you have to use joli.query().select().from('dbtable_name').where('name', 'Roel'); Which is quite long and also introduces where, select and query functions. Using this also has little benefits over using the SQL language.

It would be nice if it is possible to chain query functions and query tables immediately like this:
dbtable_name.findBy('name', 'Roel').findBy('lastname', 'kramer') or like this:
dbtable_name.where('name', 'Roel').where(lastname', 'kramer')

See the django docs for a better explanation. It uses other function names, which might inspire you, both for good new functionality and naming of functions. Docs: https://docs.djangoproject.com/en/dev/topics/db/queries/

@xavierlacot
Copy link
Owner

Hi Roel,

The query model offered by joli.js is rather consistant and almost allows what you are asking. For instance, it is possible to search records at a class-level:

// returns one 'City' record
models.city.findOneBy('name', 'Paris');

// returns a collection of 'Human' records
models.human.findBy('city_id', 12);

You can just not chain the findBy() calls, eg. models.human.findBy('city_id', 12).findBy('first_name', 'Michel'); For such needs, there are two ways in joli.js:

1- use the abstract query language:

// returns the humans living in the city 12 and with "Michel" as first name
var q = new joli.query().select().from('human').where('city_id = ?', 12).andWhere('first_name = ?', 'Michel');
var humans = q.execute();

Note that this approach as a major benefit over plain SQL: it allows to build queries over several methods / functions, and to progressively add restrictions on queries objects. This is particularly useful in the seek for code mutualisation.

2- use the class-level "all()" finder :

// returns the humans living in the city 12 and with "Michel" as first name
var humans = models.human.all({
  where: [
    'city_id = ?': 12,
    'first_name = ?': 'Michel',
  ]
});

Have a look at joli.js's code, or rather at the test suite, which provides a lot of useful examples: https://github.com/xavierlacot/joli.js-demo/tree/master/Resources/test

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