Not sure if it is the better way to achieve uniqueness, but you can find here the docs about indexing your model.
Just add a unique index on the field you want, and voila !
For your model, that would be :
{
...
"genericId": {
"type": "string",
"required": True,
"index": {"unique": true}
},
...
}
However, if the genericId
field is the actual Id of the model, I suggest you declare it as such, so you can use findById
method, and also avoid creation of a duplicate id
field, which will happen if you don't declare any in your model.
{
...
"genericId": {
"type": "string",
"id": true, // Ensure uniqueness and avoid another model id field
"generated": true // Add this if you want Loopback to manage id content for you
},
...
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…