/sails-generator-automodel

Generate models for Sails Js or Waterline from a mysql connection

Primary LanguageJavaScriptMIT LicenseMIT

Sails Generator AutoModel

Generate models automatically for Sails Js or Waterline for Mysql connections

Version 1.1.0

One bad thing when you work with sails js is do all the models by hand... until now, this generator helps to do this task in a very simple way.

The usage of this generator is a bite simple, you must clone this repo in your sails project, and run:

#git clone https://github.com/cadrogui/sails-generator-automodel.git automodel
#cd automodel

Link AutoModel

#sudo npm link

and finally on your sails project root folder, you can use AutoModel

#sails generate automodel

Automodel will find all conections that use sails-mysql adapter, and will prompt for usage.... and thats all, automodel will generate all files for represent you database.

Data Types

Automodel support all data types present in waterline:

  • string
  • text
  • integer
  • float
  • date
  • time
  • datetime
  • boolean
  • binary
  • array
  • json

Associations

At this version AutoModel can detect all foreign keys only as model attribute, if in your associations you need the virtual attribute collections you must create by your self, but half way is taken by AutoModel, in the next releases i will try to give the power to AutoModel to handle complete associations.

AutoModel in action

Example model

Models generated by AutoModel

Model Pets
module.exports = {
    "identity": "Pets",
    "connection": "sails_test",
    "autoCreatedAt": false,
    "autoUpdatedAt": false,
    "attributes": {
        "id": {
            "type": "INTEGER",
            "primaryKey": "TRUE",
            "autoIncrement": "TRUE"
        },
        "name": "STRING",
        "Owners": {
            "model": "Owners",
            "columnName": "owner_id"
        }
    }
}
Model Owners
module.exports = {
    "identity": "Owners",
    "connection": "sails_test",
    "autoCreatedAt": false,
    "autoUpdatedAt": false,
    "attributes": {
        "id": {
            "type": "INTEGER",
            "primaryKey": "TRUE",
            "autoIncrement": "TRUE"
        },
        "name": "STRING",
        "lastname": "STRING",
        "email": "STRING"
    }
}

As you can see automodel helps a lot with the tedious task of creating the models.

To-do

  • Maybe use js templates for models, now all do programmatically
  • Emebed all code in sails generator
  • Package the generator as npm
  • Implement more efficient ways to detect relations between models

I hope AutoModel helps to make all of your models as an easy task, as well for me.