index.html 94.7 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 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 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165
<!doctype html>
<html>
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="chrome=1">
    <title>Object Relational Mapping by dresende</title>

    <link rel="stylesheet" href="stylesheets/styles.css">
    <link rel="stylesheet" href="stylesheets/pygment_trac.css">
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <!--[if lt IE 9]>
    <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
  </head>
  <body>
    <div class="wrapper">
      <header>
        <h1>Object Relational Mapping</h1>
        <p>For NodeJS</p>

        <p class="view"><a href="https://github.com/dresende/node-orm2">View the Project on GitHub <small>dresende/node-orm2</small></a></p>


        <ul>
          <li><a href="https://github.com/dresende/node-orm2/zipball/master">Download <strong>ZIP File</strong></a></li>
          <li><a href="https://github.com/dresende/node-orm2/tarball/master">Download <strong>TAR Ball</strong></a></li>
          <li><a href="https://github.com/dresende/node-orm2">View On <strong>GitHub</strong></a></li>
        </ul>
      </header>
      <section>
        <h2>
<a name="install---" class="anchor" href="#install---"><span class="octicon octicon-link"></span></a>Install <a href="http://travis-ci.org/dresende/node-orm2"><img src="https://secure.travis-ci.org/dresende/node-orm2.png?branch=master" alt="Build Status"></a> <a href="https://npmjs.org/package/orm"><img src="https://badge.fury.io/js/orm.png" alt=""></a> <a href="https://gemnasium.com/dresende/node-orm2"><img src="https://gemnasium.com/dresende/node-orm2.png" alt=""></a>
</h2>

<div class="highlight"><pre>npm install orm
</pre></div>

<h2>
<a name="nodejs-version-support" class="anchor" href="#nodejs-version-support"><span class="octicon octicon-link"></span></a>Node.js Version Support</h2>

<p>Tests are done using <a href="https://travis-ci.org/">Travis CI</a> for node versions 0.4.x, 0.6.x, 0.8.x and 0.10.x. If you want you can run
tests locally.</p>

<div class="highlight"><pre>make
</pre></div>

<h2>
<a name="dbms-support" class="anchor" href="#dbms-support"><span class="octicon octicon-link"></span></a>DBMS Support</h2>

<ul>
<li>MySQL</li>
<li>PostgreSQL</li>
<li>Amazon Redshift</li>
<li>SQLite</li>
</ul><h2>
<a name="features" class="anchor" href="#features"><span class="octicon octicon-link"></span></a>Features</h2>

<ul>
<li>Create Models, sync, drop, bulk create, get, find, remove, count, aggregated functions</li>
<li>Create Model associations, find, check, create and remove</li>
<li>Define custom validations (several builtin validations, check instance properties before saving)</li>
<li>Model instance caching and integrity (table rows fetched twice are the same object, changes to one change all)</li>
<li>Plugins: <a href="http://dresende.github.io/node-orm-mysql-fts">MySQL FTS</a> , <a href="http://dresende.github.io/node-orm-paging">Pagination</a> , <a href="http://dresende.github.io/node-orm-transaction">Transaction</a>
</li>
</ul><h2>
<a name="introduction" class="anchor" href="#introduction"><span class="octicon octicon-link"></span></a>Introduction</h2>

<p>This is a node.js object relational mapping module.</p>

<p>An example:</p>

<div class="highlight"><pre><span class="kd">var</span> <span class="nx">orm</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s2">"orm"</span><span class="p">);</span>

<span class="nx">orm</span><span class="p">.</span><span class="nx">connect</span><span class="p">(</span><span class="s2">"mysql://username:password@host/database"</span><span class="p">,</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">db</span><span class="p">)</span> <span class="p">{</span>
  <span class="k">if</span> <span class="p">(</span><span class="nx">err</span><span class="p">)</span> <span class="k">throw</span> <span class="nx">err</span><span class="p">;</span>

    <span class="kd">var</span> <span class="nx">Person</span> <span class="o">=</span> <span class="nx">db</span><span class="p">.</span><span class="nx">define</span><span class="p">(</span><span class="s2">"person"</span><span class="p">,</span> <span class="p">{</span>
        <span class="nx">name</span>      <span class="o">:</span> <span class="nb">String</span><span class="p">,</span>
        <span class="nx">surname</span>   <span class="o">:</span> <span class="nb">String</span><span class="p">,</span>
        <span class="nx">age</span>       <span class="o">:</span> <span class="nb">Number</span><span class="p">,</span>
        <span class="nx">male</span>      <span class="o">:</span> <span class="nb">Boolean</span><span class="p">,</span>
        <span class="nx">continent</span> <span class="o">:</span> <span class="p">[</span> <span class="s2">"Europe"</span><span class="p">,</span> <span class="s2">"America"</span><span class="p">,</span> <span class="s2">"Asia"</span><span class="p">,</span> <span class="s2">"Africa"</span><span class="p">,</span> <span class="s2">"Australia"</span><span class="p">,</span> <span class="s2">"Antartica"</span> <span class="p">],</span> <span class="c1">// ENUM type</span>
        <span class="nx">photo</span>     <span class="o">:</span> <span class="nx">Buffer</span><span class="p">,</span> <span class="c1">// BLOB/BINARY</span>
        <span class="nx">data</span>      <span class="o">:</span> <span class="nb">Object</span> <span class="c1">// JSON encoded</span>
    <span class="p">},</span> <span class="p">{</span>
        <span class="nx">methods</span><span class="o">:</span> <span class="p">{</span>
            <span class="nx">fullName</span><span class="o">:</span> <span class="kd">function</span> <span class="p">()</span> <span class="p">{</span>
                <span class="k">return</span> <span class="k">this</span><span class="p">.</span><span class="nx">name</span> <span class="o">+</span> <span class="s1">' '</span> <span class="o">+</span> <span class="k">this</span><span class="p">.</span><span class="nx">surname</span><span class="p">;</span>
            <span class="p">}</span>
        <span class="p">},</span>
        <span class="nx">validations</span><span class="o">:</span> <span class="p">{</span>
            <span class="nx">age</span><span class="o">:</span> <span class="nx">orm</span><span class="p">.</span><span class="nx">validators</span><span class="p">.</span><span class="nx">rangeNumber</span><span class="p">(</span><span class="mi">18</span><span class="p">,</span> <span class="kc">undefined</span><span class="p">,</span> <span class="s2">"under-age"</span><span class="p">)</span>
        <span class="p">}</span>
    <span class="p">});</span>

    <span class="nx">Person</span><span class="p">.</span><span class="nx">find</span><span class="p">({</span> <span class="nx">surname</span><span class="o">:</span> <span class="s2">"Doe"</span> <span class="p">},</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">people</span><span class="p">)</span> <span class="p">{</span>
        <span class="c1">// SQL: "SELECT * FROM person WHERE surname = 'Doe'"</span>

        <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s2">"People found: %d"</span><span class="p">,</span> <span class="nx">people</span><span class="p">.</span><span class="nx">length</span><span class="p">);</span>
        <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s2">"First person: %s, age %d"</span><span class="p">,</span> <span class="nx">people</span><span class="p">[</span><span class="mi">0</span><span class="p">].</span><span class="nx">fullName</span><span class="p">(),</span> <span class="nx">people</span><span class="p">[</span><span class="mi">0</span><span class="p">].</span><span class="nx">age</span><span class="p">);</span>

        <span class="nx">people</span><span class="p">[</span><span class="mi">0</span><span class="p">].</span><span class="nx">age</span> <span class="o">=</span> <span class="mi">16</span><span class="p">;</span>
        <span class="nx">people</span><span class="p">[</span><span class="mi">0</span><span class="p">].</span><span class="nx">save</span><span class="p">(</span><span class="kd">function</span> <span class="p">(</span><span class="nx">err</span><span class="p">)</span> <span class="p">{</span>
            <span class="c1">// err.msg = "under-age";</span>
        <span class="p">});</span>
    <span class="p">});</span>
<span class="p">});</span>
</pre></div>

<h2>
<a name="express" class="anchor" href="#express"><span class="octicon octicon-link"></span></a>Express</h2>

<p>If you're using Express, you might want to use the simple middleware to integrate more easily.</p>

<div class="highlight"><pre><span class="kd">var</span> <span class="nx">express</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">'express'</span><span class="p">);</span>
<span class="kd">var</span> <span class="nx">orm</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">'orm'</span><span class="p">);</span>
<span class="kd">var</span> <span class="nx">app</span> <span class="o">=</span> <span class="nx">express</span><span class="p">();</span>

<span class="nx">app</span><span class="p">.</span><span class="nx">use</span><span class="p">(</span><span class="nx">orm</span><span class="p">.</span><span class="nx">express</span><span class="p">(</span><span class="s2">"mysql://username:password@host/database"</span><span class="p">,</span> <span class="p">{</span>
    <span class="nx">define</span><span class="o">:</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">db</span><span class="p">,</span> <span class="nx">models</span><span class="p">)</span> <span class="p">{</span>
        <span class="nx">models</span><span class="p">.</span><span class="nx">person</span> <span class="o">=</span> <span class="nx">db</span><span class="p">.</span><span class="nx">define</span><span class="p">(</span><span class="s2">"person"</span><span class="p">,</span> <span class="p">{</span> <span class="p">...</span> <span class="p">});</span>
    <span class="p">}</span>
<span class="p">}));</span>
<span class="nx">app</span><span class="p">.</span><span class="nx">listen</span><span class="p">(</span><span class="mi">80</span><span class="p">);</span>

<span class="nx">app</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="s2">"/"</span><span class="p">,</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">req</span><span class="p">,</span> <span class="nx">res</span><span class="p">)</span> <span class="p">{</span>
    <span class="c1">// req.models is a reference to models used above in define()</span>
    <span class="nx">req</span><span class="p">.</span><span class="nx">models</span><span class="p">.</span><span class="nx">person</span><span class="p">.</span><span class="nx">find</span><span class="p">(...);</span>
<span class="p">});</span>
</pre></div>

<p>You can call <code>orm.express</code> more than once to have multiple database connections. Models defined across connections
will be joined together in <code>req.models</code>. <strong>Don't forget to use it before <code>app.use(app.router)</code>, preferably right after your
assets public folder(s).</strong></p>

<h2>
<a name="settings" class="anchor" href="#settings"><span class="octicon octicon-link"></span></a>Settings</h2>

<p>Settings are used to store key value pairs. A settings object is stored on the global orm object and on each database connection.</p>

<div class="highlight"><pre><span class="kd">var</span> <span class="nx">orm</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s2">"orm"</span><span class="p">);</span>

