forked from C2FO/patio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DDL.html
853 lines (672 loc) · 41.9 KB
/
DDL.html
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>patio</title>
<link rel="stylesheet" href="./assets/css/bootstrap.css">
<link rel="stylesheet" href="./assets/css/prettify.css">
<style type="text/css">
/*.subnav-inner {*/
/*width: 100%;*/
/*height: 36px;*/
/*background-color: #EEE;*/
/*background-repeat: repeat-x;*/
/*background-image: -moz-linear-gradient(top, whiteSmoke 0%, #EEE 100%);*/
/*background-image: -webkit-gradient(linear, left top, left bottom, color-stop(0%, whiteSmoke), color-stop(100%, #EEE));*/
/*background-image: -webkit-linear-gradient(top, whiteSmoke 0%, #EEE 100%);*/
/*background-image: -ms-linear-gradient(top, whiteSmoke 0%, #EEE 100%);*/
/*background-image: -o-linear-gradient(top, whiteSmoke 0%, #EEE 100%);*/
/*filter: progid:DXImageTransform.Microsoft.gradient(startColorstr = '#f5f5f5', endColorstr = '#eeeeee', GradientType = 0);*/
/*background-image: linear-gradient(top, whiteSmoke 0%, #EEE 100%);*/
/*-webkit-border-radius: 4px;*/
/*-moz-border-radius: 4px;*/
/*border-radius: 4px;*/
/*}*/
/*.subnav .nav > li > a:hover {*/
/*color: black !important;*/
/*}*/
/*.subnav .nav li.dropdown .dropdown-toggle .caret,*/
/*.subnav .nav li.dropdown.open .caret {*/
/*border-top-color: #999 !important;*/
/*border-bottom-color: #999 !important;*/
/*}*/
/*.subnav-fixed {*/
/*position: fixed;*/
/*width : 90%;*/
/*margin-right: auto;*/
/*margin-left: auto;*/
/*top: 40px;*/
/*left: 0;*/
/*right: 0;*/
/*z-index: 1020;*/
/*border-color: #D5D5D5;*/
/*border-width: 0 0 1px;*/
/*-webkit-border-radius: 0;*/
/*-moz-border-radius: 0;*/
/*border-radius: 0;*/
/*-webkit-box-shadow: inset 0 1px 0 #fff, 0 1px 5px rgba(0,0,0,.1);*/
/*-moz-box-shadow: inset 0 1px 0 #fff, 0 1px 5px rgba(0,0,0,.1);*/
/*box-shadow: inset 0 1px 0 #fff, 0 1px 5px rgba(0,0,0,.1);*/
/*filter: progid:DXImageTransform.Microsoft.gradient(enabled=false);*/
/*}*/
/*.navbar .nav .dropdown-menu {*/
/*max-height: 500px;*/
/*overflow: auto;*/
/*}*/
</style>
<style type="text/css">
body {
padding: 90px;
}
.subnav{
margin-top: 40px;
margin-right: auto;
margin-left: auto;
z-index: 1000;
}
@media (max-width:979px) {
.subnav{
margin-top: auto;
}
}
</style>
<link rel="stylesheet" href="./assets/css/bootstrap-responsive.css">
<script type="text/javascript">
var init = (function () {
"use strict";
var processScroll = (function () {
var curr = null, prev = null;
return function (nav) {
var $win = $(window);
$('.subnav').each(function () {
var nav = $(this);
var navTop = $win.width() < 980 ? 0 : nav.offset().top - 40;
var scrollTop = $win.scrollTop();
if (scrollTop >= navTop && curr != nav) {
if(curr){
curr.removeClass('subnav-fixed')
prev = curr;
}
curr = nav;
curr.addClass('subnav-fixed')
} else if (curr == nav && scrollTop <= navTop) {
curr.removeClass('subnav-fixed');
prev.addClass('subnav-fixed');
curr = prev;
}else{
nav.removeClass('subnav-fixed');
}
});
};
})();
return function () {
window.prettyPrint && prettyPrint();
$(".collapse").collapse();
var $window = $(window);
//$(".subnav").affix ();
// fix sub nav on scroll
// processScroll();
// $(window).on('scroll', processScroll)
}
})();
</script>
</head>
<body onload="init()">
<div class="navbar navbar-fixed-top navbar-inverse">
<div class="navbar-inner">
<div class="container">
<a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"> </span>
</a>
<a href="./index.html" class="brand">patio</a>
<div class="nav-collapse">
<ul class="nav">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Docs<b class="caret"></b></a>
<ul class="dropdown-menu">
<li><a href="./connecting.html">Connecting</a></li>
<li><a href="./models.html">Models</a></li>
<li><a href="./associations.html">Associations</a></li>
<li><a href="./model-inheritance.html">Model Inheritance</a></li>
<li><a href="./validation.html">Model Validation</a></li>
<li><a href="./plugins.html">Model Plugins</a></li>
<li><a href="./querying.html">Querying</a></li>
<li><a href="./DDL.html">DDL</a></li>
<li><a href="./migrations.html">Migrations</a></li>
<li><a href="./logging.html">Logging</a></li>
<li><a href="./coverage.html">Test Coverage</a></li>
</ul>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Namespaces<b class="caret"></b></a>
<ul class="dropdown-menu">
<li><a href="patio_sql.html">patio.sql</a></li>
<li><a href="patio_sql_Constants.html">patio.sql.Constants</a></li>
</ul>
</li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Classes<b class="caret"></b></a>
<ul class="dropdown-menu">
<li><a href="./patio.html">patio</a></li>
<li><a href="./patio_AlterTableGenerator.html">patio.AlterTableGenerator</a></li>
<li><a href="./patio_AssociationError.html">patio.AssociationError</a></li>
<li><a href="./patio_ConnectionPool.html">patio.ConnectionPool</a></li>
<li><a href="./patio_Database.html">patio.Database</a></li>
<li><a href="./patio_DatabaseError.html">patio.DatabaseError</a></li>
<li><a href="./patio_Dataset.html">patio.Dataset</a></li>
<li><a href="./patio_DatasetError.html">patio.DatasetError</a></li>
<li><a href="./patio_ExpressionError.html">patio.ExpressionError</a></li>
<li><a href="./patio_MigrationError.html">patio.MigrationError</a></li>
<li><a href="./patio_Model.html">patio.Model</a></li>
<li><a href="./patio_ModelError.html">patio.ModelError</a></li>
<li><a href="./patio_NotImplemented.html">patio.NotImplemented</a></li>
<li><a href="./patio_PatioError.html">patio.PatioError</a></li>
<li><a href="./patio_QueryError.html">patio.QueryError</a></li>
<li><a href="./patio_SchemaGenerator.html">patio.SchemaGenerator</a></li>
<li><a href="./patio_Time.html">patio.Time</a></li>
<li><a href="./patio_associations_Association.html">patio.associations.Association</a></li>
<li><a href="./patio_associations_ManyToMany.html">patio.associations.ManyToMany</a></li>
<li><a href="./patio_associations_ManyToOne.html">patio.associations.ManyToOne</a></li>
<li><a href="./patio_associations_ManyToOne.html">patio.associations.ManyToOne</a></li>
<li><a href="./patio_associations_OneToMany.html">patio.associations.OneToMany</a></li>
<li><a href="./patio_dataset.html#._Query">patio.dataset._Query</a></li>
<li><a href="./patio_migrations_IntegerMigrator.html">patio.migrations.IntegerMigrator</a></li>
<li><a href="./patio_migrations_Migrator.html">patio.migrations.Migrator</a></li>
<li><a href="./patio_migrations_TimestampMigrator.html">patio.migrations.TimestampMigrator</a></li>
<li><a href="./patio_plugins_CachePlugin.html">patio.plugins.CachePlugin</a></li>
<li><a href="./patio_plugins_ClassTableInheritancePlugin.html">patio.plugins.ClassTableInheritancePlugin</a></li>
<li><a href="./patio_plugins_ColumnMapper.html">patio.plugins.ColumnMapper</a></li>
<li><a href="./patio_plugins_TimeStampPlugin.html">patio.plugins.TimeStampPlugin</a></li>
<li><a href="./patio_plugins_ValidatorPlugin.html">patio.plugins.ValidatorPlugin</a></li>
<li><a href="./patio_sql_AliasMethods.html">patio.sql.AliasMethods</a></li>
<li><a href="./patio_sql_AliasedExpression.html">patio.sql.AliasedExpression</a></li>
<li><a href="./patio_sql_BitWiseMethods.html">patio.sql.BitWiseMethods</a></li>
<li><a href="./patio_sql_BooleanConstant.html">patio.sql.BooleanConstant</a></li>
<li><a href="./patio_sql_BooleanExpression.html">patio.sql.BooleanExpression</a></li>
<li><a href="./patio_sql_BooleanMethods.html">patio.sql.BooleanMethods</a></li>
<li><a href="./patio_sql_CaseExpression.html">patio.sql.CaseExpression</a></li>
<li><a href="./patio_sql_Cast.html#.constructor">patio.sql.Cast.constructor</a></li>
<li><a href="./patio_sql_CastMethods.html">patio.sql.CastMethods</a></li>
<li><a href="./patio_sql_ColumnAll.html">patio.sql.ColumnAll</a></li>
<li><a href="./patio_sql_ComplexExpression.html">patio.sql.ComplexExpression</a></li>
<li><a href="./patio_sql_ComplexExpressionMethods.html">patio.sql.ComplexExpressionMethods</a></li>
<li><a href="./patio_sql_Constant.html">patio.sql.Constant</a></li>
<li><a href="./patio_sql_DateTime.html">patio.sql.DateTime</a></li>
<li><a href="./patio_sql_Decimal.html">patio.sql.Decimal</a></li>
<li><a href="./patio_sql_Expression.html">patio.sql.Expression</a></li>
<li><a href="./patio_sql_Float.html">patio.sql.Float</a></li>
<li><a href="./patio_sql_GenericExpression.html">patio.sql.GenericExpression</a></li>
<li><a href="./patio_sql_Identifier.html">patio.sql.Identifier</a></li>
<li><a href="./patio_sql_InequalityMethods.html">patio.sql.InequalityMethods</a></li>
<li><a href="./patio_sql_JoinClause.html">patio.sql.JoinClause</a></li>
<li><a href="./patio_sql_JoinOnClause.html">patio.sql.JoinOnClause</a></li>
<li><a href="./patio_sql_JoinUsingClause.html">patio.sql.JoinUsingClause</a></li>
<li><a href="./patio_sql_LiteralString.html#.constructor">patio.sql.LiteralString.constructor</a></li>
<li><a href="./patio_sql_NoBooleanInputMethods.html">patio.sql.NoBooleanInputMethods</a></li>
<li><a href="./patio_sql_NumericExpression.html">patio.sql.NumericExpression</a></li>
<li><a href="./patio_sql_NumericMethods.html">patio.sql.NumericMethods</a></li>
<li><a href="./patio_sql_OrderedExpression.html">patio.sql.OrderedExpression</a></li>
<li><a href="./patio_sql_OrderedMethods.html">patio.sql.OrderedMethods</a></li>
<li><a href="./patio_sql_PlaceHolderLiteralString.html">patio.sql.PlaceHolderLiteralString</a></li>
<li><a href="./patio_sql_QualifiedIdentifier.html">patio.sql.QualifiedIdentifier</a></li>
<li><a href="./patio_sql_QualifyingMethods.html">patio.sql.QualifyingMethods</a></li>
<li><a href="./patio_sql_SQLFunction.html">patio.sql.SQLFunction</a></li>
<li><a href="./patio_sql_StringConcatenationMethods.html">patio.sql.StringConcatenationMethods</a></li>
<li><a href="./patio_sql_StringExpression.html">patio.sql.StringExpression</a></li>
<li><a href="./patio_sql_StringMethods.html">patio.sql.StringMethods</a></li>
<li><a href="./patio_sql_SubScript.html">patio.sql.SubScript</a></li>
<li><a href="./patio_sql_SubscriptMethods.html">patio.sql.SubscriptMethods</a></li>
<li><a href="./patio_sql_Time.html">patio.sql.Time</a></li>
<li><a href="./patio_sql_TimeStamp.html">patio.sql.TimeStamp</a></li>
<li><a href="./patio_sql_Year.html">patio.sql.Year</a></li>
</ul>
</li>
</ul>
<ul class="nav pull-right">
<li class="divider-vertical"></li>
<li><a href="https://github.com/Pollenware/patio" target="#github" class="pull-right">github</a></li>
</ul>
</div>
</div>
</div>
</div>
<div class="container-fluid">
<a name="top"></a>
<div class="container">
<h1>DDL</h1>
<h2><a href="./patio_Database.html#createTable">createTable</a></h2>
<p>Patio supports the creation to tables through an instance of <a href="./patio_Database.html#createTable">patio.Database</a>. The <a href="./patio_Database.html#createTable">createTable</a> method is used by passing it a name of the table to create and a function which performs the actions to create <code>column</code>s, <code>indexe</code>s, <code>foreignKey</code>s, <code>constraint</code>s, and <code>primaryKey</code>s.
</p>
<pre class='prettyprint linenums lang-js'><code>DB.createTable("airport", function () {
this.primaryKey("id");
this.airportCode(String, {size:4, allowNull:false, unique:true});
this.name(String, {allowNull:false});
this.city(String, {allowNull:false});
this.state(String, {size:2, allowNull:false});
});</code></pre>
<p>Patio by default supports the built in JavaScript types
</p>
<ul>
<li><code>String</code> : varchar(255)</li>
<li><code>Boolean</code> : boolean or tinyint(1)</li>
<li><code>Number</code> : numeric</li>
<li><code>Date</code> : date</li>
</ul>
<pre class='prettyprint linenums lang-js'><code>DB.createTable("test", function(){
this.name(String); //=> `name` varchar(255)
this.num(Number); //=> `num` numeric
this.boolean(Boolean); //=> `boolean` tinyint(1)
this.date(Date); //=> `date` date
});</code></pre>
<p>Patio also has a few other built in types that can be used.
</p>
<ul>
<li><a href="./patio_sql_Time.html">patio.sql.Time</a> : time</li>
<li><a href="./patio_sql_TimeStamp.html">patio.sql.TimeStamp</a> : timestamp</li>
<li><a href="./patio_sql_DateTime.html">patio.sql.DateTime</a> - datetime</li>
<li><a href="./patio_sql_Year.html">patio.sql.Year</a> - year</li></li>
<li><a href="./patio_sql_Float.html">patio.sql.Float</a> - double precision</li>
<li><a href="./patio_sql_Decimal.html">patio.sql.Decimal</a> - double precision</li>
</ul>
<pre class='prettyprint linenums lang-js'><code>var sql = patio.sql;
DB.createTable("test", function () {
this.timestamp(sql.TimeStamp);
this.datetime(sql.DateTime);
this.time(sql.Time);
this.year(sql.Year);
this.decimal(sql.Decimal);
this.float(sql.Float);
});</code></pre>
<p>When creating a table there are a number of methods that can be invoked to create the table. For a full reference see <a href="./patio_SchemaGenerator.html">patio.SchemaGenerator</a>.
</p>
<p>The most commonly used methods are:
</p>
<h3><a href="./patio_SchemaGenerator.html#column">column</a></h3>
<p>Add a column to the DDL.
</p>
<p><strong>Options</strong>
</p>
<ul>
<li><strong>key</strong> : For foreign key columns, the column in the associated table that this column references. Unnecessary if this column references the primary key of the associated table.</li>
<li><strong>allowNull</strong> : Mark the column as allowing NULL values (if true), or not allowing NULL values (if false). If unspecified, will default to whatever the database default is.</li>
<li><strong>onDelete</strong> : Specify the behavior of this column when being deleted. Valid options ("restrict", "cascade", "setNull", "setDefault", "noAction").</li>
<li><strong>onUpdate</strong> : Specify the behavior of this column when being updated Valid options ("restrict", "cascade", "setNull", "setDefault", "noAction").</li>
<li><strong>primaryKey</strong> : Make the column as a single primary key column. This should only be used if you have a single, non-autoincrementing primary key column.</li>
<li><strong>size</strong> : The size of the column, generally used with string columns to specify the maximum number of characters the column will hold. An array of two integers can be provided to set the size and the precision, respectively, of decimal columns.</li>
<li><strong>unique</strong> : Mark the column as unique, generally has the same effect as creating a unique index on the column.</li>
<li><strong>unsigned</strong> : Make the column type unsigned, only useful for integer columns.</li>
<li><strong>elements</strong> : Available items used for set and enum columns. </li>
</ul>
<pre class='prettyprint linenums lang-js'><code> DB.createTable("test", function(){
this.column("num", "integer");
//=> num INTEGER
this.column('name", String, {allowNull : false, "default" : "a");
//=> name varchar(255) NOT NULL DEFAULT 'a'
this.column("ip", "inet");
//=> ip inet
});</code></pre>
<p>You can also create columns via method missing, so the following are equivalent:
</p>
<pre class='prettyprint linenums lang-js'><code>DB.createTable("test", function(){
this.column("number", "integer");
//Same as
this.number("integer");
});</code></pre>
<h3><a href="./patio_SchemaGenerator.html#primaryKey">primaryKey</a></h3>
<p>Adds an auto-incrementing primary key column or a primary key constraint to the DDL
</p>
<pre class='prettyprint linenums lang-js'><code>db.createTable("airplane_type", function () {
this.primaryKey("id");
//=> id integer NOT NULL PRIMARY KEY AUTOINCREMENT
this.name(String, {allowNull:false});
this.created(sql.TimeStamp);
});</code></pre>
<p>If you want a primary key that is not an auto-incrementing number use <a href="./patio_SchemaGenerator.html#column">column</a> instead.
</p>
<pre class='prettyprint linenums lang-js'><code>DB.createTable("test", function(){
this.pk("integer", {primaryKey : true}); //Non auto incrementing primary key.
});
DB.createTable("test2", function(){
this.pk(String, {primaryKey : true}); //varchar(255) primary key.
});</code></pre>
<p>If you want a composite primary key pass an array of column names. <strong>Note:</strong> when creating a composite primary key it does not create the columns so you must create those also.
</p>
<pre class='prettyprint linenums lang-js'><code>DB.createTable("test", function(){
this.firstName(String);
this.lastName(String);
this.primaryKey(["firstName", "lastName"]); //composite key
});</code></pre>
<h3><a href="./patio_SchemaGenerator.html#foreignKey">foreignKey</a></h3>
<p>Add a foreign key constraint to the DDL.
</p>
<p><strong>Options</strong>
</p>
<ul>
<li><strong>deferrable</strong> : Makes the foreign key constraint checks deferrable, so they aren't checked until the end of the transaction.</li>
<li><strong>key</strong> : For foreign key columns, the column in the associated table that this column references. Unnecessary if this column references the primary key of the associated table, at least on most databases.</li>
<li><strong>onDelete</strong> : Specify the behavior of this column when being deleted. Valid options ("restrict", "cascade", "setNull", "setDefault", "noAction").</li>
<li><strong>onUpdate </strong> : Specify the behavior of this column when being updated. Valid options ("restrict", "cascade", "setNull", "setDefault", "noAction").</li>
</ul>
<pre class='prettyprint linenums lang-js'><code>DB.createTable("flight", function () {
this.primaryKey("id");
this.airline(String, {allowNull:false});
});
DB.createTable("airport", function () {
this.primaryKey("id");
this.airportCode(String, {size:4, allowNull:false, unique:true});
this.name(String, {allowNull:false});
this.city(String, {allowNull:false});
this.state(String, {size:2, allowNull:false});
});
DB.createTable("flight_leg", function () {
this.primaryKey("id");
this.scheduled_departure_time("time");
this.scheduled_arrival_time("time");
this.foreignKey("departure_code", "airport", {key:"airport_code", type : String, size : 4});
this.foreignKey("arrival_code", "airport", {key:"airport_code", type : String, size : 4});
this.foreignKey("flight_id", "flight", {key:"id"});
});</code></pre>
<h3><a href="./patio_SchemaGenerator.html#index">index</a></h3>
<p>Adds an index to the the DDL. For single columns, calling index is the same as using the <code>index</code> option when creating the column:
</p>
<p><strong>Options</strong>
</p>
<ul>
<li><strong>name</strong> : The name of the index (generated based on the table and column names if not provided).</li>
<li><strong>type</strong> : The type of index to use (only supported by some databases)</li>
<li><strong>unique</strong> : Make the index unique, so duplicate values are not allowed.</li>
<li><strong>where</strong> : Create a partial index (only supported by some databases).</li>
</ul>
<pre class='prettyprint linenums lang-js'><code>DB.createTable("a", function(){
this.id("integer", {index : true});
});
// Same as:
DB.createTable("a", function(){
this.id("integer");
this.index("id");
});</code></pre>
<p>Similar to the <a href="./patio_SchemaGenerator.html#primaryKey">primaryKey</a> and <a href="./patio_SchemaGenerator.html#foreignKey">foreignKey</a> methods, calling index with an array of strings will create a multiple column index:
</p>
<pre class='prettyprint linenums lang-js'><code>DB.createTable("test", function(){
this.primaryKey("id");
this.first_name(String);
this.last_name(String);
this.index(["first_name", "last_name"]); //multi-column index
});</code></pre>
<h3><a href="./patio_SchemaGenerator.html#unique">unique</a></h3>
<p>The unique method creates a unique constraint on the table. A unique constraint generally operates identically to a unique index.
</p>
<pre class='prettyprint linenums lang-js'><code>DB.createTable("a", function(){
this.id("integer", {unique : true});
});
// Same as:
DB.createTable("a", function(){
this.id("integer");
this.index("id", {unique : true});
});
// Same as:
DB.createTable("a", function(){
this.id("iteger");
this.unique("id");
});</code></pre>
<p>Just like index, unique can set up a multiple column unique constraint, where the combination of the columns must be unique.
</p>
<pre class='prettyprint linenums lang-js'><code>DB.createTable("test", function(){
this.primaryKey("id");
this.first_name(String);
this.last_name(String);
this.unique(["first_name", "last_name"]);
});</code></pre>
<h3><a href="./patio_SchemaGenerator.html#constraint">constraint</a></h3>
<p>creates a named table constraint:
</p>
<pre class='prettyprint linenums lang-js'><code>DB.createTable("test", function(){
this.primaryKey("id");
this.name(String);
this.constraint("name_min_length", function(){
return this.char_length(this.name).gt(2)
});
});</code></pre>
<p>Instead of using a block, you can use arguments that will be handled similarly to <a href="./patio_Dataset.html#filter">patio.Dataset#filter</a>:
</p>
<pre class='prettyprint linenums lang-js'><code>var sql = patio.sql;
DB.createTable("test", function(){
this.primaryKey("id");
this.name(String);
this.constraint("name_min_length", sql.char_length(sql.name).gt(2));
});</code></pre>
<h3><a href="./patio_SchemaGenerator.html#check">check</a></h3>
<p>Operates just like <a href="./patio_SchemaGenerator.html#constraint">constraint</a>, except that it doesn't take a name and it creates an unnamed constraint.
</p>
<pre class='prettyprint linenums lang-js'><code>DB.createTable("test", function(){
this.primaryKey("id");
this.name(String);
this.check(function(){
return this.char_length(this.name).gt(2)
});
});</code></pre>
<h2><a href="./patio_Database.html#alterTable">alterTable</a></h2>
<p><a href="./patio_Database.html#alterTable">alterTable</a> is used to alter a tables definition. It is used just like <a href="./patio_Database.html#createTable">createTable</a> where you use a function to alter the table's definition. For a full reference see <a href="./patio_AlterTableGenerator.html">patio.AlterTableGenerator</a>.
</p>
<pre class='prettyprint linenums lang-js'><code> DB.alterTable("xyz", function() {
this.addColumn("aaa", "text", {null : false, unique : true});
this.dropColumn("bbb");
this.renameColumn("ccc", "ddd");
this.setColumnType("eee", "integer");
this.setColumnDefault("hhh", 'abcd');
this.addIndex("fff", {unique : true});
this.dropIndex("ggg");
});</code></pre>
<h3><a href="./patio_AlterTableGenerator.html#addColumn">addColumn</a></h3>
<p>This method adds a column to the table. This method is similar to <code>createTable</code>'s column method where the first parameter is the column and the second parameter is the data type and third parameter an optional options hash
</p>
<pre class='prettyprint linenums lang-js'><code> DB.alterTable("test", function(){
this.addColumn("num", "integer");
this.addColumn("name", String, {allowNull : false, "default" : "a");
this.addColumn("ip", "inet");
});</code></pre>
<h3><a href="./patio_AlterTableGenerator.html#dropColumn">dropColumn</a></h3>
<p>This method removes a column from the table definition.
</p>
<pre class='prettyprint linenums lang-js'><code>DB.alterTable("test", function(){
this.dropColumn("num");
this.dropColumn("name");
this.dropColumn("ip");
});</code></pre>
<h3><a href="./patio_AlterTableGenerator.html#renameColumn">renameColumn</a></h3>
<p>This method renames a column.
</p>
<pre class='prettyprint linenums lang-js'><code>DB.alterTable("test", function(){
this.renameColumn("num", "number");
this.renameColumn("name", "first_name");
this.renameColumn("ip", "ip_address");
});</code></pre>
<h3><a href="./patio_AlterTableGenerator.html#addPrimaryKey">addPrimaryKey</a></h3>
<p>This method is used to add a primaryKey to a table incase you forgot to include a primaryKey when creating the table.
</p>
<pre class='prettyprint linenums lang-js'><code>DB.alterTable("test", function(){
this.addPrimaryKey("id");
});</code></pre>
<p>Just like <code>createTable</code>'s primaryKey method if you provide an array of columns to use it will not create the columns but, add a composite primaryKey.
</p>
<pre class='prettyprint linenums lang-js'><code>DB.alterTable("test", function(){
this.addPrimaryKey(["first_name", "last_name"]); //composite key
});</code></pre>
<p>If you just want to take an existing single column and make it a primary key, call addPrimaryKey with an array of one element:
</p>
<pre class='prettyprint linenums lang-js'><code>DB.alterTable("test", function(){
this.addPrimarykey(["id"]);
});</code></pre>
<h3><a href="./patio_AlterTableGenerator.html#addForeignKey">addForeignKey</a></h3>
<p>This method is used to add a foreign key to a table. Like when using <code>addPrimaryKey</code> if you pass a string as the first argument then a column will be created.
</p>
<pre class='prettyprint linenums lang-js'><code>DB.alterTable("test", function(){
this.addForeignKey("test2_id", "test2");
//=>ADD COLUMN test2_id integer REFERENCES test2
});</code></pre>
<p>Just like <code>createTable</code>'s foreignKey method if you provide an array of columns to use it will not create the columns but, add a composite foreignKey.
</p>
<pre class='prettyprint linenums lang-js'><code>DB.alterTable("test", function(){
this.addForeignKey(["first_name", "last_name"], "users"); //composite key
});</code></pre>
<p>If you just want to take an existing single column and make it a foreign key, call addForeignKey with an array of one element:
</p>
<pre class='prettyprint linenums lang-js'><code>DB.alterTable("test", function(){
this.addForeignKey(["test2_id"], "test2");
});</code></pre>
<h3><a href="./patio_AlterTableGenerator.html#addIndex">addIndex</a></h3>
<p>Just like <code>createTable</code>'s index method.
</p>
<pre class='prettyprint linenums lang-js'><code>DB.alterTable("table", function(){
this.addIndex("first_name");
});</code></pre>
<p>Just like <code>createTable</code>'s index method you can create a composite key by passing in an array of column names.
</p>
<pre class='prettyprint linenums lang-js'><code>DB.alterTable("test", function(){
this.addIndex(["first_name", "last_name"]);
});</code></pre>
<h3><a href="./patio_AlterTableGenerator.html#dropIndex">dropIndex</a></h3>
<p>Drops an index from a table.
</p>
<pre class='prettyprint linenums lang-js'><code>DB.alterTable("test", function(){
this.dropIndex("first_name");
});</code></pre>
<p>To drop an index with a custom name use the name option.
</p>
<pre class='prettyprint linenums lang-js'><code>DB.alterTable("test", function(){
this.dropIndex("first_name", {name : "first_name_index"});
});</code></pre>
<h3><a href="./patio_AlterTableGenerator.html#addConstraint">addConstraint</a></h3>
<p>Adds a named constraint to a table. Just like <code>createTable</code>'s constraint method.
</p>
<pre class='prettyprint linenums lang-js'><code>DB.alterTable("test", function(){
this.addConstraint("name_min_length", function(){
return this.char_length(this.name).gt(2);
});
});</code></pre>
<p><strong>Note:</strong> there is not a method to add an unnamed constraint when altering a table.
</p>
<h3><a href="./patio_AlterTableGenerator.html#addUniqueConstraint">addUniqueConstraint</a></h3>
<p>Adds a unique constraint to a table. Just like <code>createTable</code>'s unique method.
</p>
<pre class='prettyprint linenums lang-js'><code>DB.alterTable("test", function(){
this.addUniqueConstraint("name");
});</code></pre>
<h3><a href="./patio_AlterTableGenerator.html#dropConstraint">dropConstraint</a></h3>
<p>Drops a named constraint from a table.
</p>
<pre class='prettyprint linenums lang-js'><code>DB.alterTable("albums", function(){
this.dropConstraint("name_min_length");
});</code></pre>
<p>On MySQL you specify the type of constraint you are dropping.
</p>
<pre class='prettyprint linenums lang-js'><code>DB.alterTable("albums", function(){
this.dropConstraint("albums_pk", {type : "primaryKey"});
this.dropConstraint("albums_fk", {type : "foreignKey"});
this.dropConstraint("albums_uk", {type : "unique"});
});</code></pre>
<h3><a href="./patio_AlterTableGenerator.html#setColumnDefault">setColumnDefault</a></h3>
<p>Sets a columns default value.
</p>
<pre class='prettyprint linenums lang-js'><code>DB.alterTable("test", function(){
this.setColumnDefault("first_name", "John");
this.setColumnDefault("last_name", "Doe");
});</code></pre>
<h3><a href="./patio_AlterTableGenerator.html#setColumnType">setColumnType</a></h3>
<p>Sets the columns type.
</p>
<pre class='prettyprint linenums lang-js'><code>DB.alterTable("test", function(){
this.setColumnType("first_name", "char(10)");
});</code></pre>
<h3><a href="./patio_AlterTableGenerator.html#setAllowNull">setAllowNull</a></h3>
<p>Changes the NULL/NOT NULL modifier of a column.
</p>
<pre class='prettyprint linenums lang-js'><code>DB.alterTable("test", function(){
this.setAllowNull("first_name", false); //NOT NULL
this.setAllowNull("last_name", true); //NULL
});</code></pre>
<h2><a href="./patio_Database.html">patio.Database</a> modification methods</h2>
<p><a href="./patio_Database.html">patio.Database</a> has methods that act as shortcuts to an <a href="./patio_Database.html#alterTable">alterTable</a> call these methods include
</p>
<ul>
<li><a href="./patio_Database.html#addColumn">addColumn</a></li>
<li><a href="./patio_Database.html#dropColumn">dropColumn</a></li>
<li><a href="./patio_Database.html#renameColumn">renameColumn</a></li>
<li><a href="./patio_Database.html#addIndex">addIndex</a></li>
<li><a href="./patio_Database.html#dropIndex">dropIndex</a></li>
<li><a href="./patio_Database.html#setColumnDefault">setColumnDefault</a></li>
<li><a href="./patio_Database.html#setColumnType">setColumnType</a></li>
</ul>
<p>These methods are useful when your only performing a couple of modifications at a time.
</p>
<pre class='prettyprint linenums lang-js'><code>DB.alterTable("test", function(){
this.addColumn("num", "integer");
});
//same as
DB.addColumn("test", "num", "integer");</code></pre>
<h2>Tables</h2>
<h3><a href="./patio_Database.html#dropTable">dropTable</a></h3>
<p>Can drop either a single table or multiple tables at a time.
</p>
<pre class='prettyprint linenums lang-js'><code>DB.dropTable(["leg_instance", "flight_leg", "flight", "airplane", "can_land", "airplane_type", "airport"]);
//same as
DB.dropTable("leg_instance", "flight_leg", "flight", "airplane", "can_land", "airplane_type", "airport");
//OR one table
DB.dropTable("leg_instance");</code></pre>
<h3><a href="./patio_Database.html#renameTable">renameTable</a></h3>
<p>Renames an existing table.
</p>
<pre class='prettyprint linenums lang-js'><code>DB.renameTable("test", "test_old");</code></pre>
<h3><a href="./patio_Database.html#forceCreateTable">forceCreateTable</a></h3>
<p>Forcibly creates a table, attempting to drop it unconditionally (and catching any errors), then creating it. <strong>Note:</strong> This should not be used within a transaction as it could cause the transaction to fail.
</p>
<pre class='prettyprint linenums lang-js'><code>DB.forceCreateTable("test", function(){
this.primaryKey("id);
this.first_name(String);
this.last_name(String);
this.date_of_birth(Date);
});</code></pre>
<h3><a href="./patio_Database.html#createTableUnlessExists">createTableUnlessExists</a></h3>
<p>Creates the table unless the table already exists.
</p>
<pre class='prettyprint linenums lang-js'><code>DB.createTableUnlessExists("test", function(){
this.primaryKey("id);
this.first_name(String);
this.last_name(String);
this.date_of_birth(Date);
});</code></pre>
<h2>Views</h2>
<h3><a href="./patio_Database.html#createView">createView</a></h3>
<p>Creates a view based on a dataset or an SQL string:
</p>
<pre class='prettyprint linenums lang-js'><code>//CREATE VIEW cheapItems AS SELECT * FROM items WHERE price < 100
DB.createView("cheapItems", "SELECT * FROM items WHERE price < 100");
//CREATE VIEW miscItems AS SELECT * FROM items WHERE category = 'misc'
DB.createView("miscItems", DB[:items].filter({category : 'misc'}));</code></pre>
<h3><a href="./patio_Database.html#createOrReplaceView">createOrReplaceView</a></h3>
<p>Same as create view but replaces the view if it already exists.
</p>
<pre class='prettyprint linenums lang-js'><code>//CREATE VIEW cheapItems AS SELECT * FROM items WHERE price < 100
DB.createOrReplaceView("cheapItems", "SELECT * FROM items WHERE price < 100");
//CREATE VIEW miscItems AS SELECT * FROM items WHERE category = 'misc'
DB.createOrReplaceView("miscItems", DB[:items].filter({category : 'misc'}));</code></pre>
<h3><a href="./patio_Database.html#dropView">dropView</a></h3>
<p>Similar to <code>dropTable</code> but instead of a table it drops a view.
</p>
<pre class='prettyprint linenums lang-js'><code>DB.dropView("test_view");
//=>'DROP VIEW test_view'
DB.dropTable("test_view_1", "test_view_2", "test_view_3");
//=>'DROP VIEW test_view_1',
//=>'DROP VIEW test_view_2',
//=>'DROP VIEW test_view_3'</code></pre>
<hr>
Documentation generated using <a href="https://github.com/doug-martin/coddoc">coddoc</a>.
</div>
</div>
<script type="text/javascript" src="./assets/js/jquery.js"></script>
<script type="text/javascript" src="./assets/js/bootstrap.min.js"></script>
<script type="text/javascript" src="./assets/js/prettify.js"></script>
</body>
</html>