How to generate model from exists database in Sails JS?

雨燕双飞 提交于 2019-12-10 12:59:18

问题


I begin with SailsJS and MySQL, and I have many tables in my database. So, I don't know that in SailsJS has a tool to generate models from database like Database First in Entity Framework (ASP)


回答1:


You should use, it's very good to auto generate model base DB existing.
https://www.npmjs.com/package/sails-generate-models




回答2:


SailsJS does not have such a tool implemented.

Though, it should not be hard to create that yourself since MySQL's SHOW COLUMNS FROM table works quite well. Then you'd just have to create the .js model files.

Be wary of the configuration in config/models.js and set migrate to safe as you might drop some columns if you haven't tested and are not sure if you've generated the models correctly.




回答3:


Have a look at Sails Inverse Model

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

Install using command

npm install sails-inverse-model -g

To know the tool

sails-inverse-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


来源:https://stackoverflow.com/questions/34034779/how-to-generate-model-from-exists-database-in-sails-js

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!