Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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;
}
}
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);
}
}
}
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;
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();
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
}
async function initialize() {
try {
await createConnection();
console.log('Postgres RDBMS connection is established');
} catch (e) {
console.log(e);
}
}
const conn = await retry(() => createConnection(conf))
.forNtimes(5)
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) {
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"
};
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);