<span class="nx">orm</span><span class="p">.</span><span class="nx">settings</span><span class="p">.</span><span class="nx">set</span><span class="p">(</span><span class="s2">"some.deep.value"</span><span class="p">,</span> <span class="mi">123</span><span class="p">);</span>

<span class="nx">orm</span><span class="p">.</span><span class="nx">connect</span><span class="p">(</span><span class="s2">"...."</span><span class="p">,</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">db</span><span class="p">)</span> <span class="p">{</span>
    <span class="c1">// db.settings is a snapshot of the settings at the moment</span>
    <span class="c1">// of orm.connect(). changes to it don't affect orm.settings</span>

    <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">db</span><span class="p">.</span><span class="nx">settings</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="s2">"some.deep.value"</span><span class="p">));</span> <span class="c1">// 123</span>
    <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">db</span><span class="p">.</span><span class="nx">settings</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="s2">"some.deep"</span><span class="p">));</span>       <span class="c1">// { value: 123 }</span>
<span class="p">});</span>
</pre></div>

<h2>
<a name="connecting" class="anchor" href="#connecting"><span class="octicon octicon-link"></span></a>Connecting</h2>

<p>First, add the correct driver to your <code>package.json</code>:</p>

<table>
<tr>
<th align="left">driver</th>
<th align="left">dependency</th>
</tr>
<tr>
<td align="left">mysql</td>
<td align="left"><code>"mysql" : "2.0.0-alpha7"</code></td>
</tr>
<tr>
<td align="left">postgres<br>redshift</td>
<td align="left"><code>"pg": "~1.0.0"</code></td>
</tr>
<tr>
<td align="left">sqlite</td>
<td align="left"><code>"sqlite3" : "2.1.7"</code></td>
</tr>
</table><p>These are the versions tested. Use others (older or newer) at your own risk.</p>

<h3>
<a name="options" class="anchor" href="#options"><span class="octicon octicon-link"></span></a>Options</h3>

<p>You can pass in connection options either as a string:</p>

<div class="highlight"><pre><span class="kd">var</span> <span class="nx">orm</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s2">"orm"</span><span class="p">);</span>

<span class="nx">orm</span><span class="p">.</span><span class="nx">connect</span><span class="p">(</span><span class="s2">"mysql://username:password@host/database?pool=true"</span><span class="p">,</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">db</span><span class="p">)</span> <span class="p">{</span>
    <span class="c1">// ...</span>
<span class="p">});</span>
</pre></div>

<p><strong>Note:</strong> <code>pool</code> is only supported by mysql &amp; postgres. When 'pool' is set to true, your database connections are cached so that connections can be reused, optimizing performance.</p>

<p><strong>Note:</strong> <code>strdates</code> is only supported by sqlite. When true, date fields are saved as strings, compatible with django</p>

<p>Or as an object:</p>

<div class="highlight"><pre><span class="kd">var</span> <span class="nx">opts</span> <span class="o">=</span> <span class="p">{</span>
  <span class="nx">database</span> <span class="o">:</span> <span class="s2">"dbname"</span><span class="p">,</span>
  <span class="nx">protocol</span> <span class="o">:</span> <span class="s2">"[mysql|postgres|redshift|sqlite]"</span><span class="p">,</span>
  <span class="nx">host</span>     <span class="o">:</span> <span class="s2">"127.0.0.1"</span><span class="p">,</span>
  <span class="nx">port</span>     <span class="o">:</span> <span class="mi">3306</span><span class="p">,</span>         <span class="c1">// optional, defaults to database default</span>
  <span class="nx">user</span>     <span class="o">:</span> <span class="s2">".."</span><span class="p">,</span>
  <span class="nx">password</span> <span class="o">:</span> <span class="s2">".."</span><span class="p">,</span>
  <span class="nx">query</span>    <span class="o">:</span> <span class="p">{</span>
    <span class="nx">pool</span>     <span class="o">:</span> <span class="kc">true</span><span class="o">|</span><span class="kc">false</span><span class="p">,</span>   <span class="c1">// optional, false by default</span>
    <span class="nx">debug</span>    <span class="o">:</span> <span class="kc">true</span><span class="o">|</span><span class="kc">false</span><span class="p">,</span>   <span class="c1">// optional, false by default</span>
    <span class="nx">strdates</span> <span class="o">:</span> <span class="kc">true</span><span class="o">|</span><span class="kc">false</span>    <span class="c1">// optional, false by default</span>
  <span class="p">}</span>
<span class="p">};</span>
<span class="nx">orm</span><span class="p">.</span><span class="nx">connect</span><span class="p">(</span><span class="nx">opts</span><span class="p">,</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">db</span><span class="p">)</span> <span class="p">{</span>
    <span class="c1">// ...</span>
<span class="p">});</span>
</pre></div>

<p>You can also avoid passing a callback and just listen for the connect event:</p>

<div class="highlight"><pre><span class="kd">var</span> <span class="nx">orm</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s2">"orm"</span><span class="p">);</span>
<span class="kd">var</span> <span class="nx">db</span>  <span class="o">=</span> <span class="nx">orm</span><span class="p">.</span><span class="nx">connect</span><span class="p">(</span><span class="s2">"mysql://username:password@host/database"</span><span class="p">);</span>

<span class="nx">db</span><span class="p">.</span><span class="nx">on</span><span class="p">(</span><span class="s2">"connect"</span><span class="p">,</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">db</span><span class="p">)</span> <span class="p">{</span>
    <span class="c1">// ...</span>
<span class="p">});</span>
</pre></div>

<h2>
<a name="models" class="anchor" href="#models"><span class="octicon octicon-link"></span></a>Models</h2>

<p>A Model is an abstraction over one or more database tables. Models support associations (more below). The name of the model is assumed to match the table name.</p>

<p>Models support behaviours for accessing and manipulating table data.</p>

<h2>
<a name="defining-models" class="anchor" href="#defining-models"><span class="octicon octicon-link"></span></a>Defining Models</h2>

<p>Call <code>define</code> on the database connection to setup a model. The name of the table and model is used as an identifier for the model on the database connection, so you can easily access the model later using the connection.</p>

<div class="highlight"><pre><span class="kd">var</span> <span class="nx">Person</span> <span class="o">=</span> <span class="nx">db</span><span class="p">.</span><span class="nx">define</span><span class="p">(</span><span class="s1">'person'</span><span class="p">,</span> <span class="p">{</span>        <span class="c1">// 'person' will be the table in the database as well as the model id</span>
    <span class="c1">// properties</span>
    <span class="nx">name</span>    <span class="o">:</span> <span class="nb">String</span><span class="p">,</span>                     <span class="c1">// you can use native objects to define the property type</span>
    <span class="nx">surname</span> <span class="o">:</span> <span class="p">{</span> <span class="nx">type</span><span class="o">:</span> <span class="s2">"text"</span><span class="p">,</span> <span class="nx">size</span><span class="o">:</span> <span class="mi">50</span> <span class="p">}</span>  <span class="c1">// or you can be specific and define aditional options</span>
<span class="p">},</span> <span class="p">{</span>
    <span class="c1">// options (optional)</span>
<span class="p">});</span>
</pre></div>

<h3>
<a name="properties" class="anchor" href="#properties"><span class="octicon octicon-link"></span></a>Properties</h3>

<h4>
<a name="types" class="anchor" href="#types"><span class="octicon octicon-link"></span></a>Types</h4>

<table>
<tr>
<th align="left">Native</th>
<th align="left">String</th>
<th align="left">Native</th>
<th align="left">String</th>
</tr>
<tr>
<td align="left">String</td>
<td align="left">'text'</td>
<td align="left">Date</td>
<td align="left">'date '</td>
</tr>
<tr>
<td align="left">Number</td>
<td align="left">'number'</td>
<td align="left">Object</td>
<td align="left">'object'</td>
</tr>
<tr>
<td align="left">Boolean</td>
<td align="left">'boolean'</td>
<td align="left">Buffer</td>
<td align="left">'binary'</td>
</tr>
<tr>
<td align="left"></td>
<td align="left"></td>
<td align="left">---</td>
<td align="left">'enum'</td>
</tr>
</table><h4>
<a name="options-1" class="anchor" href="#options-1"><span class="octicon octicon-link"></span></a>Options</h4>

<h5>
<a name="all-types" class="anchor" href="#all-types"><span class="octicon octicon-link"></span></a>[all types]</h5>

<ul>
<li>
<code>required</code>: true marks the column as <code>NOT NULL</code>, false (default)</li>
<li>
<code>defaultValue</code>: sets the default value for the field</li>
</ul><h5>
<a name="string" class="anchor" href="#string"><span class="octicon octicon-link"></span></a>string</h5>

<ul>
<li>
<code>size</code>: max length of the string</li>
</ul><h5>
<a name="number" class="anchor" href="#number"><span class="octicon octicon-link"></span></a>number</h5>

<ul>
<li>
<code>rational</code>: true (default) creates a FLOAT/REAL, false an INTEGER</li>
<li>
<code>size</code>: byte size of number, default is 4. Note that 8 byte numbers <a href="http://stackoverflow.com/questions/307179/what-is-javascripts-max-int-whats-the-highest-integer-value-a-number-can-go-t">have limitations</a>
</li>
<li>
<code>unsigned</code>: true to make INTEGER unsigned, default is false</li>
</ul><h5>
<a name="date" class="anchor" href="#date"><span class="octicon octicon-link"></span></a>date</h5>

<ul>
<li>
<code>time</code>: true (default) creates a DATETIME/TIMESTAMP, false a DATE</li>
</ul><p>Note that these may vary accross drivers.</p>

<h3>
<a name="instance-methods" class="anchor" href="#instance-methods"><span class="octicon octicon-link"></span></a>Instance Methods</h3>

<p>Are passed in during model definition.</p>

<div class="highlight"><pre><span class="kd">var</span> <span class="nx">Person</span> <span class="o">=</span> <span class="nx">db</span><span class="p">.</span><span class="nx">define</span><span class="p">(</span><span class="s1">'person'</span><span class="p">,</span> <span class="p">{</span>
    <span class="nx">name</span>    <span class="o">:</span> <span class="nb">String</span><span class="p">,</span>
    <span class="nx">surname</span> <span class="o">:</span> <span class="nb">String</span>
<span class="p">},</span> <span class="p">{</span>
    <span class="nx">methods</span><span class="o">:</span> <span class="p">{</span>
        <span class="nx">fullName</span><span class="o">:</span> <span class="kd">function</span> <span class="p">()</span> <span class="p">{</span>
            <span class="k">return</span> <span class="k">this</span><span class="p">.</span><span class="nx">name</span> <span class="o">+</span> <span class="s1">' '</span> <span class="o">+</span> <span class="k">this</span><span class="p">.</span><span class="nx">surname</span><span class="p">;</span>
        <span class="p">}</span>
    <span class="p">}</span>
<span class="p">});</span>

