0

I am using @mikro-orm/migrations in my Electron app, I created migration files and want to run the database migrations in production mode, while at the same time, I also want to enable asar packaging in Electron to improve the app launch speed.

If I don't enable asar packaging, the folder structure is as below, and everything is working fine:

app/node_modules/@mikro-orm
app/node_modules/...
app/migrations/Migration20220224172334.js

While with asar packaging enabled, the folder structure becomes:

app.asar // this is a package with all node modules
app.asar.unpacked/migrations/Migration20220224172334.js

As a result, I am getting error like Cannot find module '@mikro-orm/migrations' while load Migration20220224172334.js, here is the content of the migration script:

const { Migration } = require('@mikro-orm/migrations');

class Migration20220224172334 extends Migration {
...

I think this question is to either Electron developers or Mikro ORM developers.

  1. Is there a way to programmatically load node modules inside asar package from an external JS file?

  2. Is it possible to bundle migration scripts into asar and at the same time having Mikro ORM to search them inside asar package?

Hao Xi
  • 351
  • 4
  • 12
  • What's your tsconfig? I don't think you should be compiling to ES5... – kelsny Feb 24 '22 at 14:32
  • @youdateme For compiling these migratoin files, I made it simple with a `tsc` command without a tsconfig file. It seems the default target is ES3 though. I am new to Javascript, can you please tell the JS spec by reading the code above? Is it ES5 instead of ES3? – Hao Xi Feb 24 '22 at 14:37
  • Oh god that explains why it's 1000x harder to read. Yeah looks like a really old version. You can fix this by adding a `tsconfig.json` file. Make a quick trip to the documentation for how to change target and configure TypeScript. – kelsny Feb 24 '22 at 14:39
  • @youdateme I think the issue is whether I can compile Typescript to ES2015 and use in Electron because Electron has a modified NodeJS runtime. Also, on the other hand, can `mikro-orm` search migration files in CommonJS. – Hao Xi Feb 24 '22 at 15:44

1 Answers1

0

We can patch the require path to make it work in both development and production modes.

require(
  (
    __dirname.includes('asar.unpacked') 
    ? '../../../../app.asar/node_modules/' 
    : ''
  ) + '@mikro-orm/migrations'
);
Hao Xi
  • 351
  • 4
  • 12
  • Not really, this way it will generate JS migrations - they are not ES3 nor ES5, they use ES6 features like class syntax and object destructing. – Martin Adámek Feb 24 '22 at 18:30