mysql - Get only values from rows and associations with Sequelize -



mysql - Get only values from rows and associations with Sequelize -

i using sequelize, mysql , node write web application.

for of db needs, verification, fetch models (eagerly associations) , send them client, as-is (at least, now).

i wrote little utility function getvaluesfromrows extract values returned row array:

getvaluesfromrows: function(rows, valuesprop) { // pod (plain old data) values valuesprop = valuesprop || 'values'; if (rows instanceof array) { var allvalues = []; (var = 0; < rows.length; ++i) { allvalues[i] = rows[i][valuesprop]; } homecoming allvalues; } else if (rows) { // 1 object homecoming rows[valuesprop]; } homecoming null; } // ... ...findall(...)...complete(function(err, rows) { var allvalues = getvaluesfromrows(rows); sendtoclient(errtostring(err, user), allvalues); });

however, adding more , more complex relations db models. result, more associations have fetch. now, don't have phone call above function values each row, need more complicated utilities values included (eagerly loaded) associations. there way values sequelize queries (and not sequelize model instance) includes associated values instance?

else, have manually "get values each project , add together 1 item values object values property of each entry of project.members" (for example). note things worse fast if nest associations (e.g. members have tasks , tasks have , etc.).

i guessing have write such utility myself?

i found simple solution problem, extending existing pod converting function above recursion include'd associations of query. solution works find, findall, all , perchance other operations non-trivial results.

code

/** * pod (plain old data) values sequelize results. * * @param rows result object or array sequelize query's `success` or `complete` operation. * @param associations `include` parameter of sequelize query. */ getvaluesfromrows: function(rows, associations) { // pod (plain old data) values var values; if (rows instanceof array) { // phone call method on every element of given array of rows values = []; (var = 0; < rows.length; ++i) { // recurse values[i] = this.getvaluesfromrows(rows[i], associations); } } else if (rows) { // 1 row values = rows.datavalues; // values associated rows if (values && associations) { (var = 0; < associations.length; ++i) { var association = associations[i]; var propname = association.as; // recurse values[propname] = this.getvaluesfromrows(values[propname], association.include); }; } } homecoming values; }

example

var postassociations = [ // poster association { model: user, as: 'author' }, // comments association { model: comment, as: 'comments', include: [ { // author association model: user, as: 'author' } ] } ]; // ... var query = { where: ... include: postassociations; }; // query post info db homecoming post.findall(query) // convert sequelize result pod .then(function(posts) { homecoming getvaluesfromrows(posts, postassociations); }) // send pod client .then(client.sendposts);

in above example, client.sendposts receives array. each entry of array have properties author , comments. each comment in comments array have author property. entire array contains pod (plain old data).

mysql node.js associations sequelize.js

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

c# - Create a Notification Object (Email or Page) At Run Time -- Dependency Injection or Factory -

Set Up Of Common Name Of SSL Certificate To Protect Plesk Panel -