You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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.
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.
We introduce a syntax to reference JS functions by name, for example, $myFunc or myFunction().
In LoopBack, we introduce a utility function resolver which can map a utility function name to a JS function
Add app.utility(name, fn) to register utility functions to the app
By default, common/utilities/.js server/utilities/.js can be discovered.
The default utility function resolver will use knowledge from 3 & 4.
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.
For a property that can take either a function or a value, there are two options:
Use a special prefix/suffix to tell it's function
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
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 *
@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.
@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.
Activity
ritch commentedon May 29, 2014
models.jsonshould be reserved for primitive definition. For advanced usage you should create amodels/my-model.jsfile and extend theMyModelconstructor 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/updatedproperties inmodels.json/ LDL.raymondfeng commentedon May 29, 2014
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
bajtos commentedon May 30, 2014
That looks promising. We have to use a different option key though, to prevent possible conflicts.
bajtos commentedon Oct 13, 2014
Once this feature is implemented, the following places in loopback core can use the feature too:
AccessToken.properties.createdApplication.properties.createdandApplication.properties.modifiedbajtos commentedon Oct 13, 2014
Note: it is not enough to set
settings.properties.{name}.default, as that value is dropped byModelDefinition.prototype.defineProperty, which is called e.g. when a relation is set up. One has to override bothpropertiesandrawPropertiesto get correct behaviour.raymondfeng commentedon Oct 13, 2014
We can introduce a method to
ModelDefinition.prototype, for example:bajtos commentedon Oct 15, 2014
A super simple solution to cover the most frequent usage with the current time:
Since
$nowis not a valid Date, this solution will not break any existing code and cannot create any confusion.violet-day commentedon Oct 24, 2014
+1
bajtos commentedon Oct 24, 2014
@violet-day would you mind submitting a pull request implementing this feature?
raymondfeng commentedon Oct 24, 2014
Here is my proposal for the full solution that will allow us to handle
default,validation,middleware,hookand other places that require a custom function as part of the JSON config.$myFuncormyFunction().app.utility(name, fn)to register utility functions to the appbajtos commentedon Oct 24, 2014
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 bydefaultFn, 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.
raymondfeng commentedon Oct 24, 2014
For a property that can take either a function or a value, there are two options:
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 toDate9 remaining items
bajtos commentedon Feb 26, 2015
FYI, loopbackio/loopback-datasource-juggler#458 implemented
defaultFnoption that accepts one ofguid,uuidornow.bytecodedan commentedon Apr 25, 2016
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 *
Synryu commentedon Oct 11, 2017
@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.
bajtos commentedon Oct 11, 2017
@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.bajtos commentedon Oct 11, 2017
I am going to lock this conversation down. Please open new GitHub issue(s) if there is anything remaining to discuss.