question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Upsert implementation

See original GitHub issue

Is your feature request related to a problem? Please describe. A clarification on a possible feature

I’m going to create a CustomBaseRepository to implement a generic method to find or create (upsert). Can you point me to some example on how to create such a base repository? The documentation (https://mikro-orm.io/docs/repositories#custom-repository) si not very exhaustive and the links provided have no example for a custom base repository.

Alternatively, does such method exist already?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
B4nancommented, Feb 26, 2020

Its pretty simple, all what repository does is that it forwards calls to the entity manager with the entity name stored in this.entityName. But you can use the repository methods directly so it will be handled for you:

async upsert(data, where) {
  let e = await this.findOne(where);

  if (e) {
    wrap(e).assign(data);
  } else {
    e = this.create(data);
  }

  await this.persistAndFlush(e); // or maybe you do not want to flush here, depends on the use case

  return e;
}
1reaction
B4nancommented, Feb 26, 2020

Just create a generic class extending EntityRepository and pass it in the ORM config via MikroORM.init({ entityRepository: CustomBaseRepository }).

Something like this should work:

export class CustomBaseRepository<T extends AnyEntity<T>> extends EntityRepository<T> {
  upsert() { ... }
}

Alternatively, does such method exist already?

Nope.

Not sure if I understood well, do you also need help with implementation of the upsert method? Should be quite trivial.

Read more comments on GitHub >

github_iconTop Results From Across the Web

UPSERT - PostgreSQL wiki
"UPSERT" is a DBMS feature that allows a DML statement's author to atomically either insert a row, or on the basis of the...
Read more >
Upsert in SQL: What is an upsert, and when should you use ...
The UPSERT command in CockroachDB performs an upsert based on the uniqueness of the primary key column or columns, and it will perform...
Read more >
What is UPSERT and how to do it in MySQL - Linux Hint
Here we learned that UPSERT is a combination of two words Update and Insert. It works on the following principle that, if the...
Read more >
Upsert | Android Developers
The implementation of the method will insert its parameters into the database if it does not already exists (checked by primary key). If...
Read more >
Use a staging table to perform a merge (upsert)
Use a staging table to perform a merge (upsert). PDFRSS. You can efficiently update and insert new data by loading your data into...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found