Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

QUESTION. UpdateOrInsertOneAsync #28

Open
scharada opened this issue Jan 11, 2024 · 1 comment
Open

QUESTION. UpdateOrInsertOneAsync #28

scharada opened this issue Jan 11, 2024 · 1 comment

Comments

@scharada
Copy link

Hi John, is there an example on how to use UpdateOrInsertOneAsync ?
thanks

@johnknoop
Copy link
Owner

johnknoop commented Jan 11, 2024

Hi @scharada

In a project I'm involved in, we're using it when importing products from a webshop to a local database. When importing a product, we don't know beforehand if it already exists in the database. If it doesn't exist, we want to insert it. If it already exists, we want to know which product category it belongs to:

var upsertedProduct = await _productRepository.UpdateOrInsertOneAsync(
	filter: x => x.Id == newProduct.Id || x.Sku == newProduct.Sku,
	update: x => x
		.Set(y => y.DescriptionHtml, newProduct.DescriptionHtml)
		.Set(y => y.Sku, newProduct.Sku)
                /* set more fields */,
	entityToInsertIfNoMatch: newProduct,
	returnProjection: x => new MatchedProduct(x.Id, x.ProductCategoryId)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants