template_index.js.html 9.54 KB
<!DOCTYPE html>

<html lang="en">
<head>
	<meta charset="utf-8">
	<title>sequelize-pg-generator Source: template/index.js</title>

	<!--[if lt IE 9]>
	<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
	<![endif]-->
	<link type="text/css" rel="stylesheet" href="styles/sunlight.default.css">

	<link type="text/css" rel="stylesheet" href="styles/site.cerulean.css">

</head>

<body>
<div class="container-fluid">
	<div class="navbar navbar-fixed-top navbar-inverse">
		<div class="navbar-inner">
			<a class="brand" href="index.html">sequelize-pg-generator</a>
			<ul class="nav">
				
				<li class="dropdown">
					<a href="modules.list.html" class="dropdown-toggle" data-toggle="dropdown">Modules<b
						class="caret"></b></a>

					<ul class="dropdown-menu ">
						
						<li>
							<a href="module-lib_index.html">lib/index</a>
						</li>
						
						<li>
							<a href="module-path_to_model.html">path/to/model</a>
						</li>
						

					</ul>
				</li>
				
				<li class="dropdown">
					<a href="classes.list.html" class="dropdown-toggle" data-toggle="dropdown">Classes<b
						class="caret"></b></a>

					<ul class="dropdown-menu ">
						
						<li>
							<a href="GeneratorUtil.html">GeneratorUtil</a>
						</li>
						

					</ul>
				</li>
				
			</ul>
		</div>
	</div>

	<div class="row-fluid">

		
			<div class="span12">
				
				<div id="main">
					


		<h1 class="page-title">Source: template/index.js</h1>
    
<section>
	<article>
		<pre
			class="sunlight-highlight-javascript linenums">/**
 * This file is auto generated by sequelize-pg-generator.
 * @module path/to/model
 * @author Özüm Eldoğan
 */

/*jslint node: true, stupid: true, nomen: true */
"use strict";

var Sequelize           = require('sequelize'),
    fs                  = require('fs'),
    path                = require('path'),
    util                = require('util'),
    models              = {},
    relationships       = {},
    modelsPath          = '',
    definitionDir       = 'definition-files',
    definitionDirCustom = definitionDir + '-custom',
    debug               = false,
    debugFD             = '',
    sequelize           = null;

/**
 * Requires given npm module file and defines sequelize module according to exported object from that module.
 * @private
 * @param {string} file - Location of the npm module file which contains model definition.
 */
function defineModel(file) {
    var object      = require(file),
        options     = object.options || {},
        modelName   = object.modelName;

    fs.writeSync(debugFD, 'var ' + modelName + ' = Sequelize.define("' + modelName + '",\n' + util.inspect(object.attributes) + ',\n' + util.inspect(options, {depth: null}) + ');\n\n\n');

    models[modelName] = sequelize.define(modelName, object.attributes, options);
    if (object.relations !== undefined) {
        relationships[modelName] = object.relations;
    }
}


/**
 * Define 'hasMany' and 'belongsTo' relations.
 * @private
 * @param {string} modelName - Name of the model
 * @param {string} relationType - Type of the relation 'hasMany' or 'belongsTo'
 * @param {string} targetModelName - Name of the target model which this relation related to.
 * @param {Object} options - Sequelize options of relation.
 */
function defineRelation(modelName, relationType, targetModelName, options) {
    fs.writeSync(debugFD, modelName + '.' + relationType + '(' + targetModelName + ', ' + util.inspect(options, {depth: null}) + ');\n\n');
    models[modelName][relationType](models[targetModelName], options); // account.hasMany(contact, {...})
}


/**
 * Generates list of definition files by looking given modelsPath directory. It traverses 'definition-files'
 * and 'definition-files-custom' directories to generate the list. Files in 'definition-files' will be skipped
 * if a file with same name is found in 'definition-files-custom' directory.
 * @private
 * @returns {Array}
 * @example
 * var list = getFileList('../model'); // ['./definition-files-custom/public_cart.js', './definition-files/public_account.js]
 */
function getFileList() {
    var i, file, isOverridden = {}, files = [], customFiles, baseFiles;
    baseFiles = fs.readdirSync(path.join(modelsPath, definitionDir));
    customFiles = fs.readdirSync(path.join(modelsPath, definitionDirCustom));

    for (i = 0; i &lt; customFiles.length; i = i + 1) {
        file = customFiles[i];
        if (file.match(/\.js$/)) {
            isOverridden[file] = true;
            files.push('./' + path.join(definitionDirCustom, file));
        }
    }

    for (i = 0; i &lt; baseFiles.length; i = i + 1) {
        file = baseFiles[i];
        if (!isOverridden[file] &amp;&amp; file.match(/\.js$/)) {
            files.push('./' + path.join(definitionDir, file));
        }
    }
    return files;
}


