Typeorm get nested relations without intermediate table - typeorm

I have Many-to-many relations with custom properties like here
I try to get a post with all category like
PostRepository.findOne({
relations: ['postToCategories.category'],
where: { id: 1},
})
the results like this
{
id: 1,
name: 'post1',
postToCategories: [{id: 1, category: {id: 1, name: "cate1"}}]
}
But i don't want id of intermediate table. I need something like this
{
id: 1,
name: 'post1',
postToCategories: [{category: {id: 1, name: "cate1"}}]
}
or this
{
id: 1,
name: 'post1',
postToCategories: [{id: 1, name: "cate1"}]
}
I don't need id of postToCategories. How to do that thank.

Related

Identification of duplicate multiples emails for user

I have User and UserEmails models.
User columns: id, first_name, last_name
UserEmails: id, user_id, email, primary
I need a function like this(https://imgur.com/a/762iK9r)
The essence is this: duplicate users are identified by or emails.
Example:
User 1:
{ id: 1, first_name: 'Test', last_name: 'User1' }
UserEmails for User 1:
{ email: 'same_email3#test.com', primary: true, user_id: 1 }
User 2:
{ id: 2, first_name: 'Test', last_name: 'User2' }
UserEmails for User 2:
{ { email: 'same_email#test.com', primary: true, user_id: 2 }, { email: 'same_email1#test.com', primary: false, user_id: 2 }, { email: 'same_email3#test.com', primary: false, user_id: 2 } }
User 3:
{ id: 3, first_name: 'Test', last_name: 'User3' }
UserEmails for User 3:
{ { email: 'same_email#test.com', primary: true, user_id: 3 }, { email: 'same_email3#test.com', primary: false, user_id: 3 }, { email: 'same_email4#test.com', primary: false, user_id: 3 } }
I need a query which will find that duplicates and return an ids array like this:
[[1, 2, 3], [...]]
I have a query but it incorrectly displays an array of ids:
ids = User.left_outer_joins(:user_emails)
.select('array_agg(users.id) AS ids')
.where('user_emails.email IS NOT NULL')
.group("user_emails.email")
.having('COUNT(1) > 1')
.map(&:ids)
I get this array, but the array [2, 3] is already in the array [1,2,3].
[[1, 2, 3], [2, 3]]
How I can group or sort the array ids that would not be duplicated in case of display of users which are duplicates by email?
You can try self join to find duplication
select distinct(u1.user_id) from public.UserEmails u1, public.UserEmails u2
where u1.email = u2.email

Is there a way to control loading relations in gorm with mongodb?

I'm building a REST api with grails 3 and mongo. I have encountered a problem when i need to marshal an object graph with a bigger depth.
I have the following domains:
class Category extends Resource {
/* other fields */
Category parent
}
class Product extends Resource {
/* other fields */
List<Category> categories
static hasMany = [categories: Category]
}
I have in the database the following structure(simplified for the sake of understanding):
categories:
{name: 'cat1'}
{name: 'cat2', parent: 'cat3'}
{name: 'cat3', parent: 'cat4'}
{name: 'cat4', parent: 'cat5'}
{name: 'cat5'}
product:
{categories: ['cat1', 'cat2']}
I am extending from RestfullController when creating my controllers. I want to be able to get a product and have the categories with parents in the returned json.
I get the following results:
/product/${id}
{
id: '...',
categories: [{
id: '...',
name: 'cat1'
}, {
id: '...',
name: 'cat2',
parent: { id: '...' }
}]
}
/category/cat2id
{
id: '...',
name: 'cat2',
parent: { id: '...' }
}
/category
[{
id: '...',
name: 'cat1'
},{
id: '...',
name: 'cat5'
},{
id: '...',
name: 'cat4',
parent: {
id: '...',
name: 'cat5'
}
},{
id: '...',
name: 'cat3',
parent: {
id: '...',
name: 'cat4',
parent: {
id: '...',
name: 'cat5'
}
}
},{
id: '...',
name: 'cat2',
parent: {
id: '...',
name: 'cat3',
parent: {
id: '...',
name: 'cat4',
parent: {
id: '...',
name: 'cat5'
}
}
}
}]
Why would Category.list() load the whole category object graph, and Category.get(), Product.get() and Product.list() would not load it? Is there a way to control this behaviour ?
The way Grails works is that it will only render associations that have already been loaded from the database, hence why you get some associations rendered and others not.
There is no built in way to control this behaviour other than writing your own marshaller. See http://grails.github.io/grails-doc/latest/guide/webServices.html#renderers

active model serializer flat structure

I'm bulding my api server for my React/Redux app, where i need some flat response from server to manage and reduce my state.
For example, when i have this response:
[{
id: 1,
title: 'Some Article',
author: {
id: 1,
name: 'Dan'
}
}, {
id: 2,
title: 'Other Article',
author: {
id: 1,
name: 'Dan'
}
}]
How can i make it looks like:
{
result: [1, 2],
entities: {
articles: {
1: {
id: 1,
title: 'Some Article',
author: 1
},
2: {
id: 2,
title: 'Other Article',
author: 1
}
},
users: {
1: {
id: 1,
name: 'Dan'
}
}
}
}
Best feature is what each key is articles/users id, so i can easily get it from the response and merge it in my front-end app.
You can achieve this with AMS, but you will have to write a custom adapter, as none of the stock ones provide this exact format.
JSON API (and the corresponding AMS JsonApi adapter) solve this issue, but the format is slightly different from the one you want.

Fetching wrong data in query in rails

I have done this
def show_selected_students(selected_students, students)
student = students.map{|a| a.name}
selected_students = selected_students.split(",")
#student_selected = selected_students.map {|i| student[i.to_i] }
end
in students I am fetching given data
#<ActiveRecord::Relation [#<Student id: 1, name: "XYZ",>, #<Student id: 2, name: "test1">, #<Student id: 3, name: "cherry">, #<Student id: 4, name: "mary">, #<Student id: 5, name: "hary">, #<Student id: 35, name: "hen">, #<Student id: 44, name: "duck">, #<Student id: 62, name: "try">]>
and in selected_students I am getting 2,3,4 Now I want to fetch those students whose id match with selected_students for this I had written this but it gives me this output ['cherry', 'mary', 'hary'] i.e id 3,4,5 but I want 2,3,4 Please guide me how to solve this. Thanx in advance.
You can try this:
def show_selected_students(selected_students, students)
selected_students = selected_students.split(",")
#student_selected = students.where(:id => selected_students).map{|a|a.name}
end

Additional attributes in N-N mongoid (Like in pivot table)

I have a has_many_and_belongs_to relationship between central and coordinators.
So my mongo documents are represented as below:
central = {
_id: 1,
title: 'First Name',
coordinators: [
BSON[1],
BSON[2],
BSON[3]
]
}
coordinators = [
{
_id: 1,
name: 'Me',
centrals: [BSON[1], BSON[2]]
},
{
_id: 1,
name: 'Mateus'
centrals: [BSON[1]]
},
{
_id: 1,
name: 'Gomes'
centrals: [BSON[1]]
},
]
If I do this:
#central = Central.find(1)
#coordinator = #central.coordinators.find(1)
#coordinator.can_edit = false
It will apply to the coordinators document resulting in this:
coordinator = {
_id: 1,
name: 'Me',
centrals: [BSON[1], BSON[2]],
can_edit: false
}
But what I really want to do is apply this can_edit attribute in the relationship, like in pivot table in RDBMS:
central = {
_id: 1,
titulo: 'First Name',
coordinators: [
{
_id: 1,
name: 'Me',
can_edit: false
},
BSON[2],
BSON[3]
]
}
Only for the central with id 1 I want to aply the can_edit to false.
I have to keep the relation between a Central and Coordinator, but in some situation, I want to have an additional information about that relation, like if I would not allow a coordinator to edit some data only in central with id 1.
How can I do this using mongoid?
The solution for this was create another relation N-N:
Add on central.rb
has_and_belongs_to_many :blocked_coordenadors,
class_name: "Central",
inverse_of: :blocked_centrals
And in coordinator.rb:
has_and_belongs_to_many :blocked_centrals,
class_name: "Central",
inverse_of: :blocked_coordenadors
And to check I do this:
central.blocked_coordenadors.include? coordinator

Resources