<span class="nx">Person</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="mi">4</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">person</span><span class="p">)</span> <span class="p">{</span>
    <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span> <span class="nx">person</span><span class="p">.</span><span class="nx">fullName</span><span class="p">()</span> <span class="p">);</span>
<span class="p">})</span>
</pre></div>

<h3>
<a name="model-methods" class="anchor" href="#model-methods"><span class="octicon octicon-link"></span></a>Model Methods</h3>

<p>Are defined directly on the model.</p>

<div class="highlight"><pre><span class="kd">var</span> <span class="nx">Person</span> <span class="o">=</span> <span class="nx">db</span><span class="p">.</span><span class="nx">define</span><span class="p">(</span><span class="s1">'person'</span><span class="p">,</span> <span class="p">{</span>
    <span class="nx">name</span>    <span class="o">:</span> <span class="nb">String</span><span class="p">,</span>
    <span class="nx">height</span>  <span class="o">:</span> <span class="p">{</span> <span class="nx">type</span><span class="o">:</span> <span class="s1">'number'</span><span class="p">,</span> <span class="nx">rational</span><span class="o">:</span> <span class="kc">false</span> <span class="p">}</span>
<span class="p">});</span>
<span class="nx">Person</span><span class="p">.</span><span class="nx">tallerThan</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">height</span><span class="p">,</span> <span class="nx">callback</span><span class="p">)</span> <span class="p">{</span>
    <span class="k">this</span><span class="p">.</span><span class="nx">find</span><span class="p">({</span> <span class="nx">height</span><span class="o">:</span> <span class="nx">orm</span><span class="p">.</span><span class="nx">gt</span><span class="p">(</span><span class="nx">height</span><span class="p">)</span> <span class="p">},</span> <span class="nx">callback</span><span class="p">);</span>
<span class="p">};</span>

<span class="nx">Person</span><span class="p">.</span><span class="nx">tallerThan</span><span class="p">(</span> <span class="mi">192</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">tallPeople</span><span class="p">)</span> <span class="p">{</span> <span class="p">...</span> <span class="p">}</span> <span class="p">);</span>
</pre></div>

<h2>
<a name="loading-models" class="anchor" href="#loading-models"><span class="octicon octicon-link"></span></a>Loading Models</h2>

<p>Models can be in separate modules. Simply ensure that the module holding the models uses module.exports to publish a function that accepts the database connection, then load your models however you like.</p>

<p>Note - using this technique you can have cascading loads.</p>

<div class="highlight"><pre><span class="c1">// your main file (after connecting)</span>
<span class="nx">db</span><span class="p">.</span><span class="nx">load</span><span class="p">(</span><span class="s2">"./models"</span><span class="p">,</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">err</span><span class="p">)</span> <span class="p">{</span>
    <span class="c1">// loaded!</span>
    <span class="kd">var</span> <span class="nx">Person</span> <span class="o">=</span> <span class="nx">db</span><span class="p">.</span><span class="nx">models</span><span class="p">.</span><span class="nx">person</span><span class="p">;</span>
    <span class="kd">var</span> <span class="nx">Pet</span>    <span class="o">=</span> <span class="nx">db</span><span class="p">.</span><span class="nx">models</span><span class="p">.</span><span class="nx">pet</span><span class="p">;</span>
<span class="p">});</span>

<span class="c1">// models.js</span>
<span class="nx">module</span><span class="p">.</span><span class="nx">exports</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">db</span><span class="p">,</span> <span class="nx">cb</span><span class="p">)</span> <span class="p">{</span>
    <span class="nx">db</span><span class="p">.</span><span class="nx">load</span><span class="p">(</span><span class="s2">"./models-extra"</span><span class="p">,</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">err</span><span class="p">)</span> <span class="p">{</span>
        <span class="k">if</span> <span class="p">(</span><span class="nx">err</span><span class="p">)</span> <span class="p">{</span>
            <span class="k">return</span> <span class="nx">cb</span><span class="p">(</span><span class="nx">err</span><span class="p">);</span>
        <span class="p">}</span>

        <span class="nx">db</span><span class="p">.</span><span class="nx">define</span><span class="p">(</span><span class="s1">'person'</span><span class="p">,</span> <span class="p">{</span>
            <span class="nx">name</span> <span class="o">:</span> <span class="nb">String</span>
        <span class="p">});</span>

        <span class="k">return</span> <span class="nx">cb</span><span class="p">();</span>
    <span class="p">});</span>
<span class="p">};</span>

<span class="c1">// models-extra.js</span>
<span class="nx">module</span><span class="p">.</span><span class="nx">exports</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">db</span><span class="p">,</span> <span class="nx">cb</span><span class="p">)</span> <span class="p">{</span>
    <span class="nx">db</span><span class="p">.</span><span class="nx">define</span><span class="p">(</span><span class="s1">'pet'</span><span class="p">,</span> <span class="p">{</span>
        <span class="nx">name</span> <span class="o">:</span> <span class="nb">String</span>
    <span class="p">});</span>

    <span class="k">return</span> <span class="nx">cb</span><span class="p">();</span>
<span class="p">};</span>
</pre></div>

<h2>
<a name="synchronizing-models" class="anchor" href="#synchronizing-models"><span class="octicon octicon-link"></span></a>Synchronizing Models</h2>

<p>Models can create their underlying tables in the database. You may call Model.sync() on each Model to create the underlying table or you can call db.sync() at a connection level to create all tables for all models.</p>

<div class="highlight"><pre><span class="c1">// db.sync() can also be used</span>
<span class="nx">Person</span><span class="p">.</span><span class="nx">sync</span><span class="p">(</span><span class="kd">function</span> <span class="p">(</span><span class="nx">err</span><span class="p">)</span> <span class="p">{</span>
    <span class="o">!</span><span class="nx">err</span> <span class="o">&amp;&amp;</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s2">"done!"</span><span class="p">);</span>
<span class="p">});</span>
</pre></div>

<h2>
<a name="dropping-models" class="anchor" href="#dropping-models"><span class="octicon octicon-link"></span></a>Dropping Models</h2>

<p>If you want to drop a Model and remove all tables you can use the <code>.drop()</code> method.</p>

<div class="highlight"><pre><span class="nx">Person</span><span class="p">.</span><span class="nx">drop</span><span class="p">(</span><span class="kd">function</span> <span class="p">(</span><span class="nx">err</span><span class="p">)</span> <span class="p">{</span>
    <span class="o">!</span><span class="nx">err</span> <span class="o">&amp;&amp;</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s2">"person model no longer exists!"</span><span class="p">);</span>
<span class="p">});</span>
</pre></div>

<h2>
<a name="advanced-options" class="anchor" href="#advanced-options"><span class="octicon octicon-link"></span></a>Advanced Options</h2>

<p>ORM2 allows you some advanced tweaks on your Model definitions. You can configure these via settings or in the call to <code>define</code> when you setup the Model.</p>

<p>For example, each Model instance has a unique ID in the database. This table column is
by default "id" but you can change it.</p>

<div class="highlight"><pre><span class="kd">var</span> <span class="nx">Person</span> <span class="o">=</span> <span class="nx">db</span><span class="p">.</span><span class="nx">define</span><span class="p">(</span><span class="s2">"person"</span><span class="p">,</span> <span class="p">{</span>
    <span class="nx">name</span> <span class="o">:</span> <span class="nb">String</span>
<span class="p">},</span> <span class="p">{</span>
    <span class="nx">id</span>   <span class="o">:</span> <span class="s2">"person_id"</span>
<span class="p">});</span>

<span class="c1">// or just do it globally..</span>
<span class="nx">db</span><span class="p">.</span><span class="nx">settings</span><span class="p">.</span><span class="nx">set</span><span class="p">(</span><span class="s2">"properties.primary_key"</span><span class="p">,</span> <span class="s2">"UID"</span><span class="p">);</span>

<span class="c1">// ..and then define your Models</span>
<span class="kd">var</span> <span class="nx">Pet</span> <span class="o">=</span> <span class="nx">db</span><span class="p">.</span><span class="nx">define</span><span class="p">(</span><span class="s2">"pet"</span><span class="p">,</span> <span class="p">{</span>
    <span class="nx">name</span> <span class="o">:</span> <span class="nb">String</span>
<span class="p">});</span>
</pre></div>

<p><strong>Pet</strong> model will have 2 columns, an <code>UID</code> and a <code>name</code>.</p>

<p>Other options:</p>

<ul>
<li>
<code>cache</code> : (default: <code>true</code>) Set it to <code>false</code> to disable Instance cache (<a href="#singleton">Singletons</a>) or set a timeout value (in seconds);</li>
<li>
<code>autoSave</code> : (default: <code>false</code>) Set it to <code>true</code> to save an Instance right after changing any property;</li>
<li>
<code>autoFetch</code> : (default: <code>false</code>) Set it to <code>true</code> to fetch associations when fetching an instance from the database;</li>
<li>
<code>autoFetchLimit</code> : (default: <code>1</code>) If <code>autoFetch</code> is enabled this defines how many hoops (associations of associations)
you want it to automatically fetch.</li>
</ul><h2>
<a name="hooks" class="anchor" href="#hooks"><span class="octicon octicon-link"></span></a>Hooks</h2>

<p>If you want to listen for a type of event than occurs in instances of a Model, you can attach a function that
will be called when that event happens.</p>

<p>Currently the following events are supported:</p>

<ul>
<li>
<code>afterLoad</code> : (no parameters) Right after loading and preparing an instance to be used;</li>
<li>
<code>afterAutoFetch</code> : (no parameters) Right after auto-fetching associations (if any), it will trigger regardless of having associations or not;</li>
<li>
<code>beforeSave</code> : (no parameters) Right before trying to save;</li>
<li>
<code>afterSave</code> : (bool success) Right after saving;</li>
<li>
<code>beforeCreate</code> : (no parameters) Right before trying to save a new instance (prior to <code>beforeSave</code>);</li>
<li>
<code>afterCreate</code> : (bool success) Right after saving a new instance;</li>
<li>
<code>beforeRemove</code> : (no parameters) Right before trying to remove an instance;</li>
<li>
<code>afterRemove</code> : (bool success) Right after removing an instance;</li>
<li>
<code>beforeValidation</code> : (no parameters) Before all validations and prior to <code>beforeCreate</code> and <code>beforeSave</code>;</li>
</ul><p>All hook function are called with <code>this</code> as the instance so you can access anything you want related to it.</p>

