I want to stop using autogenerated number ids for my models on Strongloop. Can Strongloop generate string uids like e.g. 067e6162-3b6f-4ae2-a171-2470b63dff00
?
Asked
Active
Viewed 49 times
1

Martin
- 11
- 3
2 Answers
0
Yes, strong-loop will generate uuid with uuid function call in model definition. you can use something like below in your model properties.
"id": {
"type": "string",
"defaultFn": "uuid"
}
You can check below url to get more info. https://loopback.io/doc/en/lb3/Model-definition-JSON-file.html and https://github.com/strongloop/loopback/issues/292.

Saikumar Anireddy
- 341
- 2
- 15
0
You need to modify the .js file along with the .json file. Based on the logic, you can also add a remote method and generate the uuid from node-uuid module.
I'm assuming a User model here and properties of id, name, age and creating an entry into User model.
User.json
{
"name": "User",
"properties": {
"id": {
"type": "string",
"id": true,
"defaultFn": "uuid",
"required": true
},
"name": {
"type": "string",
"required": true
},
"age": {
"type": "string",
"required": true
}
}
User.js
var uuid = require('node-uuid');
module.exports = function(User) {
var userObj = {};
userObj.id = uuid();
userObj.name = 'John';
userObj.age = 22;
User.create(userObj, function(err, userInstance){
if (err) {
console.log(err);
} else if (userInstance) {
console.log(userInstance);
}
});
}
This will work.

Rajeev Desai
- 144
- 2
- 12
-
1"You also need to modify the .js file along with the .json file." What does that "also" refer to? [Saikumar Anireddy's answer](http://stackoverflow.com/a/41240905/3982001)? If so, please [edit] your question and make it explicit. Thank you! – Fabio says Reinstate Monica Mar 30 '17 at 16:38
-
I was refering to the earlier answer. In ny experience I needed to make changes in both files for the id to be autogenerated automatically. – Rajeev Desai Mar 30 '17 at 17:58