These are chat archives for vdemedes/mongorito

12th
Mar 2015
Matías Olivera
@olistic
Mar 12 2015 22:06
I’m here
Vadim Demedes
@vadimdemedes
Mar 12 2015 22:06
me too ;-)
I am ready and glad to hear your suggestions!
Matías Olivera
@olistic
Mar 12 2015 22:06
First of all, congrats!
I really like the simplicity of Mongorito
Vadim Demedes
@vadimdemedes
Mar 12 2015 22:07
Thank you, glad you like it ;-)
Matías Olivera
@olistic
Mar 12 2015 22:07
Well
I have a bunch of ideas related to the “changed” attribute of the models
Vadim Demedes
@vadimdemedes
Mar 12 2015 22:08
for example?
Matías Olivera
@olistic
Mar 12 2015 22:09
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
Mar 12 2015 22:11
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
Mar 12 2015 22:12
sorry, but I don’t see how that would help me.
Vadim Demedes
@vadimdemedes
Mar 12 2015 22:13
you need to determine if password field was changed, right?
Matías Olivera
@olistic
Mar 12 2015 22:13
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
Mar 12 2015 22:14
so what is your suggestion to the library here?
Matías Olivera
@olistic
Mar 12 2015 22:14
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
Mar 12 2015 22:15
don't follow the logic here, why would we assume that attributes are changed if document does not have _id?
Matías Olivera
@olistic
Mar 12 2015 22:16
If the document doesn’t have an _id, it means that is is being created (it’s not in the db)
Vadim Demedes
@vadimdemedes
Mar 12 2015 22:16
yes
Matías Olivera
@olistic
Mar 12 2015 22:17
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
Mar 12 2015 22:18
hm, don't think so, let me check
Matías Olivera
@olistic
Mar 12 2015 22:19
yeah sure
Vadim Demedes
@vadimdemedes
Mar 12 2015 22:19
no, .set() is not being called when finding models
Vadim Demedes
@vadimdemedes
Mar 12 2015 22:20
yeah, there's no .set()
so changed will be {}
Matías Olivera
@olistic
Mar 12 2015 22:21
yeah, but it is instantiating a new Mode
Model
Vadim Demedes
@vadimdemedes
Mar 12 2015 22:21
yes
and .changed object will be empty, because no properties were changed
Matías Olivera
@olistic
Mar 12 2015 22:22
yeah, but that’s the point. Maybe I didn’t explain me well
Vadim Demedes
@vadimdemedes
Mar 12 2015 22:22
yeah, I don't really understand the issue
Matías Olivera
@olistic
Mar 12 2015 22:22
I want the constructor of the model, to flag all attributes as changed if the model is new
Vadim Demedes
@vadimdemedes
Mar 12 2015 22:23
if Post.find, model can't be new, because it is stored already
Matías Olivera
@olistic
Mar 12 2015 22:23
so if I check in the changed attribute I can get the modified attributes, whether they have been modified or they are new
Vadim Demedes
@vadimdemedes
Mar 12 2015 22:23
you can check if a property is new, if its previous value is undefined
Matías Olivera
@olistic
Mar 12 2015 22:25
I will have to go in 5 minutes Vadim, maybe it would be better if I write you an email detailing the issue with more calm
Vadim Demedes
@vadimdemedes
Mar 12 2015 22:25
yeah ok
Matías Olivera
@olistic
Mar 12 2015 22:25
I think it worth mentioning that I would be pleased to help
in this or anything that comes up in the future