Skip to content

Instantly share code, notes, and snippets.

@cpsubrian
Last active December 17, 2015 03:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cpsubrian/5542689 to your computer and use it in GitHub Desktop.
Save cpsubrian/5542689 to your computer and use it in GitHub Desktop.
Streams instead of ORM.

Streams instead of ORM

Pros:

  • Streams?
  • More manual query control.
  • Combine different transforms depending on use case.

Cons:

  • Every dev has to have intimiate knowledge of schema
  • Have to know all the information you will need, or have to assume you will need more than you know about (e.g. avatars).
  • Have to know all the transforms needed at the point the data is queried for.
  • SQL clumsy to work with in javascript (no heredocs or multi-line strings)
// ORM
app.models.users.find({
status: 'active',
limit: 10,
order: 'name ASC',
populate: 'avatar'
}, function (err, users) {
if (err) return next(err);
app.sanitizeModels(users, req, function (err, users) {
// Do Something with users.
});
});
// STREAM
var sql = "SELECT users.*, files.url as avatar_url FROM users " +
"LEFT JOIN files ON files.id = users.avatar_id" +
"WHERE status = 'active' " +
"ORDER BY name ASC LIMIT 10";
var query = app.mysql.query(sql).stream()
.pipe(formatters('users'))
.pipe(sanitize('users', req));
// Do something with the stream?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment