Get only values ​​from strings and associations using Sequelize

I use Sequelize, MySQL and Node to write a web application.

For most of my database needs, I usually do some checking, then retrieve my models (with impatience with the associations) and send them to the client, almost always as they are (at least so far).

I wrote a small utility function getValuesFromRowsto extract values ​​from a returned array of strings:

getValuesFromRows: function(rows, valuesProp) {
    // get POD (plain old data) values
    valuesProp = valuesProp || 'values';
    if (rows instanceof Array) {
        var allValues = [];
        for (var i = 0; i < rows.length; ++i) {
            allValues[i] = rows[i][valuesProp];
        }
        return allValues;
    }
    else if (rows) {
        // only one object
        return rows[valuesProp];
    }
    return null;
}

// ...

...findAll(...)...complete(function(err, rows) {
     var allValues = getValuesFromRows(rows);
     sendToClient(errToString(err, user), allValues);
});

. , . , values , , values () . Sequelize ( Sequelize), ?

" values Project values values Project.members" (). , , (, tasks tasks ..).

, ​​?

+4
1

, POD include 'd . find, findAll, all , , .

/**
 * Get POD (plain old data) values from Sequelize results.
 *
 * @param rows The result object or array from a Sequelize query `success` or `complete` operation.
 * @param associations The `include` parameter of the Sequelize query.
 */
getValuesFromRows: function(rows, associations) {
    // get POD (plain old data) values
    var values;
    if (rows instanceof Array) {
        // call this method on every element of the given array of rows
        values = [];
        for (var i = 0; i < rows.length; ++i) {
            // recurse
            values[i] = this.getValuesFromRows(rows[i], associations);
        }
    }
    else if (rows) {
        // only one row
        values = rows.dataValues;

        // get values from associated rows
        if (values && associations) {
            for (var i = 0; i < associations.length; ++i) {
                var association = associations[i];
                var propName = association.as;

                // recurse
                values[propName] = this.getValuesFromRows(values[propName], association.include);
            };
        }
    }

    return values;
}

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 data from DB
return Post.findAll(query)

// convert Sequelize result to POD
.then(function(posts) {
    return getValuesFromRows(posts, postAssociations);
})

// send POD back to client
.then(client.sendPosts);

client.sendPosts . author comments. comments author. POD ( ).

+3

Source: https://habr.com/ru/post/1546139/


All Articles