How to create one-to-many and many-to-many relations in Strapi v.4? - nested

I want to create relations between Post and Tag as follows.
Post has many Tags (one-to-many)
Tags have and belongs to many Posts (many-to-many)
I created collection types for Post and Tag.
Set them accessible for public user.
I created 1 post with 3 tags.
Now, when I try to see them at http://localhost:1337/api/posts
I don’t see nested elements, i.e. tags…
Am I missing anything ?
And lastly, I was not able to create Many-to-Many relations between Post and Tag with the following error.
I understand that Tags field already exists, but I thought this is how supposed to be set.

I got reply from Strapi forum, so decided to share here.
https://docs.strapi.io/developer-docs/latest/developer-resources/database-apis-reference/rest/populating-fields.html
According to the docs above, to pull (in my case) 1st level nested elements I have to add populate[0] parameter like so:
http://localhost:1337/api/posts?populate[0]=tags

Related

Nodejs Express multi category route schema

I list the articles under the category. but there are more than one subcategory. I want to write these together in connection. I also want the page number in the form of /page/2. What kind of schema should I use here.
This is the current route. But isn't work
([a-z0-9-_/]+/)?:slug([0-9a-z-_]+)(/page/:page([0-9]+))?$
Link structure
category/[category]/[sub]/[sub-sub]/page/[number]
Sample Link
category/language/mobile/swift/page/2
And I want the last subcategory name. (swift)

How to build complex value-object?

I just have started to learn DDD. So I apologise for silly question...
So I have the Post entity. It looks fine. But it should have tags.
In code it looks like this (ruby code):
class Post
attr_reader :tags
attr_reader :title
attr_reader :text
# ...
end
class Tag
attr_reader :name
attr_reader :description
# ...
end
Tags aren't make a sense as entity. I don't need tag itself.
But how should I to implement repository for post?
I have found 2 variants:
1.
Build tags in same repository. Like this:
# PostRepository
def find(id)
# getting post data from storage here
# getting tags data
Post.new(title, text, tags_data.map { |tag_data| Tag.new(tag_data[:name], tag_data[:description]))
end
But it looks ugly. Can't clearly say why.
2.
Make separate repository for tags.
# PostRepository
def find(id)
# getting post data from storage here
Post.new(title, text, tag_repository.find(tag_ids)) # or tag_names or tag_something
end
Looks better. But is it fine to make separate repository for value-objects?
What is the right way according DDD?
UPD:
In other hand, I have to get all available tags. And I never have to change tag with posts. And tags' name looks like identity. Maybe I'm fundamentally wrong? Maybe tag is entity?
UPD2:
This problem shows me that my design skill is very poor.
Because of it, there is two question in my one.
They are:
What is right way to build value object inside entities' repository.
How to see the difference between value and entity in my problem.
After all it looks clear. According the specified conditions, tag is value. And it's ok that it's builded by Post's repository.
But this conditions is result of poor analize. If I could look wider, I would see that tag has it's own life cycle. Though, in context of post, tags are immutable.
Tag is most probably just a regular value object in your domain. Tag could be an entity, if it had its own lifecycle. Frankly, I don't think that's the case in your domain, as you can replace each tag with another copy with the same properties.
You can add methods to query tags to your domain repository. It's not a violation of DDD aggregate rules. Aggregates are really about consistency - your repositories should not return parts of your aggregate if you can modify them outside of aggregate context. However, you can explicitly return value objects of your aggregates just for read purposes (e.g. collecting all tags of all posts within selected date range). Besides that, query methods should be placed inside repository for the sake of efficiency. That being said, in your case probably the best solution is to use separate read model (using e.g. nosql db) following CQRS principles. This way you have the model explicitly adjusted to your query needs, so it can very efficient.

Loopback does not discover Oracle relations

Following
Loobback Docs: Discovering models from relational databases, Stackoverflow answer: Loopback not discovering models and Loopback datasource juggler API: Datasource I created a discover js script to get models from an oracle database. The problem is that it never managed to read the relations from the tables. I used the methods
discoverAndBuildModels - The result object had the properties of the table and an array called relations but that array was empty
discoverSchema - I managed to get the actual model JSON file and write it to the appropriate location. As the method is described in the api to not read relations i was not surprised to not find any here
discoverSchemas - Includes option to read relations (called relations but also tried it with associations) which gave me a similar result than discoverSchema but the "relations" tag only had an empty json object assigned to it.
I tried all options with a variety of relations and associations settings but none of them gave me anything but an empty object as the "relations" tag.
Am I missing something in the setup?
Oh boy, we've also had this issue. You just have to make sure that your user has the appropriated rights. Choose a privileged user (maybe admin?) to gather the data and it should work.
Check out this question for reference: How to query the permissions on an Oracle directory?

How to selectively populate waterline associations via query param in sails.js

By default, sails will populate all relationships within a model when it's corresponding API route is hit. Does anyone know if it's possible to toggle this functionality? If I'm working with a one-to-many association, I may not want to populate the association when doing a listing of all items for performance reasons. But when viewing a single item, it would be nice to complete the population.
For example, say one ticket can have many comments. I don't care about the comments when fetching a case listing but would be important when viewing a specific case. I took a guess at how it could function but it fails:
localhost:1337/tickets?populate=false
Update
I implemented the above functionality within balderdashy/sails#1695. The only change is that you selectively choose which associations to populate using:
localhost:1337/tickets?populate=[] // Don't populate anything
localhost:1337/tickets?populate=[comments] // Only populate comments
This would override whatever is defined for populate within your blueprint config.
You just need to separate your assosiactions via comma, just like this:
localhost:1337/tickets?populate=comments,owner&SOME_OTHER_PARAMS

How to create and fetch relational records in core data

Total newbie question now... Suffice to say, I have searched for a completely noddy explanation but have not found anything 'dumb' enough. The problem is...
I have created a core data stack in which I have a entity called 'Client' and an entity called 'Car'. It is a one-to-many relationship.
So far i have successfully created and fetched the client list using code from apple's tutorial. Once I select a client, I then push a new tableViewController which should list the Cars for that chosen client.
First question...
I am used to sql style database programming where if I wanted to add a car to a client, I would simply add a 'ClientID' tag to the 'Car' record thereby providing the relationship to a specific client. How do I do the equivalent in core data? My understanding from my reading is adding attributes to point to other entities isnt necessary - core data maintains this relationship for you without needing additional attributes in the entities.
Second question...
As and when I have created a 'car' entity and successfully linked it to a 'Client'. How to I create a fetch which will retrieve just THAT client's cars. I could alter the code from apple to fetch ALL cars but I don't know how to fetch cars associated with a given client. From my reading, I think I need to use predicates, but apples predicate documentation stands alone and does not give clear guidance on how to use it with core data
I realise how noddy this is, but I cant find an idiots guide anywhere...
Any help/code exmaples much appreciated.
OK, I have answered my own question. For those who have found my question and would like to know the answer, it is extremely simple...
Firstly, to create a 'Car' and associate it with a 'Client'. Firstly create a 'Car' as you normally would and simply add this line of code...
newCar.client = client;
This sets the 'client' relationship on the 'Car' record to the client in question.
Secondly, I had thought that if you had a client and needed to find their cars, you would need a new fetch. But not so! Simply use the following lines of code...
NSSet *cars = client.cars;
[self setCarsArray:[cars allObjects]];
The first line uses "client.cars" o follow the object graph to determine the cars this client has and populates them in an NSSet. The second line then populates a NSArray which is declared in the current viewcontroller which can be used to for display purposes.
Sorted!!

Resources