Loopback and default sorting - javascript

I'm starting to study loopback.
I created my app, and below this model:
{
"name": "movimenti",
"plural": "movimenti",
"base": "PersistedModel",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
"mov_id": {
"type": "number",
"required": true
},
"mov_tipo": {
"type": "string",
"required": true
},
"mov_valore": {
"type": "number",
"required": true
}
},
"validations": [],
"relations": {},
"acls": [],
"methods": {}
}
I connected the model to my MySQL DB:
"movimenti": {
"dataSource": "banca",
"public": true
}
I launched the application, and went to the address indicated.
I questioned the GET method, having this error:
"stack": "Error: ER_BAD_FIELD_ERROR: Unknown column 'id' in 'field list'\n
but I do not have an ID field in my table.
How can I fix this problem?

Loopback will automatically add an id column if none of the properties of a model is mentioned as id.
Assuming for your model, property mov_id is the id. Define so in the model by adding id: true line: Reference
{
...
"properties": {
"mov_id": {
"type": "number",
"required": true,
"id":true
},
...
}

Related

LoopbackJS Get authenticated UserId to be stored against a model without exposing it via the API

I have the below "Content" model in Loopback which requires an authenticated user to access.
Each piece of Content can be created by only one user. (user is inherited from the Loopback internal User model) So when creating Content I would like the UserId to be stored in the Content record.
However, i'd like this to happen behind the scenes without having to pass through the userId via the API.
I've had a play with relations, but this doesn't seem to be working out..
{
"name": "Content",
"base": "PersistedModel",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
"BooId": {
"type": "string",
"required": true
},
"Name": {
"type": "string",
"required": true
},
"Language": {
"type": "string",
"required": true,
"default": "en-gb"
},
"Version": {
"type": "number",
"default": 1
},
"Text": {
"type": "string",
"required": true
},
"Created": {
"type": "date",
"defaultFn": "now"
},
"Tags": {
"type": [
"string"
],
"default": []
}
},
"validations": [],
"relations": [],
"acls": [
{
"accessType": "*",
"principalType": "ROLE",
"principalId": "$unauthenticated",
"permission": "DENY"
}
],
"methods": {}
}
First of all, I would create a new relation for the content. So I would add the relation to your Content model:
"relations": {
"author": {
"type": "belongsTo",
"model": "user", // <-- your user model name
"foreignKey": ""
}
}
Then, you could use the before save hook to set the userId on your content model.
Or, you can simply just download this mixin to do it for you.
https://github.com/akkonrad/loopback-author-mixin

Loopback embedsMany Helper methods don't work

