How to create a model from a database in Sails JS?

I start with SailsJS and MySQL , and I have many tables in my database. So, I do not know that SailsJS has a tool for creating models from a database, for example Database First in the Entity Framework ( ASP )

+6
source share
3 answers

You must use this very well to automatically create an existing model database.
https://www.npmjs.com/package/sails-generate-models

+3
source

SailsJS does not have such a tool.

Although, it should not be difficult to create, since MySQL SHOW COLUMNS FROM table works quite well. Then you just need to create .js model files.

Be careful with the configuration in config/models.js and set the transition to safe , as you can delete some columns if you have not tested it yet and are not sure if you created the models correctly.

+2
source

Look at the reverse Sails model

The Sails Inverse Model helps you create JS Sails models, controllers, and views from any database. In addition, you can quickly and individually generate each model, view, controller, or all three at the same time.

Install using the command

npm install sails-inverse-model -g

Know tool

sails-reverse model --help

  Example: $ mkdir sails-output $ cd sails-output $ sails-inverse-model -u postgres -p root -d almacen -t pg -m -v -c User : postgres Password : root Database : almacen Host : localhost Models : /home/julian/Documents/sails-output/models Views : /home/julian/Documents/sails-output/views Controllers : /home/julian/Documents/sails-output/controllers DB : pg Schema (pg) : public ===================================== Views [OK] ===================================== Models [OK] ===================================== Controllers [OK] Note: Copy models => your/project_sails/api Copy controllers => your/project_sails/api Copy views/* => your/project_sails/views/ Then: $ cd your/project_sails/ $ sails lift More info: https://github.com/juliandavidmr/sails-inverse-model --------------------------------------------------------------- Options: -u, --user User of database -p, --pass Password of database -d, --database Database name -h, --host Host server Default: localhost -m, --models Folder output models Default: Folder actual -c, --controllers Folder output controllers Default: Folder actual -v, --views Folder output views Default: Folder actual (Experimental) -t, --type Type gestor database: mysql|postgres|mongodb Default: mysql -s, --schema (Only PostgreSQL) Schema database postgres: Default: public -f, --file (Only MySQL) .sql file path entry (Experimental) ====================== Individual generation ================== You can quickly generate a model, a controller, a view or these three at the same time. # Generate model $ sails-inverse-model -g model --name Pet -a "name:string:r:u owner:string" # Generate Controller $ sails-inverse-model -g controller --name Pet -a "name:string:r:u owner:string" # Generate View $ sails-inverse-model -g view --name Pet -a "name:string:r owner:string" # Generate all (Model, View and Controller) $ sails-inverse-model -g all --name Pet -a "name:string:r:k owner:string" Where: -------------------------------------------- |Param | Description | Example | |------|---------------|-------------------| | r | Required | catname:string:r | | u | Unique | catname:string:u | | a | Autoincrement | index:integer:a | | k | Primary Key | index:integer:k | -------------------------------------------- You can also set all three parameters at the same time, for example: index:integer:a:u:r 
0
source

Source: https://habr.com/ru/post/1237216/


All Articles