<p>For all <code>before*</code> hooks, you can add an additional parameter to the hook function. This parameter will be a function that
must be called to tell if the hook allows the execution to continue or to break. You might be familiar with this workflow
already from Express. Here's an example:</p>

<div class="highlight"><pre><span class="kd">var</span> <span class="nx">Person</span> <span class="o">=</span> <span class="nx">db</span><span class="p">.</span><span class="nx">define</span><span class="p">(</span><span class="s2">"person"</span><span class="p">,</span> <span class="p">{</span>
    <span class="nx">name</span>    <span class="o">:</span> <span class="nb">String</span><span class="p">,</span>
    <span class="nx">surname</span> <span class="o">:</span> <span class="nb">String</span>
<span class="p">},</span> <span class="p">{</span>
    <span class="nx">hooks</span><span class="o">:</span> <span class="p">{</span>
        <span class="nx">beforeCreate</span><span class="o">:</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">next</span><span class="p">)</span> <span class="p">{</span>
            <span class="k">if</span> <span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">surname</span> <span class="o">==</span> <span class="s2">"Doe"</span><span class="p">)</span> <span class="p">{</span>
                <span class="k">return</span> <span class="nx">next</span><span class="p">(</span><span class="k">new</span> <span class="nb">Error</span><span class="p">(</span><span class="s2">"No Does allowed"</span><span class="p">));</span>
            <span class="p">}</span>
            <span class="k">return</span> <span class="nx">next</span><span class="p">();</span>
        <span class="p">}</span>
    <span class="p">}</span>
<span class="p">});</span>
</pre></div>

<p>This workflow allows you to make asynchronous work before calling <code>next</code>.</p>

<h2>
<a name="finding-items" class="anchor" href="#finding-items"><span class="octicon octicon-link"></span></a>Finding Items</h2>

<h3>
<a name="modelgetid--options--cb" class="anchor" href="#modelgetid--options--cb"><span class="octicon octicon-link"></span></a>Model.get(id, [ options ], cb)</h3>

<p>To get a specific element from the database use <code>Model.get</code>.</p>

<div class="highlight"><pre><span class="nx">Person</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="mi">123</span><span class="p">,</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">person</span><span class="p">)</span> <span class="p">{</span>
    <span class="c1">// finds person with id = 123</span>
<span class="p">});</span>
</pre></div>

<h3>
<a name="modelfind-conditions---options---limit---order---cb-" class="anchor" href="#modelfind-conditions---options---limit---order---cb-"><span class="octicon octicon-link"></span></a>Model.find([ conditions ] [, options ] [, limit ] [, order ] [, cb ])</h3>

<p>Finding one or more elements has more options, each one can be given in no specific parameter order. Only <code>options</code> has to be after <code>conditions</code> (even if it's an empty object).</p>

<div class="highlight"><pre><span class="nx">Person</span><span class="p">.</span><span class="nx">find</span><span class="p">({</span> <span class="nx">name</span><span class="o">:</span> <span class="s2">"John"</span><span class="p">,</span> <span class="nx">surname</span><span class="o">:</span> <span class="s2">"Doe"</span> <span class="p">},</span> <span class="mi">3</span><span class="p">,</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">people</span><span class="p">)</span> <span class="p">{</span>
    <span class="c1">// finds people with name='John' AND surname='Doe' and returns the first 3</span>
<span class="p">});</span>
</pre></div>

<p>If you need to sort the results because you're limiting or just because you want them sorted do:</p>

<div class="highlight"><pre><span class="nx">Person</span><span class="p">.</span><span class="nx">find</span><span class="p">({</span> <span class="nx">surname</span><span class="o">:</span> <span class="s2">"Doe"</span> <span class="p">},</span> <span class="s2">"name"</span><span class="p">,</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">people</span><span class="p">)</span> <span class="p">{</span>
    <span class="c1">// finds people with surname='Doe' and returns sorted by name ascending</span>
<span class="p">});</span>
<span class="nx">Person</span><span class="p">.</span><span class="nx">find</span><span class="p">({</span> <span class="nx">surname</span><span class="o">:</span> <span class="s2">"Doe"</span> <span class="p">},</span> <span class="p">[</span> <span class="s2">"name"</span><span class="p">,</span> <span class="s2">"Z"</span> <span class="p">],</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">people</span><span class="p">)</span> <span class="p">{</span>
    <span class="c1">// finds people with surname='Doe' and returns sorted by name descending</span>
    <span class="c1">// ('Z' means DESC; 'A' means ASC - default)</span>
<span class="p">});</span>
</pre></div>

<p>There are more options that you can pass to find something. These options are passed in a second object:</p>

<div class="highlight"><pre><span class="nx">Person</span><span class="p">.</span><span class="nx">find</span><span class="p">({</span> <span class="nx">surname</span><span class="o">:</span> <span class="s2">"Doe"</span> <span class="p">},</span> <span class="p">{</span> <span class="nx">offset</span><span class="o">:</span> <span class="mi">2</span> <span class="p">},</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">people</span><span class="p">)</span> <span class="p">{</span>
    <span class="c1">// finds people with surname='Doe', skips the first 2 and returns the others</span>
<span class="p">});</span>
</pre></div>

<h3>
<a name="modelcount-conditions--cb" class="anchor" href="#modelcount-conditions--cb"><span class="octicon octicon-link"></span></a>Model.count([ conditions, ] cb)</h3>

<p>If you just want to count the number of items that match a condition you can just use <code>.count()</code> instead of finding all
of them and counting. This will actually tell the database server to do a count (it won't be done in the node process itself).</p>

<div class="highlight"><pre><span class="nx">Person</span><span class="p">.</span><span class="nx">count</span><span class="p">({</span> <span class="nx">surname</span><span class="o">:</span> <span class="s2">"Doe"</span> <span class="p">},</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">count</span><span class="p">)</span> <span class="p">{</span>
    <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s2">"We have %d Does in our db"</span><span class="p">,</span> <span class="nx">count</span><span class="p">);</span>
<span class="p">});</span>
</pre></div>

<h3>
<a name="modelexists-conditions--cb" class="anchor" href="#modelexists-conditions--cb"><span class="octicon octicon-link"></span></a>Model.exists([ conditions, ] cb)</h3>

<p>Similar to <code>.count()</code>, this method just checks if the count is greater than zero or not.</p>

<div class="highlight"><pre><span class="nx">Person</span><span class="p">.</span><span class="nx">exists</span><span class="p">({</span> <span class="nx">surname</span><span class="o">:</span> <span class="s2">"Doe"</span> <span class="p">},</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">exists</span><span class="p">)</span> <span class="p">{</span>
    <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s2">"We %s Does in our db"</span><span class="p">,</span> <span class="nx">exists</span> <span class="o">?</span> <span class="s2">"have"</span> <span class="o">:</span> <span class="s2">"don't have"</span><span class="p">);</span>
<span class="p">});</span>
</pre></div>

<h3>
<a name="aggregating-functions" class="anchor" href="#aggregating-functions"><span class="octicon octicon-link"></span></a>Aggregating Functions</h3>

<p>If you need to get some aggregated values from a Model, you can use <code>Model.aggregate()</code>. Here's an example to better
illustrate:</p>

<div class="highlight"><pre><span class="nx">Person</span><span class="p">.</span><span class="nx">aggregate</span><span class="p">({</span> <span class="nx">surname</span><span class="o">:</span> <span class="s2">"Doe"</span> <span class="p">}).</span><span class="nx">min</span><span class="p">(</span><span class="s2">"age"</span><span class="p">).</span><span class="nx">max</span><span class="p">(</span><span class="s2">"age"</span><span class="p">).</span><span class="nx">get</span><span class="p">(</span><span class="kd">function</span> <span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">min</span><span class="p">,</span> <span class="nx">max</span><span class="p">)</span> <span class="p">{</span>
    <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s2">"The youngest Doe guy has %d years, while the oldest is %d"</span><span class="p">,</span> <span class="nx">min</span><span class="p">,</span> <span class="nx">max</span><span class="p">);</span>
<span class="p">});</span>
</pre></div>

<p>An <code>Array</code> of properties can be passed to select only a few properties. An <code>Object</code> is also accepted to define conditions.</p>

<p>Here's an example to illustrate how to use <code>.groupBy()</code>:</p>

<div class="highlight"><pre><span class="c1">//The same as "select avg(weight), age from person where country='someCountry' group by age;"</span>
<span class="nx">Person</span><span class="p">.</span><span class="nx">aggregate</span><span class="p">([</span><span class="s2">"age"</span><span class="p">],</span> <span class="p">{</span> <span class="nx">country</span><span class="o">:</span> <span class="s2">"someCountry"</span> <span class="p">}).</span><span class="nx">avg</span><span class="p">(</span><span class="s2">"weight"</span><span class="p">).</span><span class="nx">groupBy</span><span class="p">(</span><span class="s2">"age"</span><span class="p">).</span><span class="nx">get</span><span class="p">(</span><span class="kd">function</span> <span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">stats</span><span class="p">)</span> <span class="p">{</span>
    <span class="c1">// stats is an Array, each item should have 'age' and 'avg_weight'</span>
<span class="p">});</span>
</pre></div>

<h3>
<a name="base-aggregate-methods" class="anchor" href="#base-aggregate-methods"><span class="octicon octicon-link"></span></a>Base <code>.aggregate()</code> methods</h3>

<ul>
<li>
<code>.limit()</code>: you can pass a number as a limit, or two numbers as offset and limit respectively</li>
<li>
<code>.order()</code>: same as <code>Model.find().order()</code>
</li>
</ul><h3>
<a name="additional-aggregate-methods" class="anchor" href="#additional-aggregate-methods"><span class="octicon octicon-link"></span></a>Additional <code>.aggregate()</code> methods</h3>

