Skip to content

Shortcut for changing property default value #292

Closed

Description

@bajtos

When defining a model code-first, one can specify a dynamic default value for properties:

registrationDate: {type: Date, default: function () { return new Date() }},

This is not possible to achieve when using app.boot() and models.json. This is the currently recommended solution:

app.models.Todo.definition.rawProperties.created.default =
app.models.Todo.definition.properties.created.default = function() {
  return new Date();
};

@raymondfeng suggested there should be a helper method making this easier.

Related: https://groups.google.com/forum/#!topic/loopbackjs/tIlPMxVZXwE

Activity

  1. ritch commented on May 29, 2014

    @ritch
    Member

    models.json should be reserved for primitive definition. For advanced usage you should create a models/my-model.js file and extend the MyModel constructor with any advanced behavior (eg. remote methods, dynamic proprerties, getters, setters, etc).

    However I don't think your case is "advanced". I suggest we implement some support for created / updated properties in models.json / LDL.

  2. raymondfeng commented on May 29, 2014

    @raymondfeng
    Member

    Maybe we should introduce a set of built-in default functions and reference them in models.json. For example:

    $now for Date
    $uuid for String

  3. bajtos commented on May 30, 2014

    @bajtos
    MemberAuthor

    Maybe we should introduce a set of built-in default functions and reference them in models.json. For example: $now for Date, $uuid for String

    That looks promising. We have to use a different option key though, to prevent possible conflicts.

    { registrationDate: {type: Date, dynamicDefault: '$now' } }
    // or
    { registrationDate: {type: Date, computedDefault: '$now' } }
    // or
    { registrationDate: {type: Date, defaultFn: '$now' } }
  4. bajtos commented on Oct 13, 2014

    @bajtos
    MemberAuthor

    Once this feature is implemented, the following places in loopback core can use the feature too:

    • AccessToken.properties.created
    • Application.properties.created and Application.properties.modified
  5. bajtos commented on Oct 13, 2014

    @bajtos
    MemberAuthor

    Note: it is not enough to set settings.properties.{name}.default, as that value is dropped by ModelDefinition.prototype.defineProperty, which is called e.g. when a relation is set up. One has to override both properties and rawProperties to get correct behaviour.

  6. raymondfeng commented on Oct 13, 2014

    @raymondfeng
    Member

    We can introduce a method to ModelDefinition.prototype, for example:

    ModelDefinition.prototype.updateProperty(name, changes);
  7. bajtos commented on Oct 15, 2014

    @bajtos
    MemberAuthor

    A super simple solution to cover the most frequent usage with the current time:

    if (prop.type === Date && prop.default === '$now')
      prop.default = Date;
    

    Since $now is not a valid Date, this solution will not break any existing code and cannot create any confusion.

  8. violet-day commented on Oct 24, 2014

    @violet-day

    +1

  9. bajtos commented on Oct 24, 2014

    @bajtos
    MemberAuthor

    @violet-day would you mind submitting a pull request implementing this feature?

  10. raymondfeng commented on Oct 24, 2014

    @raymondfeng
    Member

    Here is my proposal for the full solution that will allow us to handle default, validation, middleware, hook and other places that require a custom function as part of the JSON config.

    1. We introduce a syntax to reference JS functions by name, for example, $myFunc or myFunction().
    2. In LoopBack, we introduce a utility function resolver which can map a utility function name to a JS function
    3. Add app.utility(name, fn) to register utility functions to the app
    4. By default, common/utilities/.js server/utilities/.js can be discovered.
    5. The default utility function resolver will use knowledge from 3 & 4.
  11. bajtos commented on Oct 24, 2014

    @bajtos
    MemberAuthor

    We introduce a syntax to reference JS functions by name, for example, $myFunc or myFunction().

    As for syntax for referring JS functions by name, I'd rather use a different property name instead of encoding the value. E.g. { registrationDate: {type: Date, defaultFn: 'myFunction' } }

    The question is - are we going to execute (eval) the code specified by defaultFn, or does it have to be a name of a nullary function (a fn with no arguments)?

    The rest of the proposal looks good to me. Note that the full solution is not a "Beginner Friendly" feature.

    However, I still think it is worth it to add a special shortcut for specifying current date as the default value, since it is so frequently used. That part is a "Beginner Friendly" task.

  12. raymondfeng commented on Oct 24, 2014

    @raymondfeng
    Member

    For a property that can take either a function or a value, there are two options:

    1. Use a special prefix/suffix to tell it's function
    2. Use two properties, one for the value and another one for the function

    1 is a bit magic while 2 is a bit verbose. I slightly prefer 1 but I'm fine with 2.

    We will NOT eval. The function has to be a pure name. There is no need to embed a function body in the JSON as 2-5 will allow you to define referenceable function.

    We can ship a list of built-in functions, such as Date. The function resolver can resolve it to Date

  13. 9 remaining items

  14. removed their assignment
    on Feb 20, 2015
  15. bajtos commented on Feb 26, 2015

    @bajtos
    MemberAuthor

    FYI, loopbackio/loopback-datasource-juggler#458 implemented defaultFn option that accepts one of guid, uuid or now.

  16. bytecodedan commented on Apr 25, 2016

    @bytecodedan

    I haven't quite figured it out but I have deployed a sample app on OpenShift and it fails to start because of this default feature. However, it's fine running locally.

    */var/lib/openshift/570be5c42d5271676e00002e/app-root/runtime/repo/node_modules/loopback/common/models/application.js:70
    Application.definition.rawProperties.created.default =
    ^
    TypeError: Cannot assign to read only property 'default' of date *

  17. Synryu commented on Oct 11, 2017

    @Synryu

    @bajtos , concerning the "now" default for Date type, is there a way to set the seconds (and millisec) to 0 ?

    It wasn't an issue until I had to implement a client-side generic change detector. If the user change a date or datetime and then set back the original value, a change is still detected because the secs and millisecs are gone.

  18. bajtos commented on Oct 11, 2017

    @bajtos
    MemberAuthor

    @Synryu have you tried default: 0? I don't have enough information to meaningfully help you. Please open a new issue and provide us with a small app showing how to reproduce the problem you are experiencing, per our bug reporting instructions.

  19. bajtos commented on Oct 11, 2017

    @bajtos
    MemberAuthor

    I am going to lock this conversation down. Please open new GitHub issue(s) if there is anything remaining to discuss.

  20. locked and limited conversation to collaborators on Oct 11, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions