Loopback access both super model and extended models from same relation - loopbackjs

I am using Loopback 3 and i have the following three models:
UserFile.json
{
"name": "UserFile",
"base": "PersistedModel",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
"id": {
"type": "number",
"id": true,
"generated": true
},
"name": {
"type": "string",
"required": true
},
"size": {
"type": "number",
"default": 0
},
"uploadedAt": {
"type": "date",
"default": "$now"
}
},
"relations": {
"hasFile": {
"type": "hasMany",
"model": "UploadedFile",
"foreignKey": "fileId"
}
}
}
UploadedFile.json
{
"name": "UploadedFile",
"base": "PersistedModel",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
"hashId": {
"type": "string",
"required": true
},
......
},
"relations": {
"file": {
"type": "belongsTo",
"model": "UserFile",
"foreignKey": "fileId"
}
}
}
PartitionedFile.json
{
"name": "PartitionedFile",
"base": "UploadedFile",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
"partSize": {
"type": "number",
"default": 0
}
},
"relations": {
"file": {
"type": "belongsTo",
"model": "UserFile",
"foreignKey": "fileId"
}
}
}
The idea is that the UserFile has either one UploadedFile or (two or more) many PartitionedFile.
I want to be able to get from the UserFile, from the same relation the file, whether it is an UploadedFile or a PartitionedFile. Is it possible to achieve this just from the models' definitions or it can only be done with a remote method?

