Coder Social home page Coder Social logo

Comments (11)

stubailo avatar stubailo commented on July 20, 2024

I'm not a fan of autovalue after working with it for a while - personally I would prefer using SS only for schema validation?

from todos.

tmeasday avatar tmeasday commented on July 20, 2024

I'm inclined to agree TBH.

I think the way to do something like an autovalue more explicitly is to subclass the (say) update function and do it there. So you'd do

class ListsCollection extends Collection2 {
  update(query, mod) {
    mod.$set.updatedAt = new Date();
    super(query, mod);
  }
}

What do you think @stubailo? Also, are you ok with default values?

from todos.

stubailo avatar stubailo commented on July 20, 2024

Hmm... perhaps for something like that, yes. For default values I'd prefer to just have them in the insert method.

from todos.

tmeasday avatar tmeasday commented on July 20, 2024

Oh, sorry that was two questions.

So question 1. was do you think the "subclassing" approach to replace autovalue is correct (the alternative is to put that logic in every method body which is gross).

Question two is around default values -- so I guess I mean do we want to put:

A) default values in the schema

B) default values in the insert method, ala the above

I prefer A) for simple stuff like defaultValue: false.

from todos.

stubailo avatar stubailo commented on July 20, 2024
  1. I feel like overriding update will be super duper hard - like, the example you gave is super simple, but how are you going to ensure other things? Update operators are hella complex.
  2. Default values in the schema make sense, but the issue is that autoValue isn't a default value - it's actually run on all updates as well. I wish there were something like autoValue but that only ran on insert.

from todos.

tmeasday avatar tmeasday commented on July 20, 2024

Re update operators --

I'm not saying you allow arbitrary updates. Everything still goes through a method that you tightly control. It's just that it makes sense to do completely common stuff (like updatedAt as a canonical example) in one place.

from todos.

stubailo avatar stubailo commented on July 20, 2024

Let's look at an example - say you want to make sure that a certain field is always denormalized - for example, you want to make sure that fullName = firstName + lastName. How would you do that by overriding update?

from todos.

tmeasday avatar tmeasday commented on July 20, 2024

I think it's tricky. In that case, if there was a single method that updated firstName or lastName, perhaps you'd do it in the method body?

Alternatively, if you knew that you were only going to $set in update() (and you enforced that), then you could do it there.

I can see that there's an issue about deciding where to put it.

from todos.

tmeasday avatar tmeasday commented on July 20, 2024

Ok, based on our denormalizer discussion, we will make any "hooks" that are based on the shape of the modifier happen at the method level. Perhaps someone will write an awesome package that fully understands mongo operators and we can move it back to the mutator[1]

Another possibility that's more in line with our approach in the past would be to add a mutator to the collection called something like:

setOnSingle(id, attributes) {
  check(id, String); // i.e. enforce a single document
  this.update(id, {$set: attributes});

  // now I can do denormalizations with a lot more confidence
}

When you know that
a) it's only one doc
b) it's always a $set

You can actually do it without needing to be too clever (at least if you don't support dotted operators).

[1] Maybe minimongo is this package somehow (using LocalCollection._modify). But I think it's tricky in general with esoteric operators like $addToSet, and updates that may affect more than one document.

from todos.

tmeasday avatar tmeasday commented on July 20, 2024

See #42

from todos.

tmeasday avatar tmeasday commented on July 20, 2024

I think the final two points can be left for now and just remain on the guide code issues list

from todos.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.