node.js - node-orm 同步到 Alter Tables(类似于 DataMapper.auto_upgrade)

标签 node.js orm coffeescript

我正在使用 node-orm 尝试设置我的数据库。这是模型代码。

db = orm.connect("mysql", client, (success, db) ->
  Strain = db.define("strain",
    name:
      type: "string"
      validations: [ orm.validators.unique() ]
    old_body:
      type: "string"
    body:
      type: "string"
    created_at:
      type: "date"
    update_at:
      type: "date"
  )
  Strain.sync()
) 

我将/orm/lib/databases/mysql.js 文件更改为 console.log 返回的同步信息。

this._client.query(_query, function (err, info) {
  console.log(err);
  console.log(info);
  console.log("collection synced");
});

第一次运行 Strain.sync() 时,这是输出。

null

{ affectedRows: 0,
  insertId: 0,
  serverStatus: 2,
  warningCount: 0,
  message: '',
  setMaxListeners: [Function],
  emit: [Function],
  addListener: [Function],
  on: [Function],
  once: [Function],
  removeListener: [Function],
  removeAllListeners: [Function],
  listeners: [Function] }

collection synced

现在表已按其应有的样子创建了。当我重新启动服务器并再次运行 Strain.sync() 时,这是输出:

null

{ affectedRows: 0,
  insertId: 0,
  serverStatus: 2,
  warningCount: 1,
  message: '',
  setMaxListeners: [Function],
  emit: [Function],
  addListener: [Function],
  on: [Function],
  once: [Function],
  removeListener: [Function],
  removeAllListeners: [Function],
  listeners: [Function] }

collection synced

警告计数跳至“1”,但 err 为 null,且 message 为空。

我需要弄清楚如何更改模型并添加新属性(例如“deleted_at”),并更新表而不丢失数据。我知道我可以执行 Strain.sync(force: true ),但这会删除表然后重新创建它。我只是想更新表,类似于 DataMapper 的 auto_upgrade 函数。

有没有办法用node-orm或者任何与nodejs一起工作的ORM来做到这一点?

最佳答案

很确定当前版本的sync()只创建表,不更新。

Note: sync() only creates tables at the moment; table modifications will be added in an upcoming version.

https://github.com/dresende/node-orm#creating-the-model-on-the-database

关于node.js - node-orm 同步到 Alter Tables(类似于 DataMapper.auto_upgrade),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10728798/

相关文章:

node.js - 如何将 INTENT_CONFIDENCE 断言者作为全局变量放入 Botium

php - Laravel - 保存与更新

django - 如何在 Django 项目中使用 CoffeeScript ?

javascript - Express Node.JS - 接收 Redis 回调,执行 promise

node.js - 如何在 express-validator 中设置小数的最小值

node.js - 在 Azure 函数(Nodejs)中使用 Jest 进行测试时 process.env 变量未定义

c# - 使用linq在查找表中查找记录

java - 谷歌应用程序引擎的 ORM (java)

javascript - 将 CoffeeScript 与 cypress.io 结合使用

javascript - 这两个实现有什么不同