1

Does TypeORM require the "default" connection to be created? I created a named connection using the snippet below:

  await createConnection({
    name: 'usercontext',
    type: 'postgres',
    url: PGSQL_DATABASE_URL!,
    synchronize: true,
    ssl: {
      rejectUnauthorized: false
    },
    entities
  });

When I try to do an <Entity>.findOne, the program throws a Connection "default" not found error. My entities are registered on the connection creation. When I remove the "name" variable in the connection configuration, I get the expected result. I need to use named connections since I'm developing with multiple databases. I am assuming since I included the entities in the connection configuration then there is no need to set invoke the "useConnection" function of the entity.

Oneb
  • 145
  • 1
  • 12
  • See [connection](https://typeorm.io/#/connection). ```createConnections(...)``` creates multiple connections. Different ```connections``` **must have different names. By default, if connection name is not specified it's equal to default.** Once you created a **connection you can obtain it** anywhere from your app, using ```getConnection(...)``` function. ```getConnection()``` returns **default** connection. ```getConnection("usercontext")``` returns the **usercontext** connection. Later you can use the *connection* variable as always. – Carlo Corradini Jul 20 '21 at 15:54

1 Answers1

0

Your entities are what looks like a collection but you have to set it equal to the entity object.

For example:

const entities = ['entity1', 'entity3']

 
await createConnection({
  name: 'usercontext',
  type: 'postgres',
  url: PGSQL_DATABASE_URL!,
  synchronize: true,
  ssl: {
    rejectUnauthorized: false
  },
  entities: entities,
}); 

The entities property on the connection object is undefined in your example. I am also not sure what the "PGSQL_DATABASE_URL!" is, maybe just some free text, but you might be seeing some unexpected results with the "!" at the end of the definition.

Dylan Wright
  • 1,118
  • 12
  • 19
  • The default connection is just that too, the default connection. Post connection a collection of connections is created to pull from dynamically. If you don't set what the connection is named, it will take on default. If you're getting this error it means you haven't established a connection. – Dylan Wright Jul 19 '21 at 19:45
  • *The entities property on the connection object is undefined in your example*: This is **not** true, since **ES6** introduced [**Shorthand Syntax for Object Property Value**](https://www.geeksforgeeks.org/shorthand-syntax-for-object-property-value-in-es6). – Carlo Corradini Jul 20 '21 at 16:00
  • *I am also not sure what the"PGSQL_DATABASE_URL!" is, maybe just some free text, but you might be seeing some unexpected results with the "!" at the end of the definition.*: **PGSQL_DATABASE_URL** is the name of the connection URL. The **!** is Typescript's (**non-null assertion operator**)(https://stackoverflow.com/questions/42273853/in-typescript-what-is-the-exclamation-mark-bang-operator-when-dereferenci) – Carlo Corradini Jul 20 '21 at 16:00
  • Thanks - @CarloCorradini, however, that's a strong assumption considering the rest of the object is being defined pre-ES6. And you don't see the defined array of entities. I would like to point that out as it looks like no entities are being injected. There is also the SSL property that seems to be defined for a reason. Could be the SSL is wrong. Bottom line more details are needed. – Dylan Wright Jul 21 '21 at 21:13