-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): Improve speed of bulk product import
- Loading branch information
1 parent
4d1c0be
commit 92abbcb
Showing
4 changed files
with
157 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
139 changes: 139 additions & 0 deletions
139
packages/core/src/data-import/providers/importer/fast-importer.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { InjectConnection } from '@nestjs/typeorm'; | ||
import { | ||
CreateProductInput, | ||
CreateProductOptionGroupInput, | ||
CreateProductOptionInput, | ||
CreateProductVariantInput, | ||
} from '@vendure/common/lib/generated-types'; | ||
import { ID } from '@vendure/common/lib/shared-types'; | ||
import { Connection } from 'typeorm'; | ||
|
||
import { Channel } from '../../../entity/channel/channel.entity'; | ||
import { ProductOptionGroupTranslation } from '../../../entity/product-option-group/product-option-group-translation.entity'; | ||
import { ProductOptionGroup } from '../../../entity/product-option-group/product-option-group.entity'; | ||
import { ProductOptionTranslation } from '../../../entity/product-option/product-option-translation.entity'; | ||
import { ProductOption } from '../../../entity/product-option/product-option.entity'; | ||
import { ProductVariantPrice } from '../../../entity/product-variant/product-variant-price.entity'; | ||
import { ProductVariantTranslation } from '../../../entity/product-variant/product-variant-translation.entity'; | ||
import { ProductVariant } from '../../../entity/product-variant/product-variant.entity'; | ||
import { ProductTranslation } from '../../../entity/product/product-translation.entity'; | ||
import { Product } from '../../../entity/product/product.entity'; | ||
import { TranslatableSaver } from '../../../service/helpers/translatable-saver/translatable-saver'; | ||
import { ChannelService } from '../../../service/services/channel.service'; | ||
import { StockMovementService } from '../../../service/services/stock-movement.service'; | ||
|
||
/** | ||
* A service to import entities into the database. This replaces the regular `create` methods of the service layer with faster | ||
* versions which skip much of the defensive checks and other DB calls which are not needed when running an import. | ||
* | ||
* In testing, the use of the FastImporterService approximately doubled the speed of bulk imports. | ||
*/ | ||
@Injectable() | ||
export class FastImporterService { | ||
private defaultChannel: Channel; | ||
constructor( | ||
@InjectConnection() private connection: Connection, | ||
private channelService: ChannelService, | ||
private stockMovementService: StockMovementService, | ||
private translatableSaver: TranslatableSaver, | ||
) {} | ||
|
||
async initialize() { | ||
this.defaultChannel = this.channelService.getDefaultChannel(); | ||
} | ||
|
||
async createProduct(input: CreateProductInput): Promise<ID> { | ||
const product = await this.translatableSaver.create({ | ||
input, | ||
entityType: Product, | ||
translationType: ProductTranslation, | ||
beforeSave: async p => { | ||
p.channels = [this.defaultChannel]; | ||
if (input.facetValueIds) { | ||
p.facetValues = input.facetValueIds.map(id => ({ id } as any)); | ||
} | ||
if (input.featuredAssetId) { | ||
p.featuredAsset = { id: input.featuredAssetId } as any; | ||
} | ||
if (input.assetIds) { | ||
p.assets = input.assetIds.map(id => ({ id } as any)); | ||
} | ||
}, | ||
}); | ||
return product.id; | ||
} | ||
|
||
async createProductOptionGroup(input: CreateProductOptionGroupInput): Promise<ID> { | ||
const group = await this.translatableSaver.create({ | ||
input, | ||
entityType: ProductOptionGroup, | ||
translationType: ProductOptionGroupTranslation, | ||
}); | ||
return group.id; | ||
} | ||
|
||
async createProductOption(input: CreateProductOptionInput): Promise<ID> { | ||
const option = await this.translatableSaver.create({ | ||
input, | ||
entityType: ProductOption, | ||
translationType: ProductOptionTranslation, | ||
beforeSave: po => (po.group = { id: input.productOptionGroupId } as any), | ||
}); | ||
return option.id; | ||
} | ||
|
||
async addOptionGroupToProduct(productId: ID, optionGroupId: ID) { | ||
await this.connection | ||
.createQueryBuilder() | ||
.relation(Product, 'optionGroups') | ||
.of(productId) | ||
.add(optionGroupId); | ||
} | ||
|
||
async createProductVariant(input: CreateProductVariantInput): Promise<ID> { | ||
if (!input.optionIds) { | ||
input.optionIds = []; | ||
} | ||
if (input.price == null) { | ||
input.price = 0; | ||
} | ||
|
||
const createdVariant = await this.translatableSaver.create({ | ||
input, | ||
entityType: ProductVariant, | ||
translationType: ProductVariantTranslation, | ||
beforeSave: async variant => { | ||
const { optionIds } = input; | ||
if (optionIds && optionIds.length) { | ||
variant.options = optionIds.map(id => ({ id } as any)); | ||
} | ||
if (input.facetValueIds) { | ||
variant.facetValues = input.facetValueIds.map(id => ({ id } as any)); | ||
} | ||
variant.product = { id: input.productId } as any; | ||
variant.taxCategory = { id: input.taxCategoryId } as any; | ||
if (input.featuredAssetId) { | ||
variant.featuredAsset = { id: input.featuredAssetId } as any; | ||
} | ||
if (input.assetIds) { | ||
variant.assets = input.assetIds.map(id => ({ id } as any)); | ||
} | ||
}, | ||
}); | ||
if (input.stockOnHand != null && input.stockOnHand !== 0) { | ||
await this.stockMovementService.adjustProductVariantStock( | ||
createdVariant.id, | ||
0, | ||
input.stockOnHand, | ||
); | ||
} | ||
const variantPrice = new ProductVariantPrice({ | ||
price: createdVariant.price, | ||
channelId: this.defaultChannel.id, | ||
}); | ||
variantPrice.variant = createdVariant; | ||
await this.connection.getRepository(ProductVariantPrice).save(variantPrice); | ||
return createdVariant.id; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters