Sfoglia il codice sorgente

feat(core): Add indices to many-to-one relations

Relates to #1502. Performance & efficiency improvement for Postgres specifically.

BREAKING CHANGE: Explicit indexes have been added to many-to-one relations used throughout the data
model. If you are using MySQL/MariaDB you should not notice a change from this, since they
automatically add indexes to FK relations. Postgres, however, does not so this change will require
a DB migration.
Michael Bromley 3 anni fa
parent
commit
01e369fa58
39 ha cambiato i file con 118 aggiunte e 49 eliminazioni
  1. 3 1
      packages/core/src/entity/address/address.entity.ts
  2. 2 1
      packages/core/src/entity/asset/orderable-asset.entity.ts
  3. 2 1
      packages/core/src/entity/authentication-method/authentication-method.entity.ts
  4. 3 1
      packages/core/src/entity/channel/channel.entity.ts
  5. 3 6
      packages/core/src/entity/collection/collection-translation.entity.ts
  6. 2 0
      packages/core/src/entity/collection/collection.entity.ts
  7. 2 1
      packages/core/src/entity/country/country-translation.entity.ts
  8. 2 1
      packages/core/src/entity/facet-value/facet-value-translation.entity.ts
  9. 2 1
      packages/core/src/entity/facet-value/facet-value.entity.ts
  10. 2 1
      packages/core/src/entity/facet/facet-translation.entity.ts
  11. 2 1
      packages/core/src/entity/history-entry/customer-history-entry.entity.ts
  12. 2 1
      packages/core/src/entity/history-entry/history-entry.entity.ts
  13. 2 1
      packages/core/src/entity/history-entry/order-history-entry.entity.ts
  14. 3 1
      packages/core/src/entity/order-item/order-item.entity.ts
  15. 5 1
      packages/core/src/entity/order-line/order-line.entity.ts
  16. 12 1
      packages/core/src/entity/order-modification/order-modification.entity.ts
  17. 1 0
      packages/core/src/entity/order/order.entity.ts
  18. 2 1
      packages/core/src/entity/payment/payment.entity.ts
  19. 6 3
      packages/core/src/entity/product-option-group/product-option-group-translation.entity.ts
  20. 4 2
      packages/core/src/entity/product-option-group/product-option-group.entity.ts
  21. 6 3
      packages/core/src/entity/product-option/product-option-translation.entity.ts
  22. 2 1
      packages/core/src/entity/product-option/product-option.entity.ts
  23. 2 1
      packages/core/src/entity/product-variant/product-variant-price.entity.ts
  24. 6 3
      packages/core/src/entity/product-variant/product-variant-translation.entity.ts
  25. 6 2
      packages/core/src/entity/product-variant/product-variant.entity.ts
  26. 1 0
      packages/core/src/entity/product/product-translation.entity.ts
  27. 4 2
      packages/core/src/entity/product/product.entity.ts
  28. 2 1
      packages/core/src/entity/refund/refund.entity.ts
  29. 1 0
      packages/core/src/entity/session/authenticated-session.entity.ts
  30. 2 0
      packages/core/src/entity/session/session.entity.ts
  31. 3 1
      packages/core/src/entity/shipping-line/shipping-line.entity.ts
  32. 4 2
      packages/core/src/entity/shipping-method/shipping-method-translation.entity.ts
  33. 2 1
      packages/core/src/entity/stock-movement/allocation.entity.ts
  34. 2 1
      packages/core/src/entity/stock-movement/cancellation.entity.ts
  35. 2 1
      packages/core/src/entity/stock-movement/release.entity.ts
  36. 2 1
      packages/core/src/entity/stock-movement/sale.entity.ts
  37. 2 1
      packages/core/src/entity/stock-movement/stock-movement.entity.ts
  38. 3 1
      packages/core/src/entity/surcharge/surcharge.entity.ts
  39. 4 1
      packages/core/src/entity/tax-rate/tax-rate.entity.ts

+ 3 - 1
packages/core/src/entity/address/address.entity.ts

@@ -1,5 +1,5 @@
 import { DeepPartial } from '@vendure/common/lib/shared-types';
-import { Column, Entity, ManyToOne } from 'typeorm';
+import { Column, Entity, Index, ManyToOne } from 'typeorm';
 
 import { HasCustomFields } from '../../config/custom-field/custom-field-types';
 import { VendureEntity } from '../base/base.entity';
@@ -19,6 +19,7 @@ export class Address extends VendureEntity implements HasCustomFields {
         super(input);
     }
 
+    @Index()
     @ManyToOne(type => Customer, customer => customer.addresses)
     customer: Customer;
 
