1

I'd like to cache data stored in some MySQL tables that doesn't change much. Examples for discussion would be country_codes, language_codes, etc., but I have additional application specific data as well. That in itself is not hard. What makes this difficult, I think, is that I have two constraints that seem almost contradictory in the world of Node, Sequelize and Express:

  1. I want the data accessible with "inline" functions that I'll use extensively within JavaScript expressions and statements. For example, I'd like to be able to concatenate strings with a class method like Country.nameFromCode(code). Presumably, this means that these methods are synchronous. Callbacks and promises seem like a real workaround for such a seemingly straightforward request/requirement.

  2. I want the data lazy loaded. I could easily eager load the data at startup in the typical bin/www script of express-generator. But here again, it seems like my request to lazy load the data is reasonable. Presumably, the .nameFromCode function should only load the data from the database the first time it's accessed, then cache the data.

So: should I use a module such as deasync, sync, or synchronize the first time I get the .nameFromCode call? Do I have to? It seems like there should be an easier answer.

Does Sequelize or the mysql driver have a synchronous .query or .find method? If so, presumably that's a better approach?

Should I give up on synchronous anything? :) I know that asynchrony is the mantra of node, etc., but it seems like a burdensome price to pay for situations like this.

In addition to the modules linked above, here are some references that seem related:

Thanks.

Community
  • 1
  • 1
aap
  • 897
  • 11
  • 15

1 Answers1

0

Does Sequelize or the mysql driver have a synchronous .query or .find method?

Unfortunately, no that I'm aware of. Node is always asynchronous I/O, except for the fs module, which handles both.

should I use a module such as deasync, sync, or synchronize the first time I get the .nameFromCode call? Do I have to? It seems like there should be an easier answer.
[...]
Should I give up on synchronous anything? [...] it seems like a burdensome price to pay for situations like this.

One option you have is to use async/await, which is, as of January of 2016, a stage 3 proposal in the ECMAScript specification.
Basically:

  • Any function that returns a promise can be awaited upon
  • Any function that awaits on others should be marked with async keyword
  • async functions will implicitly return a Promise

Your code will still be asynchronous. But it will almost look like synchronous code. Awesome!

You could implement something like this:

async function claimThatYouAreInCountry ( code ) {
  var country = await Country.nameFromCode( code );
  console.log( "I'm in " + country.name );
}

Another example in Babel playground (check the output/console!)

My suggestion is to use Babel's require hook, so everything gets transparent in your application and you don't need a build step.

gustavohenke
  • 40,997
  • 14
  • 121
  • 129