zoukankan      html  css  js  c++  java
  • sequelize翻译(1)

    第一次翻译(由mongoose转了mysql) v 3.0.0

    1.Sequelize类 2.sequelize对象 3.sequelize.define()返回的表对象 4.表对象的方法

    1.Sequelize类(就是一个构造函数)

    new Sequelize(database, [username=null], [password=null], [options={}])

    new Sequelize(uri, [options={}])

    两种方式连接数据库

    //注意:在官方例子中,有 without password and options (省略密码的连接,坑爹玩意),对于mysql是必须写username和passport,

    如果没有username和passport,以''代替,否则会报错.同时要保证数据库mysql运行着.

    var sequelize = new Sequelize('database', 'username', 'password', {

       host: 'localhost', dialect: 'mysql'|'sqlite'|'postgres'|'mssql',

      pool: { max: 5, min: 0, idle: 10000 },

     // SQLite only storage: 'path/to/database.sqlite'

    });

    // Or you can simply use a connection uri

    var sequelize = new Sequelize('postgres://user:pass@example.com:5432/dbname');

    2.检测数据库连接

    使用sequelize对象的authenticate(),返回一个promise.(一般在项目启动的时候检测)

    sequelize .authenticate() .then(function(result) {  

       console.log('Connection has been established successfully.');

    }) .catch(function (err) {

    console.log('Unable to connect to the database:', err);

    });

    3.创建表,使用sequelize对象的define方法

    sequelize.define('name', {attributes}, {options})

    var User = sequelize.define('user', {

       firstName: { type: Sequelize.STRING },

      lastName: { type: Sequelize.STRING }

    });

    // force: true will drop the table if it already exists ,为了预防定义表的时候,表结构已经存在,所以先删除,

    If force is true, each DAO will do DROP TABLE IF EXISTS ..., before it tries to create its own table.

    建议force: false ,否则就会把以前的表都删掉,呵呵,就偷着哭吧

    //使用sync方法,是第一次的时候Sync all defined models to the DB,

    User.sync({force: true}).then(function () { 

    // Table created return

    User.create({ firstName: 'John', lastName: 'Hancock' });

    });

  • 相关阅读:
    求1000以内的勾股数的算法流程图
    23天的单车旅行,从广州到四川,重庆,大足石刻,母校,家,发完了
    好玩的Flash:跟鼠标拼命
    23天的单车旅行,从广州到四川,息烽集中营,韩国人,乌江,遵义
    如何彻底删除SQL Server2005
    EOF,EOC与感叹号的区别
    如何彻底卸载vs2008及其组件?
    PHP中extract()函数的妙用
    php 函数合并 array_merge 与 + 的区别
    unserialize error at offset
  • 原文地址:https://www.cnblogs.com/jay--zhang/p/5856897.html
Copyright © 2011-2022 走看看