How can I load data from secrets-manager synchronously in TypeScript

12 views Asked by At

I have the following:

{
  "extends": "./node_modules/gts/tsconfig-google.json",
  "compilerOptions": {
    "lib": ["ESNext"],
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "strictPropertyInitialization": false,
    "esModuleInterop": true,
    "outDir": "build/",
    "baseUrl": "./",
    "paths": {
      "*": ["test/*", "tests/*", "src/*"]
    },
    "skipLibCheck": true
  },
  "exclude": ["node_modules", "build"]
}
export const getDataSource = async (): Promise<DataSource> => {
  if (!dataSource) {
    dataSource = await initializeDataSource();
  }
  return dataSource;
};

const initializeDataSource = async (): Promise<DataSource> => {
  const connectionConfig = await getDbConnectionConfig();

  const dataSourceInstance = new DataSource({
    type: 'postgres',
    ...connectionConfig,
    ssl: !process.env.DB_USE_SSL || process.env.DB_USE_SSL === 'true',
    synchronize: false,
    logging: false,
    entities: [__dirname + '/../**/*.entity.{js,ts}'],
  });

  try {
    await dataSourceInstance.initialize();
  } catch (err) {
    console.error(err);
  }

  return dataSourceInstance;
};
export const start = async () => {
    const app = Fastify({
      ignoreTrailingSlash: true,
    });

    await registerRoutes(app, {});

    app.listen(PORT, '0.0.0.0', () => {
      console.log('running');
    });
};

I want to run typeOrm migrations but the await getDbConnectionConfig(); is running everything asynchronously, which makes hard/impossible so far to run the migrations.

  • npm run typeorm migration:create -- src/migrations/AddNumber works
  • npm run typeorm migration:run -- -d src/dataSource.ts does not work

I changed the dataSource to something that could work by changing tsconfig.json and package.json but in the end I get errors about the imports, which is a big change that I want to avoid.

So, I was wondering if there is a way to fetch secrets from secret manager synchronously and avoid the changes.

Any other solution is welcome.

0

There are 0 answers