Skip to content

Instantly share code, notes, and snippets.

@marcosdeaguiar
Created November 26, 2020 18:06
Show Gist options
  • Save marcosdeaguiar/89384391686d3fcd42543af7bba6867a to your computer and use it in GitHub Desktop.
Save marcosdeaguiar/89384391686d3fcd42543af7bba6867a to your computer and use it in GitHub Desktop.
Setup TypeORM connection loop.
import { createConnection, Connection, getConnection } from "typeorm";
import { CONNECTION_STRING } from "./config";
const setupTypeORM = async () => {
let connection: any = null;
while (connection == undefined || !connection.isConnected)
{
try {
connection = await createConnection({
type: 'postgres',
url: CONNECTION_STRING,
entities: [
"src/entity/**/*.ts"
]
});
console.log("TypeORM connected.");
}
catch(err: any)
{
console.log("Fail to connect TypeORM.");
await new Promise(resolve => setTimeout(resolve, 1000));
}
}
}
export default setupTypeORM;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment