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
},
}
}
Related
I'm not seeing my posixAccounts information from the following link:
https://developers.google.com/admin-sdk/directory/reference/rest/v1/users/get
{
"kind": "admin#directory#user",
"id": "8675309",
"etag": "\"UUID\"",
"primaryEmail": "email#example.com",
"name": {
"givenName": "Email",
"familyName": "Account",
"fullName": "Email Account"
},
"isAdmin": true,
"isDelegatedAdmin": false,
"lastLoginTime": "2021-08-04T21:11:17.000Z",
"creationTime": "2021-06-16T14:32:35.000Z",
"agreedToTerms": true,
"suspended": false,
"archived": false,
"changePasswordAtNextLogin": false,
"ipWhitelisted": false,
"emails": [
{
"address": "email#example.com",
"primary": true
},
{
"address": "email#example.com.test-google-a.com"
}
],
"phones": [
{
"value": "123-456-7890",
"type": "work"
}
],
"nonEditableAliases": [
"email#example.com.test-google-a.com"
],
"customerId": "id12345",
"orgUnitPath": "/path/to/org",
"isMailboxSetup": true,
"isEnrolledIn2Sv": false,
"isEnforcedIn2Sv": false,
"includeInGlobalAddressList": true
}
As you can see from the above output, there's no posixAccount information. I can open the ldap information in Apache Directory studio, so I know it's there, but I can't see it from the above output. Since I can see it though, I tried to update this using the update function in the API.
https://developers.google.com/admin-sdk/directory/reference/rest/v1/users/update
I used this for the payload as I'm just testing updating the gid information. I used the documentation below to get the entry details needed. At least as far as I could tell.
{
"posixAccounts": [
{
"gid": "12345",
}
]
}
https://developers.google.com/admin-sdk/directory/reference/rest/v1/users
I'm getting a 200 response, but nothing is actually changing for the user when doing a PUT to update.
I tried a similar update method from another user on here, but no avail: Google Admin SDK - Create posix attributes on existing user
I was able to get this resolved by supplying additional details in my PUT request:
{
"posixAccounts": [
{
"username": "email(excluding #domain.com)",
"uid": "1234",
"gid": "12345",
"operatingSystemType": "unspecified",
"shell": "/bin/bash",
"gecos": "Firstname Lastname"
"systemId": ""
}
]
}
The above wouldn't reflect in LDAP until I put "systemId" in there. So that part is required.
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
}
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"
],
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.
How to ensure uniqueness of a combination of multiple fields in loopback model. Like below is the model Organisation, I have two field name and contact in it, I want the combination of these two fields to be unique in the database.
For example :- while creating an organisation two records can have same value in the 'name' field but the combination of the value of the 'name' and the 'contact' field should be unique for each record in order to create it.
`{
"name": "Organisation",
"base": "PersistedModel",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
"name": {
"type": "string",
"required": true
},
"contact": {
"type": "number",
"required": true
}
}`
You can use indexes for this purpose. Check documentation, there are several very good examples that covers this topic.
"nameContactUniqueIndex": {
"keys": {
"name": 1,
"contact": -1
},
"options": {
"unique": true
}
}
A key value of 1 specifies ascending order, and -1 specifies descending order.