Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
(async () => {
const timeStart = Date.now();
if (ids.length) {
const batches = Math.ceil(ids.length / batchSize);
Logger.verbose(`Updating ${ids.length} variants...`);
let variantsInProduct: ProductVariant[] = [];
for (let i = 0; i < batches; i++) {
const begin = i * batchSize;
const end = begin + batchSize;
Logger.verbose(`Updating ids from index ${begin} to ${end}`);
const batchIds = ids.slice(begin, end);
const variants = await this.getVariantsByIds(ctx, batchIds);
const variantsToIndex: Array> = [];
const productsToIndex: Array> = [];
// tslint:disable-next-line:prefer-for-of
for (let j = 0; j < variants.length; j++) {
const variant = variants[j];
variantsInProduct.push(variant);
variantsToIndex.push({ update: { _id: variant.id.toString() } });
variantsToIndex.push({ doc: this.createVariantIndexItem(variant) });
const nextVariant = variants[j + 1];
if (nextVariant && nextVariant.productId !== variant.productId) {
(async () => {
const timeStart = Date.now();
const qb = this.getSearchIndexQueryBuilder();
const count = await qb.where('variants__product.deletedAt IS NULL').getCount();
Logger.verbose(`Reindexing ${count} ProductVariants`, loggerCtx);
const batches = Math.ceil(count / batchSize);
let variantsInProduct: ProductVariant[] = [];
for (let i = 0; i < batches; i++) {
Logger.verbose(`Processing batch ${i + 1} of ${batches}`, loggerCtx);
const variants = await this.getBatch(ctx, qb, i);
Logger.verbose(`ProductVariants count: ${variants.length}`);
const variantsToIndex: Array = [];
const productsToIndex: Array = [];
// tslint:disable-next-line:prefer-for-of
for (let j = 0; j < variants.length; j++) {
const variant = variants[j];
private async deleteIndices(prefix: string) {
try {
const index = prefix + VARIANT_INDEX_NAME;
await this.client.indices.delete({ index });
Logger.verbose(`Deleted index "${index}"`, loggerCtx);
} catch (e) {
Logger.error(e, loggerCtx);
}
try {
const index = prefix + PRODUCT_INDEX_NAME;
await this.client.indices.delete({ index });
Logger.verbose(`Deleted index "${index}"`, loggerCtx);
} catch (e) {
Logger.error(e, loggerCtx);
}
}
async compileAdminUiApp(extensions: AdminUiExtension[] | undefined): Promise {
const compiledAppExists = fs.existsSync(path.join(UI_PATH, 'index.html'));
const extensionsWithId = this.normalizeExtensions(extensions);
if (!compiledAppExists || this.extensionModulesHaveChanged(extensionsWithId)) {
Logger.info('Compiling Admin UI with extensions...', loggerCtx);
await compileAdminUiApp(path.join(__dirname, '../admin-ui'), extensionsWithId);
Logger.info('Completed compilation!', loggerCtx);
} else {
Logger.verbose('Extensions not changed since last run', loggerCtx);
}
}
const createIndex = async (indexName: string) => {
const index = indexPrefix + indexName;
const result = await this.client.indices.exists({ index });
if (result.body === false) {
Logger.verbose(`Index "${index}" does not exist. Creating...`, loggerCtx);
await this.createIndices(indexPrefix);
} else {
Logger.verbose(`Index "${index}" exists`, loggerCtx);
}
};
private async deleteIndices(prefix: string) {
try {
const index = prefix + VARIANT_INDEX_NAME;
await this.client.indices.delete({ index });
Logger.verbose(`Deleted index "${index}"`, loggerCtx);
} catch (e) {
Logger.error(e, loggerCtx);
}
try {
const index = prefix + PRODUCT_INDEX_NAME;
await this.client.indices.delete({ index });
Logger.verbose(`Deleted index "${index}"`, loggerCtx);
} catch (e) {
Logger.error(e, loggerCtx);
}
}
{ doc: updatedProductIndexItem, doc_as_upsert: true },
];
await this.executeBulkOperations(PRODUCT_INDEX_NAME, PRODUCT_INDEX_TYPE, operations);
}
if (removedVariantIds.length) {
const operations = removedVariantIds.reduce(
(ops, id) => {
Logger.verbose(`Deleting 1 ProductVariant (${id})`, loggerCtx);
return [...ops, { delete: { _id: id.toString() } }];
},
[] as BulkOperation[],
);
await this.executeBulkOperations(VARIANT_INDEX_NAME, VARIANT_INDEX_TYPE, operations);
}
if (removedProductId) {
Logger.verbose(`Deleting 1 Product (${removedProductId})`, loggerCtx);
const operations: BulkOperation[] = [{ delete: { _id: removedProductId.toString() } }];
await this.executeBulkOperations(PRODUCT_INDEX_NAME, PRODUCT_INDEX_TYPE, operations);
}
}
relations: ['variants'],
});
if (product) {
if (product.deletedAt) {
removedProductId = productId;
removedVariantIds = product.variants.map(v => v.id);
} else {
updatedProductVariants = await this.connection
.getRepository(ProductVariant)
.findByIds(product.variants.map(v => v.id), {
relations: variantRelations,
});
}
}
if (updatedProductVariants.length) {
Logger.verbose(`Updating 1 Product (${productId})`, loggerCtx);
updatedProductVariants = this.hydrateVariants(ctx, updatedProductVariants);
const updatedProductIndexItem = this.createProductIndexItem(updatedProductVariants);
const operations: [BulkOperation, BulkOperationDoc] = [
{ update: { _id: updatedProductIndexItem.productId.toString() } },
{ doc: updatedProductIndexItem, doc_as_upsert: true },
];
await this.executeBulkOperations(PRODUCT_INDEX_NAME, PRODUCT_INDEX_TYPE, operations);
}
if (removedVariantIds.length) {
const operations = removedVariantIds.reduce(
(ops, id) => {
Logger.verbose(`Deleting 1 ProductVariant (${id})`, loggerCtx);
return [...ops, { delete: { _id: id.toString() } }];
},
[] as BulkOperation[],
);
`Some errors occurred running bulk operations on ${indexType}! Set logger to "debug" to print all errors.`,
loggerCtx,
);
body.items.forEach(item => {
if (item.index) {
Logger.debug(JSON.stringify(item.index.error, null, 2), loggerCtx);
}
if (item.update) {
Logger.debug(JSON.stringify(item.update.error, null, 2), loggerCtx);
}
if (item.delete) {
Logger.debug(JSON.stringify(item.delete.error, null, 2), loggerCtx);
}
});
} else {
Logger.verbose(`Executed ${body.items.length} bulk operations on index [${fullIndexName}]`);
}
return body;
} catch (e) {
Logger.error(`Error when attempting to run bulk operations [${e.toString()}]`, loggerCtx);
Logger.error('Error details: ' + JSON.stringify(e.body.error, null, 2), loggerCtx);
}
}