<ul>
<li><code>min</code></li>
<li><code>max</code></li>
<li><code>avg</code></li>
<li><code>sum</code></li>
<li>
<code>count</code> (there's a shortcut to this - <code>Model.count</code>)</li>
</ul><p>There are more aggregate functions depending on the driver (Math functions for example).</p>

<h4>
<a name="chaining" class="anchor" href="#chaining"><span class="octicon octicon-link"></span></a>Chaining</h4>

<p>If you prefer less complicated syntax you can chain <code>.find()</code> by not giving a callback parameter.</p>

<div class="highlight"><pre><span class="nx">Person</span><span class="p">.</span><span class="nx">find</span><span class="p">({</span> <span class="nx">surname</span><span class="o">:</span> <span class="s2">"Doe"</span> <span class="p">}).</span><span class="nx">limit</span><span class="p">(</span><span class="mi">3</span><span class="p">).</span><span class="nx">offset</span><span class="p">(</span><span class="mi">2</span><span class="p">).</span><span class="nx">only</span><span class="p">(</span><span class="s2">"name"</span><span class="p">,</span> <span class="s2">"surname"</span><span class="p">).</span><span class="nx">run</span><span class="p">(</span><span class="kd">function</span> <span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">people</span><span class="p">)</span> <span class="p">{</span>
    <span class="c1">// finds people with surname='Doe', skips first 2 and limits to 3 elements,</span>
    <span class="c1">// returning only 'name' and 'surname' properties</span>
<span class="p">});</span>
</pre></div>

<p>You can also chain and just get the count in the end. In this case, offset, limit and order are ignored.</p>

<div class="highlight"><pre><span class="nx">Person</span><span class="p">.</span><span class="nx">find</span><span class="p">({</span> <span class="nx">surname</span><span class="o">:</span> <span class="s2">"Doe"</span> <span class="p">}).</span><span class="nx">count</span><span class="p">(</span><span class="kd">function</span> <span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">people</span><span class="p">)</span> <span class="p">{</span>
    <span class="c1">// people = number of people with surname="Doe"</span>
<span class="p">});</span>
</pre></div>

<p>Also available is the option to remove the selected items.</p>

<div class="highlight"><pre><span class="nx">Person</span><span class="p">.</span><span class="nx">find</span><span class="p">({</span> <span class="nx">surname</span><span class="o">:</span> <span class="s2">"Doe"</span> <span class="p">}).</span><span class="nx">remove</span><span class="p">(</span><span class="kd">function</span> <span class="p">(</span><span class="nx">err</span><span class="p">)</span> <span class="p">{</span>
    <span class="c1">// Does gone..</span>
<span class="p">});</span>
</pre></div>

<p>You can also make modifications to your instances using common Array traversal methods and save everything
in the end.</p>

<div class="highlight"><pre><span class="nx">Person</span><span class="p">.</span><span class="nx">find</span><span class="p">({</span> <span class="nx">surname</span><span class="o">:</span> <span class="s2">"Doe"</span> <span class="p">}).</span><span class="nx">each</span><span class="p">(</span><span class="kd">function</span> <span class="p">(</span><span class="nx">person</span><span class="p">)</span> <span class="p">{</span>
    <span class="nx">person</span><span class="p">.</span><span class="nx">surname</span> <span class="o">=</span> <span class="s2">"Dean"</span><span class="p">;</span>
<span class="p">}).</span><span class="nx">save</span><span class="p">(</span><span class="kd">function</span> <span class="p">(</span><span class="nx">err</span><span class="p">)</span> <span class="p">{</span>
    <span class="c1">// done!</span>
<span class="p">});</span>

<span class="nx">Person</span><span class="p">.</span><span class="nx">find</span><span class="p">({</span> <span class="nx">surname</span><span class="o">:</span> <span class="s2">"Doe"</span> <span class="p">}).</span><span class="nx">each</span><span class="p">().</span><span class="nx">filter</span><span class="p">(</span><span class="kd">function</span> <span class="p">(</span><span class="nx">person</span><span class="p">)</span> <span class="p">{</span>
    <span class="k">return</span> <span class="nx">person</span><span class="p">.</span><span class="nx">age</span> <span class="o">&gt;=</span> <span class="mi">18</span><span class="p">;</span>
<span class="p">}).</span><span class="nx">sort</span><span class="p">(</span><span class="kd">function</span> <span class="p">(</span><span class="nx">person1</span><span class="p">,</span> <span class="nx">person2</span><span class="p">)</span> <span class="p">{</span>
    <span class="k">return</span> <span class="nx">person1</span><span class="p">.</span><span class="nx">age</span> <span class="o">&lt;</span> <span class="nx">person2</span><span class="p">.</span><span class="nx">age</span><span class="p">;</span>
<span class="p">}).</span><span class="nx">get</span><span class="p">(</span><span class="kd">function</span> <span class="p">(</span><span class="nx">people</span><span class="p">)</span> <span class="p">{</span>
    <span class="c1">// get all people with at least 18 years, sorted by age</span>
<span class="p">});</span>
</pre></div>

<p>Of course you could do this directly on <code>.find()</code>, but for some more complicated tasks this can be very usefull.</p>

<p><code>Model.find()</code> does not return an Array so you can't just chain directly. To start chaining you have to call
<code>.each()</code> (with an optional callback if you want to traverse the list). You can then use the common functions
<code>.filter()</code>, <code>.sort()</code> and <code>.forEach()</code> more than once.</p>

<p>In the end (or during the process..) you can call:</p>

<ul>
<li>
<code>.count()</code> if you just want to know how many items there are;</li>
<li>
<code>.get()</code> to retrieve the list;</li>
<li>
<code>.save()</code> to save all item changes.</li>
</ul><h4>
<a name="conditions" class="anchor" href="#conditions"><span class="octicon octicon-link"></span></a>Conditions</h4>

<p>Conditions are defined as an object where every key is a property (table column). All keys are supposed
to be concatenated by the logical <code>AND</code>. Values are considered to match exactly, unless you're passing
an <code>Array</code>. In this case it is considered a list to compare the property with.</p>

<div class="highlight"><pre><span class="p">{</span> <span class="nx">col1</span><span class="o">:</span> <span class="mi">123</span><span class="p">,</span> <span class="nx">col2</span><span class="o">:</span> <span class="s2">"foo"</span> <span class="p">}</span> <span class="c1">// `col1` = 123 AND `col2` = 'foo'</span>
<span class="p">{</span> <span class="nx">col1</span><span class="o">:</span> <span class="p">[</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">5</span> <span class="p">]</span> <span class="p">}</span> <span class="c1">// `col1` IN (1, 3, 5)</span>
</pre></div>

<p>If you need other comparisons, you have to use a special object created by some helper functions. Here are
a few examples to describe it:</p>

<div class="highlight"><pre><span class="p">{</span> <span class="nx">col1</span><span class="o">:</span> <span class="nx">orm</span><span class="p">.</span><span class="nx">eq</span><span class="p">(</span><span class="mi">123</span><span class="p">)</span> <span class="p">}</span> <span class="c1">// `col1` = 123 (default)</span>
<span class="p">{</span> <span class="nx">col1</span><span class="o">:</span> <span class="nx">orm</span><span class="p">.</span><span class="nx">ne</span><span class="p">(</span><span class="mi">123</span><span class="p">)</span> <span class="p">}</span> <span class="c1">// `col1` &lt;&gt; 123</span>
<span class="p">{</span> <span class="nx">col1</span><span class="o">:</span> <span class="nx">orm</span><span class="p">.</span><span class="nx">gt</span><span class="p">(</span><span class="mi">123</span><span class="p">)</span> <span class="p">}</span> <span class="c1">// `col1` &gt; 123</span>
<span class="p">{</span> <span class="nx">col1</span><span class="o">:</span> <span class="nx">orm</span><span class="p">.</span><span class="nx">gte</span><span class="p">(</span><span class="mi">123</span><span class="p">)</span> <span class="p">}</span> <span class="c1">// `col1` &gt;= 123</span>
<span class="p">{</span> <span class="nx">col1</span><span class="o">:</span> <span class="nx">orm</span><span class="p">.</span><span class="nx">lt</span><span class="p">(</span><span class="mi">123</span><span class="p">)</span> <span class="p">}</span> <span class="c1">// `col1` &lt; 123</span>
<span class="p">{</span> <span class="nx">col1</span><span class="o">:</span> <span class="nx">orm</span><span class="p">.</span><span class="nx">lte</span><span class="p">(</span><span class="mi">123</span><span class="p">)</span> <span class="p">}</span> <span class="c1">// `col1` &lt;= 123</span>
<span class="p">{</span> <span class="nx">col1</span><span class="o">:</span> <span class="nx">orm</span><span class="p">.</span><span class="nx">between</span><span class="p">(</span><span class="mi">123</span><span class="p">,</span> <span class="mi">456</span><span class="p">)</span> <span class="p">}</span> <span class="c1">// `col1` BETWEEN 123 AND 456</span>
<span class="p">{</span> <span class="nx">col1</span><span class="o">:</span> <span class="nx">orm</span><span class="p">.</span><span class="nx">like</span><span class="p">(</span><span class="mi">12</span> <span class="o">+</span> <span class="s2">"%"</span><span class="p">)</span> <span class="p">}</span> <span class="c1">// `col1` like '12%'</span>
</pre></div>

<h3>
<a name="caching--integrity" class="anchor" href="#caching--integrity"><span class="octicon octicon-link"></span></a>Caching &amp; Integrity</h3>

<p>Model instances are cached. If multiple different queries will result in the same result, you will
get the same object. If you have other systems that can change your database (or you're developing and need
to make some manual changes) you should remove this feature by disabling cache. This can be done when you're
defining the Model.</p>

<div class="highlight"><pre><span class="kd">var</span> <span class="nx">Person</span> <span class="o">=</span> <span class="nx">db</span><span class="p">.</span><span class="nx">define</span><span class="p">(</span><span class="s1">'person'</span><span class="p">,</span> <span class="p">{</span>
    <span class="nx">name</span>    <span class="o">:</span> <span class="nb">String</span>
<span class="p">},</span> <span class="p">{</span>
    <span class="nx">cache</span>   <span class="o">:</span> <span class="kc">false</span>
<span class="p">});</span>
</pre></div>

<p>and also globally:</p>

<div class="highlight"><pre><span class="nx">orm</span><span class="p">.</span><span class="nx">connect</span><span class="p">(</span><span class="s1">'...'</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">db</span><span class="p">)</span> <span class="p">{</span>
  <span class="nx">db</span><span class="p">.</span><span class="nx">settings</span><span class="p">.</span><span class="nx">set</span><span class="p">(</span><span class="s1">'instance.cache'</span><span class="p">,</span> <span class="kc">false</span><span class="p">);</span>
<span class="p">});</span>
</pre></div>

<p>The cache can be configured to expire after a period of time by passing in a number instead of a
boolean. The number will be considered the cache timeout in seconds (you can use floating point).</p>

<p><strong>Note</strong>: One exception about Caching is that it won't be used if an instance is not saved. For example, if
you fetch a Person and then change it, while it doesn't get saved it won't be passed from Cache.</p>

<h2>
<a name="creating-items" class="anchor" href="#creating-items"><span class="octicon octicon-link"></span></a>Creating Items</h2>

<h3>
<a name="modelcreateitems-cb" class="anchor" href="#modelcreateitems-cb"><span class="octicon octicon-link"></span></a>Model.create(items, cb)</h3>

<p>To insert new elements to the database use <code>Model.create</code>.</p>

<div class="highlight"><pre><span class="nx">Person</span><span class="p">.</span><span class="nx">create</span><span class="p">([</span>
    <span class="p">{</span>
        <span class="nx">name</span><span class="o">:</span> <span class="s2">"John"</span><span class="p">,</span>
        <span class="nx">surname</span><span class="o">:</span> <span class="s2">"Doe"</span><span class="p">,</span>
        <span class="nx">age</span><span class="o">:</span> <span class="mi">25</span><span class="p">,</span>
        <span class="nx">male</span><span class="o">:</span> <span class="kc">true</span>
    <span class="p">},</span>
    <span class="p">{</span>
        <span class="nx">name</span><span class="o">:</span> <span class="s2">"Liza"</span><span class="p">,</span>
        <span class="nx">surname</span><span class="o">:</span> <span class="s2">"Kollan"</span><span class="p">,</span>
        <span class="nx">age</span><span class="o">:</span> <span class="mi">19</span><span class="p">,</span>
        <span class="nx">male</span><span class="o">:</span> <span class="kc">false</span>
    <span class="p">}</span>
<span class="p">],</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">items</span><span class="p">)</span> <span class="p">{</span>
    <span class="c1">// err - description of the error or null</span>
    <span class="c1">// items - array of inserted items</span>
<span class="p">});</span>
</pre></div>