I have these two models:
Student
{
"name": "student",
"plural": "students",
"base": "User",
"idInjection": false,
"options": {
"validateUpsert": true
},
"relations": {
"test": {
"type": "embedsMany",
"model": "test",
"property": "mytest",
"options": {
"validate": true,
"forceId": false
}
}
}
and
Test
{
"name": "test",
"base": "Model",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
"text": {
"type": "string",
"required": true
}
}
}
When I try to create a new test I get this error
Error: Invalid reference: undefined
I create the test in this way:
Student.js
studentInstance.test.add({text : "something "})
What am I doing wrong?
Update
Delete in embedsMany
update id in test.
Student.js
Student.show = function(email, cb) {
Student.findById(email,function(err, student) {
...
var tmp = student.mytest;
for (var i = 0; i < tmp.length; i++) {
student.test.destroy(tmp[i].id);
}
})
...
}
I tried with
destroy not work correctly, not always remove the data
and
remove show this error
Error: Invalid reference: undefined
at EmbedsMany.remove
Update
Added a example of db
{
"_id": "value",
"property1": "value",
.
.
"mytest": [
{
"text": "something",
"creation": {
"$date": "2016-08-23T14:31:44.678Z"
},
"id": "d738253472876b17feb4b46b"
}
]
}
You don't have test model.
In test.json you defined its names as notification => "name": "notification",
UPDATE
For building (without persisting) an embedded instance please use studentInstance.test.build({text : "something "})
and for creating (with persisting) that please use studentInstance.test.create({text : "something "})
After 3 years, I've faced with same problem by using remove, destroyById.
And then I use unset method. It works.
Person.findById(body.metadata.person.id, (err, person) => {
person.creditCards.unset(
body.creditCard.id,
(err, res) => {
}
);
}
This works well.

How to override and rename properties of User built-in model in Loopback Framework

I'm using the loopback framework to create a RESTful API for my application.
Following the documentation, I create my own Customer Model extending the built-in model User.
What I'm trying to achieve is:
How can I rename and remove some properties from this built-in model?
{
"name": "Cliente",
"plural": "Clientes",
"base": "User",
"idInjection": false,
"strict":"true",
...
}
{
"name": "User",
"properties": {
"realm": {
"type": "string"
},
"username": {
"type": "string"
},
"password": {
"type": "string",
"required": true
},
"email": {
"type": "string",
"required": true
},
"emailVerified": "boolean",
"verificationToken": "string"
},
...
}
I reached the results modyfing the loopbacks models inside the node modules, but this solution does not seem the right way, is there a way to config this in my code instead change loopback base models?
I think what you are trying to do is "rename" a property, am I correct?
If so, you can do the following:
"senha": {
"type": "string",
"id": true,
"required": true,
"index": true,
"postgresql": {
"columnName": "password"
}
}
Notice that I have a "postgresql" attribute, which depends on your database connector. Check it here. Inside that attribute I have a "columnName", which is the real name of that column in my database. So "senha" is the new name of that attribute.
For hiding the username property, you could do the following in the root object:
"hidden":["username"]
Your final file should look something like this:
{
"name": "Cliente",
"plural": "Clientes",
"base": "User",
"idInjection": false,
"strict": "true",
"properties": {
"realm": {
"type": "string"
},
"username": {
"type": "string"
},
"senha": {
"type": "string",
"required": true,
"postgresql": {
"columnName": "password"
}
},
"email": {
"type": "string",
"required": true
},
"emailVerified": "boolean",
"verificationToken": "string"
},
"hidden": ["username"]
}

Why are relations not being added to my loopback.io objects in the models?

I am quite new to Loopback and NodeJS, so please tell me if there is a "Node way" of doing something that I am missing. I decided to write a basic application to try and learn more.
I have two models, 'UserInformation' and 'ClothingArticle'. I have created a 'hasMany' relation from UserInformation to ClothingArticle.
As a basic test, I wanted to add a remote method to UserInformation to get recommendations for ClothingArticles. However, I cannot seem to get access to anything related to ClothingArticles. I added code into the common/models/user-information.js file to try and retrieve information about the relation, but am not sure if this is even the right spot to be putting it.
My code is below, could you help?
common/models/user-information.js:
module.exports = function(UserInformation) {
get_methods = function(obj) {
var result = [];
for(var id in obj) {
try {
if(typeof(obj[id]) == "function") {
result.push(id + " (function): "); //+ obj[id].toString());
}
else
result.push(id + ": "); // + obj[id].toString());
}
catch (err) {
result.push(id + ": inaccessible");
}
}
return result;
}
// This doesn't anything about my new relations?
console.log(get_methods(UserInformation.prototype));
UserInformation.recommendations = function(source, callback) {
var response = "I don't have any recommendations.";
var test_function = UserInformation.findById(3, function(err, instances) {
if(err) return console.log("Errors: " + err);
console.log("Instances: " + String(instances));
// Nothing here either about the relations.
console.log(get_methods(UserInformation));
console.log(UserInformation.app);
/*
instances.clothingArticles.create({
id:92,
colors:['red','blue']
});
*/
console.log("Created a new clothing article.");
});
console.log (response);
callback(null, response);
}
UserInformation.remoteMethod(
'recommendations',
{
accepts: [
{arg: 'source', type: 'string'} // Used to mark the source (closet, generic, etc)
],
http: {path: '/recommendations', verb: 'get'},
returns: {arg: 'recommendations', type: 'string'}
}
);
};
common/models/user-information.json:
{
"name": "UserInformation",
"base": "PersistedModel",
"strict": false,
"idInjection": false,
"properties": {
"birthday": {
"type": "date"
},
"id": {
"type": "number",
"id": true,
"required": true
},
"eye_color": {
"type": "string"
},
"hair_color": {
"type": "string"
},
"weight": {
"type": "string",
"comments": "pounds"
},
"height": {
"type": "number",
"comments": "inches"
}
},
"validations": [],
"relations": {
"clothingArticles": {
"type": "hasMany",
"model": "ClothingArticle",
"foreignKey": "owner_id"
}
},
"acls": [],
"methods": []
}
common/models/clothing-article.json:
{
"name": "ClothingArticle",
"base": "PersistedModel",
"strict": false,
"idInjection": false,
"properties": {
"id": {
"type": "number",
"id": true,
"required": true
},
"colors": {
"type": [
"Color"
],
"required": true
},
"owner_id": {
"type": "number"
}
},
"validations": [],
"relations": {},
"acls": [],
"methods": []
}
I suggest starting with our getting started example and working your way through through the tutorial series here: https://github.com/strongloop/loopback-example
The questions you ask are answered throughout the examples (ie. model relations). To answer your question, if you defined a relation properly, you should be able to access the relation via dot.
...
UserInformation.ClothingArticle...
...
See http://docs.strongloop.com/display/LB/HasMany+relations for more information.

How to cast property in nested objects to ObjectId with loopback and mongodb?

Let's say I have the following model definition:
{
"name": "Report",
"idInjection": true,
"trackChanges": true,
"mongodb": {
"collection": "report"
},
"properties": {
"resource" : {"type": "String"},
"date" : {"type": "Date"},
"people" : [ {
// Here's where I like to have an id property.
"role" : {"type": "String"},
"hours" : {"type": "Number"}
} ],
"name" : {"type": "String"}
},
"validations": [],
"relations": {},
"acls": [],
"methods": []
}
Now I want to have id property in each object in people array(to be accessed with like report.people[0].id) and it should be casted to ObjectId on inserts and updates. But well, loopback doesn't have an ObjectId type and the only way seems to be using relations but then how should the foreign key be?
Is there any way to have the id property casted to ObjectId on inserts and updates?
Update:
I tried using embedsMany, but the id wasn't converted:
Here's my report.json:
{
"name": "Report",
"base": "PersistedModel",
"idInjection": true,
"properties": {
"name": {
"type": "string",
"required": true
}
},
"validations": [],
"relations": {
"people" : {
"type": "embedsMany",
"model": "ReportPerson",
"options": {
"validate": true,
"autoId": false
}
}
},
"acls": [],
"methods": []
}
and here's my report-person.json:
{
"name": "ReportPerson",
"base": "Model",
"idInjection": true,
"properties": {
"hours": {"type" : "number"}
},
"validations": [{
"person" : {
"model": "Person",
"type": "belongsTo",
"foreignKey": "id"
}
}],
"relations": {},
"acls": [],
"methods": []
}
When I try to insert this Report using the http API:
{
"name" : "report",
"people" : [
{
"id" : "54c7926e1d621dc65495f069",
"hours" : 2
}
]
}
The id wouldn't be casted to ObjectId and stays as string on the database.
Anyone playing with loopback and mongodb hits this one once in a while.
To get around the lack of ObjectId type in loopback:
-one way is to indeed describe a relation using the property as a foreign key, as discussed in this post
-the other way, much cleaner imo is to define the property as an id in the JSON file
see model definition documentation
for example:
{
"myId": {
"type": "string",
"id": true,
"generated": true
}
}
now a request on this property will work either with pass the actuel objectId or its string representation

Categories