2015-08-30 114 views
0

我爲我的ORM使用SequelizeJS。在Sequelize遷移中創建時間戳

我有一個「視頻」模型。該模型使用「視頻」表。

如何創建包含時間戳的遷移?我是否需要定義自己的時間戳列,還是有快捷方式?

/migrations/123412341234-create-videos-table.js

'use strict'; 

module.exports = { 
    up: function (queryInterface, Sequelize) { 
    queryInterface.createTable(
     'Videos', 
     { 
     id: { 
      type: Sequelize.INTEGER, 
      primaryKey: true, 
      autoIncrement: true 
     }, 
     title: { 
      type: Sequelize.STRING, 
      allowNull: false, 
      unique: true 
     }, 
     author: { 
      type: Sequelize.STRING, 
      allowNull: false 
     }, 
     videoUrl: { 
      type: Sequelize.STRING, 
     }, 
     coverUrl: { 
      type: Sequelize.STRING, 
     } 
     } 
    ); 
    }, 

    down: function (queryInterface, Sequelize) { 
    queryInterface.dropTable('Videos'); 
    } 
}; 

/models/video.js

module.exports = function(sequelize, DataTypes) { 
    return sequelize.define('Video', { 
    title: { 
     type: DataTypes.STRING, 
     allowNull: false, 
     unique: true 
    }, 
    author: { 
     type: DataTypes.STRING, 
     allowNull: false 
    }, 
    videoUrl: { 
     type: DataTypes.STRING, 
    }, 
    coverUrl: { 
     type: DataTypes.STRING, 
    } 
    }); 
} 

/models/index.js(這是通過運行$ sequelize init創建的默認值)

'use strict'; 

var fs  = require('fs'); 
var path  = require('path'); 
var Sequelize = require('sequelize'); 
var basename = path.basename(module.filename); 
var env  = process.env.NODE_ENV || 'development'; 
var config = require(__dirname + '/../config/config.json')[env]; 
var db  = {}; 

if (config.use_env_variable) { 
    var sequelize = new Sequelize(process.env[config.use_env_variable]); 
} else { 
    var sequelize = new Sequelize(config.database, config.username, config.password, config); 
} 

fs 
    .readdirSync(__dirname) 
    .filter(function(file) { 
    return (file.indexOf('.') !== 0) && (file !== basename); 
    }) 
    .forEach(function(file) { 
    if (file.slice(-3) !== '.js') return; 
    var model = sequelize['import'](path.join(__dirname, file)); 
    db[model.name] = model; 
    }); 

Object.keys(db).forEach(function(modelName) { 
    if (db[modelName].associate) { 
    db[modelName].associate(db); 
    } 
}); 

db.sequelize = sequelize; 
db.Sequelize = Sequelize; 

module.exports = db; 

回答

0

使用遷移時,主鍵,updatedAt和createdAt字段不會爲您創建。相反,您應確保在遷移過程中創建這些列。

對於updatedAt和createdAt列自動更新,您可以在模型中使用掛鉤。

即:

module.exports = function (sequelize, DataTypes) { 

    var Person = sequelize.define('Person', { 
     id: { 
      type: DataTypes.INTEGER, 
      primary: true, 
      autoincrement: true 
     }, 
     name: DataTypes.STRING, 
     updatedAt: DataTypes.DATE, 
     createdAt: DataTypes.DATE 
    }, { 
     hooks: { 
      beforeCreate: function (person, options, fn) { 
       person.createdAt = new Date(); 
       person.updatedAt = new Date(); 
       fn(null, person); 
      }, 
      beforeUpdate: function (person, options, fn) { 
       person.updatedAt = new Date(); 
       fn(null, person); 
      } 
     } 
    }); 

    return Person; 
} 

^去了我的頭頂部,沒有測試!