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.

Typeorm singleton with serverless-offline

See original GitHub issue

Issue type:

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

Database system/driver:

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

TypeORM version:

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

Steps to reproduce or a small repository showing the problem:

Crossposting https://github.com/dherault/serverless-offline/issues/468 hoping someone might have some insight. Using TypeOrm with the Serverless.com framework and serverless-offline to develop my lambda functions locally. On the actual deploy code, the singleton works fine but serverless-offline causes issues with this.

Since I’m using Typeorm, serverless, serverless-typescript and serverless-offline this is just trying to make all these packages work together, hoping someone has run into this before and knows the secret.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:23 (4 by maintainers)

github_iconTop GitHub Comments

13reactions
DennisKocommented, Jan 13, 2019

So I am having similiar problems. I am using this db manager class to handle connections:

import {
  Connection,
  ConnectionManager,
  ConnectionOptions,
  createConnection,
  getConnectionManager
} from "typeorm";
import User from "./entity/User";


export default class Database {
  private connectionManager: ConnectionManager;

  constructor() {
    this.connectionManager = getConnectionManager();
  }

  public async getConnection(): Promise<Connection> {
    const CONNECTION_NAME = `default`;
    try {
      let connection: Connection;
      if (this.connectionManager.has(CONNECTION_NAME)) {
        connection = await this.connectionManager.get(CONNECTION_NAME);
        if (!connection.isConnected) {
          connection = await connection.connect();
        }
      } else {
        const connectionOptions: ConnectionOptions = {
          // ...options
          entities: [User]
        };

        connection = await createConnection(connectionOptions);
      }
      return connection;
    } catch (error) {
      console.log("DB CONNECTION ERROR: ", error);
      throw new Error();
    }
  }
}

Every first DB call after serverless offline start succeeds but when I do another call I get:

{ RepositoryNotFoundError: No repository for "User" was found. Looks like this entity is not registered in current "default" connection?
    at new RepositoryNotFoundError (/home/dennis/omagram-api/src/error/RepositoryNotFoundError.ts:10:9)
    at EntityManager.getRepository (/home/dennis/omagram-api/src/entity-manager/EntityManager.ts:730:19)
    at Connection.getRepository (/home/dennis/omagram-api/src/connection/Connection.ts:325:29)
    at Function.BaseEntity.getRepository (/home/dennis/omagram-api/src/repository/BaseEntity.ts:85:27)
    at Function.BaseEntity.create (/home/dennis/omagram-api/src/repository/BaseEntity.ts:140:30)
    at Object.<anonymous> (/home/dennis/omagram-api/.webpack/service/src/webpack:/src/resolvers.ts:34:33)
    at Generator.next (<anonymous>)
    at fulfilled (/home/dennis/omagram-api/.webpack/service/src/api.js:328:58)
  name: 'RepositoryNotFoundError',
  message:
   'No repository for "User" was found. Looks like this entity is not registered in current "default" connection?' }

I think I am pretty close to solving this without any dirty hacks but how could I proceed from here?

4reactions
ymoran00commented, Oct 7, 2020

Just for future generations: skipCacheInvalidation doesn’t exist anymore in new sls offline. Instead use --allowCache.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Developers - Typeorm singleton with serverless-offline -
Coming soon: A brand new website interface for an even better experience!
Read more >
Wiring up TypeORM with Serverless | by Karim Varela - Medium
Setting up TypeORM in my Serverless project wasn't completely straightforward since the project assumes that you're running some semi-stateful ...
Read more >
Typeorm doesn't work with Serverless Framework AWS Lambda
Well, basically I get this error when I try to serverless deploy or serverless offline : PS C:\Users\joaov\Desktop\lambda-ts> serverless ...
Read more >
How to use typeDORM with NestJS and AWS Lambda
For local testing I am using serverless offline . Here is a project to demonstrate my problem: https://github.com/m-kotek/serverless-typedorm.
Read more >
Data source management | Midway - Midway.js
Singleton ) export class MySqlDataSourceManager extends DataSourceManager<mysql. ... For example, orm frameworks such as typeorm are designed based on this.
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