@@ -39,6 +40,7 @@ export class Address extends VendureEntity implements HasCustomFields {
 
     @Column({ default: '' }) postalCode: string;
 
+    @Index()
     @ManyToOne(type => Country)
     country: Country;
 

+ 2 - 1
packages/core/src/entity/asset/orderable-asset.entity.ts

@@ -1,5 +1,5 @@
 import { DeepPartial, ID } from '@vendure/common/lib/shared-types';
-import { Column, ManyToOne } from 'typeorm';
+import { Column, Index, ManyToOne } from 'typeorm';
 
 import { Orderable } from '../../common/types/common-types';
 import { Asset } from '../asset/asset.entity';
@@ -23,6 +23,7 @@ export abstract class OrderableAsset extends VendureEntity implements Orderable
     @Column()
     assetId: ID;
 
+    @Index()
     @ManyToOne(type => Asset, { eager: true, onDelete: 'CASCADE' })
     asset: Asset;
 

+ 2 - 1
packages/core/src/entity/authentication-method/authentication-method.entity.ts

@@ -1,4 +1,4 @@
-import { Entity, ManyToOne, TableInheritance } from 'typeorm';
+import { Entity, Index, ManyToOne, TableInheritance } from 'typeorm';
 
 import { VendureEntity } from '../base/base.entity';
 import { User } from '../user/user.entity';
@@ -14,6 +14,7 @@ import { User } from '../user/user.entity';
 @Entity()
 @TableInheritance({ column: { type: 'varchar', name: 'type' } })
 export abstract class AuthenticationMethod extends VendureEntity {
+    @Index()
     @ManyToOne(type => User, user => user.authenticationMethods)
     user: User;
 }

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

@@ -1,6 +1,6 @@
 import { CurrencyCode, LanguageCode } from '@vendure/common/lib/generated-types';
 import { DeepPartial } from '@vendure/common/lib/shared-types';
-import { Column, Entity, ManyToOne } from 'typeorm';
+import { Column, Entity, Index, ManyToOne } from 'typeorm';
 
 import { VendureEntity } from '../base/base.entity';
 import { CustomChannelFields } from '../custom-entity-fields';
@@ -30,9 +30,11 @@ export class Channel extends VendureEntity {
 
     @Column('varchar') defaultLanguageCode: LanguageCode;
 
+    @Index()
     @ManyToOne(type => Zone)
     defaultTaxZone: Zone;
 
+    @Index()
     @ManyToOne(type => Zone)
     defaultShippingZone: Zone;
 

+ 3 - 6
packages/core/src/entity/collection/collection-translation.entity.ts

@@ -1,6 +1,6 @@
 import { LanguageCode } from '@vendure/common/lib/generated-types';
 import { DeepPartial } from '@vendure/common/lib/shared-types';
-import { Column, Entity, ManyToOne } from 'typeorm';
+import { Column, Entity, Index, ManyToOne } from 'typeorm';
 
 import { Translation } from '../../common/types/locale-types';
 import { HasCustomFields } from '../../config/custom-field/custom-field-types';
@@ -23,11 +23,8 @@ export class CollectionTranslation extends VendureEntity implements Translation<
 
     @Column('text') description: string;
 
-    @ManyToOne(
-        type => Collection,
-        base => base.translations,
-        { onDelete: 'CASCADE' },
-    )
+    @Index()
+    @ManyToOne(type => Collection, base => base.translations, { onDelete: 'CASCADE' })
     base: Collection;
 
     @Column(type => CustomCollectionFieldsTranslation)

+ 2 - 0
packages/core/src/entity/collection/collection.entity.ts

@@ -3,6 +3,7 @@ import { DeepPartial } from '@vendure/common/lib/shared-types';
 import {
     Column,
     Entity,
+    Index,
     JoinTable,
     ManyToMany,
     ManyToOne,
@@ -58,6 +59,7 @@ export class Collection
     @OneToMany(type => CollectionTranslation, translation => translation.base, { eager: true })
     translations: Array<Translation<Collection>>;
 
+    @Index()
     @ManyToOne(type => Asset, { onDelete: 'SET NULL' })
     featuredAsset: Asset;
 

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

@@ -1,6 +1,6 @@
 import { LanguageCode } from '@vendure/common/lib/generated-types';
 import { DeepPartial } from '@vendure/common/lib/shared-types';
-import { Column, Entity, ManyToOne } from 'typeorm';
+import { Column, Entity, Index, ManyToOne } from 'typeorm';
 
 import { Translation } from '../../common/types/locale-types';
 import { HasCustomFields } from '../../config/custom-field/custom-field-types';
@@ -19,6 +19,7 @@ export class CountryTranslation extends VendureEntity implements Translation<Cou
 
     @Column() name: string;
 
+    @Index()
     @ManyToOne(type => Country, base => base.translations, { onDelete: 'CASCADE' })
     base: Country;
 

+ 2 - 1
packages/core/src/entity/facet-value/facet-value-translation.entity.ts

@@ -1,6 +1,6 @@
 import { LanguageCode } from '@vendure/common/lib/generated-types';
 import { DeepPartial } from '@vendure/common/lib/shared-types';
-import { Column, Entity, ManyToOne } from 'typeorm';
+import { Column, Entity, Index, ManyToOne } from 'typeorm';
 
 import { Translation } from '../../common/types/locale-types';
 import { VendureEntity } from '../base/base.entity';
@@ -18,6 +18,7 @@ export class FacetValueTranslation extends VendureEntity implements Translation<
 
     @Column() name: string;
 
+    @Index()
     @ManyToOne(type => FacetValue, base => base.translations, { onDelete: 'CASCADE' })
     base: FacetValue;
 

+ 2 - 1
packages/core/src/entity/facet-value/facet-value.entity.ts

@@ -1,5 +1,5 @@
 import { DeepPartial } from '@vendure/common/lib/shared-types';
-import { Column, Entity, JoinTable, ManyToMany, ManyToOne, OneToMany } from 'typeorm';
+import { Column, Entity, Index, JoinTable, ManyToMany, ManyToOne, OneToMany } from 'typeorm';
 
 import { ChannelAware } from '../../common/types/common-types';
 import { LocaleString, Translatable, Translation } from '../../common/types/locale-types';
@@ -29,6 +29,7 @@ export class FacetValue extends VendureEntity implements Translatable, HasCustom
     @OneToMany(type => FacetValueTranslation, translation => translation.base, { eager: true })
     translations: Array<Translation<FacetValue>>;
 
+    @Index()
     @ManyToOne(type => Facet, group => group.values, { onDelete: 'CASCADE' })
     facet: Facet;
 

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

@@ -1,6 +1,6 @@
 import { LanguageCode } from '@vendure/common/lib/generated-types';
 import { DeepPartial } from '@vendure/common/lib/shared-types';
-import { Column, Entity, ManyToOne } from 'typeorm';
+import { Column, Entity, Index, ManyToOne } from 'typeorm';
 
 import { Translation } from '../../common/types/locale-types';
 import { HasCustomFields } from '../../config/custom-field/custom-field-types';
@@ -19,6 +19,7 @@ export class FacetTranslation extends VendureEntity implements Translation<Facet
 
     @Column() name: string;
 
+    @Index()
     @ManyToOne(type => Facet, base => base.translations, { onDelete: 'CASCADE' })
     base: Facet;
 

+ 2 - 1
packages/core/src/entity/history-entry/customer-history-entry.entity.ts

@@ -1,5 +1,5 @@
 import { DeepPartial } from '@vendure/common/lib/shared-types';
-import { ChildEntity, ManyToOne } from 'typeorm';
+import { ChildEntity, Index, ManyToOne } from 'typeorm';
 
 import { Customer } from '../customer/customer.entity';
 
@@ -17,6 +17,7 @@ export class CustomerHistoryEntry extends HistoryEntry {
         super(input);
     }
 
+    @Index()
     @ManyToOne(type => Customer, { onDelete: 'CASCADE' })
     customer: Customer;
 }

+ 2 - 1
packages/core/src/entity/history-entry/history-entry.entity.ts

@@ -1,5 +1,5 @@
 import { HistoryEntryType } from '@vendure/common/lib/generated-types';
-import { Column, Entity, ManyToOne, TableInheritance } from 'typeorm';
+import { Column, Entity, Index, ManyToOne, TableInheritance } from 'typeorm';
 
 import { Administrator } from '../administrator/administrator.entity';
 import { VendureEntity } from '../base/base.entity';
@@ -14,6 +14,7 @@ import { VendureEntity } from '../base/base.entity';
 @Entity()
 @TableInheritance({ column: { type: 'varchar', name: 'discriminator' } })
 export abstract class HistoryEntry extends VendureEntity {
+    @Index()
     @ManyToOne(type => Administrator)
     administrator?: Administrator;
 

+ 2 - 1
packages/core/src/entity/history-entry/order-history-entry.entity.ts

@@ -1,5 +1,5 @@
 import { DeepPartial } from '@vendure/common/lib/shared-types';
-import { ChildEntity, ManyToOne } from 'typeorm';
+import { ChildEntity, Index, ManyToOne } from 'typeorm';
 
 import { Order } from '../order/order.entity';
 
@@ -17,6 +17,7 @@ export class OrderHistoryEntry extends HistoryEntry {
         super(input);
     }
 
+    @Index()
     @ManyToOne(type => Order, { onDelete: 'CASCADE' })
     order: Order;
 }

+ 3 - 1
packages/core/src/entity/order-item/order-item.entity.ts

@@ -1,7 +1,7 @@
 import { Adjustment, AdjustmentType, TaxLine } from '@vendure/common/lib/generated-types';
 import { DeepPartial, ID } from '@vendure/common/lib/shared-types';
 import { summate } from '@vendure/common/lib/shared-utils';
-import { Column, Entity, JoinTable, ManyToMany, ManyToOne, OneToOne } from 'typeorm';
+import { Column, Entity, Index, JoinTable, ManyToMany, ManyToOne, OneToOne } from 'typeorm';
 
 import { Calculated } from '../../common/calculated-decorator';
 import { grossPriceOf, netPriceOf } from '../../common/tax-utils';
@@ -24,6 +24,7 @@ export class OrderItem extends VendureEntity {
         super(input);
     }
 
+    @Index()
     @ManyToOne(type => OrderLine, line => line.items, { onDelete: 'CASCADE' })
     line: OrderLine;
 
@@ -65,6 +66,7 @@ export class OrderItem extends VendureEntity {
     @JoinTable()
     fulfillments: Fulfillment[];
 
+    @Index()
     @ManyToOne(type => Refund)
     refund: Refund;
 

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

@@ -1,7 +1,7 @@
 import { Adjustment, AdjustmentType, Discount, TaxLine } from '@vendure/common/lib/generated-types';
 import { DeepPartial } from '@vendure/common/lib/shared-types';
 import { summate } from '@vendure/common/lib/shared-utils';
-import { Column, Entity, ManyToOne, OneToMany } from 'typeorm';
+import { Column, Entity, Index, ManyToOne, OneToMany } from 'typeorm';
 
 import { Calculated } from '../../common/calculated-decorator';
 import { grossPriceOf, netPriceOf } from '../../common/tax-utils';
@@ -26,18 +26,22 @@ export class OrderLine extends VendureEntity implements HasCustomFields {
         super(input);
     }
 
+    @Index()
     @ManyToOne(type => ProductVariant)
     productVariant: ProductVariant;
 
+    @Index()
     @ManyToOne(type => TaxCategory)
     taxCategory: TaxCategory;
 
+    @Index()
     @ManyToOne(type => Asset)
     featuredAsset: Asset;
 
     @OneToMany(type => OrderItem, item => item.line)
     items: OrderItem[];
 
+    @Index()
     @ManyToOne(type => Order, order => order.lines, { onDelete: 'CASCADE' })
     order: Order;
 

+ 12 - 1
packages/core/src/entity/order-modification/order-modification.entity.ts

@@ -1,6 +1,16 @@
 import { Adjustment, OrderAddress } from '@vendure/common/lib/generated-types';
 import { DeepPartial } from '@vendure/common/lib/shared-types';
-import { Column, Entity, JoinColumn, JoinTable, ManyToMany, ManyToOne, OneToMany, OneToOne } from 'typeorm';
+import {
+    Column,
+    Entity,
+    Index,
+    JoinColumn,
+    JoinTable,
+    ManyToMany,
+    ManyToOne,
+    OneToMany,
+    OneToOne,
+} from 'typeorm';
 
 import { Calculated } from '../../common/calculated-decorator';
 import { VendureEntity } from '../base/base.entity';
@@ -27,6 +37,7 @@ export class OrderModification extends VendureEntity {
     @Column()
     note: string;
 
+    @Index()
     @ManyToOne(type => Order, order => order.modifications, { onDelete: 'CASCADE' })
     order: Order;
 

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

@@ -76,6 +76,7 @@ export class Order extends VendureEntity implements ChannelAware, HasCustomField
     @Column({ nullable: true })
     orderPlacedAt?: Date;
 
+    @Index()
     @ManyToOne(type => Customer)
     customer?: Customer;
 

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

@@ -1,5 +1,5 @@
 import { DeepPartial } from '@vendure/common/lib/shared-types';
-import { Column, Entity, ManyToOne, OneToMany } from 'typeorm';
+import { Column, Entity, Index, ManyToOne, OneToMany } from 'typeorm';
 
 import { PaymentMetadata } from '../../common/types/common-types';
 import { PaymentState } from '../../service/helpers/payment-state-machine/payment-state';
@@ -34,6 +34,7 @@ export class Payment extends VendureEntity {
 
     @Column('simple-json') metadata: PaymentMetadata;
 
+    @Index()
     @ManyToOne(type => Order, order => order.payments)
     order: Order;
 

+ 6 - 3
packages/core/src/entity/product-option-group/product-option-group-translation.entity.ts

@@ -1,6 +1,6 @@
 import { LanguageCode } from '@vendure/common/lib/generated-types';
 import { DeepPartial } from '@vendure/common/lib/shared-types';
-import { Column, Entity, ManyToOne } from 'typeorm';
+import { Column, Entity, Index, ManyToOne } from 'typeorm';
 
 import { Translation } from '../../common/types/locale-types';
 import { HasCustomFields } from '../../config/custom-field/custom-field-types';
@@ -10,8 +10,10 @@ import { CustomProductOptionGroupFieldsTranslation } from '../custom-entity-fiel
 import { ProductOptionGroup } from './product-option-group.entity';
 
 @Entity()
-export class ProductOptionGroupTranslation extends VendureEntity
-    implements Translation<ProductOptionGroup>, HasCustomFields {
+export class ProductOptionGroupTranslation
+    extends VendureEntity
+    implements Translation<ProductOptionGroup>, HasCustomFields
+{
     constructor(input?: DeepPartial<Translation<ProductOptionGroup>>) {
         super(input);
     }
@@ -20,6 +22,7 @@ export class ProductOptionGroupTranslation extends VendureEntity
 
     @Column() name: string;
 
+    @Index()
     @ManyToOne(type => ProductOptionGroup, base => base.translations)
     base: ProductOptionGroup;
 

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

@@ -1,5 +1,5 @@
 import { DeepPartial } from '@vendure/common/lib/shared-types';
-import { Column, Entity, ManyToOne, OneToMany } from 'typeorm';
+import { Column, Entity, Index, ManyToOne, OneToMany } from 'typeorm';
 
 import { SoftDeletable } from '../../common/types/common-types';
 import { LocaleString, Translatable, Translation } from '../../common/types/locale-types';
@@ -20,7 +20,8 @@ import { ProductOptionGroupTranslation } from './product-option-group-translatio
 @Entity()
 export class ProductOptionGroup
     extends VendureEntity
-    implements Translatable, HasCustomFields, SoftDeletable {
+    implements Translatable, HasCustomFields, SoftDeletable
+{
     constructor(input?: DeepPartial<ProductOptionGroup>) {
         super(input);
     }
@@ -38,6 +39,7 @@ export class ProductOptionGroup
     @OneToMany(type => ProductOption, option => option.group)
     options: ProductOption[];
 
+    @Index()
     @ManyToOne(type => Product)
     product: Product;
 

+ 6 - 3
packages/core/src/entity/product-option/product-option-translation.entity.ts

@@ -1,6 +1,6 @@
 import { LanguageCode } from '@vendure/common/lib/generated-types';
 import { DeepPartial } from '@vendure/common/lib/shared-types';
-import { Column, Entity, ManyToOne } from 'typeorm';
+import { Column, Entity, Index, ManyToOne } from 'typeorm';
 
 import { Translation } from '../../common/types/locale-types';
 import { HasCustomFields } from '../../config/custom-field/custom-field-types';
@@ -10,8 +10,10 @@ import { CustomProductOptionFieldsTranslation } from '../custom-entity-fields';
 import { ProductOption } from './product-option.entity';
 
 @Entity()
-export class ProductOptionTranslation extends VendureEntity
-    implements Translation<ProductOption>, HasCustomFields {
+export class ProductOptionTranslation
+    extends VendureEntity
+    implements Translation<ProductOption>, HasCustomFields
+{
     constructor(input?: DeepPartial<Translation<ProductOption>>) {
         super(input);
     }
@@ -20,6 +22,7 @@ export class ProductOptionTranslation extends VendureEntity
 
     @Column() name: string;
 
+    @Index()
     @ManyToOne(type => ProductOption, base => base.translations)
     base: ProductOption;
 

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

@@ -1,5 +1,5 @@
 import { DeepPartial, ID } from '@vendure/common/lib/shared-types';
-import { Column, Entity, ManyToOne, OneToMany } from 'typeorm';
+import { Column, Entity, Index, ManyToOne, OneToMany } from 'typeorm';
 
 import { SoftDeletable } from '../../common/types/common-types';
 import { LocaleString, Translatable, Translation } from '../../common/types/locale-types';
@@ -32,6 +32,7 @@ export class ProductOption extends VendureEntity implements Translatable, HasCus
     @OneToMany(type => ProductOptionTranslation, translation => translation.base, { eager: true })
     translations: Array<Translation<ProductOption>>;
 
+    @Index()
     @ManyToOne(type => ProductOptionGroup, group => group.options)
     group: ProductOptionGroup;
 

+ 2 - 1
packages/core/src/entity/product-variant/product-variant-price.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 { VendureEntity } from '../base/base.entity';
 import { EntityId } from '../entity-id.decorator';
@@ -23,6 +23,7 @@ export class ProductVariantPrice extends VendureEntity {
 
     @EntityId() channelId: ID;
 
+    @Index()
     @ManyToOne(type => ProductVariant, variant => variant.productVariantPrices)
     variant: ProductVariant;
 }

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

@@ -1,6 +1,6 @@
 import { LanguageCode } from '@vendure/common/lib/generated-types';
 import { DeepPartial } from '@vendure/common/lib/shared-types';
-import { Column, Entity, ManyToOne } from 'typeorm';
+import { Column, Entity, Index, ManyToOne } from 'typeorm';
 
 import { Translation } from '../../common/types/locale-types';
 import { HasCustomFields } from '../../config/custom-field/custom-field-types';
@@ -10,8 +10,10 @@ import { CustomProductVariantFieldsTranslation } from '../custom-entity-fields';
 import { ProductVariant } from './product-variant.entity';
 
 @Entity()
-export class ProductVariantTranslation extends VendureEntity
-    implements Translation<ProductVariant>, HasCustomFields {
+export class ProductVariantTranslation
+    extends VendureEntity
+    implements Translation<ProductVariant>, HasCustomFields
+{
     constructor(input?: DeepPartial<Translation<ProductVariant>>) {
         super(input);
     }
@@ -20,6 +22,7 @@ export class ProductVariantTranslation extends VendureEntity
 
     @Column() name: string;
 
+    @Index()
     @ManyToOne(type => ProductVariant, base => base.translations)
     base: ProductVariant;
 

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

@@ -1,6 +1,6 @@
 import { CurrencyCode, GlobalFlag } from '@vendure/common/lib/generated-types';
 import { DeepPartial, ID } from '@vendure/common/lib/shared-types';
-import { Column, Entity, JoinTable, ManyToMany, ManyToOne, OneToMany } from 'typeorm';
+import { Column, Entity, Index, JoinTable, ManyToMany, ManyToOne, OneToMany } from 'typeorm';
 
 import { Calculated } from '../../common/calculated-decorator';
 import { ChannelAware, SoftDeletable } from '../../common/types/common-types';
@@ -35,7 +35,8 @@ import { ProductVariantTranslation } from './product-variant-translation.entity'
 @Entity()
 export class ProductVariant
     extends VendureEntity
-    implements Translatable, HasCustomFields, SoftDeletable, ChannelAware {
+    implements Translatable, HasCustomFields, SoftDeletable, ChannelAware
+{
     constructor(input?: DeepPartial<ProductVariant>) {
         super(input);
     }
@@ -95,6 +96,7 @@ export class ProductVariant
      */
     taxRateApplied: TaxRate;
 
+    @Index()
     @ManyToOne(type => Asset, { onDelete: 'SET NULL' })
     featuredAsset: Asset;
 
@@ -103,6 +105,7 @@ export class ProductVariant
     })
     assets: ProductVariantAsset[];
 
+    @Index()
     @ManyToOne(type => TaxCategory)
     taxCategory: TaxCategory;
 
@@ -112,6 +115,7 @@ export class ProductVariant
     @OneToMany(type => ProductVariantTranslation, translation => translation.base, { eager: true })
     translations: Array<Translation<ProductVariant>>;
 
+    @Index()
     @ManyToOne(type => Product, product => product.variants)
     product: Product;
 

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

@@ -23,6 +23,7 @@ export class ProductTranslation extends VendureEntity implements Translation<Pro
 
     @Column('text') description: string;
 
+    @Index()
     @ManyToOne(type => Product, base => base.translations)
     base: Product;
 

+ 4 - 2
packages/core/src/entity/product/product.entity.ts

@@ -1,5 +1,5 @@
 import { DeepPartial } from '@vendure/common/lib/shared-types';
-import { Column, Entity, JoinTable, ManyToMany, ManyToOne, OneToMany } from 'typeorm';
+import { Column, Entity, Index, JoinTable, ManyToMany, ManyToOne, OneToMany } from 'typeorm';
 
 import { ChannelAware, SoftDeletable } from '../../common/types/common-types';
 import { LocaleString, Translatable, Translation } from '../../common/types/locale-types';
@@ -25,7 +25,8 @@ import { ProductTranslation } from './product-translation.entity';
 @Entity()
 export class Product
     extends VendureEntity
-    implements Translatable, HasCustomFields, ChannelAware, SoftDeletable {
+    implements Translatable, HasCustomFields, ChannelAware, SoftDeletable
+{
     constructor(input?: DeepPartial<Product>) {
         super(input);
     }
@@ -42,6 +43,7 @@ export class Product
     @Column({ default: true })
     enabled: boolean;
 
+    @Index()
     @ManyToOne(type => Asset, { onDelete: 'SET NULL' })
     featuredAsset: Asset;
 

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

@@ -1,5 +1,5 @@
 import { DeepPartial, ID } from '@vendure/common/lib/shared-types';
-import { Column, Entity, JoinColumn, JoinTable, ManyToOne, OneToMany } from 'typeorm';
+import { Column, Entity, Index, JoinColumn, JoinTable, ManyToOne, OneToMany } from 'typeorm';
 
 import { PaymentMetadata } from '../../common/types/common-types';
 import { RefundState } from '../../service/helpers/refund-state-machine/refund-state';
@@ -34,6 +34,7 @@ export class Refund extends VendureEntity {
     @JoinTable()
     orderItems: OrderItem[];
 
+    @Index()
     @ManyToOne(type => Payment)
     @JoinColumn()
     payment: Payment;

+ 1 - 0
packages/core/src/entity/session/authenticated-session.entity.ts

@@ -21,6 +21,7 @@ export class AuthenticatedSession extends Session {
      * @description
      * The {@link User} who has authenticated to create this session.
      */
+    @Index()
     @ManyToOne(type => User)
     user: User;
 

+ 2 - 0
packages/core/src/entity/session/session.entity.ts

@@ -29,12 +29,14 @@ export abstract class Session extends VendureEntity {
     @EntityId({ nullable: true })
     activeOrderId?: ID;
 
+    @Index()
     @ManyToOne(type => Order)
     activeOrder: Order | null;
 
     @EntityId({ nullable: true })
     activeChannelId?: ID;
 
+    @Index()
     @ManyToOne(type => Channel)
     activeChannel: Channel | null;
 }

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

@@ -1,7 +1,7 @@
 import { Adjustment, AdjustmentType, Discount, TaxLine } from '@vendure/common/lib/generated-types';
 import { DeepPartial, ID } from '@vendure/common/lib/shared-types';
 import { summate } from '@vendure/common/lib/shared-utils';
-import { Column, Entity, ManyToOne } from 'typeorm';
+import { Column, Entity, Index, ManyToOne } from 'typeorm';
 
 import { Calculated } from '../../common/calculated-decorator';
 import { grossPriceOf, netPriceOf } from '../../common/tax-utils';
@@ -19,10 +19,12 @@ export class ShippingLine extends VendureEntity {
     @EntityId()
     shippingMethodId: ID | null;
 
+    @Index()
     @ManyToOne(type => ShippingMethod)
     shippingMethod: ShippingMethod | null;
 
     // TODO: v2 - Add `{ onDelete: 'CASCADE' }` constraint
+    @Index()
     @ManyToOne(type => Order, order => order.shippingLines)
     order: Order;
 

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

@@ -1,6 +1,6 @@
 import { LanguageCode } from '@vendure/common/lib/generated-types';
 import { DeepPartial } from '@vendure/common/lib/shared-types';
-import { Column, Entity, ManyToOne } from 'typeorm';
+import { Column, Entity, Index, ManyToOne } from 'typeorm';
 
 import { Translation } from '../../common/types/locale-types';
 import { HasCustomFields } from '../../config/custom-field/custom-field-types';
@@ -13,7 +13,8 @@ import { ShippingMethod } from './shipping-method.entity';
 @Entity()
 export class ShippingMethodTranslation
     extends VendureEntity
-    implements Translation<ShippingMethod>, HasCustomFields {
+    implements Translation<ShippingMethod>, HasCustomFields
+{
     constructor(input?: DeepPartial<Translation<Product>>) {
         super(input);
     }
@@ -24,6 +25,7 @@ export class ShippingMethodTranslation
 
     @Column({ default: '' }) description: string;
 
+    @Index()
     @ManyToOne(type => ShippingMethod, base => base.translations)
     base: ShippingMethod;
 

+ 2 - 1
packages/core/src/entity/stock-movement/allocation.entity.ts

@@ -1,6 +1,6 @@
 import { StockMovementType } from '@vendure/common/lib/generated-types';
 import { DeepPartial } from '@vendure/common/lib/shared-types';
-import { ChildEntity, ManyToOne } from 'typeorm';
+import { ChildEntity, Index, ManyToOne } from 'typeorm';
 
 import { OrderLine } from '../order-line/order-line.entity';
 
@@ -22,6 +22,7 @@ export class Allocation extends StockMovement {
         super(input);
     }
 
+    @Index()
     @ManyToOne(type => OrderLine)
     orderLine: OrderLine;
 }

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

@@ -1,6 +1,6 @@
 import { StockMovementType } from '@vendure/common/lib/generated-types';
 import { DeepPartial } from '@vendure/common/lib/shared-types';
-import { ChildEntity, ManyToOne } from 'typeorm';
+import { ChildEntity, Index, ManyToOne } from 'typeorm';
 
 import { OrderItem } from '../order-item/order-item.entity';
 
@@ -21,6 +21,7 @@ export class Cancellation extends StockMovement {
         super(input);
     }
 
+    // @Index() omitted as it would conflict with the orderItemId index from the Release entity
     @ManyToOne(type => OrderItem, orderItem => orderItem.cancellation)
     orderItem: OrderItem;
 }

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

@@ -1,6 +1,6 @@
 import { StockMovementType } from '@vendure/common/lib/generated-types';
 import { DeepPartial } from '@vendure/common/lib/shared-types';
-import { ChildEntity, ManyToOne } from 'typeorm';
+import { ChildEntity, Index, ManyToOne } from 'typeorm';
 
 import { OrderItem } from '../order-item/order-item.entity';
 
@@ -22,6 +22,7 @@ export class Release extends StockMovement {
         super(input);
     }
 
+    @Index()
     @ManyToOne(type => OrderItem)
     orderItem: OrderItem;
 }

+ 2 - 1
packages/core/src/entity/stock-movement/sale.entity.ts

@@ -1,6 +1,6 @@
 import { StockMovementType } from '@vendure/common/lib/generated-types';
 import { DeepPartial } from '@vendure/common/lib/shared-types';
-import { ChildEntity, ManyToOne } from 'typeorm';
+import { ChildEntity, Index, ManyToOne } from 'typeorm';
 
 import { OrderLine } from '../order-line/order-line.entity';
 
@@ -21,6 +21,7 @@ export class Sale extends StockMovement {
         super(input);
     }
 
+    // @Index() omitted as it would conflict with the orderLineId index from the Allocation entity
     @ManyToOne(type => OrderLine)
     orderLine: OrderLine;
 }

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

@@ -1,5 +1,5 @@
 import { StockMovementType } from '@vendure/common/lib/generated-types';
-import { Column, Entity, ManyToOne, TableInheritance } from 'typeorm';
+import { Column, Entity, Index, ManyToOne, TableInheritance } from 'typeorm';
 
 import { VendureEntity } from '../base/base.entity';
 import { ProductVariant } from '../product-variant/product-variant.entity';
@@ -19,6 +19,7 @@ export abstract class StockMovement extends VendureEntity {
     @Column({ nullable: false, type: 'varchar' })
     readonly type: StockMovementType;
 
+    @Index()
     @ManyToOne(type => ProductVariant, variant => variant.stockMovements)
     productVariant: ProductVariant;
 

+ 3 - 1
packages/core/src/entity/surcharge/surcharge.entity.ts

@@ -1,7 +1,7 @@
 import { TaxLine } from '@vendure/common/lib/generated-types';
 import { DeepPartial } from '@vendure/common/lib/shared-types';
 import { summate } from '@vendure/common/lib/shared-utils';
-import { Column, Entity, ManyToOne } from 'typeorm';
+import { Column, Entity, Index, ManyToOne } from 'typeorm';
 
 import { Calculated } from '../../common/calculated-decorator';
 import { grossPriceOf, netPriceOf } from '../../common/tax-utils';
@@ -37,9 +37,11 @@ export class Surcharge extends VendureEntity {
     @Column('simple-json')
     taxLines: TaxLine[];
 
+    @Index()
     @ManyToOne(type => Order, order => order.surcharges, { onDelete: 'CASCADE' })
     order: Order;
 
+    @Index()
     @ManyToOne(type => OrderModification, orderModification => orderModification.surcharges)
     orderModification: OrderModification;
 

+ 4 - 1
packages/core/src/entity/tax-rate/tax-rate.entity.ts

@@ -1,6 +1,6 @@
 import { TaxLine } from '@vendure/common/lib/generated-types';
 import { DeepPartial } from '@vendure/common/lib/shared-types';
-import { Column, Entity, ManyToOne } from 'typeorm';
+import { Column, Entity, Index, ManyToOne } from 'typeorm';
 
 import { grossPriceOf, netPriceOf, taxComponentOf, taxPayableOn } from '../../common/tax-utils';
 import { idsAreEqual } from '../../common/utils';
@@ -34,12 +34,15 @@ export class TaxRate extends VendureEntity implements HasCustomFields {
 
     @Column({ type: 'decimal', precision: 5, scale: 2, transformer: new DecimalTransformer() }) value: number;
 
+    @Index()
     @ManyToOne(type => TaxCategory)
     category: TaxCategory;
 
+    @Index()
     @ManyToOne(type => Zone)
     zone: Zone;
 
+    @Index()
     @ManyToOne(type => CustomerGroup, { nullable: true })
     customerGroup?: CustomerGroup;