<h2>
<a name="updating-items" class="anchor" href="#updating-items"><span class="octicon octicon-link"></span></a>Updating Items</h2>

<p>Every item returned has the properties that were defined to the Model and also a couple of methods you can
use to change each item.</p>

<div class="highlight"><pre><span class="nx">Person</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">John</span><span class="p">)</span> <span class="p">{</span>
    <span class="nx">John</span><span class="p">.</span><span class="nx">name</span> <span class="o">=</span> <span class="s2">"Joe"</span><span class="p">;</span>
    <span class="nx">John</span><span class="p">.</span><span class="nx">surname</span> <span class="o">=</span> <span class="s2">"Doe"</span><span class="p">;</span>
    <span class="nx">John</span><span class="p">.</span><span class="nx">save</span><span class="p">(</span><span class="kd">function</span> <span class="p">(</span><span class="nx">err</span><span class="p">)</span> <span class="p">{</span>
        <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s2">"saved!"</span><span class="p">);</span>
    <span class="p">});</span>
<span class="p">});</span>
</pre></div>

<p>Updating and then saving an instance can be done in a single call:</p>

<div class="highlight"><pre><span class="nx">Person</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">John</span><span class="p">)</span> <span class="p">{</span>
    <span class="nx">John</span><span class="p">.</span><span class="nx">save</span><span class="p">({</span> <span class="nx">name</span><span class="o">:</span> <span class="s2">"Joe"</span><span class="p">,</span> <span class="nx">surname</span><span class="o">:</span> <span class="s2">"Doe"</span> <span class="p">},</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">err</span><span class="p">)</span> <span class="p">{</span>
        <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s2">"saved!"</span><span class="p">);</span>
    <span class="p">});</span>
<span class="p">});</span>
</pre></div>

<p>If you want to remove an instance, just do:</p>

<div class="highlight"><pre><span class="c1">// you could do this without even fetching it, look at Chaining section above</span>
<span class="nx">Person</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">John</span><span class="p">)</span> <span class="p">{</span>
    <span class="nx">John</span><span class="p">.</span><span class="nx">remove</span><span class="p">(</span><span class="kd">function</span> <span class="p">(</span><span class="nx">err</span><span class="p">)</span> <span class="p">{</span>
        <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s2">"removed!"</span><span class="p">);</span>
    <span class="p">});</span>
<span class="p">});</span>
</pre></div>

<h2>
<a name="validations" class="anchor" href="#validations"><span class="octicon octicon-link"></span></a>Validations</h2>

<p>You can define validations for every property of a Model. You can have one or more validations for each property.
You can also use the predefined validations or create your own.</p>

<div class="highlight"><pre><span class="kd">var</span> <span class="nx">Person</span> <span class="o">=</span> <span class="nx">db</span><span class="p">.</span><span class="nx">define</span><span class="p">(</span><span class="s2">"person"</span><span class="p">,</span> <span class="p">{</span>
    <span class="nx">name</span> <span class="o">:</span> <span class="nb">String</span><span class="p">,</span>
    <span class="nx">age</span>  <span class="o">:</span> <span class="nb">Number</span>
<span class="p">},</span> <span class="p">{</span>
    <span class="nx">validations</span> <span class="o">:</span> <span class="p">{</span>
        <span class="nx">name</span> <span class="o">:</span> <span class="nx">orm</span><span class="p">.</span><span class="nx">validators</span><span class="p">.</span><span class="nx">rangeLength</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="kc">undefined</span><span class="p">,</span> <span class="s2">"missing"</span><span class="p">),</span> <span class="c1">// "missing" is a name given to this validation, instead of default</span>
        <span class="nx">age</span>  <span class="o">:</span> <span class="p">[</span> <span class="nx">orm</span><span class="p">.</span><span class="nx">validators</span><span class="p">.</span><span class="nx">rangeNumber</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">10</span><span class="p">),</span> <span class="nx">orm</span><span class="p">.</span><span class="nx">validators</span><span class="p">.</span><span class="nx">insideList</span><span class="p">([</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">7</span><span class="p">,</span> <span class="mi">9</span> <span class="p">])</span> <span class="p">]</span>
    <span class="p">}</span>
<span class="p">});</span>
</pre></div>

<p>The code above defines that the <code>name</code> length must be between 1 and undefined (undefined means any) and <code>age</code>
must be a number between 0 and 10 (inclusive) but also one of the listed values. The example might not make sense
but you get the point.</p>

<p>When saving an item, if it fails to validate any of the defined validations you'll get an <code>error</code> object with the property
name and validation error description. This description should help you identify what happened.</p>

<div class="highlight"><pre><span class="kd">var</span> <span class="nx">John</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Person</span><span class="p">({</span>
    <span class="nx">name</span> <span class="o">:</span> <span class="s2">""</span><span class="p">,</span>
    <span class="nx">age</span> <span class="o">:</span> <span class="mi">20</span>
<span class="p">});</span>
<span class="nx">John</span><span class="p">.</span><span class="nx">save</span><span class="p">(</span><span class="kd">function</span> <span class="p">(</span><span class="nx">err</span><span class="p">)</span> <span class="p">{</span>
    <span class="c1">// err.field = "name" , err.value = "" , err.msg = "missing"</span>
<span class="p">});</span>
</pre></div>

<p>The validation stops after the first validation error. If you want it to validate every property and return all validation
errors, you can change this behavior on global or local settings:</p>

<div class="highlight"><pre><span class="kd">var</span> <span class="nx">orm</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s2">"orm"</span><span class="p">);</span>

<span class="nx">orm</span><span class="p">.</span><span class="nx">settings</span><span class="p">.</span><span class="nx">set</span><span class="p">(</span><span class="s2">"instance.returnAllErrors"</span><span class="p">,</span> <span class="kc">true</span><span class="p">);</span> <span class="c1">// global or..</span>

<span class="nx">orm</span><span class="p">.</span><span class="nx">connect</span><span class="p">(</span><span class="s2">"...."</span><span class="p">,</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">db</span><span class="p">)</span> <span class="p">{</span>
    <span class="nx">db</span><span class="p">.</span><span class="nx">settings</span><span class="p">.</span><span class="nx">set</span><span class="p">(</span><span class="s2">"instance.returnAllErrors"</span><span class="p">,</span> <span class="kc">true</span><span class="p">);</span> <span class="c1">// .. local</span>

    <span class="c1">// ...</span>

    <span class="kd">var</span> <span class="nx">John</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Person</span><span class="p">({</span>
        <span class="nx">name</span> <span class="o">:</span> <span class="s2">""</span><span class="p">,</span>
        <span class="nx">age</span> <span class="o">:</span> <span class="mi">15</span>
    <span class="p">});</span>
    <span class="nx">John</span><span class="p">.</span><span class="nx">save</span><span class="p">(</span><span class="kd">function</span> <span class="p">(</span><span class="nx">err</span><span class="p">)</span> <span class="p">{</span>
        <span class="nx">assert</span><span class="p">(</span><span class="nb">Array</span><span class="p">.</span><span class="nx">isArray</span><span class="p">(</span><span class="nx">err</span><span class="p">));</span>
        <span class="c1">// err[0].field = "name" , err[0].value = "" , err[0].msg = "missing"</span>
        <span class="c1">// err[1].field = "age"  , err[1].value = 15 , err[1].msg = "out-of-range-number"</span>
        <span class="c1">// err[2].field = "age"  , err[2].value = 15 , err[2].msg = "outside-list"</span>
    <span class="p">});</span>
<span class="p">});</span>
</pre></div>

<h3>
<a name="predefined-validations" class="anchor" href="#predefined-validations"><span class="octicon octicon-link"></span></a>Predefined Validations</h3>

<p>Predefined validations accept an optional last parameter <code>msg</code> that is the <code>Error.msg</code> if it's triggered.</p>

<h4>
<a name="requiredmsg" class="anchor" href="#requiredmsg"><span class="octicon octicon-link"></span></a><code>required(msg)</code>
</h4>

<p>Ensures property is not <code>null</code> or <code>undefined</code>. It does not trigger any error if property is <code>0</code> or empty string.</p>

<h4>
<a name="rangenumbermin-max-msg" class="anchor" href="#rangenumbermin-max-msg"><span class="octicon octicon-link"></span></a><code>rangeNumber(min, max, msg)</code>
</h4>

<p>Ensures a property is a number between <code>min</code> and <code>max</code>. Any of the parameters can be passed as <code>undefined</code>
to exclude a minimum or maximum value.</p>

<h4>
<a name="rangelengthmin-max-msg" class="anchor" href="#rangelengthmin-max-msg"><span class="octicon octicon-link"></span></a><code>rangeLength(min, max, msg)</code>
</h4>

<p>Same as previous validator but for property length (strings).</p>

