D
D
D_E_S2017-04-14 08:22:08
Node.js
D_E_S, 2017-04-14 08:22:08

How to use VIRTUAL with get and set in sequelize Model?

Good afternoon. There are connection requests are working. I can't figure out what's wrong with the data. The users table has already been written, there is no acl in it, only pageaccess as a string. The problem is that if you look at the debug during the formation, then acl comes out with the correct data, but if after watching ures, then acl stores incorrect data. At what point is acl called and is it necessary to do a map to get an array with all the data?
Request:

let ures = await Users.findAll({ where: {deleted: '0'}}).then(function (items) {
                    return items.map(x=>{
                        var item = x.dataValues;
                        item.acl = x.acl;
                        return item;
                    });
                });

Model:
var Table:UserModel = ORM.define('users', {
    id: {
        type: DataTypes.UUID,
        primaryKey: true,
        autoIncrement: true
    },
    acl: {
        allowNull: false,
        type: DataTypes.VIRTUAL,
        get: function () {
            var pageaccess = this.getDataValue('pageaccess');
            var result = defaultObject;
            if (!pageaccess || pageaccess == 'null'){
                defaultOk.forEach(k => result[k] = true);
            } else {
                pageaccess.split(/,/).forEach(x => result[x] = true);
                return result;
            }
        },
        set: function (val) {
            var acl = val;
            var pageaccess = Object.keys(acl).filter(k=>acl[k]).join(',');
            this.setDataValue('pageaccess', pageaccess);
        }
    },
    pageaccess: {
        type: DataTypes.STRING,
    },
    deleted: {
        type: DataTypes.INTEGER,
    }
},{
    getterMethods   : {},
    setterMethods   : {},
    classMethods: {
        setAcl: function(key: string, value: boolean) {
            if (!this._acl) var q = this.acl;
            this._acl[key] = value;
            this.pageaccess = Object.keys(this._acl).filter(k=>this._acl[k]).join(',');
        }
});

export var Users = Table;

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
emp1re, 2017-04-14
@emp1re

let ures = await Users.findAll({ where: {deleted: '0'}}).then(function (items) {
                    return items.map(x=>{
                        var item = x.dataValues;
                        item.acl = x.acl;
                        return item;
                    });
                });

Users.findAll({ where: {deleted: '0'}}) возращает промисс котрый вы обрабатываете через then
все что вы внутри питаетесь вернуть никуда не вернеться.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question