How do I join two entities linked with 1-1 relationship and add the joined entity on a particular property of the first entity? - sql

I have 2 entities with a one to one relationship between them created with typeorm.
#ObjectType()
#Entity()
export class Interaction extends BaseEntity {
#Field()
#PrimaryGeneratedColumn()
id!: number;
#Field(() => AF_Metadata)
#JoinColumn()
#OneToOne(() => AF_Metadata, { eager: true })
metadata: AF_Metadata;
}
}
#ObjectType()
#Entity()
export class AF_Metadata extends BaseEntity {
#Field()
#PrimaryGeneratedColumn()
id!: number;
//Relationships
#OneToOne(() => Interaction, { onDelete: "CASCADE" })
interaction: Interaction;
}
How do I join these entities by adding the AF_Metadata entity to the metadata property in the Interaction entity. I am able to add the whole AF_Metadata object to the interaction but not to the metadata propery. This is my sql query so far.
select * from interaction int
left join af_metadata metadata on int."metadataId" = metadata.id
order by int."createdAt" DESC
Current result:
{
id: 8327,
metadataId: 1,
}
Desired result:
{
id: 8327,
metadata: {
metadataId: 1
}
}

Related

Multiple OneToOne relation with TypeOrm doesn't work

I currently using NestJS 8.4.7 and TypeORM 0.3.10
I want to make two (or more) OneToOne relations in my entity class based on an existing SQL Database
Here is my code :
// Article Entity
#Entity({ name: 'node' })
export class Article {
#PrimaryGeneratedColumn('increment')
public articleId: number
#OneToOne(() => ArticleBody)
#JoinColumn({ name: 'articleId', referencedColumnName: 'bodyId' })
public body: ArticleBody
#OneToOne(() => ArticleTitle)
#JoinColumn({ name: 'articleId', referencedColumnName: 'titleId' })
public title: ArticleTitle
}
// Body Entity
#Entity({ name: 'article_body' })
export class ArticleBody {
#PrimaryGeneratedColumn()
public bodyId: number
#Column({ nullable: false })
public bodyValue: string
}
// Title Entity
#Entity({ name: 'article_title' })
export class ArticleTitle {
#PrimaryGeneratedColumn()
public titleId: number
#Column({ nullable: false })
public titleValue: string
}
And I have this error:
sqlMessage: ERROR [ExceptionsHandler] Unknown column 'Article__Article_body.titleId' in 'on clause'
sql: SELECT
`Article`.`articleId` AS `Article_nid`,
`Article__Article_body`.`bodyValue` AS `Article__Article_body_bodyValue`,
`Article__Article_title`.`titleValue` AS `Article__Article_title_titleValue`
FROM `Article` `Article`
LEFT JOIN `article_body` `Article__Article_body` ON Article__Article_body.titleId=`Article`.`articleId`
LEFT JOIN `article_title` `Article__Article_title` ON `Article__Article_title`.`titleId`=`Article`.`articleId`
WHERE (`Article`.`articleId` = 1)
It looks like if the last JoincolumnOptions overwrites all previous JoincolumnOptions
// Article Entity
#Entity({ name: 'node' })
export class Article {
#PrimaryGeneratedColumn('increment')
public articleId: number;
#OneToOne(() => ArticleBody)
#JoinColumn({ name: 'articleBodyId' })
public body: ArticleBody;
#OneToOne(() => ArticleTitle)
#JoinColumn({ name: 'articleTitleId' })
public title: ArticleTitle;
}
// Body Entity
#Entity({ name: 'article_body' })
export class ArticleBody {
#PrimaryGeneratedColumn("increment")
public bodyId: number;
#Column({ name: 'body_value', nullable: false })
public bodyValue: string;
}
// Title Entity
#Entity({ name: 'article_title' })
export class ArticleTitle {
#PrimaryGeneratedColumn("increment")
public titleId: number;
#Column({ name: 'title_value', nullable: false })
public titleValue: string;
}
In Article Entity your joinColumn name is same that's why the error is giving

