简体   繁体   English

使用Knex动态迁移和播种数据库?

[英]Migrating and seeding a database dynamically using Knex?

I am using Knex.js in my Node application in order to communicate with my MySQL database. 我在Node应用程序中使用Knex.js以便与MySQL数据库进行通信。 I want to build my app to the point where the user can define what columns they want to create a table with in my app. 我想构建我的应用程序,以便用户可以定义他们想要在我的应用程序中创建表的列。 My question is how would I go about creating the migrations for the database dynamically because I won't know columns my user wants in their table. 我的问题是我将如何动态地为数据库创建迁移,因为我不知道用户希望在其表中使用哪些列。 And when I create migrations via Knex (using knex migrate:object ), I have to specify the constraints of the table using the migration file that is automatically generated. 当我通过Knex创建迁移(使用knex migrate:object )时,我必须使用自动生成的迁移文件指定表的约束。 Is there a way I can programatically implement the process of migrating the database and seeding with whatever the user provides? 有没有一种方法可以以编程方式实现迁移数据库和使用用户提供的内容进行播种的过程?

That sounds like a really bad idea. 这听起来像是一个非常糟糕的主意。 Try to google + read something about dynamic schema modification anti pattern. 尝试Google +阅读有关动态模式修改反模式的内容。

That being said you cannot use knex's migrations/populate framework for that. 话虽这么说,您不能为此使用knex的迁移/填充框架。 Just call knex.schema.* methods directly from your application code. 只需直接从您的应用程序代码中调用knex.schema。*方法即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM