不要怂,就是干,撸起袖子干!

lib_sequelize.js.html 9.67 KB
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>lib&#x2F;sequelize.js</title>
    <link rel="stylesheet" href="http:&#x2F;&#x2F;yui.yahooapis.com&#x2F;3.8.0&#x2F;build&#x2F;cssgrids&#x2F;cssgrids-min.css">
    <link rel="stylesheet" href="..&#x2F;assets/vendor/prettify/prettify-min.css">
    <link rel="stylesheet" href="..&#x2F;assets/css/main.css" id="site_styles">
    <link rel="shortcut icon" type="image/png" href="..&#x2F;assets/favicon.png">
    <script src="http:&#x2F;&#x2F;yui.yahooapis.com&#x2F;combo?3.8.0&#x2F;build&#x2F;yui&#x2F;yui-min.js"></script>
</head>
<body class="yui3-skin-sam">

<div id="doc">
    <div id="hd" class="yui3-g header">
        <div class="yui3-u-3-4">
            
                <h1><img src="..&#x2F;assets/css/logo.png" title=""></h1>
            
        </div>
        <div class="yui3-u-1-4 version">
            <em>API Docs for: </em>
        </div>
    </div>
    <div id="bd" class="yui3-g">

        <div class="yui3-u-1-4">
            <div id="docs-sidebar" class="sidebar apidocs">
                <div id="api-list">
    <h2 class="off-left">APIs</h2>
    <div id="api-tabview" class="tabview">
        <ul class="tabs">
            <li><a href="#api-classes">Classes</a></li>
            <li><a href="#api-modules">Modules</a></li>
        </ul>

        <div id="api-tabview-filter">
            <input type="search" id="api-filter" placeholder="Type to filter APIs">
        </div>

        <div id="api-tabview-panel">
            <ul id="api-classes" class="apis classes">
            
                <li><a href="..&#x2F;classes/QueryInterface.html">QueryInterface</a></li>
            
                <li><a href="..&#x2F;classes/Sequelize.html">Sequelize</a></li>
            
            </ul>

            <ul id="api-modules" class="apis modules">
            
                <li><a href="..&#x2F;modules/Sequelize.html">Sequelize</a></li>
            
            </ul>
        </div>
    </div>
</div>

            </div>
        </div>
        <div class="yui3-u-3-4">
                <div id="api-options">
        Show:
        <label for="api-show-inherited">
            <input type="checkbox" id="api-show-inherited" checked>
            Inherited
        </label>

        <label for="api-show-protected">
            <input type="checkbox" id="api-show-protected">
            Protected
        </label>

        <label for="api-show-private">
            <input type="checkbox" id="api-show-private">
            Private
        </label>
        <label for="api-show-deprecated">
            <input type="checkbox" id="api-show-deprecated">
            Deprecated
        </label>

    </div>


            <div class="apidocs">
                <div id="docs-main">
                    <div class="content">
                        <h1 class="file-heading">File: lib&#x2F;sequelize.js</h1>

<div class="file">
    <pre class="code prettyprint linenums">
