Skip to content

Commit

Permalink
feat(core): Add indices to many-to-one relations
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
michaelbromley committed Apr 25, 2022
1 parent 7efd2e0 commit 01e369f
Show file tree
Hide file tree
Showing 39 changed files with 118 additions and 49 deletions.
4 changes: 3 additions & 1 deletion packages/core/src/entity/address/address.entity.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -19,6 +19,7 @@ export class Address extends VendureEntity implements HasCustomFields {
super(input);
}

@Index()
@ManyToOne(type => Customer, customer => customer.addresses)
customer: Customer;

Expand All @@ -39,6 +40,7 @@ export class Address extends VendureEntity implements HasCustomFields {

@Column({ default: '' }) postalCode: string;

@Index()
@ManyToOne(type => Country)
country: Country;

Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/entity/asset/orderable-asset.entity.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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;

Expand Down
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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;
}
4 changes: 3 additions & 1 deletion packages/core/src/entity/channel/channel.entity.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/entity/collection/collection.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { DeepPartial } from '@vendure/common/lib/shared-types';
import {
Column,
Entity,
Index,
JoinTable,
ManyToMany,
ManyToOne,
Expand Down Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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;

Expand Down
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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;

Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/entity/facet-value/facet-value.entity.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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;

Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/entity/facet/facet-translation.entity.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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;

Expand Down
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -17,6 +17,7 @@ export class CustomerHistoryEntry extends HistoryEntry {
super(input);
}

@Index()
@ManyToOne(type => Customer, { onDelete: 'CASCADE' })
customer: Customer;
}
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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;

Expand Down
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -17,6 +17,7 @@ export class OrderHistoryEntry extends HistoryEntry {
super(input);
}

@Index()
@ManyToOne(type => Order, { onDelete: 'CASCADE' })
order: Order;
}
4 changes: 3 additions & 1 deletion packages/core/src/entity/order-item/order-item.entity.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -24,6 +24,7 @@ export class OrderItem extends VendureEntity {
super(input);
}

@Index()
@ManyToOne(type => OrderLine, line => line.items, { onDelete: 'CASCADE' })
line: OrderLine;

Expand Down Expand Up @@ -65,6 +66,7 @@ export class OrderItem extends VendureEntity {
@JoinTable()
fulfillments: Fulfillment[];

@Index()
@ManyToOne(type => Refund)
refund: Refund;

Expand Down
6 changes: 5 additions & 1 deletion packages/core/src/entity/order-line/order-line.entity.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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;

Expand Down
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -27,6 +37,7 @@ export class OrderModification extends VendureEntity {
@Column()
note: string;

@Index()
@ManyToOne(type => Order, order => order.modifications, { onDelete: 'CASCADE' })
order: Order;

Expand Down
1 change: 1 addition & 0 deletions packages/core/src/entity/order/order.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export class Order extends VendureEntity implements ChannelAware, HasCustomField
@Column({ nullable: true })
orderPlacedAt?: Date;

@Index()
@ManyToOne(type => Customer)
customer?: Customer;

Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/entity/payment/payment.entity.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -34,6 +34,7 @@ export class Payment extends VendureEntity {

@Column('simple-json') metadata: PaymentMetadata;

@Index()
@ManyToOne(type => Order, order => order.payments)
order: Order;

Expand Down
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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);
}
Expand All @@ -20,6 +22,7 @@ export class ProductOptionGroupTranslation extends VendureEntity

@Column() name: string;

@Index()
@ManyToOne(type => ProductOptionGroup, base => base.translations)
base: ProductOptionGroup;

Expand Down
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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);
}
Expand All @@ -38,6 +39,7 @@ export class ProductOptionGroup
@OneToMany(type => ProductOption, option => option.group)
options: ProductOption[];

@Index()
@ManyToOne(type => Product)
product: Product;

Expand Down
Loading

0 comments on commit 01e369f

Please sign in to comment.