Browse Source

chore(core): Add missing many-to-one indexes

Closes #2196, relates to #1502
Michael Bromley 2 years ago
parent
commit
e78ce5c000

+ 1 - 0
packages/core/src/entity/channel/channel.entity.ts

@@ -33,6 +33,7 @@ export class Channel extends VendureEntity {
     @Column({ default: '', nullable: true })
     description: string;
 
+    @Index()
     @ManyToOne(type => Seller)
     seller?: Seller;
 

+ 3 - 2
packages/core/src/entity/collection/collection-asset.entity.ts

@@ -1,5 +1,5 @@
 import { DeepPartial, ID } from '@vendure/common/lib/shared-types';
-import { Column, Entity, ManyToOne } from 'typeorm';
+import { Column, Entity, Index, ManyToOne } from 'typeorm';
 
 import { OrderableAsset } from '../asset/orderable-asset.entity';
 
@@ -13,6 +13,7 @@ export class CollectionAsset extends OrderableAsset {
     @Column()
     collectionId: ID;
 
-    @ManyToOne((type) => Collection, (collection) => collection.assets, { onDelete: 'CASCADE' })
+    @Index()
+    @ManyToOne(type => Collection, collection => collection.assets, { onDelete: 'CASCADE' })
     collection: Collection;
 }

+ 1 - 0
packages/core/src/entity/order-line/order-line.entity.ts

@@ -36,6 +36,7 @@ export class OrderLine extends VendureEntity implements HasCustomFields {
      * @description
      * The {@link Channel} of the {@link Seller} for a multivendor setup.
      */
+    @Index()
     @ManyToOne(type => Channel, { nullable: true, onDelete: 'SET NULL' })
     sellerChannel?: Channel;
 

+ 6 - 2
packages/core/src/entity/order/order.entity.ts

@@ -52,11 +52,12 @@ export class Order extends VendureEntity implements ChannelAware, HasCustomField
     @OneToMany(type => Order, sellerOrder => sellerOrder.aggregateOrder)
     sellerOrders: Order[];
 
+    @Index()
     @ManyToOne(type => Order, aggregateOrder => aggregateOrder.sellerOrders)
-    aggregateOrder: Order;
+    aggregateOrder?: Order;
 
     @EntityId({ nullable: true })
-    aggregateOrderId: ID;
+    aggregateOrderId?: ID;
 
     /**
      * @description
@@ -93,6 +94,9 @@ export class Order extends VendureEntity implements ChannelAware, HasCustomField
     @ManyToOne(type => Customer)
     customer?: Customer;
 
+    @EntityId({ nullable: true })
+    customerId?: ID;
+
     @OneToMany(type => OrderLine, line => line.order)
     lines: OrderLine[];
 

+ 1 - 2
packages/core/src/entity/product-option-group/product-option-group-translation.entity.ts

@@ -22,9 +22,8 @@ export class ProductOptionGroupTranslation
 
     @Column() name: string;
 
-    // TODO: V2 need to add onDelete: CASCADE here
     @Index()
-    @ManyToOne(type => ProductOptionGroup, base => base.translations)
+    @ManyToOne(type => ProductOptionGroup, base => base.translations, { onDelete: 'CASCADE' })
     base: ProductOptionGroup;
 
     @Column(type => CustomProductOptionGroupFieldsTranslation)

+ 1 - 2
packages/core/src/entity/product-option/product-option-translation.entity.ts

@@ -22,9 +22,8 @@ export class ProductOptionTranslation
 
     @Column() name: string;
 
-    // TODO: V2 need to add onDelete: CASCADE here
     @Index()
-    @ManyToOne(type => ProductOption, base => base.translations)
+    @ManyToOne(type => ProductOption, base => base.translations, { onDelete: 'CASCADE' })
     base: ProductOption;
 
     @Column(type => CustomProductOptionFieldsTranslation)

+ 3 - 2
packages/core/src/entity/product-variant/product-variant-asset.entity.ts

@@ -1,5 +1,5 @@
 import { DeepPartial, ID } from '@vendure/common/lib/shared-types';
-import { Column, Entity, ManyToOne } from 'typeorm';
+import { Column, Entity, Index, ManyToOne } from 'typeorm';
 
 import { OrderableAsset } from '../asset/orderable-asset.entity';
 
@@ -13,6 +13,7 @@ export class ProductVariantAsset extends OrderableAsset {
     @Column()
     productVariantId: ID;
 
-    @ManyToOne((type) => ProductVariant, (variant) => variant.assets, { onDelete: 'CASCADE' })
+    @Index()
+    @ManyToOne(type => ProductVariant, variant => variant.assets, { onDelete: 'CASCADE' })
     productVariant: ProductVariant;
 }

+ 1 - 1
packages/core/src/entity/product-variant/product-variant-translation.entity.ts

@@ -23,7 +23,7 @@ export class ProductVariantTranslation
     @Column() name: string;
 
     @Index()
-    @ManyToOne(type => ProductVariant, base => base.translations)
+    @ManyToOne(type => ProductVariant, base => base.translations, { onDelete: 'CASCADE' })
     base: ProductVariant;
 
     @Column(type => CustomProductVariantFieldsTranslation)

+ 3 - 2
packages/core/src/entity/product/product-asset.entity.ts

@@ -1,5 +1,5 @@
 import { DeepPartial, ID } from '@vendure/common/lib/shared-types';
-import { Column, Entity, ManyToOne } from 'typeorm';
+import { Column, Entity, Index, ManyToOne } from 'typeorm';
 
 import { OrderableAsset } from '../asset/orderable-asset.entity';
 
@@ -13,6 +13,7 @@ export class ProductAsset extends OrderableAsset {
     @Column()
     productId: ID;
 
-    @ManyToOne((type) => Product, (product) => product.assets, { onDelete: 'CASCADE' })
+    @Index()
+    @ManyToOne(type => Product, product => product.assets, { onDelete: 'CASCADE' })
     product: Product;
 }

+ 2 - 1
packages/core/src/entity/region/region.entity.ts

@@ -1,5 +1,5 @@
 import { ID } from '@vendure/common/lib/shared-types';
-import { Column, Entity, JoinColumn, ManyToOne, OneToMany, TableInheritance } from 'typeorm';
+import { Column, Entity, Index, JoinColumn, ManyToOne, OneToMany, TableInheritance } from 'typeorm';
 
 import { LocaleString, Translatable, Translation } from '../../common/types/locale-types';
 import { HasCustomFields } from '../../config/custom-field/custom-field-types';
@@ -35,6 +35,7 @@ export abstract class Region extends VendureEntity implements Translatable, HasC
 
     name: LocaleString;
 
+    @Index()
     @ManyToOne(type => Region, { nullable: true, onDelete: 'SET NULL' })
     parent?: Region;
 

+ 1 - 2
packages/core/src/entity/shipping-line/shipping-line.entity.ts

@@ -25,9 +25,8 @@ export class ShippingLine extends VendureEntity {
     @ManyToOne(type => ShippingMethod)
     shippingMethod: ShippingMethod;
 
-    // TODO: v2 - Add `{ onDelete: 'CASCADE' }` constraint
     @Index()
-    @ManyToOne(type => Order, order => order.shippingLines)
+    @ManyToOne(type => Order, order => order.shippingLines, { onDelete: 'CASCADE' })
     order: Order;
 
     @Money()

+ 1 - 1
packages/core/src/entity/shipping-method/shipping-method-translation.entity.ts

@@ -26,7 +26,7 @@ export class ShippingMethodTranslation
     @Column({ default: '' }) description: string;
 
     @Index()
-    @ManyToOne(type => ShippingMethod, base => base.translations)
+    @ManyToOne(type => ShippingMethod, base => base.translations, { onDelete: 'CASCADE' })
     base: ShippingMethod;
 
     @Column(type => CustomShippingMethodFieldsTranslation)

+ 2 - 0
packages/core/src/entity/stock-level/stock-level.entity.ts

@@ -20,12 +20,14 @@ export class StockLevel extends VendureEntity {
         super(input);
     }
 
+    @Index()
     @ManyToOne(type => ProductVariant, productVariant => productVariant.stockLevels, { onDelete: 'CASCADE' })
     productVariant: ProductVariant;
 
     @EntityId()
     productVariantId: ID;
 
+    @Index()
     @ManyToOne(type => StockLocation, { onDelete: 'CASCADE' })
     stockLocation: StockLocation;
 

+ 1 - 0
packages/core/src/entity/stock-movement/cancellation.entity.ts

@@ -21,6 +21,7 @@ export class Cancellation extends StockMovement {
         super(input);
     }
 
+    // @Index() omitted as it would conflict with the orderLineId index from the Allocation entity
     @ManyToOne(type => OrderLine)
     orderLine: OrderLine;
 }

+ 1 - 0
packages/core/src/entity/stock-movement/release.entity.ts

@@ -22,6 +22,7 @@ export class Release extends StockMovement {
         super(input);
     }
 
+    // @Index() omitted as it would conflict with the orderLineId index from the Allocation entity
     @ManyToOne(type => OrderLine)
     orderLine: OrderLine;
 }