0

I have built a test app using nestjs + Sequelize ORM + docker database (as of now local). As per documentation, I am using umzug library and AWS Lambda SAM template and triggering lambda handler. Below is the code for it. Connection Pooling is implemented to reuse existing sequelize connection. Below is the lambdaEntry.ts file where I trigger umzug.up() function. It is triggering but not migrating files.

When done from command prompt node migrate up it works correctly. I am testing using sam invoke command to test it.

  require('ts-node/register');
  import { Server } from 'http';
  import { NestFactory } from '@nestjs/core';
  import { Context } from 'aws-lambda';
  import * as serverlessExpress from 'aws-serverless-express';
  import * as express from 'express';
  import { ExpressAdapter } from '@nestjs/platform-express';
  import { eventContext } from 'aws-serverless-express/middleware';
  import { AppModule } from './app.module';
  import sharedBootstrap from './sharedBootstrap';
  const { Sequelize } = require('sequelize');
  const { Umzug, SequelizeStorage } = require('umzug');
  import configuration from '.././config/config';
  const fs = require('fs');

  let lambdaProxy: Server;
  let sequelize = null;

  async function bootstrap() {
   const expressServer = express();
   const nestApp = await NestFactory.create(
                   AppModule,
                   new ExpressAdapter(expressServer),
                  );
    nestApp.use(eventContext());
    sharedBootstrap(nestApp);
    await nestApp.init();
    return serverlessExpress.createServer(expressServer);
   }
   export const handler = (event: any, context: Context) => {
    if (!lambdaProxy) {
       bootstrap().then((server) => {
       lambdaProxy = server;
       serverlessExpress.proxy(lambdaProxy, event, context);
       (async () => {
        if (!sequelize) {
          console.log('New connection::');
           sequelize = await loadSequelize();
        } else {
           sequelize.connectionManager.initPools();
           if (sequelize.connectionManager.hasOwnProperty('getConnection')) {
             delete sequelize.connectionManager.getConnection;
           }
         }

         try {
        console.log('MIGRATOR::');
        const umzug = new Umzug({
          migrations: { glob: 'src/migrations/*.ts' },
          context: sequelize.getQueryInterface(),
          storage: new SequelizeStorage({ sequelize }),
          logger: console,
        });
        await umzug
          .pending()
          .then((migrations: any) => {
            console.log('pending ?  : ', JSON.stringify(migrations));

            //test for file exists.
            for (const migration of migrations) {
              try {
                if (fs.existsSync(migration.path)) {
                  console.log('file exists');
                }
              } catch (err) {
                console.log('file does not exists');
                console.error(err);
              }
            }
            async () => {
              //BELOW FUNCTION IS TRIGGERING BUT NOT GETTING MIGRATION LOADED.
              await umzug.up();
            };
          })
          .catch((e: any) => console.log('error2 ? ', e));
      } finally {
        await sequelize.connectionManager.close();
      }
    })();
  });
  } else {
  serverlessExpress.proxy(lambdaProxy, event, context);
 }
};

async function loadSequelize() {
 const sequelize = new Sequelize(
  configuration.database,
  configuration.username,
  configuration.password,
  {
    dialect: 'mysql',
    host: configuration.host,
    port: Number(configuration.port),
    pool: {
      max: 2,
      min: 0,
      idle: 0,
      acquire: 3000,
      evict: 600,
     },
   },
 );
 await sequelize.authenticate();
 return sequelize;
}

1 Answer 1

0

I am able to solve the issue after lot of tries. I seperated out the sequelize connection code and called it from app side and triggered from lambdaentry

lambdaEntry.js file.

async function bootstrap(uuid = null) {
  console.log('Calling bootstrap');
  const expressServer = express();
  const nestApp = await NestFactory.create(
    AppModule,
    new ExpressAdapter(expressServer),
  );
  nestApp.use(eventContext());
  sharedBootstrap(nestApp);
  await nestApp.init();

  try {
    // Write a function in Service (ex: purhaslistservice) and trigger the service with umzug up from here.
    const migrateResult1 =  await nestApp.get(PurchaseListService).migrate('down');
    console.log(migrateResult1);
    const migrateResult2 =  await nestApp.get(PurchaseListService).migrate('up');
    console.log(migrateResult2);
  } catch (err) {
    throw err;
  }
  return serverlessExpress.createServer(expressServer);
}

export const handler = (event: any, context: Context) => {
   if (!lambdaProxy) {
    bootstrap(uuid).then((server) => {
      lambdaProxy = server;
      serverlessExpress.proxy(lambdaProxy, event, context);
    });
  } else {
    serverlessExpress.proxy(lambdaProxy, event, context);
  }
};

/code/src/purchaselist/purchaselist.service.ts

async migrate(id: string): Promise<any> {
    console.log('migrate script triggered', id);
    const sequelize = PurchaseListItem.sequelize;
    const umzug = new Umzug({
      migrations: { glob: 'src/migrations/*.{ts,js}' },
      context: sequelize.getQueryInterface(),
      storage: new SequelizeStorage({ sequelize }),
      logger: console,
    });
    let consoleDisplay = 'Umzug LOGS:::<br/>';
    switch (id) {
      default:
      case 'up':
        await umzug.up().then(function (migrations) {
          console.log('Umzug Migrations UP::<br/>', migrations);
          consoleDisplay +=
            'Umzug Migrations UP::<br/>' + JSON.stringify(migrations);
        });
        break;
    }
    return consoleDisplay;
  }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.