From 01e369fa58ae4585eed34ff7b6d888827f7d8501 Mon Sep 17 00:00:00 2001 From: Michael Bromley Date: Mon, 25 Apr 2022 12:34:53 +0200 Subject: [PATCH] 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. --- packages/core/src/entity/address/address.entity.ts | 4 +++- .../core/src/entity/asset/orderable-asset.entity.ts | 3 ++- .../authentication-method.entity.ts | 3 ++- packages/core/src/entity/channel/channel.entity.ts | 4 +++- .../collection/collection-translation.entity.ts | 9 +++------ .../core/src/entity/collection/collection.entity.ts | 2 ++ .../entity/country/country-translation.entity.ts | 3 ++- .../facet-value/facet-value-translation.entity.ts | 3 ++- .../src/entity/facet-value/facet-value.entity.ts | 3 ++- .../src/entity/facet/facet-translation.entity.ts | 3 ++- .../history-entry/customer-history-entry.entity.ts | 3 ++- .../entity/history-entry/history-entry.entity.ts | 3 ++- .../history-entry/order-history-entry.entity.ts | 3 ++- .../core/src/entity/order-item/order-item.entity.ts | 4 +++- .../core/src/entity/order-line/order-line.entity.ts | 6 +++++- .../order-modification/order-modification.entity.ts | 13 ++++++++++++- packages/core/src/entity/order/order.entity.ts | 1 + packages/core/src/entity/payment/payment.entity.ts | 3 ++- .../product-option-group-translation.entity.ts | 9 ++++++--- .../product-option-group.entity.ts | 6 ++++-- .../product-option-translation.entity.ts | 9 ++++++--- .../entity/product-option/product-option.entity.ts | 3 ++- .../product-variant/product-variant-price.entity.ts | 3 ++- .../product-variant-translation.entity.ts | 9 ++++++--- .../product-variant/product-variant.entity.ts | 8 ++++++-- .../entity/product/product-translation.entity.ts | 1 + packages/core/src/entity/product/product.entity.ts | 6 ++++-- packages/core/src/entity/refund/refund.entity.ts | 3 ++- .../entity/session/authenticated-session.entity.ts | 1 + packages/core/src/entity/session/session.entity.ts | 2 ++ .../entity/shipping-line/shipping-line.entity.ts | 4 +++- .../shipping-method-translation.entity.ts | 6 ++++-- .../src/entity/stock-movement/allocation.entity.ts | 3 ++- .../entity/stock-movement/cancellation.entity.ts | 3 ++- .../src/entity/stock-movement/release.entity.ts | 3 ++- .../core/src/entity/stock-movement/sale.entity.ts | 3 ++- .../entity/stock-movement/stock-movement.entity.ts | 3 ++- .../core/src/entity/surcharge/surcharge.entity.ts | 4 +++- .../core/src/entity/tax-rate/tax-rate.entity.ts | 5 ++++- 39 files changed, 118 insertions(+), 49 deletions(-) diff --git a/packages/core/src/entity/address/address.entity.ts b/packages/core/src/entity/address/address.entity.ts index 2b8ee46718..bae9bac16b 100644 --- a/packages/core/src/entity/address/address.entity.ts +++ b/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; diff --git a/packages/core/src/entity/asset/orderable-asset.entity.ts b/packages/core/src/entity/asset/orderable-asset.entity.ts index 301e41dc3e..507385f49a 100644 --- a/packages/core/src/entity/asset/orderable-asset.entity.ts +++ b/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; diff --git a/packages/core/src/entity/authentication-method/authentication-method.entity.ts b/packages/core/src/entity/authentication-method/authentication-method.entity.ts index 2d49ac9078..c65a1ebcb8 100644 --- a/packages/core/src/entity/authentication-method/authentication-method.entity.ts +++ b/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; } diff --git a/packages/core/src/entity/channel/channel.entity.ts b/packages/core/src/entity/channel/channel.entity.ts index 91113cf981..6524cf7a2d 100644 --- a/packages/core/src/entity/channel/channel.entity.ts +++ b/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; diff --git a/packages/core/src/entity/collection/collection-translation.entity.ts b/packages/core/src/entity/collection/collection-translation.entity.ts index bd1993ef51..486d6b5be6 100644 --- a/packages/core/src/entity/collection/collection-translation.entity.ts +++ b/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) diff --git a/packages/core/src/entity/collection/collection.entity.ts b/packages/core/src/entity/collection/collection.entity.ts index 50e9e771aa..77815451e9 100644 --- a/packages/core/src/entity/collection/collection.entity.ts +++ b/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>; + @Index() @ManyToOne(type => Asset, { onDelete: 'SET NULL' }) featuredAsset: Asset; diff --git a/packages/core/src/entity/country/country-translation.entity.ts b/packages/core/src/entity/country/country-translation.entity.ts index a57b55f9ab..b2612e656d 100644 --- a/packages/core/src/entity/country/country-translation.entity.ts +++ b/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 Country, base => base.translations, { onDelete: 'CASCADE' }) base: Country; diff --git a/packages/core/src/entity/facet-value/facet-value-translation.entity.ts b/packages/core/src/entity/facet-value/facet-value-translation.entity.ts index 9d8dd35628..09b98c6b86 100644 --- a/packages/core/src/entity/facet-value/facet-value-translation.entity.ts +++ b/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; diff --git a/packages/core/src/entity/facet-value/facet-value.entity.ts b/packages/core/src/entity/facet-value/facet-value.entity.ts index 5358742fc2..41fc9daa64 100644 --- a/packages/core/src/entity/facet-value/facet-value.entity.ts +++ b/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>; + @Index() @ManyToOne(type => Facet, group => group.values, { onDelete: 'CASCADE' }) facet: Facet; diff --git a/packages/core/src/entity/facet/facet-translation.entity.ts b/packages/core/src/entity/facet/facet-translation.entity.ts index f16c893f3d..8d76f8f0a2 100644 --- a/packages/core/src/entity/facet/facet-translation.entity.ts +++ b/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, base => base.translations, { onDelete: 'CASCADE' }) base: Facet; diff --git a/packages/core/src/entity/history-entry/customer-history-entry.entity.ts b/packages/core/src/entity/history-entry/customer-history-entry.entity.ts index 696263756f..ffdc147b5c 100644 --- a/packages/core/src/entity/history-entry/customer-history-entry.entity.ts +++ b/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; } diff --git a/packages/core/src/entity/history-entry/history-entry.entity.ts b/packages/core/src/entity/history-entry/history-entry.entity.ts index dd4c1a6a08..5ae7d2ebbe 100644 --- a/packages/core/src/entity/history-entry/history-entry.entity.ts +++ b/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; diff --git a/packages/core/src/entity/history-entry/order-history-entry.entity.ts b/packages/core/src/entity/history-entry/order-history-entry.entity.ts index 5f7dc9f730..56aa0be2b4 100644 --- a/packages/core/src/entity/history-entry/order-history-entry.entity.ts +++ b/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; } diff --git a/packages/core/src/entity/order-item/order-item.entity.ts b/packages/core/src/entity/order-item/order-item.entity.ts index b615321e41..c30865b2a3 100644 --- a/packages/core/src/entity/order-item/order-item.entity.ts +++ b/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; diff --git a/packages/core/src/entity/order-line/order-line.entity.ts b/packages/core/src/entity/order-line/order-line.entity.ts index 084e22137f..8c4cf990e8 100644 --- a/packages/core/src/entity/order-line/order-line.entity.ts +++ b/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; diff --git a/packages/core/src/entity/order-modification/order-modification.entity.ts b/packages/core/src/entity/order-modification/order-modification.entity.ts index 0276b27b0b..17142cf460 100644 --- a/packages/core/src/entity/order-modification/order-modification.entity.ts +++ b/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; diff --git a/packages/core/src/entity/order/order.entity.ts b/packages/core/src/entity/order/order.entity.ts index 2e0954eade..b86cdd2547 100644 --- a/packages/core/src/entity/order/order.entity.ts +++ b/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; diff --git a/packages/core/src/entity/payment/payment.entity.ts b/packages/core/src/entity/payment/payment.entity.ts index f4d12668ee..9ca2fdd159 100644 --- a/packages/core/src/entity/payment/payment.entity.ts +++ b/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; diff --git a/packages/core/src/entity/product-option-group/product-option-group-translation.entity.ts b/packages/core/src/entity/product-option-group/product-option-group-translation.entity.ts index d98f61269f..7046ccfd99 100644 --- a/packages/core/src/entity/product-option-group/product-option-group-translation.entity.ts +++ b/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, HasCustomFields { +export class ProductOptionGroupTranslation + extends VendureEntity + implements Translation, HasCustomFields +{ constructor(input?: DeepPartial>) { super(input); } @@ -20,6 +22,7 @@ export class ProductOptionGroupTranslation extends VendureEntity @Column() name: string; + @Index() @ManyToOne(type => ProductOptionGroup, base => base.translations) base: ProductOptionGroup; diff --git a/packages/core/src/entity/product-option-group/product-option-group.entity.ts b/packages/core/src/entity/product-option-group/product-option-group.entity.ts index 99665f16cf..b4d7535e9b 100644 --- a/packages/core/src/entity/product-option-group/product-option-group.entity.ts +++ b/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) { super(input); } @@ -38,6 +39,7 @@ export class ProductOptionGroup @OneToMany(type => ProductOption, option => option.group) options: ProductOption[]; + @Index() @ManyToOne(type => Product) product: Product; diff --git a/packages/core/src/entity/product-option/product-option-translation.entity.ts b/packages/core/src/entity/product-option/product-option-translation.entity.ts index 04290d147b..44189c342c 100644 --- a/packages/core/src/entity/product-option/product-option-translation.entity.ts +++ b/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, HasCustomFields { +export class ProductOptionTranslation + extends VendureEntity + implements Translation, HasCustomFields +{ constructor(input?: DeepPartial>) { super(input); } @@ -20,6 +22,7 @@ export class ProductOptionTranslation extends VendureEntity @Column() name: string; + @Index() @ManyToOne(type => ProductOption, base => base.translations) base: ProductOption; diff --git a/packages/core/src/entity/product-option/product-option.entity.ts b/packages/core/src/entity/product-option/product-option.entity.ts index 5f2939d43e..ccc570dd5a 100644 --- a/packages/core/src/entity/product-option/product-option.entity.ts +++ b/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>; + @Index() @ManyToOne(type => ProductOptionGroup, group => group.options) group: ProductOptionGroup; diff --git a/packages/core/src/entity/product-variant/product-variant-price.entity.ts b/packages/core/src/entity/product-variant/product-variant-price.entity.ts index caeac7efd2..b1c3e8492c 100644 --- a/packages/core/src/entity/product-variant/product-variant-price.entity.ts +++ b/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; } diff --git a/packages/core/src/entity/product-variant/product-variant-translation.entity.ts b/packages/core/src/entity/product-variant/product-variant-translation.entity.ts index 7d768b2e28..1751ba12d9 100644 --- a/packages/core/src/entity/product-variant/product-variant-translation.entity.ts +++ b/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, HasCustomFields { +export class ProductVariantTranslation + extends VendureEntity + implements Translation, HasCustomFields +{ constructor(input?: DeepPartial>) { super(input); } @@ -20,6 +22,7 @@ export class ProductVariantTranslation extends VendureEntity @Column() name: string; + @Index() @ManyToOne(type => ProductVariant, base => base.translations) base: ProductVariant; diff --git a/packages/core/src/entity/product-variant/product-variant.entity.ts b/packages/core/src/entity/product-variant/product-variant.entity.ts index cff09dec1b..5ac4eb1ebf 100644 --- a/packages/core/src/entity/product-variant/product-variant.entity.ts +++ b/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) { 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>; + @Index() @ManyToOne(type => Product, product => product.variants) product: Product; diff --git a/packages/core/src/entity/product/product-translation.entity.ts b/packages/core/src/entity/product/product-translation.entity.ts index d7beedbfb2..d48c03aed4 100644 --- a/packages/core/src/entity/product/product-translation.entity.ts +++ b/packages/core/src/entity/product/product-translation.entity.ts @@ -23,6 +23,7 @@ export class ProductTranslation extends VendureEntity implements Translation Product, base => base.translations) base: Product; diff --git a/packages/core/src/entity/product/product.entity.ts b/packages/core/src/entity/product/product.entity.ts index 7a490d3375..2197e89cac 100644 --- a/packages/core/src/entity/product/product.entity.ts +++ b/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) { super(input); } @@ -42,6 +43,7 @@ export class Product @Column({ default: true }) enabled: boolean; + @Index() @ManyToOne(type => Asset, { onDelete: 'SET NULL' }) featuredAsset: Asset; diff --git a/packages/core/src/entity/refund/refund.entity.ts b/packages/core/src/entity/refund/refund.entity.ts index 4043d1bd26..0a8905f2ac 100644 --- a/packages/core/src/entity/refund/refund.entity.ts +++ b/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; diff --git a/packages/core/src/entity/session/authenticated-session.entity.ts b/packages/core/src/entity/session/authenticated-session.entity.ts index cfb9d6e02e..cd386de544 100644 --- a/packages/core/src/entity/session/authenticated-session.entity.ts +++ b/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; diff --git a/packages/core/src/entity/session/session.entity.ts b/packages/core/src/entity/session/session.entity.ts index 9954f2a55c..dbc65e8f65 100644 --- a/packages/core/src/entity/session/session.entity.ts +++ b/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; } diff --git a/packages/core/src/entity/shipping-line/shipping-line.entity.ts b/packages/core/src/entity/shipping-line/shipping-line.entity.ts index 2c57cdcaa4..3d44f1c8b0 100644 --- a/packages/core/src/entity/shipping-line/shipping-line.entity.ts +++ b/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; diff --git a/packages/core/src/entity/shipping-method/shipping-method-translation.entity.ts b/packages/core/src/entity/shipping-method/shipping-method-translation.entity.ts index 963847047b..942ec184a0 100644 --- a/packages/core/src/entity/shipping-method/shipping-method-translation.entity.ts +++ b/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, HasCustomFields { + implements Translation, HasCustomFields +{ constructor(input?: DeepPartial>) { super(input); } @@ -24,6 +25,7 @@ export class ShippingMethodTranslation @Column({ default: '' }) description: string; + @Index() @ManyToOne(type => ShippingMethod, base => base.translations) base: ShippingMethod; diff --git a/packages/core/src/entity/stock-movement/allocation.entity.ts b/packages/core/src/entity/stock-movement/allocation.entity.ts index 27a533c4fd..39311ea65a 100644 --- a/packages/core/src/entity/stock-movement/allocation.entity.ts +++ b/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; } diff --git a/packages/core/src/entity/stock-movement/cancellation.entity.ts b/packages/core/src/entity/stock-movement/cancellation.entity.ts index ff4de3fc59..e6d68b2310 100644 --- a/packages/core/src/entity/stock-movement/cancellation.entity.ts +++ b/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; } diff --git a/packages/core/src/entity/stock-movement/release.entity.ts b/packages/core/src/entity/stock-movement/release.entity.ts index 2c1d10a003..ef9c381b82 100644 --- a/packages/core/src/entity/stock-movement/release.entity.ts +++ b/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; } diff --git a/packages/core/src/entity/stock-movement/sale.entity.ts b/packages/core/src/entity/stock-movement/sale.entity.ts index 67b33b8e3e..b05b182bed 100644 --- a/packages/core/src/entity/stock-movement/sale.entity.ts +++ b/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; } diff --git a/packages/core/src/entity/stock-movement/stock-movement.entity.ts b/packages/core/src/entity/stock-movement/stock-movement.entity.ts index de5b792d15..b503df6083 100644 --- a/packages/core/src/entity/stock-movement/stock-movement.entity.ts +++ b/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; diff --git a/packages/core/src/entity/surcharge/surcharge.entity.ts b/packages/core/src/entity/surcharge/surcharge.entity.ts index ff463a11a5..87ea1fa37c 100644 --- a/packages/core/src/entity/surcharge/surcharge.entity.ts +++ b/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; diff --git a/packages/core/src/entity/tax-rate/tax-rate.entity.ts b/packages/core/src/entity/tax-rate/tax-rate.entity.ts index b55edaec24..2d9889356a 100644 --- a/packages/core/src/entity/tax-rate/tax-rate.entity.ts +++ b/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;