View on GitHub

ant-js

Generic query manager that scales!

Queries

A query is a request for data or information from a data source. In the context of AntJS, a query is in the context of a model, a request for ids of certain entities (of the model) from a data source.

We define queries as ids requests because it allows us to have a fast cache system:

  1. Cached entities are mapped by its id field.
  2. Any decent data source should be able to index entities by its id.
  3. Ids are inmutable.

Keeping that in mind, defining queries as ids requests allow us to sync the cache system in an efficient way because a change in an entity can be reflected in the cache system with just two simple write operations:

  1. Write the new entity.
  2. Move (if necessary) the entity id from a query to other one. This operation is necessary if the entity is no longer associated to the cached query because of the entity’s change.

Remember that a query is a request for entities of the same model. If you want to perform queries that targets different models, you will have to create (at least) one query for each model.

Another important restriction when creating queries is the following one:

For each query, there must be only a way to find an entity

Example of a good query: A query that takes a letter and finds all the users whose username starts with the letter is a valid query because, for each user, there is only a way to find the user. The only way to find the user “notaphplover” is passing “n” to the query.

Example of a bad query: A query that takes a letter and finds all the users whose username contains the letter is a bad query. There are multuple ways to find the user “notaphplover” (“n”, “o”, “t”…)

This restriction is one of the keys to build a fast cache algorithm.

If you really need to cache a “bad” query, you can try to simulate it as a set of multiple “good” queries:

Sometimes you won’t be able to create a “good” query. In these cases, it’s probably because it’s not a good idea to create the cached query.