Many To Many Relation only accecepts Unique Values

Orders can have 0 to n Items in them. One Item can belongs to 0 to n Orders.
I have the Relationship set up the following way
#Entity()
export class Order {
#PrimaryGeneratedColumn()
id: number;
#ManyToOne(() => Customer, (customer) => customer.orders, {
eager: true,
})
customer: Customer;
#JoinTable()
#ManyToMany(() => Item, { eager: true })
items: Item[];
}
But I can only add Unique Items to my order. When I try to safe a item twice, it gets ignored?
This is the code for adding Items to a order
async addItemToOrder(orderId: number, itemId: number) {
const order = await this.findOne(orderId);
const item = await this.itemService.findOne(itemId);
if (!order.items) {
order.items = [];
order.items = [...order.items, item];
} else {
order.items = [...order.items, item];
}
order.totalPrice = this.calcTotalPrice(order.items);
await this.orderRepository.save(order);
return order;
}
This is the item
#Entity()
export class Item {
#PrimaryGeneratedColumn()
id: number;
#Column()
name: string;
#Column()
price: number;
}
It is working as intended. In the underlying join table a itemId gets connected to a orderId. Typeorm only inserts a new entry if it cannot find a combination of itemId and orderId.
For your usecase it would make sense to define a own join table that includes a amount attribute. So your join table looks like this
itemId
orderId
amount
1
1
2
1
2
1
2
2
5
You can achive this using typeorm like this:
You create a new Entity that is the join entity between an item and an order and includes a attribute amount
#Entity()
export class OrderItem {
#Column('int')
amount: number;
#ManyToOne(() => Item, item => item.orders, { primary: true })
item: Item;
#ManyToOne(() => Order, order => order.items, { primary: true })
order: Order;
}
#Entity()
export class Order {
#PrimaryGeneratedColumn()
id: number;
#ManyToOne(() => Customer, (customer) => customer.orders, {
eager: true,
})
customer: Customer;
#OneToMany(() => OrderItem, { eager: true })
items: OrderItem[];
}
#Entity()
export class Item {
#PrimaryGeneratedColumn()
id: number;
#Column()
name: string;
#Column()
price: number;
#OneToMany(() => OrderItem, { eager: true })
orders: OrderItem[];
}
The reason you do this is normalization. Relational databases rely on normalization to prevent inconsistent data.
You can read more about normalization here

Create query builder that the source table (FROM) is a join table in TypeORM

I'm trying to implement the following SQL in TypeORM using QueryBuilder:
SELECT
user_places.user_id,
place.mpath
FROM
public.user_root_places_place user_places
INNER JOIN
public.place place
ON place.id = user_places.place_id
The entities are:
#Entity()
export class User {
#Column({ unique: true, primary: true })
id: string;
#ManyToMany(() => Place)
#JoinTable()
rootPlaces: Place[];
}
#Entity()
export class Place {
#PrimaryGeneratedColumn()
id: number;
#Column()
mpath: string;
}
When you create a query builder you have to use some entity or table but the join table is "hidden" by TypeORM
I know I can replace the inner join table order and it will solve the problem but I'm looking for when the source table is the join table
If you don't want to use the generated name just specify explicitly the join table name
#Entity()
export class User {
#Column({ unique: true, primary: true })
id: string;
#ManyToMany(() => Place)
#JoinTable({
name: 'user_places' // <---
})
rootPlaces: Place[];
}
And then:
createQueryBuilder('user_places')
.select(['user_places.userId', 'place.mpath'])
.innerJoin(Place, 'place', 'place.id = user_places.place_id')
.getMany();

How to return the entity with its relations after saving it?