/**
 * Iterates all relations and executes given callback on them. Callback is passed
 * ({string} modelName, {string} relationType, {string} targetModelName, {Object} relationOptions)
 * @private
 * @param {function} callback - Callback function.
 * @returns {Array}
 */
function processAllRelations(callback) {
    var i, modelName, relation, relations, result = [];
    for (modelName in relationships) {
        if (relationships.hasOwnProperty(modelName)) {
            relations = relationships[modelName];
            for (i = 0; i &lt; relations.length; i = i + 1) {
                relation = relations[i];
                result.push(callback(modelName, relation.type, relation.model, relation.details));
            }
        }
    }
    return result;
}


/**
 * Initializes models.
 * @private
 */
function init() {
    var debugFile = path.join(__dirname, 'debug.js');
    if (fs.existsSync(debugFile)) { fs.unlinkSync(debugFile); }
    debugFD = fs.openSync(debugFile, 'a');

    getFileList(modelsPath).forEach(defineModel);
    processAllRelations(defineRelation);

    fs.closeSync(debugFD);
}

module.exports = {};

/**
 * Sets up sequelize models based on model files in given directory for given database details.
 * @param {string} database - Name of the database to connect
 * @param {string} username - Username of the database
 * @param {string} password - Password of the database
 * @param {Object} obj - Object to pass new Sequelize() function. See Sequelize for details.
 * @example
 * var orm = require('../model');
 * orm.setup('path/to/model', 'database', 'user', 'password', {
     *     host: '127.0.0.1',
     *     logging: false,
     *     native: true
     * });
 */
module.exports.setup = function (database, username, password, obj) {
    if (typeof obj !== 'object') { obj = {}; }
    if (obj.dialect === undefined || obj.dialect === null) { obj.dialect = 'postgres'; }

    if (arguments.length === 2) {
        sequelize = new Sequelize(database, username);
    } else if (arguments.length === 3) {
        sequelize = new Sequelize(database, username, password);
    } else if (arguments.length === 4) {
        sequelize = new Sequelize(database, username, password, obj);
    }
    modelsPath = __dirname;
    init();
};

/**
 * Returns requested model with given name.
 * @param {string} name - Model name
 * @returns {object} - Sequelize model
 */
module.exports.model = function (name) {
    return models[name];
};

/**
 * Returns Sequelize object.
 * @returns {Sequelize} - Sequelize object
 */
module.exports.Sequelize = function () {
    return Sequelize;
};

/**
 * Returns sequelize instance.
 * @returns {Sequelize} - sequelize instance
 */
module.exports.sequelize = function () {
    return sequelize;
};
</pre>
	</article>
</section>





				</div>

				<div class="clearfix"></div>
				<footer>
					
					
		<span class="copyright">
		sequelize-pg-generator Copyright © 2014 Özüm Eldoğan.
		</span>
					<br />
					
		<span class="jsdoc-message">
		Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.3.1</a>
		on Mon Aug 3rd 2015 using the <a
			href="https://github.com/terryweiss/docstrap">DocStrap template</a>.
		</span>
				</footer>
			</div>

			
			<br clear="both">
		</div>

	</div>
	<!--<script src="scripts/sunlight.js"></script>-->
	<script src="scripts/docstrap.lib.js"></script>
	<script src="scripts/bootstrap-dropdown.js"></script>
	<script src="scripts/toc.js"></script>

	<script>
		$( function () {
			$( "[id*='$']" ).each( function () {
				var $this = $( this );

				$this.attr( "id", $this.attr( "id" ).replace( "$", "__" ) );
			} );

			$( "#toc" ).toc( {
				anchorName  : function ( i, heading, prefix ) {
					return $( heading ).attr( "id" ) || ( prefix + i );
				},
				selectors   : "h1,h2,h3,h4",
				showAndHide : false,
				scrollTo    : "100px"
			} );

			$( "#toc>ul" ).addClass( "nav nav-pills nav-stacked" );
			$( "#main span[id^='toc']" ).addClass( "toc-shim" );
			$( '.dropdown-toggle' ).dropdown();
//			$( ".tutorial-section pre, .readme-section pre" ).addClass( "sunlight-highlight-javascript" ).addClass( "linenums" );

			$( ".tutorial-section pre, .readme-section pre" ).each( function () {
				var $this = $( this );

				var example = $this.find( "code" );
				exampleText = example.html();
				var lang = /{@lang (.*?)}/.exec( exampleText );
				if ( lang && lang[1] ) {
					exampleText = exampleText.replace( lang[0], "" );
					example.html( exampleText );
					lang = lang[1];
				} else {
					lang = "javascript";
				}

				if ( lang ) {

					$this
						.addClass( "sunlight-highlight-" + lang )
						.addClass( "linenums" )
						.html( example.html() );

				}
			} );

			Sunlight.highlightAll( {
				lineNumbers : true,
				showMenu : true,
				enableDoclinks : true
			} );
		} );
	 </script>



	<!--Navigation and Symbol Display-->
	


	<!--Google Analytics-->
	

</body>
</html>