Skip to content

sequelize model:create#12

Merged
janmeier merged 1 commit intomasterfrom
feature/model-generation
Jun 30, 2014
Merged

sequelize model:create#12
janmeier merged 1 commit intomasterfrom
feature/model-generation

Conversation

@sdepold
Copy link
Member

@sdepold sdepold commented Jun 22, 2014

Description

Add sequelize model:create which generates a model and it corresponding migration file.

This task generates a model file and its respective migration.
It is necessary to specify the name of the new model as well as
the model's attributes.

The attributes can be specified as in the following (and semantically equal) examples:

    sequelize model:create --name User --attributes first_name:string,last_name:string,bio:text
    sequelize model:create --name User --attributes "first_name:string last_name:string bio:text"
    sequelize model:create --name User --attributes "first_name:string, last_name:string, bio:text"

This command will generate a new migration and model definition:

    // the model file
    // located under models/user.js
    module.exports = function(sequelize, DataTypes) {
      var User = sequelize.define('User', {
        first_name: DataTypes.STRING,
        last_name: DataTypes.STRING,
        bio: DataTypes.TEXT
      }, {
        classMethods: {
          associate: function(models) {
             // associations can be defined here
          }
        }
      })

      return User
    }
    // the migration file
    // located under migrations/20140622195935-create-user.js
    module.exports = {
      up: function(migration, DataTypes, done) {
        migration
          .createTable('Users', {
            first_name: DataTypes.STRING
            last_name: DataTypes.STRING
            bio: DataTypes.TEXT
          })
          .complete(done)
      },
      down: function(migration, DataTypes, done) {
        migration
          .dropTable('Users')
          .complete(done)
      }
    }

Discussion area:

I wonder if the API is OK or if there would be a better syntax for the attributes.

@janmeier
Copy link
Member

I think the proposed API looks good, I like that better than specifying each attribute with a flag :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While this code is pretty readable and simple, I wonder if it wouldn't better to use some kind of templating language for the skeleton where we just insert tableName and attributes?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that is really true and will be done in an upcoming PR. i thought exactly the same, but didn't want to do it within that PR as it is already quite big

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fine :)

@sdepold
Copy link
Member Author

sdepold commented Jun 30, 2014

@janmeier @mickhansen can you merge it ? :)

janmeier added a commit that referenced this pull request Jun 30, 2014
@janmeier janmeier merged commit d8dd97d into master Jun 30, 2014
@janmeier
Copy link
Member

sure can ;)

@sdepold sdepold deleted the feature/model-generation branch July 1, 2014 06:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants