非映射属性 - Sequelize

时间:2016-05-13 08:43:52

标签: node.js reactjs sequelize.js redux sequelize-cli

支持包括:React,Redux和Sequelize。 基本上我希望能够在Redux存储更新时将对象标记为dirty

function updateCar(carToUpdate, car) {
    switch(car[1]) {
        case "models":
            carToUpdate.models = car[3];
            carToUpdate.setDirty();
            break;
    }    
};

然后当有人点击保存按钮时,我想只更新那些脏标志设置为true的模型

var state = request.body;

state.forEach(function (car) {
    if (car && car.isDirty) {
        updateCar(car);
    }
}

现在我有以下车型:

module.exports = function (sequelize, DataTypes) {
  var Cars = sequelize.define('Cars',
    {
      name: DataTypes.STRING,
      type: DataTypes.INTEGER,
      models: DataTypes.INTEGER,
      //Next is NOT working, this does not make it non-mapped
      isDirty: {
        type: DataTypes.BOOLEAN,
        scope: false
      },
    },
    {
      classMethods: {
        associate: function (models) {
          // associations can be defined here
        }
      },
      instanceMethods: {
        setDirty: function() {
          this.isDirty = true;
        }
      }
    });
  return Cars;
};

是否有使用非映射字段或类似内容的人?

1 个答案:

答案 0 :(得分:1)

最终找到它。

汽车的型号应包含VIRTUAL属性:

module.exports = function (sequelize, DataTypes) {
  var Cars = sequelize.define('Cars',
    {
      name: DataTypes.STRING,
      type: DataTypes.INTEGER,
      models: DataTypes.INTEGER,
      isDirty: {
        type: DataTypes.VIRTUAL
      },
    },
    {
      classMethods: {
        associate: function (models) {
          // associations can be defined here
        }
      }
    });
  return Cars;
};

接下来,在更新值时应设置标志:

function updateCar(carToUpdate, car) {
    switch(car[1]) {
        case "models":
            carToUpdate.models = car[3];
            carToUpdate.isDirty = true;
            break;
    }    
};

然后save方法可以检查isDirty标志     var state = request.body;

state.forEach(function (car) {
    if (car && car.isDirty) {
        console.log(car.name +": "+ car.isDirty);
        updateCar(car);
    }
}, this);

最后但并非最不重要的是,我们将isDirty标记重置为false,因此我们不会一遍又一遍地更新相同的模型。

var save = function () {
    var state = carStore.getState();
    return $.ajax({
        url: "/Cars/update",
        data: JSON.stringify(state),
        method: "POST",
        contentType: "application/json"
    }).then(function() {
        carStore.dispatch({
            type: 'resetDirty',
            data: null
        });
    });
};

重置标志的调度方法:

function updateReducer(state, action) {
    switch (action.type) {
        case 'load':
            return action.data;
        case 'update':
            return updateCars(action.carChanges, state);
        case 'resetDirty':
            return resetDirtyFlags(state);
        default:
            return action.data;
    }

}

function resetDirtyFlags(state) {
    var newState = $.extend(true, [], state);
    if(state) {
        newState.forEach(function(car) {
            car.isDirty = false;
        }, this);
        return newState;
    }
    return newState;
}