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.

How can I extend Repository in a generic way?

See original GitHub issue

Issue type:

[*] question [ ] bug report [ ] feature request [ ] documentation issue

Database system/driver:

[ ] cordova [ ] mongodb [ ] mssql [ ] mysql / mariadb [ ] oracle [*] postgres [ ] sqlite [ ] sqljs [ ] react-native

TypeORM version:

[ ] latest [ ] @next [0.1.21] 0.x.x (or put your version here)

Hi I’m using typeorm with routing-controllers to create a rest endpoint. I want to exend base repository class to have a method named saveAndReturn() which first calls a save() and the returns findOneById() to use for Put and Post request to send the newly created or updated object with default values in place. because I need this for all my repositories I can not use current approaches mentioned in here cause I need it to be generic. How can I do it? thanks

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:18 (2 by maintainers)

github_iconTop GitHub Comments

58reactions
pleerockcommented, Jun 5, 2018

ORM provides you all functionality you need. You can organize classes whatever way you want, it does not depend on ORM. I even was thinking to drop custom repositories support since its one of the opinionated way how to organize your code. You can create hundred of others, orm provides all you need, everything else is up to you. If you need something that manages more then one entity then create that class and don’t use custom repositories functionality. Repository is one repository for each entity in orm context. You probably need something else that you can create.

4reactions
w11p3333commented, Jan 19, 2021

my way

generic repository:

import 'reflect-metadata';
import { Repository, Connection, ObjectType } from 'typeorm';

export class BaseRepository<T> extends Repository<T> {
  private connection: Connection;
  private entity: ObjectType<T>;

  constructor(entity: ObjectType<T>, connection: Connection) {
    super();
    this.entity = entity;
    this.connection = connection;
    Object.assign(this, {
      manager: connection.manager,
      metadata: connection.getMetadata(entity),
      queryRunner: connection.manager.queryRunner,
    });
  }

  test() {
    console.log('repo test', this.entity, this.connection);
  }

}

use:

import { getConnectionManager } from 'typeorm';
import { MyEntity } from '../entity';
import { BaseRepository } from '../repository';

const repo = new BaseRepository(MyEntity, getConnectionManager().get('default'));
repo.test();
repo.findOne(options);
Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - How should I extend a generic repository - Stack Overflow
Here is the solution to my problem, I need to inherit the base repository if I want to add a custom method for...
Read more >
Composite Repositories - Extend your Spring Data JPA ...
A Compose Repository provides an easy way to customize your Spring Data JPA Repository using fragment interfaces and custom implementations.
Read more >
1. Working with Spring Data Repositories
Spring Data repositories easily allow you to provide custom repository code and integrate it with generic CRUD abstraction and query method functionality.
Read more >
Spring Data JPA - Add Method in All Repositories - Baeldung
A practical guide to adding custom methods to all repositories in Spring Data JPA.
Read more >
Patterns — Generic Repository with Typescript and Node.js
The same way that you have a power, you have dangerous implicit codes, an clean example for that is: You have two Entity...
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