index.html
41.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>sequelize-pg-generator Index</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="span8">
<div id="main">
<span class="page-title">Index</span>
<section class="readme-section">
<article><h1>Use pg-generator instead of this module (NEWER & BETTER)</h1><p>pg-generator is successor of this module and it is much more intuitive, flexible and easier to use.</p>
<p>It can generate more than sequelize. Also it generates sequelize much better.</p>
<p>Links of pg-generator:</p>
<ul>
<li>pg-generator's <a href="http://www.pg-generator.com/builtin-templates/sequelize/">sequelize page</a> </li>
<li><a href="https://www.npmjs.com/package/pg-generator">npm</a></li>
<li><a href="https://github.com/ozum/pg-generator">github</a></li>
<li><a href="http://www.pg-generator.com">web</a></li>
</ul>
<h2>Other Languages</h2><p>Türkçe: <a href="https://github.com/ozum/sequelize-pg-generator/blob/master/README-TR.md">Turkish Documentation is here</a></p>
<h2>Description</h2><p>This module is for auto generating Sequelize model from PostgreSQL databases. It reverse engineers your database and generates separate model files for each table. Default configuration parameters are carefully selected and they are sane to use. However it is possible to change most of the behaviours via configuration. This document is based on default configuration. Configuration parameters and default values are written as (Config parameter:value) where appropriate.</p>
<h2>Special Thanks</h2><p>This module is developed with the sponsorship of Ozcorp.</p>
<h2>Usage</h2><h3>Step 1: Install globally via npm -g</h3><p>This will install module and CLI command</p>
<pre class="prettyprint source"><code>$ npm install -g sequelize-pg-generator</code></pre><h3>Step 2: Generate model files</h3><p>Open terminal, go to your app.js root and create your models automatically into 'model' directory.</p>
<pre class="prettyprint source"><code>$ cd path/to/my/node-app.js
$ spgen -d my_database -u my_user -p my_password</code></pre><h3>Step 3: Use it in your node.js app</h3><p>Use Sequelize models provided by auto generated files in your application. See examples below.</p>
<pre class="prettyprint source"><code>var orm = require('./model');
orm.setup('my_database', 'my_user', 'my_password', {
host: '127.0.0.1',
logging: false,
native: false
});
var sequelize = orm.sequelize;
var contact = orm.model('public.contact'); // Can be configured without schema.</code></pre><h2>Windows Users</h2><p>Some tips to install on windows. This module uses pg. If you like to use pg-native, below are some tips for windows users for a successful install:</p>
<ul>
<li>Python 2 should be installed. As of this writing it is not compatible with Python 3.</li>
<li>You should add Python to the path and set PYTHONPATH via environment variables.</li>
<li>PostgreSQL pg_config and libpq.dll must be on path. Usually adding bin and lib folders is ok. (For eaxmple: C:\Program Files\PostgreSQL\9.3\bin C:\Program Files\PostgreSQL\9.3\lib)</li>
<li>Visual Studio Build Tools (C:\Program Files (x86)\MSBuild) should be installed. Installed automatically after VS 2012. If during install npm asks for different version of Visual Studio you can set it from command line during installation:<blockquote>
<p>npm install -g sequelize-pg-generator --msvs_version=2013</p>
</blockquote>
</li>
</ul>
<h2>CLI Options</h2><pre class="prettyprint source"><code>spgen [options]
-h, --host [host] IP address or host name of the database server
--port [port] Port of database server to connect
-d, --database [database] Database name
-u, --user [user] Username to connect to database
-p, --password [password] Password to connect to database
-s, --schema [schema] Comma separated names of the database schemas
-o, --output [output] Output folder
-c, --config [config] Path of the configuration file
-t, --templateName Use builtin template folder with given name
--nolog No log output
--resetConfig Reset configuration. (Side-step. Not for production.)
--throwError Instead of logging errors to console, throws error.</code></pre><ul>
<li>Fully documented. (JSDoc HTML files are under doc directory),</li>
<li>Tested,</li>
<li>No Dependencies on Generated Files,</li>
<li>Multi schema support,</li>
<li>One to many relation support (hasMany and belongsTo),</li>
<li>Many to many relation support (hasMany through and belongsToMany),</li>
<li>Inter-schema relation support. (i.e. public.account table to other_schema.cutomer table),</li>
<li>Highly configurable,</li>
<li>Fully customizable,</li>
<li>CLI support,</li>
<li>Smart naming of models and relations,</li>
<li>Very easy to override auto generated files,</li>
<li>Exclude tables,</li>
<li>Debug,</li>
<li>Table Specific Configuration,</li>
<li>Validates and prevents naming clash.</li>
</ul>
<h2>WARNING: belongsToMany</h2><p>For many to many relations Sequelize version 2.0 RC3 and older does not support belongsToMany relation. After this version hasMany through relations are deprecated. Behaviour on this subject can be adjusted via configuration. (Config: generate.hasManyThrough:false and generate.belongsToMany:true)</p>
<h2>Features</h2><h3>No Dependencies On Generated Files</h3><p>Generated files have no dependencies besides core modules and Sequelize.</p>
<h3>Multi Schema Support</h3><p>Supports multi PostgreSQL schemas. It is possible to have other schemas than public. User can select which schemas to reverse engineer via CLI or config. If more than one schema is included, models may be prefixed with schema names (Config: generate.useSchemaName: true, database.schema: ["public"]) to prevent tables with same name in different schemas try to have same model name.</p>
<pre class="prettyprint source"><code>contact = orm.model('public.contact'); // Returns sequelize model for contact table.</code></pre><h3>No Schema Prefix</h3><p>User can configure not to prefix model names with schema</p>
<pre class="prettyprint source"><code>// In config
{...
generate{
useSchemaName: false
}
}
contact = orm.model('contact'); // Returns sequelize model for contact table.</code></pre><h3>One To Many Relation Support</h3><p>This module automatically detects one to many relations and generates model.hasMany and model.belongsTo sequelize relations.</p>
<h3>Many To Many Relation Support</h3><p>If two tables are joined via a join table this module detects it automatically and generates many to many sequelize relations. If a table has more than one foreign key, then it is considered many to many relation join table.</p>
<pre class="prettyprint source"><code> hasMany hasMany
TABLE: product --------< line_item >--------- cart
COLUMNS: id cart_id (FK) id
name product_id (FK) customer_id (FK)
color quantity</code></pre><p>This module generates belongsToMany relation and hasMany relation with through option. As of this writing master branch of Sequelize deprecated hasMany through relations. According to version of Sequelize you use, it should be adjusted via config of this module.</p>
<h3>Inter-Schema Relation Support</h3><p>Detects relations between tables in different schemas. For example relations between public.account table and other_schema.customer table.</p>
<h3>Highly configurable</h3><p>This module uses <a href="https://www.npmjs.com/package/config">config</a> module. It is also possible to point a custom configuration file via CLI. See configuration parameters below in this document.</p>
<h3>Fully Customizable</h3><p>This module uses <a href="https://www.npmjs.com/package/consolidate">consolidate</a> compatible templates to generate model files. It uses <a href="https://www.npmjs.com/package/swig">Swig</a> by default. User can use his/her custom templates without altering original one by pointing (Config: template.folder and template.engine:'swig') config values. Looking default templates in template folder of this module is highly recommended.</p>
<p>There should at least be three files in custom template folder:<br> index.ext Default template file. ext is whatever extension is used for your template engine.<br> index.js This file is copied with generated files. It's purpose is use generated files<br> utils.js This file is copied with generated files. Contains helper functions.</p>
<h3>CLI Support</h3><p>If this module is installed as suggested globally with npm -g then spgen command would be available system wide to generate model files.</p>
<h3>Easily Override Relationship Aliases and Smart Names</h3><p>If auto generated relationship names and aliases are not so smart for you, it is very easy to override aliases. After first use, a file called <code>alias.json</code> is generated in output folder. Every alias name generated is listed in this file. You can override any names/aliases. Future generated files will use overridden names from this file. If you are OK with some names, leave it as null. </p>
<h3>Smart Naming of Models and Relations</h3><p>sequelize-pg-generator uses table names or schema.table names for model naming. For relations it uses foreign key names and relation names from your database. (You are naming your relations in database meaningfully right?) Both camel case (tableName) or untouched names (table_name) methods can be used via configuration. Naming conventions are based on Sequelize module suggestions and generated explicitly with 'as' parameter.</p>
<pre class="prettyprint source"><code> product_cart_line_items cart_cart_line_items
TABLE: product -------------------------< line_item >--------------------- cart
COLUMNS: id cart_id (FK) id
name product (FK) customer_id (FK)
color quantity
NOTE: Beware line_item.cart_id has id suffix but line_item.product hasn't. This inconsistency is made purposefully for the sake of this example.
Type of object Naming Rule
-------------- -----------
Model tableName or schema.tableName
hasMany Plural of the relation name in database. Table name from beginning can be stripped.
(Config: generate.stripFirstTableFromHasMany:true)
belongsTo Singular of foreign key. If key name ends with _id it will be stripped. Otherwise
'related' is added at the beginning to prevent it gets clash with column name.
(Config: generate.prefixForBelongsTo:'related')
belongsToMany Plural of the join table name + foreign key which refers other table in join table.
hasMany({through:..}) Plural of the join table name + foreign key which refers other table in join table. (DEPRECATED in Sequelize)
For the example structure:
Relation as Details
-------- -- -------
product.hasMany as:'cartLineItems' (Plural) Table name 'product' is stripped from the beginning
of relation name 'product_cart_line_items'
product.belongsToMany as:'cartLineItemCarts' (Plural) _id suffix is stripped from, relation name (table name striiped)
added to foreign key name 'cart_id'
product.hasMany Through as:'cartLineItemCarts' (Plural) _id suffix is stripped from, relation name (table name striiped)
added to foreign key name 'cart_id'
cart.hasMany as:'cartLineItems' (Plural) Table name 'cart' is stripped from the beginning of
relation name 'cart_cart_line_items'
cart.belongsToMany as:'relatedCartLineItemProducts' (Plural) No _id suffix. 'related' and relation name (table name striiped)
are added as prefix.
cart.hasMany Through as:'relatedcartLineItemProducts' (Plural) No _id suffix. 'related' and relation name (table name striiped)
are added as prefix.
lineItem.belongsTo as:'relatedProduct' (Singular) No _id suffix. 'related' is added as prefix.
lineItem.belongsTo as:'cart' (Singular) _id suffix is stripped from foreign key name
'cart_id'.</code></pre><p>Of course as all attributes, you can modify generated files in a non-destructive way as explained below.</p>
<h3>Very Easy to Override Auto Generated Files</h3><p>By default auto generated files are located path/to/model/definition-files directory. Also there is 'definition-files-custom' directory. Users can create files with same names as auto generated files to override its attributes. There is also utils module generated to make modifications easier.</p>
<p>Those modifications are non destructive, because they override generated file in another file by inheriting it and default index.js file uses inherited files if it exists. Please bear in mind, those modifications occur before sequelize instances are generated.</p>
<p>For example for cart table 'definition-files/cart.js' is generated. User can create 'definition-files-custom/cart.js' and override necessary parts like example below. For all attributes you can look inside auto generated files.</p>
<pre class="prettyprint source"><code>"use strict";
var orm = require('../index.js'),
model = require('../definition-files/public_cart.js'),
util = require('../utils.js')(model),
Seq = orm.Sequelize();
module.exports = model;
util.getAttribute('id').validate = {... Some Sequelize Validations}; // Add Sequelize validation.
util.getRelation('relatedProducts').details.as = 'soldItems'; // Don't like default relation name? Change it.
util.renameAttribute('customerId', 'clientId'); // Change name of the attribute.</code></pre><h3>Exclude Tables</h3><p>It is possible to exclude some table from auto generation. (Config generate.skipTable:[]) array is used to define excluded tables. sequelize-pg-generator skips those tables and relations from and to those tables.</p>
<h3>Debug</h3><p>When required and executed first time from your app, default index.js file creates a file called debug.js in the model directory. This file can be examined what type of code is used by index.js. It is same code that would be used if there is no index.js file exists. However if this type of static file is used, it is harder to allow modifications in a non-destructive way.</p>
<h3>Validates and Prevents Naming Clash</h3><p>sequelize-pg-generator prevents naming clas by validating all relation names if same name/alias exists on the same table.</p>
<h3>Table Specific Configuration</h3><p>Sometimes for some tables it is needed to have different rules then other tables have. In such situations configuration file allows table level overrides. All 'generate' and 'tableOptions' config parameters can be overridden with 'generateOverride' and 'tableOptionsOverride'.</p>
<p>Below is an example for contact table have specific configuration overrides.</p>
<pre class="prettyprint source"><code>"generate": {
"columnDescription": true,
"tableDescription": true,
...
},
"generateOverride": {
"contact": {
"tableDescription": false
}
},
"tableOptions": {
"timestamps": false,
"camelCase": true,
"paranoid": false,
...
},
"tableOptionsOverride": {
"contact": {
"paranoid": true
}
}
...</code></pre><h1>Configuration</h1><p>Configuration parameters and default values are described below. Configuration is enclosed in "sequelize-pg-generator" key, because you may want to combine sequelize-pg-generator configuration with your main application configuration. This way generator's configuration does not clash with yours. <a href="https://www.npmjs.com/package/node-config">node-config</a> allows this.</p>
<table><br> <tr><br> <td colspan="3"><h4><strong>database</strong></h4></td><br> </tr><br> <tr><br> <td width="130">host</td><br> <td width="85">string</td><br> <td>IP address or host name of the database server.</td><br> </tr><br> <tr><br> <td>port</td><br> <td>number</td><br> <td>Port of database server to connect.</td><br> </tr><br> <tr><br> <td>database</td><br> <td>string</td><br> <td>Database name.</td><br> </tr><br> <tr><br> <td>user</td><br> <td>string</td><br> <td>Username to connect to database.</td><br> </tr><br> <tr><br> <td>password</td><br> <td>string</td><br> <td>Password to connect to database.</td><br> </tr><br> <tr><br> <td>schema</td><br> <td>Array(string)</td><br> <td>Array of names of the database schemas to be parsed.</td><br> </tr><br> <tr><br> <td colspan="3"><h4><strong>template</strong></h4></td><br> </tr><br> <tr><br> <td>engine</td><br> <td>string</td><br> <td>Template engine to use for generating model files. Any <a href="https://www.npmjs.com/package/consolidate">consolidate</a> compatible template engine can be used.</td><br> </tr><br> <tr><br> <td>extension</td><br> <td>string</td><br> <td>Extension of template files.</td><br> </tr><br> <tr><br> <td>folder</td><br> <td>string</td><br> <td>Path of the template directory which contains template files.</td><br> </tr><br> <tr><br> <td colspan="3"><h4><strong>output</strong></h4></td><br> </tr><br> <tr><br> <td>log</td><br> <td>boolean</td><br> <td>Generate log during auto-generation to console.</td><br> </tr><br> <tr><br> <td>folder</td><br> <td>string</td><br> <td>Path to output directory for generated model files.</td><br> </tr><br> <tr><br> <td>beautify</td><br> <td>boolean</td><br> <td>Format code nicely with <a href="http://jsbeautifier.org">js-beautifier</a>.</td><br> </tr><br> <tr><br> <td>indent</td><br> <td>number</td><br> <td>Number of spaces used for each indentation level in generated files.</td><br> </tr><br> <tr><br> <td>preserveNewLine</td><br> <td>boolean</td><br> <td>Preserve new lines coming from templates during generation.</td><br> </tr><br> <tr><br> <td>warning</td><br> <td>boolean</td><br> <td>If set true, generator includes informative warning text inside generated files. This text is about how to customize and override default models.</td><br> </tr><br> <tr><br> <td colspan="3"><h4><strong>generate</strong></h4></td><br> </tr><br> <tr><br> <td>stripFirstTableFromHasMany</td><br> <td>boolean</td><br> <td>If this is set true. Generator strips first table name from has many relations' name if it begins with table name. For example "product_cart_line_items" relation becomes "cart_line_items" for "product" table.</td><br> </tr><br> <tr><br> <td>addTableNameToManyToMany</td><br> <td>boolean</td><br> <td>If this is set true. Generator adds name of the join table to many to many relationships. This prevents name collision.</td><br> </tr><br> <tr><br> <td>addRelationNameToManyToMany</td><br> <td>boolean</td><br> <td>If this is set true. Generator adds name of the relation to many to many relationships. This prevents name collision further than addTableNameToManyToMany, because more than two table can be connected to join table.</td><br> </tr><br> <tr><br> <td>stripFirstTableNameFromManyToMany</td><br> <td>boolean</td><br> <td>If this is set true. Generator strips first table name from many to many relations' name if it begins with table name. For example "product_cart_line_items" relation becomes "cart_line_items" for "product" table.</td><br> </tr><br> <tr><br> <td>hasManyThrough</td><br> <td>boolean</td><br> <td>Tells generator to generate has many through relations like hasMany(modelName, { through: '..' }. After Sequelize version 2.0 RC3 has many through relations are DEPRECATED. Use belongToMany instead. hasMany through and belongsToMany cannot be true at the same time for the same table.</td><br> </tr><br> <tr><br> <td>belongsToMany</td><br> <td>boolean</td><br> <td>Tells generator to generate belongsToMany relations which comes to Sequelize version 2.0 RC4. Prior Sequelize versions do not work if this option set true. hasMany through and belongsToMany cannot be true at the same time for the same table.</td><br> </tr><br> <tr><br> <td>prefixForBelongsTo</td><br> <td>string</td><br> <td>belongsTo relations use foreign key name "_id" suffix stripped. If foreign key does not contain "_id" suffix, generator add this prefix to belongsTo relations to prevent column accessor and relation accessor clash. See "Smart Naming of Models and Relations" section above.</td><br> </tr><br> <tr><br> <td>useSchemaName</td><br> <td>boolean</td><br> <td>If this is set true, generator adds schema name beginning of generated file names and model names. This is useful for multi schema databases for preventing same table name from clashing.</td><br> </tr><br> <tr><br> <td>modelCamelCase</td><br> <td>boolean</td><br> <td>Use camel case (like schemaName) in schema names.</td><br> </tr><br> <tr><br> <td>relationAccessorCamelCase</td><br> <td>boolean</td><br> <td>Use camel case (like relationName) in relation accessor names.</td><br> </tr><br> <tr><br> <td>columnAccessorCamelCase</td><br> <td>boolean</td><br> <td>Use came case (like columnName) in column accessor names.</td><br> </tr><br> <tr><br> <td>columnDefault</td><br> <td>boolean</td><br> <td>Generate default values to the model. WARNING: Does not support SQL functions yet. It is hard to implement this in Sequelize way. IMHO it is best to leave that to DBMS. However you can set it true and override false generated SQL functions.</td><br> </tr><br> <tr><br> <td>columnDescription</td><br> <td>boolean</td><br> <td>Include column description in generated model files.</td><br> </tr><br> <tr><br> <td>columnAutoIncrement</td><br> <td>boolean</td><br> <td>Include auto increment option of attributes in generated model files.</td><br> </tr><br> <tr><br> <td>tableDescription</td><br> <td>boolean</td><br> <td>Include table description in generated model files.</td><br> </tr><br> <tr><br> <td>dataTypeVariable</td><br> <td>string</td><br> <td>Sequelize uses object variable to define data types like "Sequelize.BOOLEAN". This configuration parameter sets the name of the variable.</td><br> </tr><br> <tr><br> <td>skipTable</td><br> <td>Array(string)</td><br> <td>List of table names not to generate model files for.</td><br> </tr><br> <tr><br> <td colspan="3"><h4><strong>tableOptions</strong></h4>User can include any Sequelize.define options here. These options are directly passed to Sequelize.define. See Sequelize docs. Some examples:</td><br> </tr><br> <tr><br> <td>timestamps</td><br> <td>boolean</td><br> <td>Adds createdAt and updatedAt timestamps to the model.</td><br> </tr><br></table>
<h2>Default Configuration Settings</h2><p>Default configuration settings are listed below:</p>
<pre class="prettyprint source"><code>module.exports = {
"sequelize-pg-generator": {
"database": {
"host": "127.0.0.1",
"port": 5432,
"user": "user",
"password": "password",
"database": "",
"schema": ["public"]
},
"template": {
"engine": "swig",
"extension": "html",
"folder": path.join(__dirname, '..', 'template')
},
"output": {
"log": true,
"folder": "./model",
"beautify": true,
"indent": 4,
"preserveNewLines": false,
"warning": true
},
"generate": {
"stripFirstTableFromHasMany": true,
"addTableNameToManyToMany": false,
"addRelationNameToManyToMany": true,
"stripFirstTableNameFromManyToMany": true,
"hasManyThrough": false,
"belongsToMany": true,
"prefixForBelongsTo": "related",
"useSchemaName": true,
"modelCamelCase": true,
"relationAccessorCamelCase": true,
"columnAccessorCamelCase": true,
"columnDefault": false,
"columnDescription": true,
"columnAutoIncrement": true,
"tableDescription": true,
"dataTypeVariable": "Seq",
"skipTable": []
},
"tableOptions": {
"timestamps": false
}
}
};</code></pre><h2>CAVEAT: Singleton Nature of Configuration</h2><p>This module uses config module via require('config') for configuration. Config module is a singleton as of this writing, which returns same config object for each request. As a result subsequent calls in the same process return same configuration even configuration file changed and/or sequelize-pg-generator constructor called with different config file.</p>
<p>This is usually no problem since generator supposed to be called once in the same process. However this behaviour prevents testing. Additionally you may want to avoid this behavior whatever reason. To side step this, it is added "resetConfig" option to constructor. If it is set to true it resets and rereads configuration. To do this we clear config module from node cache. It is sub-optimal solution suggested by lorenwest in github issues section.</p>
<p>To activate this behavior just set resetConfig to true or from cli add --resetConfig:</p>
<pre class="prettyprint source"><code>var generator = require('sequelize-pg-generator');
generator(function (err) {
if (err) { callback(err); }
}, {
database: 'my_database',
resetConfig: true
);</code></pre><p>sequelize-pg-creator uses the code below:</p>
<pre class="prettyprint source"><code>global.NODE_CONFIG = null;
delete require.cache[require.resolve('config')];
config = require('config');</code></pre><h1>Template Variables (Customizing Templates)</h1><p>To create custom templates user can copy default templates or create from scratch then configure "template.folder" to use newly created templates. 3 files are required: index.ext (.ext is whatever your template engine's extension is), index.js, utils.js.</p>
<p>index.js and utils.js files will be copied directly to target model directory. index.ext template is executed for each table to create model files.</p>
<p>Variables available to use in templates are listed below. Please note if a value is undefined, it's key is also is deleted to make it easy to iterate only defined values in templates.</p>
<table><br> <tr><br> <td width="270"><strong>mainScript</strong></td><br> <td>Path to generated index.js file in target model directory.</td><br> </tr><br> <tr><br> <td><strong>warning</strong></td><br> <td>This value comes from configuration which indicates if user wants to include warning message about customizations in generated model files.</td><br> </tr><br> <tr><br> <td><strong>table</strong></td><br> <td>Table object which has table details, columns and relations etc.</td><br> </tr><br> <tr><br> <td>table.modelName</td><br> <td>Model name for table.</td><br> </tr><br> <tr><br> <td>table.tableName</td><br> <td>Name of the table.</td><br> </tr><br> <tr><br> <td>table.schema</td><br> <td>PostgreSQL schema name of the table</td><br> </tr><br> <tr><br> <td>table.comment</td><br> <td>Comment of the table.</td><br> </tr><br> <tr><br> <td>table.baseFileName</td><br> <td>Base part of the file name.</td><br> </tr><br> <tr><br> <td>SPECIAL</td><br> <td>Options from configuration file is also available under table. For example: table.timestamps</td><br> </tr><br> <tr><br> <td><strong>table.columns</strong></td><br> <td>Array which contains columns of the table.</td><br> </tr><br> <tr><br> <td>table.columns[n].source</td><br> <td>'generator' string. Indicates that this is generated by auto generator. If user needs to access manually modified parts, it is possible to use this field to filter out automatically generated parts.</td><br> </tr><br> <tr><br> <td>table.columns[n].type</td><br> <td>Sequelize type of the column.</td><br> </tr><br> <tr><br> <td>table.columns[n].accessorName</td><br> <td>Accessor name of column.</td><br> </tr><br> <tr><br> <td>table.columns[n].name</td><br> <td>Name of the column</td><br> </tr><br> <tr><br> <td>table.columns[n].primaryKey</td><br> <td>True if this column is a primary key.</td><br> </tr><br> <tr><br> <td>table.columns[n].autoIncrement</td><br> <td>True if this column is a auto increment column.</td><br> </tr><br> <tr><br> <td>table.columns[n].allowNull</td><br> <td>Boolean value to indicate if this column is allowed to have null values.</td><br> </tr><br> <tr><br> <td>table.columns[n].defaultValue</td><br> <td>Dafault value of column.</td><br> </tr><br> <tr><br> <td>table.columns[n].unique</td><br> <td>String value of unique key's name. Sequelize use unique key name to support composite unique keys. It also works for single column unique values.</td><br> </tr><br> <tr><br> <td>table.columns[n].comment</td><br> <td>Description of the column. This comes from database server.</td><br> </tr><br> <tr><br> <td>table.columns[n].references</td><br> <td>If this column has a reference, this value is the name of table which this column references to.</td><br> </tr><br> <tr><br> <td>table.columns[n].referencesKey</td><br> <td>If this column has a reference, this value is the name of foreign key which this column references to.</td><br> </tr><br> <tr><br> <td>table.columns[n].onUpdate</td><br> <td>On update value of the column. (SET NULL, CASCADE, RESTRICT etc.)</td><br> </tr><br> <tr><br> <td>table.columns[n].onDelete</td><br> <td>On delete value of the column. (SET NULL, CASCADE, RESTRICT etc.)</td><br> </tr><br> <tr><br> <td><strong>table.hasManies</strong></td><br> <td>Array which contains hasMany relations of the table. </td><br> </tr><br> <tr><br> <td>table.hasManies[n].type</td><br> <td>'hasMany' string which indicates relation type.</td><br> </tr><br> <tr><br> <td>table.hasManies[n].source</td><br> <td>'generator' string. Indicates that this is generated by auto generator. If user needs to access manually modified parts, it is possible to use this field to filter out automatically generated parts.</td><br> </tr><br> <tr><br> <td>table.hasManies[n].name</td><br> <td>Name of the relation in database server.</td><br> </tr><br> <tr><br> <td>table.hasManies[n].model</td><br> <td>Model name which this relation refers to.</td><br> </tr><br> <tr><br> <td>table.hasManies[n].as</td><br> <td>Alias for relation. This alias is used to access this relation from Sequelize.</td><br> </tr><br> <tr><br> <td>table.hasManies[n].targetSchema</td><br> <td>PostgreSQL schema name which this relation refers to.</td><br> </tr><br> <tr><br> <td>table.hasManies[n].targetTable</td><br> <td>Table name which this table refers to.</td><br> </tr><br> <tr><br> <td>table.hasManies[n].foreignKey</td><br> <td>Foreign key column name in the target table.</td><br> </tr><br> <tr><br> <td>table.hasManies[n].onDelete</td><br> <td>onDelete value from database.</td><br> </tr><br> <tr><br> <td>table.hasManies[n].onUpdate</td><br> <td>onUpdate value from database.</td><br> </tr><br> <tr><br> <td>table.hasManies[n].through</td><br> <td>If this is a through relationship (many to many) name of the join table. Through relations are DEPRECATED as of Sequelize 2.0 RC4.</td><br> </tr><br> <tr><br> <td><strong>table.belongsTos</strong></td><br> <td>Array which contains belongsTo relations of the table. </td><br> </tr><br> <tr><br> <td>table.belongsTos[n].type</td><br> <td>'belongsTo' string which indicates relation type.</td><br> </tr><br> <tr><br> <td>table.belongsTos[n].source</td><br> <td>'generator' string. Indicates that this is generated by auto generator. If user needs to access manually modified parts, it is possible to use this field to filter out automatically generated parts.</td><br> </tr><br> <tr><br> <td>table.belongsTos[n].name</td><br> <td>Name of the relation in database server.</td><br> </tr><br> <tr><br> <td>table.belongsTos[n].model</td><br> <td>Model name which this relation refers to.</td><br> </tr><br> <tr><br> <td>table.belongsTos[n].as</td><br> <td>Alias for relation. This alias is used to access this relation from Sequelize.</td><br> </tr><br> <tr><br> <td>table.belongsTos[n].targetSchema</td><br> <td>PostgreSQL schema name which this relation refers to.</td><br> </tr><br> <tr><br> <td>table.belongsTos[n].targetTable</td><br> <td>Table name which this table refers to.</td><br> </tr><br> <tr><br> <td>table.belongsTos[n].foreignKey</td><br> <td>Foreign key column name in this table regarding this relation.</td><br> </tr><br> <tr><br> <td>table.belongsTos[n].onDelete</td><br> <td>onDelete value from database.</td><br> </tr><br> <tr><br> <td>table.belongsTos[n].onUpdate</td><br> <td>onUpdate value from database.</td><br> </tr><br> <tr><br> <td><strong>table.belongsToManies</strong></td><br> <td>Array which contains belongsToMany relations of the table. belongsToMany relations are available in Sequelize 2.0 RC4 and newer versions. </td><br> </tr><br> <tr><br> <td>table.belongsToManies[n].type</td><br> <td>'belongsToMany' string which indicates relation type.</td><br> </tr><br> <tr><br> <td>table.belongsToManies[n].source</td><br> <td>'generator' string. Indicates that this is generated by auto generator. If user needs to access manually modified parts, it is possible to use this field to filter out automatically generated parts.</td><br> </tr><br> <tr><br> <td>table.belongsToManies[n].name</td><br> <td>Name of the relation in database server.</td><br> </tr><br> <tr><br> <td>table.belongsToManies[n].model</td><br> <td>Model name which this relation refers to.</td><br> </tr><br> <tr><br> <td>table.belongsToManies[n].as</td><br> <td>Alias for relation. This alias is used to access this relation from Sequelize.</td><br> </tr><br> <tr><br> <td>table.belongsToManies[n].targetSchema</td><br> <td>PostgreSQL schema name which this relation refers to.</td><br> </tr><br> <tr><br> <td>table.belongsToManies[n].targetTable</td><br> <td>Table name which this table refers to.</td><br> </tr><br> <tr><br> <td>table.belongsToManies[n].foreignKey</td><br> <td>Foreign key column name in join table referencing to this table.</td><br> </tr><br> <tr><br> <td>table.belongsToManies[n].otherKey</td><br> <td>Foreign key column name in join table referencing to target table.</td><br> </tr><br> <tr><br> <td>table.belongsToManies[n].onDelete</td><br> <td>onDelete value from database.</td><br> </tr><br> <tr><br> <td>table.belongsToManies[n].onUpdate</td><br> <td>onUpdate value from database.</td><br> </tr><br> <tr><br> <td>table.belongsToManies[n].through</td><br> <td>Name of the join table.</td><br> </tr><br> <tr><br> <td>table.relations</td><br> <td>Array which contains all relations combined of the table. This array contains hasMany relations, hasMany through ilişkileri, belongsTo relations, belongsToMany relations.</td><br> </tr><br></table>
<h1>Examples</h1><h2>Eager Loading</h2><p>sequelize-pg-generator sets up relations with an "as". Otherwise multiple relations between same two tables collides. For example:</p>
<p>account has many contacts as primaryContacts (account -----< contact)</p>
<p>account has many contacts as secondaryContacts (account ----< contact)</p>
<p>In this case sequelize.js requires you to specify "as" alias in the "as" attribute during eager loading.</p>
<pre class="prettyprint source"><code>account = orm.model('public.account'); // Can be configured without schema.
contact = orm.model('public.contact'); // Can be configured without schema.
account.findAll({ include: [ { model: contact, as: "primaryContacts" } ] }).then(function(data) {
console.log(data[0].primaryContacts[0].name);
});</code></pre><h1>API</h1></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.4.0-dev</a>
on Fri Nov 27th 2015 using the <a
href="https://github.com/terryweiss/docstrap">DocStrap template</a>.
</span>
</footer>
</div>
<div class="span3">
<div id="toc"></div>
</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>