美文网首页
Sequelize.js

Sequelize.js

作者: moxhuis | 来源:发表于2015-10-27 17:27 被阅读0次

    Sequelize.js --node ORM

    提供对MySQL,MariaDB,SQLite和PostgreSQL数据库的简单访问,通过映射数据库条目到对象,或者对象到数据库条目。

    install

    $ npm install --save sequelize
    

    建立连接

    var sequelize = new Sequelize(
    'database',
     'username',
     'password', 
     {
       host: 'localhost',
       dialect: 'mysql'|'mariadb'|'sqlite'|'postgres'|'mssql',
       pool: { max: 5, min: 0, idle: 10000 }, // SQLite only
       storage: 'path/to/database.sqlite'
      }  
    );
    

    创建modal

    var User = sequelize.define('user', 
    { 
      firstName: {
       type: Sequelize.STRING,
       field: 'first_name' 
      },
      lastName: {
       type: Sequelize.STRING 
      }
    }, {
     freezeTableName: true // 
    }
    );
    
    User.sync({force: true}).then(function () { // Table created
       return User.create({ firstName: 'John', lastName: 'Hancock' });
    });
    

    相关文章

      网友评论

          本文标题:Sequelize.js

          本文链接:https://www.haomeiwen.com/subject/mrvxhttx.html