How to use the typeorm.createConnection function in typeorm

To help you get started, we’ve selected a few typeorm examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github bradymholt / koa-vuejs-template / api / db / Initializer.ts View on Github external
static async init(
    connectionOptions: PostgresConnectionOptions,
    seed: boolean = false
  ) {
    // Get the options and clone to a new object since node-config gives a read-only object and TypeORM attempts to modify it.
    let options: any = Object.assign({}, connectionOptions);
    // Prepend absolute path to entities/migrations items
    options.entities = options.entities.map(item => {
      return `${__dirname}/../${item}`;
    });
    options.migrations = options.migrations.map(item => {
      return `${__dirname}/../${item}`;
    });

    try {
      let connection = await createDbConnection(options as ConnectionOptions);
      if (seed) {
        console.log("Seeding the database...");
        await this.seedData();
      }
    } catch (err) {
      console.log(`Error initializing the database: ${err}`);
      throw err;
    }
  }
github ShipChain / engine / RunMigrations.ts View on Github external
const defaultOptions = await getConnectionOptions();
            const rdsOptions = await getRDSconfig();

            const fullOptions = Object.assign(
                {},
                defaultOptions,
                rdsOptions,
                {
                    subscribers: [],
                    synchronize: false,
                    migrationsRun: false,
                    dropSchema: false,
                    logging: ["query", "error", "schema"]
                });

            connection = await createConnection(fullOptions);

            const options = { transaction: true };
            await connection.runMigrations(options);
            await connection.close();

            // exit process if no errors
            process.exit(0);

        } catch (err) {
            if (connection) await (connection as Connection).close();

            logger.error(`Error during migration run: ${err}`);
            process.exit(1);
        }
    }
}
github birkir / prime / packages / prime-core / src / utils / connect.ts View on Github external
export const connect = (url = process.env.DATABASE_URL) => {
  const ssl = Boolean(
    String(url).indexOf('ssl=true') >= 0 || String(url).indexOf('amazonaws.com') >= 0
  );
  pg.defaults.ssl = ssl;

  return createConnection({
    type: 'postgres',
    url,
    entities: [
      ...require('@accounts/typeorm').entities,
      path.join(__dirname, '..', 'entities', '*.ts'),
      path.join(__dirname, '..', 'entities', '*.js'),
    ],
    ssl,
    synchronize: true,
    logger: 'debug',
  }).then(connection => {
    const driver = connection.driver as PostgresDriver;

    // Fixes postgres timezone bug
    if (driver.postgres) {
      driver.postgres.defaults.parseInputDatesAsUTC = true;
github MichalLytek / type-graphql / examples / typeorm-lazy-relations / index.ts View on Github external
async function bootstrap() {
  try {
    // create TypeORM connection
    await TypeORM.createConnection({
      type: "mysql",
      database: "type-graphql",
      username: "root", // fill this with your username
      password: "qwerty123", // and password
      port: 3306,
      host: "localhost",
      entities: [Recipe, Rate, User],
      synchronize: true,
      logger: "advanced-console",
      logging: "all",
      dropSchema: true,
      cache: true,
    });

    // seed database with some data
    const { defaultUser } = await seedDatabase();
github bitwit / typescript-express-api-template / api / server.ts View on Github external
export async function startServer(): Promise {
    try {
        connection = await createConnection(postGresConfig)
        console.log("db connection established")
    }
    catch (err) { console.log("connection error", err) }
    httpServer.listen(3000, () => console.log('Example app listening on port 3000!'))

    return connection
}
github velopert / velog-server / src / search / cli.ts View on Github external
async function initialize() {
  try {
    await createConnection();
    console.log('Postgres RDBMS connection is established');
  } catch (e) {
    console.log(e);
  }
}
github wix / quix / quix-frontend / service / src / main.ts View on Github external
      const conn = await retry(() => createConnection(conf))
        .forNtimes(5)
github fullstack-build / fullstack-one / packages / db / lib / index.ts View on Github external
private async runMigrations(): Promise {
    try {
      this.logger.debug("db.orm.migrations.start");
      const connection = await typeorm.createConnection({
        ...this.config,
        type: "postgres",
        synchronize: false,
        name: "migration",
        migrations: this.migrations,
        migrationsTableName: "_meta.migrations"
      });
      const queryRunner = (await connection.createQueryRunner()) as PostgresQueryRunner;
      await queryRunner.connect();
      await queryRunner.createSchema("_meta", true);
      await this.recreateGraphqlSchema(queryRunner);
      await queryRunner.release();
      await connection.runMigrations();
      await connection.close();
      this.logger.debug("db.orm.migrations.end");
    } catch (err) {
github typeorm / javascript-example / src / app2-es5-json-schemas / index.js View on Github external
var typeorm = require("typeorm");
var EntitySchema = typeorm.EntitySchema;

typeorm.createConnection({
    type: "mysql",
    host: "localhost",
    port: 3306,
    username: "test",
    password: "test",
    database: "test",
    synchronize: true,
    entities: [
        new EntitySchema(require("./entity/post.json")),
        new EntitySchema(require("./entity/category.json")),
    ]
}).then(function (connection) {

    var category1 = {
        name: "TypeScript"
    };
github serranoarevalo / nuber / server / src / index.ts View on Github external
const appOptions: Options = {
  port: PORT,
  subscriptions: {
    path: SUBSCRIPTIONS_ENDPOINT,
    onConnect: async connectionParams => {
      const user = await getUserFromToken(connectionParams["X-JWT"]);
      return {
        currentUser: user
      };
    }
  },
  playground: PLAYGROUND_ENDPOINT,
  endpoint: GRAPHQL_ENDPOINT
};

createConnection().then(() => {
  app.start(appOptions, handleListening);
});

app.express.on("error", handleAppError);