Jim*_*Jim 8 database-migration mongodb typeorm nestjs
我在为 nestjs-typeorm-mongo 项目创建初始迁移时遇到问题。
我从 nestjs克隆了这个示例项目,它使用 typeorm 和 mongodb。该项目确实有效,当我在将“照片”文档放入我的本地 mongo 并使用名为“test”和集合“photos”的数据库后在本地运行它时,我可以调用 localhost:3000/photo 并接收照片文档。
现在我正在尝试使用以下命令使用 typeorm cli 创建迁移:
./node_modules/.bin/ts-node ./node_modules/typeorm/cli.js migration:generate -n initial
Run Code Online (Sandbox Code Playgroud)
...但它不起作用。我无法创建初始提交 - 即使在我的app.module.ts文件中设置“同步:false”后,我总是收到错误消息:
未发现数据库架构更改 - 无法生成迁移。要创建新的空迁移,请在尝试生成迁移时使用“typeorm migration:create”命令...
除了将同步更改为 false 之外,我所做的唯一其他更改是ormconfig.json通过运行在项目根目录中添加一个文件typeorm init --database mongodb:
{
"type": "mongodb",
"database": "test",
"synchronize": true,
"logging": false,
"entities": [
"src/**/*.entity.ts"
],
"migrations": [
"src/migration/**/*.ts"
],
"subscribers": [
"src/subscriber/**/*.ts"
],
"cli": {
"entitiesDir": "src",
"migrationsDir": "src/migration",
"subscribersDir": "src/subscriber"
}
}
Run Code Online (Sandbox Code Playgroud)
一旦使用 MongoDB,您就没有表,也无需提前创建集合。本质上,MongoDB 模式是动态创建的!
\n在幕后,如果驱动程序是 MongoDB,该命令typeorm migration:create将被绕过,因此在这种情况下它是无用的。您可以自行检查PR #3304和Issue #2867。
但是,有一种名为migrate-mongo 的替代方案,它提供了一种归档增量、可逆和版本控制的方式来应用架构和数据更改的方法。它\xe2\x80\x99 有很好的文档记录并且正在积极开发。
\n迁移 mongo 示例
\n运行npm install -g migrate-mongo安装它。
运行migrate-mongo init以初始化迁移工具。migrate-mongo-config.js这将在我们项目的根目录下创建一个配置文件和一个迁移文件夹:
|_ src/\n|_ migrations/\n |- 20200606204524-migration-1.js\n |- 20200608124524-migration-2.js\n |- 20200808114324-migration-3.js\n|- migrate-mongo.js\n|- package.json\n|- package-lock.json\nRun Code Online (Sandbox Code Playgroud)\n您的migrate-mongo-config.js配置文件可能如下所示:
// In this file you can configure migrate-mongo\nconst env = require(\'./server/config\')\nconst config = {\n mongodb: {\n // TODO Change (or review) the url to your MongoDB:\n url: env.mongo.url || "mongodb://localhost:27017",\n\n // TODO Change this to your database name:\n databaseName: env.mongo.dbname || "YOURDATABASENAME",\n\n options: {\n useNewUrlParser: true, // removes a deprecation warning when connecting\n useUnifiedTopology: true, // removes a deprecating warning when connecting\n // connectTimeoutMS: 3600000, // increase connection timeout up to 1 hour\n // socketTimeoutMS: 3600000, // increase socket timeout up to 1 hour\n }\n },\n\n // The migrations dir can be a relative or absolute path. Only edit this when really necessary.\n migrationsDir: "migrations",\n\n // The MongoDB collection where the applied changes are stored. Only edit this when really necessary.\n changelogCollectionName: "changelog"\n};\nmodule.exports = config;\nRun Code Online (Sandbox Code Playgroud)\n运行migrate-mongo create name-of-my-script以添加新的迁移脚本。将创建一个带有相应时间戳的新文件。
/*\n|_ migrations/\n |- 20210108114324-name-of-my-script.js\n*/\n\nmodule.exports = {\n function up(db) {\n return db.collection(\'products\').updateMany({}, { $set: { quantity: 10 } })\n }\n \n function down(db) {\n return db.collection(\'products\').updateMany({}, { $unset: { quantity: null } })\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n\n\n\n数据库变更日志:为了了解当前的数据库版本以及接下来应该应用哪个迁移,有一个特殊的集合来存储数据库变更日志,其中包含应用的迁移以及应用的时间等信息。
\n
要运行迁移,只需运行以下命令:migrate-mongo up
您可以在本文《Node.js 中的 MongoDB 架构迁移》中找到完整示例
\n| 归档时间: |
|
| 查看次数: |
2885 次 |
| 最近记录: |