<h4>
<a name="insidelistlist-msg" class="anchor" href="#insidelistlist-msg"><span class="octicon octicon-link"></span></a><code>insideList(list, msg)</code>
</h4>

<p>Ensures a property value is inside a list of values.</p>

<h4>
<a name="outsidelistlist-msg" class="anchor" href="#outsidelistlist-msg"><span class="octicon octicon-link"></span></a><code>outsideList(list, msg)</code>
</h4>

<p>Ensures a property value is not inside a list of values.</p>

<h4>
<a name="equaltopropertyproperty-msg" class="anchor" href="#equaltopropertyproperty-msg"><span class="octicon octicon-link"></span></a><code>equalToProperty(property, msg)</code>
</h4>

<p>Ensures a property value is not the same as another property value in the instance. This validator is good for example for
password and password repetition check.</p>

<h4>
<a name="notemptystringmsg" class="anchor" href="#notemptystringmsg"><span class="octicon octicon-link"></span></a><code>notEmptyString(msg)</code>
</h4>

<p>This is an alias for <code>rangeLength(1, undefined, 'empty-string')</code>.</p>

<h4>
<a name="uniquemsg" class="anchor" href="#uniquemsg"><span class="octicon octicon-link"></span></a><code>unique(msg)</code>
</h4>

<p>Ensures there's not another instance in your database already with that property value. This validator is good for example for
unique identifiers.</p>

<h4>
<a name="password-checks-msg" class="anchor" href="#password-checks-msg"><span class="octicon octicon-link"></span></a><code>password([ checks, ]msg)</code>
</h4>

<p>Ensures the property value has some defined types of characters, usually wanted in a password. <code>checks</code> is optional and
defaults to <code>"luns6"</code> which leans <code>l</code>owercase letters, <code>u</code>ppercase letters, <code>n</code>umbers, <code>s</code>pecial characters, with a minimum
length of <code>6</code>.</p>

<h4>
<a name="patternsmatchpattern-modifiers-msg" class="anchor" href="#patternsmatchpattern-modifiers-msg"><span class="octicon octicon-link"></span></a><code>patterns.match(pattern, modifiers, msg)</code>
</h4>

<p>Ensures the property value passes the regular expression pattern (and regex modifiers).</p>

<p>The next <code>patterns.*</code> are comodity alias to this one.</p>

<h4>
<a name="patternshexstringmsg" class="anchor" href="#patternshexstringmsg"><span class="octicon octicon-link"></span></a><code>patterns.hexString(msg)</code>
</h4>

<p>Ensures the property value is an hexadecimal string (uppercase or lowercase).</p>

<h4>
<a name="patternsemailmsg" class="anchor" href="#patternsemailmsg"><span class="octicon octicon-link"></span></a><code>patterns.email(msg)</code>
</h4>

<p>Ensures the property value is a valid e-mail (more or less).</p>

<h4>
<a name="patternsipv4msg" class="anchor" href="#patternsipv4msg"><span class="octicon octicon-link"></span></a><code>patterns.ipv4(msg)</code>
</h4>

<p>Ensures the property value is a valid IPv4 address. It does not accept masks (example: <code>0</code> as last number is not valid).</p>

<h2>
<a name="associations" class="anchor" href="#associations"><span class="octicon octicon-link"></span></a>Associations</h2>

<p>An association is a relation between one or more tables.</p>

<h3>
<a name="hasone" class="anchor" href="#hasone"><span class="octicon octicon-link"></span></a>hasOne</h3>

<p>Is a <strong>many to one</strong> relationship. It's the same as <strong>belongs to.</strong><br>
Eg: <code>Animal.hasOne('owner', Person)</code>.<br>
Animal can only have one owner, but Person can have many animals.<br>
Animal will have the <code>owner_id</code> property automatically added.</p>

<p>The following functions will become available:</p>

<div class="highlight"><pre><span class="nx">animal</span><span class="p">.</span><span class="nx">getOwner</span><span class="p">(</span><span class="kd">function</span><span class="p">..)</span>         <span class="c1">// Gets owner</span>
<span class="nx">animal</span><span class="p">.</span><span class="nx">setOwner</span><span class="p">(</span><span class="nx">person</span><span class="p">,</span> <span class="kd">function</span><span class="p">..)</span> <span class="c1">// Sets owner_id</span>
<span class="nx">animal</span><span class="p">.</span><span class="nx">hasOwner</span><span class="p">(</span><span class="kd">function</span><span class="p">..)</span>         <span class="c1">// Checks if owner exists</span>
<span class="nx">animal</span><span class="p">.</span><span class="nx">removeOwner</span><span class="p">()</span>                <span class="c1">// Sets owner_id to 0</span>
</pre></div>

<p><strong>Reverse access</strong></p>

<div class="highlight"><pre><span class="nx">Animal</span><span class="p">.</span><span class="nx">hasOne</span><span class="p">(</span><span class="s1">'owner'</span><span class="p">,</span> <span class="nx">Person</span><span class="p">,</span> <span class="p">{</span><span class="nx">reverse</span><span class="o">:</span> <span class="s1">'pets'</span><span class="p">})</span>
</pre></div>

<p>will add the following:</p>

<div class="highlight"><pre><span class="nx">person</span><span class="p">.</span><span class="nx">getPets</span><span class="p">(</span><span class="kd">function</span><span class="p">..)</span>
<span class="nx">person</span><span class="p">.</span><span class="nx">setPets</span><span class="p">(</span><span class="nx">cat</span><span class="p">,</span> <span class="kd">function</span><span class="p">..)</span>
</pre></div>

<h3>
<a name="hasmany" class="anchor" href="#hasmany"><span class="octicon octicon-link"></span></a>hasMany</h3>

<p>Is a <strong>many to many</strong> relationship (includes join table).<br>
Eg: <code>Patient.hasMany('doctors', Doctor, { why: String }, { reverse: 'patients' })</code>.<br>
Patient can have many different doctors. Each doctor can have many different patients.</p>

<p>This will create a join table <code>patient_doctors</code> when you call <code>Patient.sync()</code>:</p>

<table>
<tr>
<th align="left">column name</th>
<th align="left">type</th>
</tr>
<tr>
<td align="left">patient_id</td>
<td align="left">Integer</td>
</tr>
<tr>
<td align="left">doctor_id</td>
<td align="left">Integer</td>
</tr>
<tr>
<td align="left">why</td>
<td align="left">varchar(255)</td>
</tr>
</table><p>The following functions will be available:</p>

<div class="highlight"><pre><span class="nx">patient</span><span class="p">.</span><span class="nx">getDoctors</span><span class="p">(</span><span class="kd">function</span><span class="p">..)</span>           <span class="c1">// List of doctors</span>
<span class="nx">patient</span><span class="p">.</span><span class="nx">addDoctors</span><span class="p">(</span><span class="nx">docs</span><span class="p">,</span> <span class="kd">function</span><span class="p">...)</span>    <span class="c1">// Adds entries to join table</span>
<span class="nx">patient</span><span class="p">.</span><span class="nx">setDoctors</span><span class="p">(</span><span class="nx">docs</span><span class="p">,</span> <span class="kd">function</span><span class="p">...)</span>    <span class="c1">// Removes existing entries in join table, adds new ones</span>
<span class="nx">patient</span><span class="p">.</span><span class="nx">hasDoctors</span><span class="p">(</span><span class="nx">docs</span><span class="p">,</span> <span class="kd">function</span><span class="p">...)</span>    <span class="c1">// Checks if patient is associated to specified doctors</span>
<span class="nx">patient</span><span class="p">.</span><span class="nx">removeDoctors</span><span class="p">(</span><span class="nx">docs</span><span class="p">,</span> <span class="kd">function</span><span class="p">...)</span> <span class="c1">// Removes specified doctors from join table</span>

<span class="nx">doctor</span><span class="p">.</span><span class="nx">getPatients</span><span class="p">(</span><span class="kd">function</span><span class="p">..)</span>
<span class="nx">etc</span><span class="p">...</span>
</pre></div>

<p>To associate a doctor to a patient:</p>

<div class="highlight"><pre><span class="nx">patient</span><span class="p">.</span><span class="nx">addDoctor</span><span class="p">(</span><span class="nx">surgeon</span><span class="p">,</span> <span class="p">{</span><span class="nx">why</span><span class="o">:</span> <span class="s2">"remove appendix"</span><span class="p">},</span> <span class="kd">function</span><span class="p">(</span><span class="nx">err</span><span class="p">)</span> <span class="p">{</span> <span class="p">...</span> <span class="p">}</span> <span class="p">)</span>
</pre></div>

<p>which will add <code>{patient_id: 4, doctor_id: 6, why: "remove appendix"}</code> to the join table.</p>

<h3>
<a name="extendsto" class="anchor" href="#extendsto"><span class="octicon octicon-link"></span></a>extendsTo</h3>

<p>If you want to split maybe optional properties into different tables or collections. Every extension will be in a new table,
where the unique identifier of each row is the main model instance id. For example:</p>

<div class="highlight"><pre><span class="kd">var</span> <span class="nx">Person</span> <span class="o">=</span> <span class="nx">db</span><span class="p">.</span><span class="nx">define</span><span class="p">(</span><span class="s2">"person"</span><span class="p">,</span> <span class="p">{</span>
    <span class="nx">name</span> <span class="o">:</span> <span class="nb">String</span>
<span class="p">});</span>
<span class="kd">var</span> <span class="nx">PersonAddress</span> <span class="o">=</span> <span class="nx">Person</span><span class="p">.</span><span class="nx">extendsTo</span><span class="p">(</span><span class="s2">"address"</span><span class="p">,</span> <span class="p">{</span>
    <span class="nx">street</span> <span class="o">:</span> <span class="nb">String</span><span class="p">,</span>
    <span class="nx">number</span> <span class="o">:</span> <span class="nb">Number</span>
<span class="p">});</span>
</pre></div>

<p>This will create a table <code>person</code> with columns <code>id</code> and <code>name</code>. The extension will create a table <code>person_address</code> with
columns <code>person_id</code>, <code>street</code> and <code>number</code>. The methods available in the <code>Person</code> model are similar to an <code>hasOne</code>
association. In this example you would be able to call <code>.getAddress(cb)</code>, <code>.setAddress(Address, cb)</code>, ..</p>

<p><strong>Note:</strong> you don't have to save the result from <code>Person.extendsTo</code>. It returns an extended model. You can use it to query
directly this extended table (and even find the related model) but that's up to you. If you only want to access it using the
original model you can just discard the return.</p>

<h3>
<a name="examples--options" class="anchor" href="#examples--options"><span class="octicon octicon-link"></span></a>Examples &amp; options</h3>

