Sails Models from MySQL

Sails.js
Let’s say you start new project (or refactor old one) using amazing NodeJS framework Sails, but already have a complicated database with lot of data. You first problem will be create Models for all your tables. Actually it’s lot of manual work.

In this case you can use my small tool named ‘sails-genmodels‘, which will do most of this monkey-work for you.

Before use

After create new project edit 2 files:

In file /config/connections.js:

localMySQL: {
    adapter: 'sails-mysql',
    host: 'localhost',
    user: 'superuser',
    password: 'superpassword',
    database: 'your_database'
},

In file /config/models.js:

connection: 'localMySQL',
migrate: 'safe'

Actual name of your connection may vary, but both must be set!

Install and use
cd ~/your_project_dir
git clone git@github.com:sukharenko/sails-genmodels.git
cd sails-genmodels
npm install
cd ..
node sails-genmodels/genmodels.js

That’s it!

As result you will get files in api/models and api/controllers named according to your tables. For example for table named ‘countries’ you will get:

  • api/controllers/CountriesController.js
  • api/models/Countries.js

If for some reason you already have model with same name – it will be ignored! As same time – if you already have controller with same name, but don’t have model – model will be created and controller will be overridden!

Contributions

Please, fell free to fork project and send me a pull-requests.

Sources

GitHub: https://github.com/sukharenko/sails-genmodels

Link to this post:

<a href="https://sukharenko.com/2015/11/09/sails-models-mysql/">Sails Models from MySQL</a>

Вам також має сподобатись...

Залишити відповідь