I am using loopback 3 and idInjection false is not working - loopbackjs

My database is in PostgreSQL.My model defination is as below -
{
"name": "City",
"base": "PersistedModel",
"idInjection": false,
"options": {
"validateUpsert": true
},
"properties": {
"name": {
"type": "string"
}
},
"validations": [],
"relations": {},
"acls": [],
"methods": {}
}
I am using code below to auto update the tables but when I go to database after running the code below and I check fields, I see id field created with an auto increment. How can I prevent creation of "id" field ?
ds.autoupdate("City", function (er) {
if (er) throw err
});

How can I prevent creation of "id" field?
If you're using a database, you can't. See this issue.
A model without any id properties can only be used without attaching to a database.
Making the name property an Id looks like the only way to use it with a relational database.
"name": {
"type": "string",
"id": 1
}

Related

Loopback: Unknown column 'realm' in 'field list'"

I have been stuck with LoopBack for the last couple of hours trying to extend the User model with a custom model called Client.
{
"name": "Client",
"base": "User",
"plural": "Clients",
"idInjection": true,
"options": {
"validateUpsert": true,
"mysql": {
"schema": "LOOPBACK",
"table": "my_table_name"
}
},
"properties": {
"test": {
"type": "string"
}
},
"validations": [],
"relations": {},
"acls": [],
"methods": {}
}
After doing the autoupdate I am not seeing any other properties in the client table than "test".
From the explorer I try to submit a new user, but I get the following error:
"uniqueness.Error: ER_BAD_FIELD_ERROR: Unknown column 'realm' in 'field list'"
Read every possible answer on google, but couldn't find a solution.
Try to manually create a column called 'realm' on your table 'my_table_name' and check if it fixes.
You need to exclude properties present in User but absent in your Model. My extended user has this property right before "properties" property:
"excludeBaseProperties": [
"realm",
"password",
"emailVerified",
"verificationToken"
],

loopback 2 - defining an array of objects on a model

if I have a usermodel and I define:
"events": {
"type": [
"Object"
]
},
Do I need to define anything else in the usermodel.js to be able to post things like: [{name: 'sample', ...}, ...] to the user table's events column?
I ask because if I remove this particular definition from the .json the app compiles and the database migrates, but with it in, the app compiles but the database states there was an issue with the users findByid. My debugging has narrowed it down to this particular set of code.
I think you can simply use this structure
{
"events":{
"type": [
{
"key": "type",
"key2": "type"
}
]
}
}
You can see a .js example here and .json example here.
but I can also see an issue with the implementation here that says
this model has problem. When we fetch the data with any get call, it
renders this particular field as ["Object Object"] even though the
data is properly saved in the database.
which I would recommend you to try on your own as it will depend a lot on versions and drivers.
Though I would like to ask that what kind of database are you using?
Another way would be to define the object you want in the array as a model, then use that model as your type:
Model: Class
{
"name": "Class",
"base": "Model",
"strict": true,
"idInjection": false,
"properties": {
"label": {
"type": "string",
"required": true
}
}
}
Model: Student
{
"name": "Student",
"base": "PersistedModel",
"strict": true,
"idInjection": true,
"properties": {
"classes": {
"type": ["Class"],
"required": false
},
}
}

How to query using numeric id in Loopback

I have a loopback model as
{
"name": "myModel",
"base": "PersistedModel",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
"name": {
"type": "string",
"description": "Channel name"
}
},
"validations": [],
"relations": {},
"acls": [],
"methods": {}
}
Now this is connected to a mongodb collection.This collection can have custom id in numeric like 1234 or Mongodb ObjectId when created using loopback.
Now when I query using GET
http://localhost:3000/api/myModel/1234.It is not able to get the object of that id.
I get an error message as
{"status":404,"statusCode":404,"code":"MODEL_NOT_FOUND","message": "Unknown \"myModel\" id \"1234\"}
If my id is alphanumeric like abcd1234 then I dont get this error.Is there a way in loopback to be able to process a numeric id.
The only reference I have is this that suggests to use alphanumeric ids.

Can a PersistedModel relation name be the same as the property name?

In the Loopback documentation relations always have different names than the property they are stored in. For example:
{
"name": "Customer",
"base": "PersistedModel",
"idInjection": true,
"properties": {
"name": {
"type": "string"
}
},
"relations": {
"address": {
"type": "embedsOne",
"model": "Address",
"property": "billingAddress",
"options": {
"validate": true,
"forceId": false
}
}
...
}
Note in this case the address relation has a property of billingAddress. Things seem to break when we use a property of address as instead.
Sometimes its hard to come up with an arbitrary adjective to prepend to the property. For example, say the relation was to a model already called BillingAddress instead of Address. It would be natural to want to name both the relation and the property billingAddress.
For example:
{
"name": "Customer",
"base": "PersistedModel",
"idInjection": true,
"properties": {
"name": {
"type": "string"
}
},
"relations": {
"billingAddress": {
"type": "embedsOne",
"model": "BillingAddress",
"property": "billingAddress",
"options": {
"validate": true,
"forceId": false
}
}
...
}
How do you handle this situation in Loopback?
With the current state of loopback, I believe it's a bad idea. I tried it a few times on different types of relationships having the same reasons you came up with, in most cases it breaks something if it works. Looking at the documentation and loopback I can tell relationships were designed in a way that relation name and the property must be different, even if it works, it might break in the future.
For these things I would add a postfix to the relation name, for example billingAddressDetails or billingAddressRelation.
In the mean time you could also open an issue in github, but I think things will stay the same way in the future.

strongloop loopback id getting sent to the user owning the object

I have a strongloop app in which I am trying to post some data to my REST APIs and then trying to retrieve it from another user but I am not getting the "id" of that record to the user not owning the object. How do I access the generated "id" from the user not owning the object. There are no ACLs in my model file
I am expecting the generated unique user identifiers to be returned along with the json response, for example if I make a request like this -
http://localhost:80/api/tuitions?access_token=qvpvtsbWOLZYydUDcbO0VP6YrlfHhbURHpkvPjnUyU9wRHpclAyJh44RKJ6VU36x
the output is -
[
{
"area": [
"901"
],
"city": "9",
"class": "9",
"subjects": [
"902"
],
"userId": "5696915dec62196433db7950",
"created": "2016-01-14T18:40:07.761Z",
"updated": "2016-01-14T18:40:07.761Z",
"identifier": "5697eb8767e585347755c23b"
}
]
in my loopback explorer and my loopback tuition model file defines identifier as -
{
"name": "tuition",
"base": "PersistedModel",
"strict": false,
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
// ...
"identifier": {
"type": "string",
"id": true
}
},
"validations": [],
"relations": {},
"acls": [],
"methods": {}
}
But when I make the same request with my Angular App in my controller with something like -
Tuition.find({
filter: {
order: 'created DESC'
}
}).$promise;
I get this -
[{"area":["901"],"city":"9","class":"9","subjects":["902"],"userId":"5696915dec62196433db7950","created":"2016-01-14T18:40:07.761Z","updated":"2016-01-14T18:40:07.761Z"}]
which does not have identifier