seppevs / migrate-mongo

A database migration tool for MongoDB in Node
MIT License
931 stars 166 forks source link

Support for ES6 imports #248

Closed georgeben closed 2 years ago

georgeben commented 4 years ago

Is your feature request related to a problem? Please describe. I can't import modules into my migrate-mongo-config.js using ES6 imports, which is unfortunate because the whole codebase is written using ES6 module imports

Describe the solution you'd like This library should support ES6+ syntax

redgumnfp commented 3 years ago

Never worked on an open-source project, but happy to do if someone explains the process. Just raise a PR?

Non-breaking fix anyway is: migrationsDir.js

async loadMigration(fileName) {
    const configContent = await config.read();
    const { useES6 } = configContent;
    const migrationsDir = await resolveMigrationsDirPath();

    if (useES6) {
      return import(path.join(migrationsDir, fileName));
    }
    return require(path.join(migrationsDir, fileName)); // eslint-disable-line
  },

New config option: useES6

This allows me to write in ES6 format:

import { Settings } from '../src/models/settings';

const up = async (_db, _client) => {
  await Settings()
    .updateOne({}, {
      inboundCallsAcceptedFrom: '08:00',
      inboundCallsAcceptedTo: '21:00',
      inboundCallsAcceptedTimezone: 'Australia/Sydney',
    });
};

export {
  up,
};
nuno-ferrao commented 3 years ago

Would second this request. Would like to implement migrate-mongo on my project but I cannot export the config setup because of ES6 incompatibility. If there's anything I'm missing, please let me know.

Thanks

seppevs commented 2 years ago

ESM is finally here! Upgrade to migrate-mongo >= 9.0.0, and see the README for details.

tahsinZilani commented 1 year ago

i have to use type: module in package.json. is there any other workaround?