mycodeself / mongo-migrate-ts Goto Github PK
View Code? Open in Web Editor NEWRun mongodb migrations easy from TypeScript
License: MIT License
Run mongodb migrations easy from TypeScript
License: MIT License
The second execution of ts-node migrations/index.ts up will prompt "No migrations found". how to solve it
Hi, I've installed the package using npm install -g mongo-migrate-ts
according to the instructions, but when I try to run the CLI using mongo-migrate
I get:
internal/modules/cjs/loader.js:1015
throw new ERR_REQUIRE_ESM(filename, parentPath, packageJsonPath);
^
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /opt/homebrew/lib/node_modules/mongo-migrate-ts/node_modules/ora/index.js
require() of ES modules is not supported.
require() of /opt/homebrew/lib/node_modules/mongo-migrate-ts/node_modules/ora/index.js from /opt/homebrew/lib/node_modules/mongo-migrate-ts/dist/lib/commands/down.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /opt/homebrew/lib/node_modules/mongo-migrate-ts/node_modules/ora/package.json.
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1015:13)
at Module.load (internal/modules/cjs/loader.js:863:32)
at Function.Module._load (internal/modules/cjs/loader.js:708:14)
at Module.require (internal/modules/cjs/loader.js:887:19)
at require (internal/modules/cjs/helpers.js:74:18)
at Object.<anonymous> (/opt/homebrew/lib/node_modules/mongo-migrate-ts/dist/lib/commands/down.js:7:31)
at Module._compile (internal/modules/cjs/loader.js:999:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
at Module.load (internal/modules/cjs/loader.js:863:32)
at Function.Module._load (internal/modules/cjs/loader.js:708:14) {
code: 'ERR_REQUIRE_ESM'
}
I tried both on node v14.18.2
and v12.22.10
installed via homebrew on an M1 mac
Is this a known issue, or is this something I've messed up on my side?
migrate-mongo exports and API that you can use to run the commands programatically.
I don't see this available in this library but I am not sure if it is hidden somewhere. Please let me know.
Hey, thanks a lot for this package!
I think it would be really nice to be able to enable transactions (when the instance supports it) to wrap up the migration execution.
I'd be thrilled to implement this if you support this :)
Steps I have taken
index.tsβ¨
import { mongoMigrateCli } from "mongo-migrate-ts";
const MONGO_URL = "mongodb://localhost:27017";
mongoMigrateCli({
uri: MONGO_URL,
database: "db",
migrationsDir: "migrations",
migrationsCollection: "migrations_collection",
});
migrations/migration_1.ts
import { MigrationInterface } from "mongo-migrate-ts";
import { Db } from "mongodb";
import { getItem, MY_COLLECTION } from "../common";
export class MyMigration implements MigrationInterface {
async up(db: Db): Promise<any> {
const item = getItem();
const data = await db
.collection(MY_COLLECTION)
.find()
.toArray();
if (data.length === 0) {
await db.collection(MY_COLLECTION).insertOne(item);
}
}
async down(db: Db): Promise<any> {
const item = getItem();
await db.collection(MY_COLLECTION).deleteOne(resident);
}
}
response: β No migrations found
Tryed installing globally and using CLI, resilt is the same
Just thinking out loud: since this package is based on migrate-mongo (https://github.com/seppevs/migrate-mongo/) I think it's nice to have transition plan to how to switch from migrate-mongo JS to mongo-migrate-ts.
Anyway, for this particular case, during that switch (from migrate-mongo JS to mongo-migrate-ts) I faced the issue of different format of files:
1697236860399_Migration.ts
| generated by mongo-migrate-ts20230105075728-migration.js
| generated by migrate-mongo JSSo this is enhancement suggestion: unify format for file naming OR add the config option (migrations.json
) to use specific time format.
π The difference of timestamps:
Overriding the sample migration
To override the content of the sample migration that will be created by the create command, create a file sample-migration.js in the migrations directory.
Is it possible to make something similar, to avoid linter erorrs and e.t.c?
master
branch failed. π¨I recommend you give this issue a high priority, so other packages depending on you can benefit from your bug fixes and new features again.
You can find below the list of errors reported by semantic-release. Each one of them has to be resolved in order to automatically publish your package. Iβm sure you can fix this πͺ.
Errors are usually caused by a misconfiguration or an authentication problem. With each error reported below you will find explanation and guidance to help you to resolve it.
Once all the errors are resolved, semantic-release will release your package the next time you push a commit to the master
branch. You can also manually restart the failed CI job that runs semantic-release.
If you are not sure how to resolve this, here are some links that can help you:
If those donβt help, or if this issue is reporting something you think isnβt right, you can always ask the humans behind semantic-release.
semantic-release cannot push the version tag to the branch master
on the remote Git repository with URL https://[secure]@github.com/mycodeself/mongo-migrate-ts.git
.
This can be caused by:
Good luck with your project β¨
Your semantic-release bot π¦π
Hello, first of all, thank you for developing this package it is very useful and easy to use.
I've mostly used migrate-mongo, but now I'm working on a TypeScript project and decided to try this out.
Something I found is that if an error occurs during for instance the down
process of migration, the error case of this is not handled by the package, and the CLI gets stuck.
I've added the --unhandled-rejections=strict
flag and it seems like a workaround, but not sure if it is the best way to handle such cases.
Thanks in advance!
Is it possible to use an environment variable to set the connection URI with mongo?
For all the commands, in windows we get Unexpected Token export
Hey! I would like to add support for .env (https://npmjs.com/package/dotenv).
Is that acceptable for you?
When using connection string in SRV format like mongodb://mongo1:27017,mongo2:27017,mongo3:27017/socio?replicaSet=replset0
or mongodb+srv://mongo1:27017,mongo2:27017,mongo3:27017/socio?replicaSet=replset0
module fails as it uses standard URL
parser which cannot work with connection string syntaxt (which actually not WHATWG URL).
It can be fixed by using special module like connection-string module which is aware of multi-host URIs
This tool can't be used with a CI/CD platform because if an exception happens it exits with 0. This means that no one will be able to detect that an issue has happened. The pipeline will simply imply everything is ok.
Using the version 1.0.9 doesn't detect properly when running in ts-node.
I tried to make a migration using the following commands:
$ env-cmd tsnd src/migrations/index.ts up
$ ts-node-dev -r dotenv/config src/migrations/index.ts up
$ ts-node -r dotenv/config src/migrations/index.ts up
And despite them executes the index with ts-node (direct or indirectly), the function isTsNode doesn't return true making the loadMigrations function looking for files with js extensions, so the command ends without finding any migrations:
I solved this downgrading to the version 1.0.6, but I think it's something to take in consideration in future versions.
Hello,
Is it possible to add support for nested directories ?
Let's say i have many directories ordered by version :
--- migrations /
---------------V1.1/
---------------------V1.1.001_add_user.ts
---------------V1.2/
---------------------V1.2.001_update_user.ts
up
command does not find migration files for /migrations
path.
Thanks a lot for the package, works like a charm :)
Regards
When I use the ts-node migrations/index.ts up
command to execute migrations I get the following error
Error executing migration Migration1658840191491 β Error executing migrations ExecuteMigrationError: MongoServerError: ns not found
The command only executes the first migration and all others throw an error.
my index.ts file contains the following:
`import { mongoMigrateCli } from "mongo-migrate-ts";
import 'dotenv/config';
mongoMigrateCli({
uri: mongodb:${process.env.MONGO_HOST}
,
database: ${process.env.MONGO_DB}
,
migrationsDir: __dirname,
migrationsCollection: "migrations_collection",
});`
I think it is better to accept a name for creating new migration
It would be nice to configure a custom migration template for the mongo-migrate new
command. This would enable us to prevent copy-paste mistakes with boilerplate code. At the moment, the template is hard-coded into the library source.
I imagine it could be an easy win to implement something like the -t
option in migrate-mongoose. There, you can supply a template file (via command line flag or config file). The content of the template is copied verbatim into new migrations.
When running mongo-migrate init
(or mongo-migrate -h
for that matter), it throws an error due to no config file being found, even though the command itself should create that config file.
When you compile the project ant try tu run it using command node build/migrations/index up
migration fails with error:
Migrations up/home/projects/dist/migrations/20200326_01.d.ts:1
import { MigrationInterface } from 'mongo-migrate-ts';
^^^^^^
SyntaxError: Cannot use import statement outside a module
at wrapSafe (internal/modules/cjs/loader.js:1063:16)
at Module._compile (internal/modules/cjs/loader.js:1111:27)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1167:10)
at Module.load (internal/modules/cjs/loader.js:996:32)
at Function.Module._load (internal/modules/cjs/loader.js:896:14)
at Module.require (internal/modules/cjs/loader.js:1036:19)
at require (internal/modules/cjs/helpers.js:72:18)
at /home/danielius/projects/efi/git/efi-fatca/node_modules/mongo-migrate-ts/dist/lib/migrations.js:27:69
at processTicksAndRejections (internal/process/task_queues.js:97:5)
at async Object.exports.loadMigrationFile (/home/danielius/projects/efi/git/efi-fatca/node_modules/mongo-migrate-ts/dist/lib/migrations.js:27:21)
The issue is that node tryes to load *.ts files found in migration diretory and fails. It should load only *.js files
mostly if somebody is ever interested into testing their migrations before, the obvious choice will be to do:
/migrations
1234567890-migration.ts
1234567890-migration.test.ts
then when you do index up
then the .test
runs as a migration. bummer
The mongodb
package is listed in the peerDependencies at major version 4, which is preventing me from updating mongodb
to the latest major version.
Is there any compatibility issues with version 5 that require version 4 specifically? My testing so far shows that version 5 works fine, but it's possible I haven't tested everything.
In any case, thank you for sharing this package, it is very useful.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. πππ
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google β€οΈ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.