I am building a storage application, with GraphQL as the backend, using Typegraphql and TypeORM.
The categories need to be added separately and then when adding a product, you choose from a dropdown one of the available categories. This in turn passes the categoryId to the product in a one-to-many/many-to-one relationship.
Here is my Category entity:
import {
Entity,
PrimaryColumn,
Column,
BaseEntity,
Generated,
OneToMany
} from 'typeorm';
import Product from './Product';
#ObjectType()
#Entity('categories')
export default class Category extends BaseEntity {
#Field()
#PrimaryColumn()
#Generated('uuid')
categoryId: string;
#Field()
#Column()
categoryName: string;
#OneToMany(() => Product, (product: Product) => product.category)
products: Product[];
}
and here is my Product entity
import {
Entity,
PrimaryColumn,
Column,
BaseEntity,
Generated,
ManyToOne,
JoinColumn
} from 'typeorm';
import Category from './Category';
#ObjectType()
#Entity('products')
export default class Product extends BaseEntity {
#Field()
#PrimaryColumn()
#Generated('uuid')
productID: string;
#Field()
#Column()
productName: string;
#Field(() => Category)
#ManyToOne(() => Category, (category: Category) => category.products, {
cascade: true,
lazy: true
})
#JoinColumn()
category: Category;
#Field()
#Column()
productQuantity: number;
#Field()
#Column({ type: 'decimal', precision: 2 })
productPrice: number;
#Field()
#Column({ type: 'decimal', precision: 2 })
productPriceRA: number;
#Field()
#Column({ type: 'decimal', precision: 2 })
productPriceKK: number;
#Field()
#Column('varchar', { length: 255 })
productSupplier: string;
#Field()
#Column('varchar', { length: 255 })
productOrderLink: string;
#Field()
#Column('longtext')
productImage: string;
}
For the save mutation, I've created an Input type as well:
export default class ProductInput implements Partial<Product> {
#Field()
productName: string;
#Field(() => String)
category: Category;
#Field()
productQuantity: number;
#Field()
productPrice: number;
#Field()
productPriceRA: number;
#Field()
productPriceKK: number;
#Field()
productSupplier: string;
#Field()
productOrderLink: string;
#Field()
productImage: string;
}
The relations work, as I am able to query the products, along with their category data with the following query:
{
getProducts {
productID
productName
category {
categoryId
categoryName
}
}
}
However, when saving a product it always returns
"message": "Cannot return null for non-nullable field Category.categoryName."
This is the Mutation's code in the Resolver:
#Mutation(() => Product, { description: 'Add new product' })
async addProduct(
#Arg('product') productInput: ProductInput
): Promise<Product | any> {
try {
const product = await Product.create(productInput).save();
console.log('product: ', product);
return product;
} catch (error) {
return error;
}
}
I've been trying different things, however nothing seems to work and I am wondering if it's even possible to directly return the entity with its relations. If it's not, the other option I can think of is to return true/false based on the result and re-query all of the data. But this seems very inefficient and I am actively trying to avoid going this route.
Any help will be much appreciated.
After some more research and I decided to go with the following approach:
try {
const { productID } = await Product.create(productInput).save();
return await Product.findOne(productID);
} catch (error) {
return error;
}
This allows me to directly return the product, based on the productID after it's saved in the database and properly returns the object with it's relationship.
GraphQL uses an notation to recognize data. You can see it as __typename object property. Of course, this must be turned on in the GraphQL server configuration. If you see it, it's already clear. You can reach the correct result without refetching the relation changes in the cached data on the client side with a trick like this.
For example, let's say we have updated the Product with category. In the data to return from the update mutation, it is sufficient to return only the id of the relation.
For this to work, category and product must be cached separately on the client beforehand.
for example:
mutation UpdateProduct($product: UpdateProductInput!) {
updateProduct(product: $product) {
id
title
category {
id
}
}
}
You can also write in writeFragment, which is a separate method, which is the most stingy, but it can make your job difficult in nested data.
export class ProductFragmentService {
constructor(private apollo: Apollo) {}
updateProduct(product: Product): void {
const client = this.apollo.client;
client.writeFragment({
id: `Product:${product.id}`,
fragment: gql`
fragment UpdateProductCategoryFragment on Product {
__typename
id
title
category {
id
}
}
`,
data: {
__typename: 'Product',
...product,
},
});
}
}
If you want all the fields belonging to category, you need to send them to resolver and return as a response from there. Otherwise, yes, it gives a warning that I could not find the name property.
The more profitable way of doing it is to send this data to the resolver with the input, as I wrote above, and return to the client as a response from the server.
If you still have to make another SQL request, it is necessary to call the same id after registration.
#Authorized()
#Mutation(() => Product, { description: 'Add new product' })
async addProduct(
#Arg('product') productInput: ProductInput
): Promise<Product> {
await this.productRepo.save(productInput);
return await this.productRepo.findOne({ where: { id: productInfo.id } });
}
that's all :)

