Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
    Matías Olivera
    @olistic
    I’m here
    Vadim Demedes
    @vadimdemedes
    me too ;-)
    I am ready and glad to hear your suggestions!
    Matías Olivera
    @olistic
    First of all, congrats!
    I really like the simplicity of Mongorito
    Vadim Demedes
    @vadimdemedes
    Thank you, glad you like it ;-)
    Matías Olivera
    @olistic
    Well
    I have a bunch of ideas related to the “changed” attribute of the models
    Vadim Demedes
    @vadimdemedes
    for example?
    Matías Olivera
    @olistic
    I was writing a middleware to hash the user password when it has been modified or is new
    so it was hooked to the before save action
    so I thought that maybe the constructor of the model class could flag all attributes as changed
    from undefined to the value passed
    what do you think?
    Vadim Demedes
    @vadimdemedes
    hm, there's a previous property, which you might use for that task
    previous contains previous value, in case property was changed
    Matías Olivera
    @olistic
    sorry, but I don’t see how that would help me.
    Vadim Demedes
    @vadimdemedes
    you need to determine if password field was changed, right?
    Matías Olivera
    @olistic
    yeah, but also if it’s assigned for the first time
    (on model creation)
    • hashPassword (next) {
      // Only hash the password if it has been modified (or is new)
      if (!this.changed.password) {

      return yield next;

      }

      // Generate a salt given a work factor
      let salt = yield bcrypt.genSalt(SALT_WORK_FACTOR);

      // Hash the password along with our new salt
      let hash = yield bcrypt.hash(this.get('password'), salt);

      // Override the cleartext password with the hashed one
      this.set('password', hash);

      yield next;

    something like that
    Vadim Demedes
    @vadimdemedes
    so what is your suggestion to the library here?
    Matías Olivera
    @olistic
    I’ve thought on something like this:
    constructor (attrs = {}, options = {}) {
    this.attributes = attrs;
    this.changed = {};
    this.previous = {};
    this.options = options;
    // flag attributes as changed if document is new
    if (!attrs._id) this.set(attrs);
    that’s the modified contructor of the Model class
    Vadim Demedes
    @vadimdemedes
    don't follow the logic here, why would we assume that attributes are changed if document does not have _id?
    Matías Olivera
    @olistic
    If the document doesn’t have an _id, it means that is is being created (it’s not in the db)
    Vadim Demedes
    @vadimdemedes
    yes
    Matías Olivera
    @olistic
    that check is necessary because when you do something like Post.find({ title: ‘Sad title!’ })
    it instantiates a Model in the back
    if that check is not there, it would flag the attrs as changed when retrieving the object from the db
    Vadim Demedes
    @vadimdemedes
    hm, don't think so, let me check
    Matías Olivera
    @olistic
    yeah sure
    Vadim Demedes
    @vadimdemedes
    no, .set() is not being called when finding models
    Vadim Demedes
    @vadimdemedes
    yeah, there's no .set()
    so changed will be {}
    Matías Olivera
    @olistic
    yeah, but it is instantiating a new Mode
    Model
    Vadim Demedes
    @vadimdemedes
    yes
    and .changed object will be empty, because no properties were changed
    Matías Olivera
    @olistic
    yeah, but that’s the point. Maybe I didn’t explain me well
    Vadim Demedes
    @vadimdemedes
    yeah, I don't really understand the issue
    Matías Olivera
    @olistic
    I want the constructor of the model, to flag all attributes as changed if the model is new
    Vadim Demedes
    @vadimdemedes
    if Post.find, model can't be new, because it is stored already
    Matías Olivera
    @olistic
    so if I check in the changed attribute I can get the modified attributes, whether they have been modified or they are new