how to defined and object collections in Jhipster? - jhipster

How would you do this? Let's say you have a FrontPage object that is a collection of Many Post object. But it is not any kind of collection. It is a collection made of known members: 5 latest news (LatestNews#1, LatestNews#2, ... LatestNews#5) 5 popular news, and more, up to 25 individual Post Objects.
You can have a FrontPageObject like this....
entity FrontPageConfig {
creationDate Instant required
latestNews1 Long
latestNews2 Long
latestNews3 Long
latestNews4 Long
latestNews5 Long
topNews1 Long
and so on....}
entity Post {
body TextBlob minbytes(3) maxbytes(50000) required
}
... where the Long attribute represents the id of the Post object.
But when you do it, you have to load the object in Angular and make 25 calls to the the server to call the Posts.
How can I have an FrontPageConfig object that has all the properties as defined (latestNews1) Post objects inside and that I only have to call once.
Thanks again.
PD: I tried with OneToOne relationships:
FrontPageConfig{topNews1(id)} to Post{topNews1(id)}
And OneToMany or ManyToOne:
FrontPageConfig{topNews1(id)} to Post{topNews1(id)}
But it is not a defined collection!
And this relationship OneToMany {
Post{topNews1} to FrontPageConfig{topNews1}
Does not give the object, only the id!

This is a good use case for Data Transfer Objects (DTOs).
Create a custom API endpoint and in that method, query the database.
Create a custom DTO object (such as FrontPageDTO) with the fields you want to return to the user interface.
In your service class (or resource if you don't use services), make the queries through the repository to get the information you need.
Query for 5 latest news and 5 top news, then map that data to the DTO fields
Return the DTO once the fields are set

Related

NodeJS MongoDB populate() bringing string instead of JSON

I have a product and I have a property that brings an array with a provider id and its price. The idea is that every product has an array with multiples providers, each one with its own price.
However, when I use populate() looking for ProviderID, it brings me populated as string instead of a JSON. Below is GraphQL query return from a query that bring products.
ProviderPrice class, inside ProductModel
Optional parameter in ProductModel
This is how I am executing populate
Has anyone gone through this?
Found the problem.
I was using the class ProviderPrice instead of the model ProviderPriceModel.

How to create #Transient properties in JHipster?

I was thinking about how to create a Proposal object like this in JHipster: so a User can create a Proposal and other users can vote for it.
entity Proposal {
proposalText String minlength(2) maxlength(100) required
proposalVotes Integer
}
entity Vote {
numberOfPoints Integer
}
relationship ManyToOne {
Vote{proposal(id) required} to Proposal{vote}
Proposal{user(id) required} to User{proposal}
Vote{user(id) required} to User{vote}
}
In Spring I would create that #Transient proposalVotes Integer and the Controller would go and find all the Votes that a Proposal has and add them together to get to the result to be sent to the frontend. That property would not be stored in the database.
If I use JHipster and I add a proposalVotes property, the result would be saved in the database and could be changed in the dialogs(... and I do not like the result), so my question is:
What is the best practice in JHipster when you need a property that is calculated everytime his object is called?
Think of the number of comments in a Blog with Posts, if it is more familiar.
Where do you calculate the result: I would do it in the ProposalResource, but I’m not sure and I haven’t seen any use case like this in the examples, but it looks like a common case.
Thanks a lot
PD: If there is any example in Github, that could be great!
Actually if you are generating entities using JDL(Jhipster domain language) then you wont get any option to make field Transient as JDL is database design mechanism and Transient fields are not going to be placed in DB.
Solution is that after importing JDL to our app you can add Transient fields in your entity class.

Mongoose Architecture to add a document and update another document with the ID of the first

More of a general question. I'm setting up a Mongoose Node back end. I have 3 collections, User, Department and Activity.
If I add an Activity, I get the newly created Activity's _ID. I then need to add that ID to the appropriate Users array of activities and add the activity to the correct department.
Just wondering what the architecture for this should be. I've got my endpoints set up using Express. And an ActivityController and User/Department controller. Which add, get, update and destroy Activities/Users/Departments.
When I add an Activity I'm effecting a document in the other 2 collections though so how would I set this up... would create in the Activity controller also update User and Department as well? Or should I have a controller sitting on top of them all? I'm not really sure.
Activity Schema:
activityName: String
activityOwner: ObjectId
activityLocation: String
Department Schema:
departmentName: String
departmentActivities: [ObjectId]
User Schema:
userName: String
userAddress: String
userActivities: [ObjectId]

Giving your own ids to Core Data objects

I have some points on a map with associated informations contained by Core Data, to link the points on the map with the associated info, I would like to have an ID for each point, so in my entity, I would need some ID property, I have read that Core Data has it's own IDs for every managed object but I'm wondering wether or not it would be a good approach for me to directly use them or if I should create my own ID system ?
If you think I should create my OWN ID system, how would I do that ?
Thank you.
CoreData is not an relational database and you should avoid thinking about own ID’s. You may need them only for syncing purposes with external databases. For more precise answer, you should write how your model looks.
[edited after comment]
I don't see that you need any relations. Let's sat you have MapPoint entity with lat, and long properties. If there is only one user note, you just add another property to it like notes. If you have many informations (many records) stored with one MapPoint you need to add Notes entity with properties note and mappoint and make a relation between them. When you insert new Notes object into CoreData you set mappoint property to already existing MapPoint object (fetched after user tap).

Set Core Data to lazy load to-one relationship

I'm using AFIncrementalStore to connect my iOS application to my REST API. When I load my initial view controller, I create an NSFetchRequest that loads the latest 100 Events (NSManagedObjects) into view. The problem is that each Event has a 1:1 relationship with a Group object, and as soon as the Event is loaded, the incremental store is asked to fill that Group object, which in my case triggers an individual request to the server for each of the 100 Events.
I can see a couple ways to solve this problem, such as not requesting Groups from the server if they are already saved locally, caching the network request, or not storing the relationship in the NSManagedObject. But ideally, the Group object could start out as a Fault and only request to be filled once one of its field is accessed, similar to what happens with one-to-many relationships. Unfortunately I can't find any documentation that says how to force a one-to-one relationship in core data to be lazy-loaded. Is it possible?
Maybe this is what you are looking for?
From AFIncrementalStore.h:
/**
Returns whether the client should fetch remote relationship values for a
particular managed object. This method is consulted when a managed object
faults on a particular relationship, and will call
`-requestWithMethod:pathForRelationship:forObjectWithID:withContext:` if `YES`.
#param relationship The relationship of the specifified managed object
#param objectID The object ID for the specified managed object.
#param context The managed object context for the managed object.
#return `YES` if an HTTP request should be made, otherwise `NO. */
- (BOOL)shouldFetchRemoteValuesForRelationship:(NSRelationshipDescription*)
relationship forObjectWithID:(NSManagedObjectID *)objectID
inManagedObjectContext:(NSManagedObjectContext *)context;
If so, you could set this with
- (BOOL)shouldFetchRemoteValuesForRelationship:(NSRelationshipDescription *)relationship forObjectWithID:(NSManagedObjectID *)objectID inManagedObjectContext:(NSManagedObjectContext *)context
{
return NO;
}
in your AFRESTClient <AFIncrementalStoreHTTPClient> subclass.
I've been struggling this as well. Looks like one-to-one relations will always be eager loaded. One way around this could be to declare it as a one-to-many relation so that it automatically does a lazy load instead. Then in your model class, you can have a method that returns the first one in the set.

Resources