TypeORM: how to implement bidirectional relationship, multiple fields --> one entity type

I've created a 'document' entity:
e.g.
#Entity()
export class Document {
#PrimaryGeneratedColumn()
id: number;
#Column()
name: string;
#Column()
path: string;
...
}
Multiple documents can be related to different entity types: post, userProfile etc
in the post entity for example, I have several fields which all specify document relationships.
#OneToOne(type => DocumentEntity)
#JoinColumn({ name: 'default_document' })
defaultDocument: DocumentEntity;
#OneToOne(type => DocumentEntity)
#JoinColumn({ name: 'featured_document' })
featuredDocument: DocumentEntity;
#OneToMany(type => DocumentEntity, document => document.post)
#JoinColumn({ name: 'other_documents' })
otherDocs: DocumentEntity[];
I'm unclear how to make the document relationships bidirectional.
I had hoped to have a single field on document like:
#ManyToOne(type => abstractEntity, entity => entity.document)
parentEntity: abstractEntity;
This way if I'm querying document entities for their parent relationships,
I would have a result like:
documents: [
{
id: 1,
name: 'document 1',
path: 'https://image.hosted.service/1.jpg',
parentEntityId: 23
},
{
id: 2
name: 'document 2',
path: 'https://image.hosted.service/2.jpg'
parentEntityId: 27
}
]
But Typeorm seems to want me to define an exact matching field for each parent relationship field on documentEntity like:
#Entity()
export class Document {
#PrimaryGeneratedColumn()
id: number;
#Column()
name: string;
#Column()
path: string;
...
#OneToOne(type => PostEntity, post => post.defaultDocument)
postEntityDefaultDoc: PostEntity;
#OneToOne(type => PostEntity, post => post.featuredDocument)
postEntityFeaturedDoc: PostEntity;
#ManyToOne(type => PostEntity, post => post.otherDocs)
otherDocs: PostEntity[];
}
For the sake of simplicity in this example, there are no M:N relationships: document can have at most one parent.
It doesn't seem correct that I would have to define a new field on document entity, for every possible instance where a parent entity field references a document.
A query on document would not return a list with one field defining the parent entity, instead I have to parse/aggregate an arbitrary number of fields.
I can't seem to find any tutorials/examples in which a single entity has many fields each referencing the same other entity, which is making me think my basic approach is flawed.
The secret ingridient is leftJoinAndMapMany which allows you to join abitrary entities and map it onto attributes.
Here is what I would do in your case. The DocumentEntity would look like that:
#Entity()
class DocumentEntity {
#PrimaryGeneratedColumn()
public id!: number;
#Column()
public entity!: string;
#Column({
name: 'entity_id',
})
public entityId!: string;
#Column()
public name!: string;
}
Your PostEntity would look like that:
#Entity()
class PostEntity {
#PrimaryGeneratedColumn()
public id!: number;
#Column()
public name: string;
public documents?: DocumentEntity[];
}
As you might notice, the documents on the post has no anotation. Thats because we will do the join with the aforementioned method. Your query would look something like that:
connection
.getRepository(PostEntity)
.createQueryBuilder('p')
.leftJoinAndMapMany(
'p.documents',
DocumentEntity,
'p__d',
'(p.id = md.entityId AND md.entity = :documentEntity)',
{
documentEntity: PostEntity.name,
},
)
.getMany()
These methods are available for joining these entities:
leftJoinAndMapMany
innerJoinAndMapMany
leftJoinAndMapOne
innerJoinAndMapOne