A relation from a model to another is bound to only one model; so you cannot have one relation called "file" to point to different models. In your example, it's bound to UploadedFile. In order for your model UserFile to have relations with both UploadedFile and PartitionedFile, you'll need two different relations.
For your UserFile to have either one UploadedFile or many PartitionedFile, it needs to have them both within its relations:
{
"name": "UserFile",
...
"relations": {
"uploadedFiles": {
"type": "hasOne", // Zero on one uploaded file
"model": "UploadedFile",
"foreignKey": "uploadedFileId"
},
"partitionedFiles": {
"type": "hasMany", // Zero or many partitioned files
"model": "PartitionedFile"
}
}
}
Then, to retrieve files from both UploadedFiles and PartitionedFiles through UserFile, you don't need a remote, but to include the models within your request.
For example, in ReactJS, it would look like this:
const response = await Axios.get(`/api/UserFile`, {
params: {
filter: {
include: [
{ relation: 'uploadedFiles' },
{ relation: 'partitionedFiles' },
]
}
}
}
Another solution would be to specify another model File that contains all these relations (UploadedFile, PartitionedFile, etc), so that the model UserFile can refer to the table File as one unique relation to retrieve the files...

Related

correct way to define Loopback.io belongsTo relationship

I have a simple has_many-belongst_to relationship between 2 Loopback models:
{
"name": "ApiUser",
"base": "PersistedModel",
"idInjection": true,
"options": {
"postgresql": {
"table": "users"
},
"validateUpsert": true
},
"properties": {
"id": {
"type": "string",
"id": true,
"required": true,
"defaultFn": "uuid",
"postgresql": {
"dataType": "uuid"
}
},
"email": {
...
and
{
"name": "ShopifyAccount",
"base": "PersistedModel",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
"api_key": {
"type": "string",
"required": true
},
"password": {
"type": "string",
"required": true
}
},
"validations": [],
"relations": {
"orders": {
"type": "hasMany",
"model": "ShopifyOrder",
"foreignKey": ""
},
"user": {
"type": "belongsTo",
"model": "ApiUser",
"foreignKey": "userid"
}
},
"acls": [],
"methods": {}
}
When I run automigration, the shopifyaccount table is created, but it looks weird:
Column | Type | Modifiers
-------------------+---------+-------------------------------------------------------------
api_key | text | not null
password | text | not null
id | integer | not null default nextval('shopifyaccount_id_seq'::regclass)
userid | uuid |
apiuserid | uuid |
Indexes:
"shopifyaccount_pkey" PRIMARY KEY, btree (id)
Why did it create 2 columns named like this? Even if I try to specify that foreignkey is "userid", it will still create the apiuserid column. The insert will never update the apiuserid column but it will update the userid column. And then at join time, it will try to join on apiuserid. What am I doing wrong?
So... it seems that the "foreignkey" needs to be specified in both places, in ApiUser and in ShopifyAccount:
{
"name": "ApiUser",
"base": "PersistedModel",
"idInjection": true,
"options": {
"postgresql": {
"table": "users"
},
"validateUpsert": true
},
"properties": {
"id": {
"type": "string",
"id": true,
"required": true,
"defaultFn": "uuid",
"postgresql": {
"dataType": "uuid"
}
},
"email": {
"type": "string",
...
"relations": {
"shopifyAccounts": {
"type": "hasOne",
"model": "ShopifyAccount",
"foreignKey": "userid"
}
},
"acls": [],
"methods": {}
}

One to Many relation using non-primary field

I'm looking into Loopback and stuck in finding a solution when two models (let's say Customer/Orders) have a one-to-many relationship but the primary key (id) of the parent isn't a foreign key in child table, but rather some non-key field.
For example, consider following two tables
Customer[ id(pk), name, social_security_number]
Order [ id(pk), social_security_number(fk)]
I'm not able to create the relationship based on the above scenario. Loopback isn't returning orders when I query against a particular customer. Here is my actual code snippet (partial). I'm using default id field as Primary Key
{
"name": "Customer",
"properties": {
"name": {
"type": "string",
"required": true
},
"social_security_number": {
"type": "number",
"required": true
}
},
"relations": {
"orders": {
"type": "hasMany",
"model": "Order",
"foreignKey": "social_security_number"
}
}
}
{
"name": "Order",
"base": "PersistedModel",
"properties": {
"social_security_number": {
"type": "number",
"required": true
}
},
"relations": {
"customer": {
"type": "belongsTo",
"model": "Customer",
"foreignKey": "social_security_number"
}
}
}
How can I make this work?

Loopback: How to define a property with an array of strings in Loopback?

I have the following model in a loopback application, that will be persisted in a MongoDB:
Model
Name Coffeshop:
Id
Name (string)
City (String)
Question:
Now i want to be able to store a list of strings in a new property called "tags":
Tags (Array of string)
There is no relation to other models necessary. I need just a plain flat list of strings.
How can i achieve this?
Code:
{
"name": "CoffeeShop",
"plural": "CoffeeShops",
"base": "PersistedModel",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
"name": {
"type": "string",
"required": true
},
"city": {
"type": "string",
"required": true
}
},
"validations": [],
"relations": {},
"acls": [],
"methods": {}
}
Thats easy:
{
"name": "CoffeeShop",
"plural": "CoffeeShops",
"base": "PersistedModel",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
"name": {
"type": "string",
"required": true
},
"city": {
"type": "string",
"required": true
},
"tags": {
"type": [
"string"
],
"required": false
}
},
"validations": [],
"relations": {},
"acls": [],
"methods": {}
}

scope in inherited model

I have a contact db table and model. Employee model inherits from contact.
If i do GET employees/ it returns all the contacts.
How should I set up my employee.json if I want to return only the contacts with partnerId = 1?
{
"name": "employee",
"base": "contact",
"strict": false,
"idInjection": false,
"options": {
"validateUpsert": true,
"postgresql": {
"schema": "public",
"table": "contact"
}
},
"scope": {
"where": {
"partnerId": 1
}
},
//...
}
Debug says calling GET employees/ makes the following query:
SELECT "name", "position", "email", "password", "id" FROM "public"."contact" ORDER BY "id"
It does not seem that scope is added.
models/partner.json
{
"name": "partner",
// ...
"properties": {
"name": {
"type": "string",
"required": true
},
// ...
},
"validations": [],
"relations": {
"contacts": {
"type": "hasMany",
"model": "contact"
}
//...
},
"acls": [],
"methods": {}
}
Try using the where filter, either in the REST API
/employees?filter[where][partnerId]=1
or in your Employee.js
Employee.find({ where: {partnerId:1} });
https://docs.strongloop.com/display/APIC/Where+filter

Createmany in Strongloop Loopback

I have an Order model which hasMany OrderItem models. But once a client wants to create an Order, it has to create an Order object first then for each product he added to his basket, he needs to create responding OrderItems separately. As you may notice it causes many reduntant requests. May be I can make a custom method for OrderItems which consumes a product list. But i was wondering if there is a built in mechanism for this like createMany since it is a very useful operation.
ORDER MODEL
{
"name": "Order",
"plural": "Orders",
"base": "PersistedModel",
"idInjection": true,
"properties": {
"customerId": {
"type": "number",
"required": true
},
"branchId": {
"type": "number",
"required": true
}
},
"validations": [],
"relations": {
"orderItems": {
"type": "hasMany",
"model": "OrderItem",
"foreignKey": "orderId"
}
},
"acls": [],
"methods": []
}
ORDERITEM MODEL
{
"name": "OrderItem",
"plural": "OrderItems",
"base": "PersistedModel",
"idInjection": true,
"properties": {
"UnitPrice": {
"type": "number"
},
"productId": {
"type": "number",
"required": true
},
"purchaseOrderId": {
"type": "number",
"required": true
},
"quantity": {
"type": "number"
}
},
"validations": [],
"relations": {
"product": {
"type": "belongsTo",
"model": "Product",
"foreignKey": "productId"
},
"purchaseOrder": {
"type": "belongsTo",
"model": "PurchaseOrder",
"foreignKey": ""
}
},
"acls": [],
"methods": []
}
Loopback "create" method accepts also an array of objects (see PersistedModel.create docs) so you should try creating one "create" call and send an array of OrderItems.