lukeed / ley

(WIP) Driver-agnostic database migrations
MIT License
259 stars 14 forks source link

Plan to support import means "type": "module" in package.json #15

Closed dirk-ecker closed 3 years ago

dirk-ecker commented 3 years ago

I am currently developing an application in nodejs using modules that means my package.json contains a

"type": "module",

and so I have to import and export everything.

Hey, ley is a great idea and module I want to use.

Using it gaves me an error because it tries to require my ley.config.js that should be module. I could manage to support it by modifying util.js using import instead of require:

exports.local = function (str, cwd) {
    str = resolve(cwd || '.', str);
    // return existsSync(str) && require(str);
    return existsSync(str) && import(str)
}

and adjustment to postgres.js by using this config

exports.connect = function (config = {}) {
    // console.log('config', config)
    return require('postgres')({
        onnotice: () => {},
        ...(config?.default || config),
        max: 1,
    });
}

My problem is, I do not know to differentiate between import and require during execution. How could we do this and would this be a good extension for ley?

lukeed commented 3 years ago

Hey, I may add native ESM as part of 1.0, but for now, you can get around this by using a ley.config.cjs file inside your project, and then passing that value/path to the --config flag.

All your commands will start with this: ley --config ley.config.cjs. If you add the flag as part of your "migrate" npm-script, then you don't have to worry about typing it every time:

{
  "scripts": {
    "migrate": "ley --config ley.config.cjs"
  }
}
$ yarn migrate up

I think your migration files will also need to be .cjs extension, which is fine since ley just reads everything inside the --dir value ("migrations" by default).

dirk-ecker commented 3 years ago

Thank you for the hint to rename to .cjs. In the end renaming to .mjs works.