<p>If you have a relation of 1 to n, you should use <code>hasOne</code> (belongs to) association.</p>

<div class="highlight"><pre><span class="kd">var</span> <span class="nx">Person</span> <span class="o">=</span> <span class="nx">db</span><span class="p">.</span><span class="nx">define</span><span class="p">(</span><span class="s1">'person'</span><span class="p">,</span> <span class="p">{</span>
    <span class="nx">name</span> <span class="o">:</span> <span class="nb">String</span>
<span class="p">});</span>
<span class="kd">var</span> <span class="nx">Animal</span> <span class="o">=</span> <span class="nx">db</span><span class="p">.</span><span class="nx">define</span><span class="p">(</span><span class="s1">'animal'</span><span class="p">,</span> <span class="p">{</span>
    <span class="nx">name</span> <span class="o">:</span> <span class="nb">String</span>
<span class="p">});</span>
<span class="nx">Animal</span><span class="p">.</span><span class="nx">hasOne</span><span class="p">(</span><span class="s2">"owner"</span><span class="p">,</span> <span class="nx">Person</span><span class="p">);</span> <span class="c1">// creates column 'owner_id' in 'animal' table</span>

<span class="c1">// get animal with id = 123</span>
<span class="nx">Animal</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="mi">123</span><span class="p">,</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">animal</span><span class="p">)</span> <span class="p">{</span>
    <span class="c1">// animal is the animal model instance, if found</span>
    <span class="nx">Foo</span><span class="p">.</span><span class="nx">getOwner</span><span class="p">(</span><span class="kd">function</span> <span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">person</span><span class="p">)</span> <span class="p">{</span>
        <span class="c1">// if animal has really an owner, person points to it</span>
    <span class="p">});</span>
<span class="p">});</span>
</pre></div>

<p>You can mark the <code>owner_id</code> field as required in the database by specifying the <code>required</code> option:</p>

<div class="highlight"><pre><span class="nx">Animal</span><span class="p">.</span><span class="nx">hasOne</span><span class="p">(</span><span class="s2">"owner"</span><span class="p">,</span> <span class="nx">Person</span><span class="p">,</span> <span class="p">{</span> <span class="nx">required</span><span class="o">:</span> <span class="kc">true</span> <span class="p">});</span>
</pre></div>

<p>If you prefer to use another name for the field (owner_id) you can change this parameter in the settings.</p>

<div class="highlight"><pre><span class="nx">db</span><span class="p">.</span><span class="nx">settings</span><span class="p">.</span><span class="nx">set</span><span class="p">(</span><span class="s2">"properties.association_key"</span><span class="p">,</span> <span class="s2">"id_{name}"</span><span class="p">);</span> <span class="c1">// {name} will be replaced by 'owner' in this case</span>
</pre></div>

<p><strong>Note: This has to be done before the association is specified.</strong></p>

<p>The <code>hasMany</code> associations can have additional properties in the association table.</p>

<div class="highlight"><pre><span class="kd">var</span> <span class="nx">Person</span> <span class="o">=</span> <span class="nx">db</span><span class="p">.</span><span class="nx">define</span><span class="p">(</span><span class="s1">'person'</span><span class="p">,</span> <span class="p">{</span>
    <span class="nx">name</span> <span class="o">:</span> <span class="nb">String</span>
<span class="p">});</span>
<span class="nx">Person</span><span class="p">.</span><span class="nx">hasMany</span><span class="p">(</span><span class="s2">"friends"</span><span class="p">,</span> <span class="p">{</span>
    <span class="nx">rate</span> <span class="o">:</span> <span class="nb">Number</span>
<span class="p">});</span>

<span class="nx">Person</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="mi">123</span><span class="p">,</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">John</span><span class="p">)</span> <span class="p">{</span>
    <span class="nx">John</span><span class="p">.</span><span class="nx">getFriends</span><span class="p">(</span><span class="kd">function</span> <span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">friends</span><span class="p">)</span> <span class="p">{</span>
        <span class="c1">// assumes rate is another column on table person_friends</span>
        <span class="c1">// you can access it by going to friends[N].extra.rate</span>
    <span class="p">});</span>
<span class="p">});</span>
</pre></div>

<p>If you prefer you can activate <code>autoFetch</code>.
This way associations are automatically fetched when you get or find instances of a model.</p>

<div class="highlight"><pre><span class="kd">var</span> <span class="nx">Person</span> <span class="o">=</span> <span class="nx">db</span><span class="p">.</span><span class="nx">define</span><span class="p">(</span><span class="s1">'person'</span><span class="p">,</span> <span class="p">{</span>
  <span class="nx">name</span> <span class="o">:</span> <span class="nb">String</span>
<span class="p">});</span>
<span class="nx">Person</span><span class="p">.</span><span class="nx">hasMany</span><span class="p">(</span><span class="s2">"friends"</span><span class="p">,</span> <span class="p">{</span>
    <span class="nx">rate</span> <span class="o">:</span> <span class="nb">Number</span>
<span class="p">},</span> <span class="p">{</span>
    <span class="nx">autoFetch</span> <span class="o">:</span> <span class="kc">true</span>
<span class="p">});</span>

<span class="nx">Person</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="mi">123</span><span class="p">,</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">John</span><span class="p">)</span> <span class="p">{</span>
    <span class="c1">// no need to do John.getFriends() , John already has John.friends Array</span>
<span class="p">});</span>
</pre></div>

<p>You can also define this option globally instead of a per association basis.</p>

<div class="highlight"><pre><span class="kd">var</span> <span class="nx">Person</span> <span class="o">=</span> <span class="nx">db</span><span class="p">.</span><span class="nx">define</span><span class="p">(</span><span class="s1">'person'</span><span class="p">,</span> <span class="p">{</span>
    <span class="nx">name</span> <span class="o">:</span> <span class="nb">String</span>
<span class="p">},</span> <span class="p">{</span>
    <span class="nx">autoFetch</span> <span class="o">:</span> <span class="kc">true</span>
<span class="p">});</span>
<span class="nx">Person</span><span class="p">.</span><span class="nx">hasMany</span><span class="p">(</span><span class="s2">"friends"</span><span class="p">,</span> <span class="p">{</span>
    <span class="nx">rate</span> <span class="o">:</span> <span class="nb">Number</span>
<span class="p">});</span>
</pre></div>

<p>Associations can make calls to the associated Model by using the <code>reverse</code> option. For example, if you have an
association from ModelA to ModelB, you can create an accessor in ModelB to get instances from ModelA.
Confusing? Look at the next example.</p>

<div class="highlight"><pre><span class="kd">var</span> <span class="nx">Pet</span> <span class="o">=</span> <span class="nx">db</span><span class="p">.</span><span class="nx">define</span><span class="p">(</span><span class="s1">'pet'</span><span class="p">,</span> <span class="p">{</span>
    <span class="nx">name</span> <span class="o">:</span> <span class="nb">String</span>
<span class="p">});</span>
<span class="kd">var</span> <span class="nx">Person</span> <span class="o">=</span> <span class="nx">db</span><span class="p">.</span><span class="nx">define</span><span class="p">(</span><span class="s1">'person'</span><span class="p">,</span> <span class="p">{</span>
    <span class="nx">name</span> <span class="o">:</span> <span class="nb">String</span>
<span class="p">});</span>
<span class="nx">Pet</span><span class="p">.</span><span class="nx">hasOne</span><span class="p">(</span><span class="s2">"owner"</span><span class="p">,</span> <span class="nx">Person</span><span class="p">,</span> <span class="p">{</span>
    <span class="nx">reverse</span> <span class="o">:</span> <span class="s2">"pets"</span>
<span class="p">});</span>

<span class="nx">Person</span><span class="p">(</span><span class="mi">4</span><span class="p">).</span><span class="nx">getPets</span><span class="p">(</span><span class="kd">function</span> <span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">pets</span><span class="p">)</span> <span class="p">{</span>
    <span class="c1">// although the association was made on Pet,</span>
    <span class="c1">// Person will have an accessor (getPets)</span>
    <span class="c1">//</span>
    <span class="c1">// In this example, ORM will fetch all pets</span>
    <span class="c1">// whose owner_id = 4</span>
<span class="p">});</span>
</pre></div>

<p>This makes even more sense when having <code>hasMany</code> associations since you can manage the <em>many to many</em>
associations from both sides.</p>

<div class="highlight"><pre><span class="kd">var</span> <span class="nx">Pet</span> <span class="o">=</span> <span class="nx">db</span><span class="p">.</span><span class="nx">define</span><span class="p">(</span><span class="s1">'pet'</span><span class="p">,</span> <span class="p">{</span>
    <span class="nx">name</span> <span class="o">:</span> <span class="nb">String</span>
<span class="p">});</span>
<span class="kd">var</span> <span class="nx">Person</span> <span class="o">=</span> <span class="nx">db</span><span class="p">.</span><span class="nx">define</span><span class="p">(</span><span class="s1">'person'</span><span class="p">,</span> <span class="p">{</span>
    <span class="nx">name</span> <span class="o">:</span> <span class="nb">String</span>
<span class="p">});</span>
<span class="nx">Person</span><span class="p">.</span><span class="nx">hasMany</span><span class="p">(</span><span class="s2">"pets"</span><span class="p">,</span> <span class="nx">Pet</span><span class="p">,</span> <span class="p">{</span>
    <span class="nx">bought</span>  <span class="o">:</span> <span class="nb">Date</span>
<span class="p">},</span> <span class="p">{</span>
    <span class="nx">reverse</span> <span class="o">:</span> <span class="s2">"owners"</span>
<span class="p">});</span>

<span class="nx">Person</span><span class="p">(</span><span class="mi">1</span><span class="p">).</span><span class="nx">getPets</span><span class="p">(...);</span>
<span class="nx">Pet</span><span class="p">(</span><span class="mi">2</span><span class="p">).</span><span class="nx">getOwners</span><span class="p">(...);</span>
</pre></div>
      </section>
      <footer>
        <p>This project is maintained by <a href="https://github.com/dresende">dresende</a></p>
        <p><small>Hosted on GitHub Pages &mdash; Theme by <a href="https://github.com/orderedlist">orderedlist</a></small></p>
      </footer>
    </div>
    <script src="javascripts/scale.fix.js"></script>
              <script type="text/javascript">
            var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
            document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
          </script>
          <script type="text/javascript">
            try {
              var pageTracker = _gat._getTracker("UA-36807397-1");
            pageTracker._trackPageview();
            } catch(err) {}
          </script>

  </body>
</html>