Skip to content

Commit 633235e

Browse files
committed
Improve ESM support
1 parent 4b9a955 commit 633235e

File tree

10 files changed

+94
-10
lines changed

10 files changed

+94
-10
lines changed

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,31 @@ $ npm init --yes
263263
Now you have a package.json file, and you can install your favorite npm modules that might help you in your migration scripts.
264264
For example, one of the very useful [promise-fun](https://github.com/sindresorhus/promise-fun) npm modules.
265265

266+
267+
### Using ESM (ECMAScript Modules) instead of CommonJS
268+
Since migrate-mongo 7.0.0, it's possible to use ESM instead of CommonJS.
269+
270+
#### Using ESM when initializing a new project
271+
Pass the `-m esm` option to the `init` action:
272+
````bash
273+
$ migrate-mongo init -m esm
274+
````
275+
276+
It's also required to have package.json file in the root of your project with `"type": "module"`.
277+
Create a new package.json file:
278+
````bash
279+
$ npm init --yes
280+
````
281+
282+
Then edit this package.json file, and add:
283+
````bash
284+
"type": "module"
285+
````
286+
287+
When you create migration files with `migrate-mongo create`, they will be prepared for you in ESM style.
288+
289+
Please note that CommonJS is still the default module loading system.
290+
266291
### Using MongoDB's Transactions API
267292
You can make use of the [MongoDB Transaction API](https://docs.mongodb.com/manual/core/transactions/) in your migration scripts.
268293

bin/migrate-mongo.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ program.version(pkgjson.version);
3232
program
3333
.command("init")
3434
.description("initialize a new migration project")
35-
.action(() =>
35+
.option("-m --module <module loading system>", "module loading system (commonjs (DEFAULT) or esm)")
36+
.action(options => {
37+
global.options = options;
3638
migrateMongo
3739
.init()
3840
.then(() =>
@@ -41,7 +43,7 @@ program
4143
)
4244
)
4345
.catch(err => handleError(err))
44-
);
46+
});
4547

4648
program
4749
.command("create [description]")

lib/actions/create.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const fs = require("fs-extra");
22
const path = require("path");
33
const date = require("../utils/date");
44
const migrationsDir = require("../env/migrationsDir");
5+
const config = require("../env/config");
56

67
module.exports = async description => {
78
if (!description) {
@@ -16,7 +17,8 @@ module.exports = async description => {
1617
if (await migrationsDir.doesSampleMigrationExist()) {
1718
source = await migrationsDir.resolveSampleMigrationPath();
1819
} else {
19-
source = path.join(__dirname, "../../samples/migration.js");
20+
const configContent = await config.read();
21+
source = path.join(__dirname, `../../samples/${configContent.moduleSystem}/migration.js`);
2022
}
2123

2224
const filename = `${date.nowAsString()}-${description

lib/actions/init.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ const migrationsDir = require("../env/migrationsDir");
55
const config = require("../env/config");
66

77
function copySampleConfigFile() {
8-
const source = path.join(__dirname, "../../samples/migrate-mongo-config.js");
8+
const moduleSystem = global.options.module === 'esm' ? 'esm' : 'commonjs';
9+
const source = path.join(__dirname, `../../samples/${moduleSystem}/migrate-mongo-config.js`);
910
const destination = path.join(
1011
process.cwd(),
1112
config.DEFAULT_CONFIG_FILE_NAME

samples/migrate-mongo-config.js renamed to samples/commonjs/migrate-mongo-config.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@ const config = {
2727

2828
// Enable the algorithm to create a checksum of the file contents and use that in the comparison to determin
2929
// if the file should be run. Requires that scripts are coded to be run multiple times.
30-
useFileHash: false
30+
useFileHash: false,
31+
32+
// Don't change this, unless you know what you're doing
33+
moduleSystem: 'commonjs',
3134
};
3235

33-
// Return the config as a promise
3436
module.exports = config;
File renamed without changes.

samples/esm/migrate-mongo-config.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// In this file you can configure migrate-mongo
2+
3+
const config = {
4+
mongodb: {
5+
// TODO Change (or review) the url to your MongoDB:
6+
url: "mongodb://localhost:27017",
7+
8+
// TODO Change this to your database name:
9+
databaseName: "YOURDATABASENAME",
10+
11+
options: {
12+
useNewUrlParser: true, // removes a deprecation warning when connecting
13+
useUnifiedTopology: true, // removes a deprecating warning when connecting
14+
// connectTimeoutMS: 3600000, // increase connection timeout to 1 hour
15+
// socketTimeoutMS: 3600000, // increase socket timeout to 1 hour
16+
}
17+
},
18+
19+
// The migrations dir, can be an relative or absolute path. Only edit this when really necessary.
20+
migrationsDir: "migrations",
21+
22+
// The mongodb collection where the applied changes are stored. Only edit this when really necessary.
23+
changelogCollectionName: "changelog",
24+
25+
// The file extension to create migrations and search for in migration dir
26+
migrationFileExtension: ".js",
27+
28+
// Enable the algorithm to create a checksum of the file contents and use that in the comparison to determin
29+
// if the file should be run. Requires that scripts are coded to be run multiple times.
30+
useFileHash: false,
31+
32+
// Don't change this, unless you know what you're doing
33+
moduleSystem: 'esm',
34+
};
35+
36+
export default config;

samples/esm/migration.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export const up = async (db, client) => {
2+
// TODO write your migration here.
3+
// See https://github.com/seppevs/migrate-mongo/#creating-a-new-migration-script
4+
// Example:
5+
// await db.collection('albums').updateOne({artist: 'The Beatles'}, {$set: {blacklisted: true}});
6+
};
7+
8+
export const down = async (db, client) => {
9+
// TODO write the statements to rollback your migration (if possible)
10+
// Example:
11+
// await db.collection('albums').updateOne({artist: 'The Beatles'}, {$set: {blacklisted: false}});
12+
};

test/actions/create.test.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ describe("create", () => {
2121
function mockConfig() {
2222
return {
2323
shouldExist: sinon.stub().returns(Promise.resolve()),
24+
read: sinon.stub().returns(Promise.resolve({
25+
moduleSystem: 'commonjs',
26+
}))
2427
};
2528
}
2629

@@ -79,7 +82,7 @@ describe("create", () => {
7982
const filename = await create("my_description");
8083
expect(fs.copy.called).to.equal(true);
8184
expect(fs.copy.getCall(0).args[0]).to.equal(
82-
path.join(__dirname, "../../samples/migration.js")
85+
path.join(__dirname, "../../samples/commonjs/migration.js")
8386
);
8487
expect(fs.copy.getCall(0).args[1]).to.equal(
8588
path.join(process.cwd(), "migrations", "20160609080700-my_description.js")
@@ -96,7 +99,7 @@ describe("create", () => {
9699
const filename = await create("my_description");
97100
expect(fs.copy.called).to.equal(true);
98101
expect(fs.copy.getCall(0).args[0]).to.equal(
99-
path.join(__dirname, "../../samples/migration.js")
102+
path.join(__dirname, "../../samples/commonjs/migration.js")
100103
);
101104
expect(fs.copy.getCall(0).args[1]).to.equal(
102105
path.join(process.cwd(), "migrations", "20160609080700-my_description.ts")
@@ -112,7 +115,7 @@ describe("create", () => {
112115
await create("this description contains spaces");
113116
expect(fs.copy.called).to.equal(true);
114117
expect(fs.copy.getCall(0).args[0]).to.equal(
115-
path.join(__dirname, "../../samples/migration.js")
118+
path.join(__dirname, "../../samples/commonjs/migration.js")
116119
);
117120
expect(fs.copy.getCall(0).args[1]).to.equal(
118121
path.join(

test/actions/init.test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ describe("init", () => {
2929
}
3030

3131
beforeEach(() => {
32+
global.options = { module: 'commonjs' };
3233
migrationsDir = mockMigrationsDir();
3334
config = mockConfig();
3435
fs = mockFs();
@@ -82,7 +83,7 @@ describe("init", () => {
8283

8384
const source = fs.copy.getCall(0).args[0];
8485
expect(source).to.equal(
85-
path.join(__dirname, "../../samples/migrate-mongo-config.js")
86+
path.join(__dirname, "../../samples/commonjs/migrate-mongo-config.js")
8687
);
8788

8889
const destination = fs.copy.getCall(0).args[1];

0 commit comments

Comments
 (0)