【问题标题】:How to generate model from exists database in Sails JS?如何从 Sails JS 中的现有数据库生成模型?
【发布时间】:2016-03-06 05:23:01
【问题描述】:

我从SailsJSMySQL 开始,我的数据库中有很多表。所以,我不知道SailsJS 有一个工具可以从数据库中生成模型,比如Database FirstEntity Framework (ASP)

【问题讨论】:

    标签: sails.js


    【解决方案1】:

    SailsJS 没有实现这样的工具。

    不过,由于 MySQL 的 SHOW COLUMNS FROM table 运行良好,因此自己创建它应该不难。然后你只需要创建 .js 模型文件。

    注意config/models.js 中的配置并将 migrate 设置为 safe,因为如果您尚未测试并且不确定您是否会删除某些列,则可能会删除已正确生成模型。

    【讨论】:

      【解决方案2】:

      你应该使用,自动生成现有的模型库非常好。
      https://www.npmjs.com/package/sails-generate-models

      【讨论】:

        【解决方案3】:

        看看Sails Inverse Model

        Sails 逆模型可帮助您从任何数据库构建模型、控制器和视图 JS Sails。此外,您可以快速、单独地生成每个模型、视图、控制器或同时生成所有三个。

        使用命令安装

        npm installsails-inverse-model -g

        了解工具

        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
        

        【讨论】:

          猜你喜欢
          • 2016-03-15
          • 1970-01-01
          • 1970-01-01
          • 2011-02-02
          • 2012-07-01
          • 1970-01-01
          • 2012-05-18
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多