var Utils             = require(&quot;.&#x2F;utils&quot;)
  , DAOFactory        = require(&quot;.&#x2F;dao-factory&quot;)
  , DataTypes         = require(&#x27;.&#x2F;data-types&#x27;)
  , DAOFactoryManager = require(&quot;.&#x2F;dao-factory-manager&quot;)
  , QueryInterface    = require(&quot;.&#x2F;query-interface&quot;)

if (typeof process != &#x27;undefined&#x27; &amp;&amp; parseFloat(process.version.replace(&#x27;v&#x27;, &#x27;&#x27;)) &lt; 0.6) {
  console.log(&quot;DEPRECATION WARNING: Support for Node.JS &lt; v0.6 will be canceled in the next minor release.&quot;)
}

module.exports = (function() {
  &#x2F;**
    Main class of the project.

    @param {String} database The name of the database.
    @param {String} username The username which is used to authenticate against the database.
    @param {String} [password] The password which is used to authenticate against the database.
    @param {Object} [options] An object with options.

    @example
        &#x2F;&#x2F; without password and options
        var sequelize = new Sequelize(&#x27;database&#x27;, &#x27;username&#x27;)

        &#x2F;&#x2F; without options
        var sequelize = new Sequelize(&#x27;database&#x27;, &#x27;username&#x27;, &#x27;password&#x27;)

        &#x2F;&#x2F; without password &#x2F; with blank password
        var sequelize = new Sequelize(&#x27;database&#x27;, &#x27;username&#x27;, null, {})

        &#x2F;&#x2F; with password and options
        var sequelize = new Sequelize(&#x27;my_database&#x27;, &#x27;john&#x27;, &#x27;doe&#x27;, {})

    @class Sequelize
    @constructor
  *&#x2F;
  var Sequelize = function(database, username, password, options) {
    this.options = Utils._.extend({
      dialect: &#x27;mysql&#x27;,
      host: &#x27;localhost&#x27;,
      port: 3306,
      protocol: &#x27;tcp&#x27;,
      define: {},
      query: {},
      sync: {},
      logging: console.log,
      omitNull: false,
      queue: true,
      native: false,
      replication: false,
      pool: {}
    }, options || {})

    if (this.options.logging === true) {
      console.log(&#x27;DEPRECATION WARNING: The logging-option should be either a function or false. Default: console.log&#x27;)
      this.options.logging = console.log
    }

    this.config = {
      database: database,
      username: username,
      password: (( ([&quot;&quot;, null, false].indexOf(password) &gt; -1) || (typeof password == &#x27;undefined&#x27;)) ? null : password),
      host    : this.options.host,
      port    : this.options.port,
      pool    : this.options.pool,
      protocol: this.options.protocol,
      queue   : this.options.queue,
      native  : this.options.native,
      replication: this.options.replication,
      maxConcurrentQueries: this.options.maxConcurrentQueries
    }

    var ConnectorManager = require(&quot;.&#x2F;dialects&#x2F;&quot; + this.options.dialect + &quot;&#x2F;connector-manager&quot;)

    this.daoFactoryManager = new DAOFactoryManager(this)
    this.connectorManager  = new ConnectorManager(this, this.config)

    this.importCache = {}
  }

  &#x2F;**
    Reference to Utils
  *&#x2F;
  Sequelize.Utils = Utils

  for (var dataType in DataTypes) {
    Sequelize[dataType] = DataTypes[dataType]
  }

  Sequelize.prototype.getQueryInterface = function() {
    this.queryInterface = this.queryInterface || new QueryInterface(this)
    return this.queryInterface
  }

  Sequelize.prototype.getMigrator = function(options, force) {
    var Migrator = require(&quot;.&#x2F;migrator&quot;)
    if (force) {
      this.migrator = new Migrator(this, options)
    } else {
      this.migrator = this.migrator || new Migrator(this, options)
    }

    return this.migrator
  }

  Sequelize.prototype.define = function(daoName, attributes, options) {
    options = options || {}
    var globalOptions = this.options

    if (globalOptions.define) {
      options = Utils._.extend({}, globalOptions.define, options)
      Utils._([&#x27;classMethods&#x27;, &#x27;instanceMethods&#x27;]).each(function(key) {
        if (globalOptions.define[key]) {
          options[key] = options[key] || {}
          Utils._.extend(options[key], globalOptions.define[key])
        }
      })
    }
    options.omitNull = globalOptions.omitNull

    var factory = new DAOFactory(daoName, attributes, options)
    this.daoFactoryManager.addDAO(factory.init(this.daoFactoryManager))
    return factory
  }

  Sequelize.prototype.isDefined = function(daoName) {
    var daos = this.daoFactoryManager.daos
    return (daos.filter(function(dao) { return dao.name === daoName }).length !== 0)
  }

  Sequelize.prototype.import = function(path) {
    if (!this.importCache[path]) {
      var defineCall = require(path)
      this.importCache[path] = defineCall(this, DataTypes)
    }

    return this.importCache[path]
  }

  Sequelize.prototype.migrate = function(options) {
    this.getMigrator().migrate(options)
  }

  Sequelize.prototype.query = function(sql, callee, options) {
    if (arguments.length === 3) {
      options = options
    } else if (arguments.length === 2) {
      options = {}
    } else {
      options = { raw: true }
    }

    options = Utils._.extend(Utils._.clone(this.options.query), options)
    options = Utils._.defaults(options, {
      logging: this.options.hasOwnProperty(&#x27;logging&#x27;) ? this.options.logging : console.log,
      type: (sql.toLowerCase().indexOf(&#x27;select&#x27;) === 0) ? &#x27;SELECT&#x27; : false
    })

    return this.connectorManager.query(sql, callee, options)
  }

  Sequelize.prototype.sync = function(options) {
    options = options || {}

    if (this.options.sync) {
      options = Utils._.extend({}, this.options.sync, options)
    }

    var chainer = new Utils.QueryChainer()

    this.daoFactoryManager.daos.forEach(function(dao) {
      chainer.add(dao.sync(options))
    })

    return chainer.run()
  }

  Sequelize.prototype.drop = function() {
    var self = this

    return new Utils.CustomEventEmitter(function(emitter) {
      var chainer = new Utils.QueryChainer

      self.daoFactoryManager.daos.forEach(function(dao) { chainer.add(dao.drop()) })

      chainer
        .run()
        .success(function() { emitter.emit(&#x27;success&#x27;, null) })
        .error(function(err) { emitter.emit(&#x27;error&#x27;, err) })
    }).run()
  }

  return Sequelize
})()

    </pre>
</div>

                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script src="..&#x2F;assets/vendor/prettify/prettify-min.js"></script>
<script>prettyPrint();</script>
<script src="..&#x2F;assets/js/yui-prettify.js"></script>
<script src="..&#x2F;assets/../api.js"></script>
<script src="..&#x2F;assets/js/api-filter.js"></script>
<script src="..&#x2F;assets/js/api-list.js"></script>
<script src="..&#x2F;assets/js/api-search.js"></script>
<script src="..&#x2F;assets/js/apidocs.js"></script>
</body>
</html>