<?xml version="1.0" encoding="utf-8" ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <!-- This file is generated by Nim. --> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <!-- Favicon --> <link rel="shortcut icon" href="data:image/x-icon;base64,AAABAAEAEBAAAAEAIABoBAAAFgAAACgAAAAQAAAAIAAAAAEAIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AAAAAAUAAAAF////AP///wD///8A////AP///wD///8A////AP///wD///8A////AAAAAAIAAABbAAAAlQAAAKIAAACbAAAAmwAAAKIAAACVAAAAWwAAAAL///8A////AP///wD///8A////AAAAABQAAADAAAAAYwAAAA3///8A////AP///wD///8AAAAADQAAAGMAAADAAAAAFP///wD///8A////AP///wAAAACdAAAAOv///wD///8A////AP///wD///8A////AP///wD///8AAAAAOgAAAJ3///8A////AP///wAAAAAnAAAAcP///wAAAAAoAAAASv///wD///8A////AP///wAAAABKAAAAKP///wAAAABwAAAAJ////wD///8AAAAAgQAAABwAAACIAAAAkAAAAJMAAACtAAAAFQAAABUAAACtAAAAkwAAAJAAAACIAAAAHAAAAIH///8A////AAAAAKQAAACrAAAAaP///wD///8AAAAARQAAANIAAADSAAAARf///wD///8AAAAAaAAAAKsAAACk////AAAAADMAAACcAAAAnQAAABj///8A////AP///wAAAAAYAAAAGP///wD///8A////AAAAABgAAACdAAAAnAAAADMAAAB1AAAAwwAAAP8AAADpAAAAsQAAAE4AAAAb////AP///wAAAAAbAAAATgAAALEAAADpAAAA/wAAAMMAAAB1AAAAtwAAAOkAAAD/AAAA/wAAAP8AAADvAAAA3gAAAN4AAADeAAAA3gAAAO8AAAD/AAAA/wAAAP8AAADpAAAAtwAAAGUAAAA/AAAA3wAAAP8AAAD/AAAA/wAAAP8AAAD/AAAA/wAAAP8AAAD/AAAA/wAAAP8AAADfAAAAPwAAAGX///8A////AAAAAEgAAADtAAAAvwAAAL0AAADGAAAA7wAAAO8AAADGAAAAvQAAAL8AAADtAAAASP///wD///8A////AP///wD///8AAAAAO////wD///8A////AAAAAIcAAACH////AP///wD///8AAAAAO////wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A//8AAP//AAD4HwAA7/cAAN/7AAD//wAAoYUAAJ55AACf+QAAh+EAAAAAAADAAwAA4AcAAP5/AAD//wAA//8AAA=="/> <link rel="icon" type="image/png" sizes="32x32" href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWXMAAA3XAAAN1wFCKJt4AAAAB3RJTUUH4QQQEwksSS9ZWwAAAk1JREFUWMPtll2ITVEUx39nn/O7Y5qR8f05wtCUUr6ZIS++8pEnkZInPImneaCQ5METNdOkeFBKUhMPRIkHKfEuUZSUlGlKPN2TrgfncpvmnntnmlEyq1Z7t89/rf9a6+y99oZxGZf/XeIq61EdtgKXgdXA0xrYAvBjOIF1AI9zvjcC74BSpndrJPkBWDScTF8Aa4E3wDlgHbASaANmVqlcCnwHvgDvgVfAJ+AikAAvgfVZwLnSVZHZaOuKoQi3ZOMi4NkYkpe1p4J7A8BpYAD49hfIy/oqG0+hLomiKP2L5L+1ubn5115S+3OAn4EnwBlgMzCjyt6ZAnQCJ4A7wOs88iRJHvw50HoujuPBoCKwHWiosy8MdfZnAdcHk8dxXFJ3VQbQlCTJvRBCGdRbD4M6uc5glpY3eAihpN5S5w12diSEcCCEcKUO4ljdr15T76ur1FDDLIQQ3qv71EdDOe3Kxj3leRXyk+pxdWnFWod6Wt2bY3de3aSuUHcPBVimHs7mK9WrmeOF6lR1o9qnzskh2ar2qm1qizpfXaPeVGdlmGN5pb09qMxz1Xb1kLqgzn1RyH7JUXW52lr5e/Kqi9qpto7V1atuUzfnARrV7jEib1T76gG2qxdGmXyiekkt1GswPTtek0aBfJp6YySGBfWg2tPQ0FAYgf1stUfdmdcjarbYJEniKIq6gY/Aw+zWHAC+p2labGpqiorFYgGYCEzN7oQdQClN07O1/EfDyGgC0ALMBdYAi4FyK+4H3gLPsxfR1zRNi+NP7nH5J+QntnXe5B5mpfQAAAAASUVORK5CYII="> <!-- Google fonts --> <link href='https://fonts.googleapis.com/css?family=Lato:400,600,900' rel='stylesheet' type='text/css'/> <link href='https://fonts.googleapis.com/css?family=Source+Code+Pro:400,500,600' rel='stylesheet' type='text/css'/> <!-- CSS --> <title>README</title> <link rel="stylesheet" type="text/css" href="nimdoc.out.css"> <script type="text/javascript" src="dochack.js"></script> <script type="text/javascript"> function main() { var pragmaDots = document.getElementsByClassName("pragmadots"); for (var i = 0; i < pragmaDots.length; i++) { pragmaDots[i].onclick = function(event) { // Hide tease event.target.parentNode.style.display = "none"; // Show actual event.target.parentNode.nextElementSibling.style.display = "inline"; } } function switchTheme(e) { if (e.target.checked) { document.documentElement.setAttribute('data-theme', 'dark'); localStorage.setItem('theme', 'dark'); } else { document.documentElement.setAttribute('data-theme', 'light'); localStorage.setItem('theme', 'light'); } } const toggleSwitch = document.querySelector('.theme-switch input[type="checkbox"]'); if (toggleSwitch !== null) { toggleSwitch.addEventListener('change', switchTheme, false); } var currentTheme = localStorage.getItem('theme'); if (!currentTheme && window.matchMedia('(prefers-color-scheme: dark)').matches) { currentTheme = 'dark'; } if (currentTheme) { document.documentElement.setAttribute('data-theme', currentTheme); if (currentTheme === 'dark' && toggleSwitch !== null) { toggleSwitch.checked = true; } } } window.addEventListener('DOMContentLoaded', main); </script> </head> <body> <div class="document" id="documentId"> <div class="container"> <h1 class="title">README</h1> <h1 id="fiber-orm">Fiber ORM</h1><p>Lightweight ORM supporting the <a class="reference external" href="https://nim-lang.org/docs/db_postgres.html">Postgres</a> and <a class="reference external" href="https://nim-lang.org/docs/db_sqlite.html">SQLite</a> databases in Nim. It supports a simple, opinionated model mapper to generate SQL queries based on Nim objects. It also includes a simple connection pooling implementation.</p> <p>Fiber ORM is not intended to be a 100% all-cases-covered ORM that handles every potential data access pattern one might wish to implement. It is best thought of as a collection of common SQL generation patterns. It is intended to cover 90% of the common queries and functions one might write when implementing an SQL-based access layer. It is expected that there may be a few more complicated queries that need to be implemented to handle specific access patterns.</p> <p>The simple mapping pattern provided by Fiber ORM also works well on top of databases that encapsulate data access logic in SQL with, for example, views.</p> <h2 id="basic-usage">Basic Usage</h2><p>Consider a simple TODO list application that keeps track of TODO items as well as time logged against those items. You might have a schema such as:</p> <pre class="listing">create extension if not exists "pgcrypto"; create table todo_items columns ( id uuid not null primary key default gen_random_uuid(), owner varchar not null, summary varchar not null, details varchar default null, priority integer not null default 0, related_todo_item_ids uuid[] not null default '{}' ); create table time_entries columns ( id uuid not null primary key default gen_random_uuid(), todo_item_id uuid not null references todo_items (id) on delete cascade, start timestamp with timezone not null default current_timestamp, stop timestamp with timezone default null, );</pre><p>Models may be defined as:</p> <pre class="listing"><span class="Comment"># models.nim</span> <span class="Keyword">import</span> <span class="Identifier">std</span><span class="Operator">/</span><span class="Identifier">options</span><span class="Punctuation">,</span> <span class="Identifier">std</span><span class="Operator">/</span><span class="Identifier">times</span> <span class="Keyword">import</span> <span class="Identifier">uuids</span> <span class="Keyword">type</span> <span class="Identifier">TodoItem</span><span class="Operator">*</span> <span class="Operator">=</span> <span class="Keyword">object</span> <span class="Identifier">id</span><span class="Operator">*:</span> <span class="Identifier">UUID</span> <span class="Identifier">owner</span><span class="Operator">*:</span> <span class="Identifier">string</span> <span class="Identifier">summary</span><span class="Operator">*:</span> <span class="Identifier">string</span> <span class="Identifier">details</span><span class="Operator">*:</span> <span class="Identifier">Option</span><span class="Punctuation">[</span><span class="Identifier">string</span><span class="Punctuation">]</span> <span class="Identifier">priority</span><span class="Operator">*:</span> <span class="Identifier">int</span> <span class="Identifier">relatedTodoItemIds</span><span class="Operator">*:</span> <span class="Identifier">seq</span><span class="Punctuation">[</span><span class="Identifier">UUID</span><span class="Punctuation">]</span> <span class="Identifier">TimeEntry</span><span class="Operator">*</span> <span class="Operator">=</span> <span class="Keyword">object</span> <span class="Identifier">id</span><span class="Operator">*:</span> <span class="Identifier">UUID</span> <span class="Identifier">todoItemId</span><span class="Operator">*:</span> <span class="Identifier">Option</span><span class="Punctuation">[</span><span class="Identifier">UUID</span><span class="Punctuation">]</span> <span class="Identifier">start</span><span class="Operator">*:</span> <span class="Identifier">DateTime</span> <span class="Identifier">stop</span><span class="Operator">*:</span> <span class="Identifier">Option</span><span class="Punctuation">[</span><span class="Identifier">DateTime</span><span class="Punctuation">]</span></pre><p>Using Fiber ORM we can generate a data access layer with:</p> <pre class="listing"><span class="Comment"># db.nim</span> <span class="Keyword">import</span> <span class="Identifier">fiber_orm</span> <span class="Keyword">import</span> <span class="Operator">./</span><span class="Identifier">models</span><span class="Operator">.</span><span class="Identifier">nim</span> <span class="Keyword">type</span> <span class="Identifier">TodoDB</span><span class="Operator">*</span> <span class="Operator">=</span> <span class="Identifier">DbConnPool</span> <span class="Keyword">proc</span> <span class="Identifier">initDb</span><span class="Operator">*</span><span class="Punctuation">(</span><span class="Identifier">connString</span><span class="Punctuation">:</span> <span class="Identifier">string</span><span class="Punctuation">)</span><span class="Punctuation">:</span> <span class="Identifier">TodoDB</span> <span class="Operator">=</span> <span class="Identifier">fiber_orm</span><span class="Operator">.</span><span class="Identifier">initPool</span><span class="Punctuation">(</span><span class="Identifier">connect</span> <span class="Operator">=</span> <span class="Keyword">proc</span><span class="Punctuation">(</span><span class="Punctuation">)</span><span class="Punctuation">:</span> <span class="Identifier">DbConn</span> <span class="Operator">=</span> <span class="Identifier">open</span><span class="Punctuation">(</span><span class="StringLit">""</span><span class="Punctuation">,</span> <span class="StringLit">""</span><span class="Punctuation">,</span> <span class="StringLit">""</span><span class="Punctuation">,</span> <span class="Identifier">connString</span><span class="Punctuation">)</span><span class="Punctuation">)</span> <span class="Identifier">generateProcsForModels</span><span class="Punctuation">(</span><span class="Identifier">TodoDB</span><span class="Punctuation">,</span> <span class="Punctuation">[</span><span class="Identifier">TodoItem</span><span class="Punctuation">,</span> <span class="Identifier">TimeEntry</span><span class="Punctuation">]</span><span class="Punctuation">)</span> <span class="Identifier">generateLookup</span><span class="Punctuation">(</span><span class="Identifier">TodoDB</span><span class="Punctuation">,</span> <span class="Identifier">TimeEntry</span><span class="Punctuation">,</span> <span class="Operator">@</span><span class="Punctuation">[</span><span class="StringLit">"todoItemId"</span><span class="Punctuation">]</span><span class="Punctuation">)</span></pre><p>This will generate the following procedures:</p> <pre class="listing"><span class="Keyword">proc</span> <span class="Identifier">getTodoItem</span><span class="Operator">*</span><span class="Punctuation">(</span><span class="Identifier">db</span><span class="Punctuation">:</span> <span class="Identifier">TodoDB</span><span class="Punctuation">,</span> <span class="Identifier">id</span><span class="Punctuation">:</span> <span class="Identifier">UUID</span><span class="Punctuation">)</span><span class="Punctuation">:</span> <span class="Identifier">TodoItem</span><span class="Punctuation">;</span> <span class="Keyword">proc</span> <span class="Identifier">getAllTodoItems</span><span class="Operator">*</span><span class="Punctuation">(</span><span class="Identifier">db</span><span class="Punctuation">:</span> <span class="Identifier">TodoDB</span><span class="Punctuation">)</span><span class="Punctuation">:</span> <span class="Identifier">seq</span><span class="Punctuation">[</span><span class="Identifier">TodoItem</span><span class="Punctuation">]</span><span class="Punctuation">;</span> <span class="Keyword">proc</span> <span class="Identifier">createTodoItem</span><span class="Operator">*</span><span class="Punctuation">(</span><span class="Identifier">db</span><span class="Punctuation">:</span> <span class="Identifier">TodoDB</span><span class="Punctuation">,</span> <span class="Identifier">rec</span><span class="Punctuation">:</span> <span class="Identifier">TodoItem</span><span class="Punctuation">)</span><span class="Punctuation">:</span> <span class="Identifier">TodoItem</span><span class="Punctuation">;</span> <span class="Keyword">proc</span> <span class="Identifier">updateTodoItem</span><span class="Operator">*</span><span class="Punctuation">(</span><span class="Identifier">db</span><span class="Punctuation">:</span> <span class="Identifier">TodoDB</span><span class="Punctuation">,</span> <span class="Identifier">rec</span><span class="Punctuation">:</span> <span class="Identifier">TodoItem</span><span class="Punctuation">)</span><span class="Punctuation">:</span> <span class="Identifier">bool</span><span class="Punctuation">;</span> <span class="Keyword">proc</span> <span class="Identifier">deleteTodoItem</span><span class="Operator">*</span><span class="Punctuation">(</span><span class="Identifier">db</span><span class="Punctuation">:</span> <span class="Identifier">TodoDB</span><span class="Punctuation">,</span> <span class="Identifier">rec</span><span class="Punctuation">:</span> <span class="Identifier">TodoItem</span><span class="Punctuation">)</span><span class="Punctuation">:</span> <span class="Identifier">bool</span><span class="Punctuation">;</span> <span class="Keyword">proc</span> <span class="Identifier">deleteTodoItem</span><span class="Operator">*</span><span class="Punctuation">(</span><span class="Identifier">db</span><span class="Punctuation">:</span> <span class="Identifier">TodoDB</span><span class="Punctuation">,</span> <span class="Identifier">id</span><span class="Punctuation">:</span> <span class="Identifier">UUID</span><span class="Punctuation">)</span><span class="Punctuation">:</span> <span class="Identifier">bool</span><span class="Punctuation">;</span> <span class="Keyword">proc</span> <span class="Identifier">findTodoItemsWhere</span><span class="Operator">*</span><span class="Punctuation">(</span><span class="Identifier">db</span><span class="Punctuation">:</span> <span class="Identifier">TodoDB</span><span class="Punctuation">,</span> <span class="Identifier">whereClause</span><span class="Punctuation">:</span> <span class="Identifier">string</span><span class="Punctuation">,</span> <span class="Identifier">values</span><span class="Punctuation">:</span> <span class="Identifier">varargs</span><span class="Punctuation">[</span><span class="Identifier">string</span><span class="Punctuation">,</span> <span class="Identifier">dbFormat</span><span class="Punctuation">]</span><span class="Punctuation">)</span><span class="Punctuation">:</span> <span class="Identifier">seq</span><span class="Punctuation">[</span><span class="Identifier">TodoItem</span><span class="Punctuation">]</span><span class="Punctuation">;</span> <span class="Keyword">proc</span> <span class="Identifier">getTimeEntry</span><span class="Operator">*</span><span class="Punctuation">(</span><span class="Identifier">db</span><span class="Punctuation">:</span> <span class="Identifier">TodoDB</span><span class="Punctuation">,</span> <span class="Identifier">id</span><span class="Punctuation">:</span> <span class="Identifier">UUID</span><span class="Punctuation">)</span><span class="Punctuation">:</span> <span class="Identifier">TimeEntry</span><span class="Punctuation">;</span> <span class="Keyword">proc</span> <span class="Identifier">getAllTimeEntries</span><span class="Operator">*</span><span class="Punctuation">(</span><span class="Identifier">db</span><span class="Punctuation">:</span> <span class="Identifier">TodoDB</span><span class="Punctuation">)</span><span class="Punctuation">:</span> <span class="Identifier">seq</span><span class="Punctuation">[</span><span class="Identifier">TimeEntry</span><span class="Punctuation">]</span><span class="Punctuation">;</span> <span class="Keyword">proc</span> <span class="Identifier">createTimeEntry</span><span class="Operator">*</span><span class="Punctuation">(</span><span class="Identifier">db</span><span class="Punctuation">:</span> <span class="Identifier">TodoDB</span><span class="Punctuation">,</span> <span class="Identifier">rec</span><span class="Punctuation">:</span> <span class="Identifier">TimeEntry</span><span class="Punctuation">)</span><span class="Punctuation">:</span> <span class="Identifier">TimeEntry</span><span class="Punctuation">;</span> <span class="Keyword">proc</span> <span class="Identifier">updateTimeEntry</span><span class="Operator">*</span><span class="Punctuation">(</span><span class="Identifier">db</span><span class="Punctuation">:</span> <span class="Identifier">TodoDB</span><span class="Punctuation">,</span> <span class="Identifier">rec</span><span class="Punctuation">:</span> <span class="Identifier">TimeEntry</span><span class="Punctuation">)</span><span class="Punctuation">:</span> <span class="Identifier">bool</span><span class="Punctuation">;</span> <span class="Keyword">proc</span> <span class="Identifier">deleteTimeEntry</span><span class="Operator">*</span><span class="Punctuation">(</span><span class="Identifier">db</span><span class="Punctuation">:</span> <span class="Identifier">TodoDB</span><span class="Punctuation">,</span> <span class="Identifier">rec</span><span class="Punctuation">:</span> <span class="Identifier">TimeEntry</span><span class="Punctuation">)</span><span class="Punctuation">:</span> <span class="Identifier">bool</span><span class="Punctuation">;</span> <span class="Keyword">proc</span> <span class="Identifier">deleteTimeEntry</span><span class="Operator">*</span><span class="Punctuation">(</span><span class="Identifier">db</span><span class="Punctuation">:</span> <span class="Identifier">TodoDB</span><span class="Punctuation">,</span> <span class="Identifier">id</span><span class="Punctuation">:</span> <span class="Identifier">UUID</span><span class="Punctuation">)</span><span class="Punctuation">:</span> <span class="Identifier">bool</span><span class="Punctuation">;</span> <span class="Keyword">proc</span> <span class="Identifier">findTimeEntriesWhere</span><span class="Operator">*</span><span class="Punctuation">(</span><span class="Identifier">db</span><span class="Punctuation">:</span> <span class="Identifier">TodoDB</span><span class="Punctuation">,</span> <span class="Identifier">whereClause</span><span class="Punctuation">:</span> <span class="Identifier">string</span><span class="Punctuation">,</span> <span class="Identifier">values</span><span class="Punctuation">:</span> <span class="Identifier">varargs</span><span class="Punctuation">[</span><span class="Identifier">string</span><span class="Punctuation">,</span> <span class="Identifier">dbFormat</span><span class="Punctuation">]</span><span class="Punctuation">)</span><span class="Punctuation">:</span> <span class="Identifier">seq</span><span class="Punctuation">[</span><span class="Identifier">TimeEntry</span><span class="Punctuation">]</span><span class="Punctuation">;</span> <span class="Keyword">proc</span> <span class="Identifier">findTimeEntriesByTodoItemId</span><span class="Punctuation">(</span><span class="Identifier">db</span><span class="Punctuation">:</span> <span class="Identifier">TodoDB</span><span class="Punctuation">,</span> <span class="Identifier">todoItemId</span><span class="Punctuation">:</span> <span class="Identifier">UUID</span><span class="Punctuation">)</span><span class="Punctuation">:</span> <span class="Identifier">seq</span><span class="Punctuation">[</span><span class="Identifier">TimeEntry</span><span class="Punctuation">]</span><span class="Punctuation">;</span></pre> <h2 id="objectminusrelational-modeling">Object-Relational Modeling</h2> <h3 id="model-class">Model Class</h3><p>Fiber ORM uses simple Nim <tt class="docutils literal"><span class="pre">object</span></tt>s and <tt class="docutils literal"><span class="pre">ref object</span></tt>s as model classes. Fiber ORM expects there to be one table for each model class.</p> <h4 id="name-mapping">Name Mapping</h4><p>Fiber ORM uses <tt class="docutils literal"><span class="pre">snake_case</span></tt> for database identifiers (column names, table names, etc.) and <tt class="docutils literal"><span class="pre">camelCase</span></tt> for Nim identifiers. We automatically convert model names to and from table names (<tt class="docutils literal"><span class="pre">TodoItem</span></tt> <-> <tt class="docutils literal"><span class="pre">todo_items</span></tt>), as well as column names (<tt class="docutils literal"><span class="pre">userId</span></tt> <-> <tt class="docutils literal"><span class="pre">user_id</span></tt>).</p> <p>Notice that table names are automatically pluralized from model class names. In the above example, you have:</p> <table border="1" class="docutils"><tr><th>Model Class</th><th>Table Name</th></tr> <tr><td>TodoItem</td><td>todo_items</td></tr> <tr><td>TimeEntry</td><td>time_entries</td></tr> </table><p>Because Nim is style-insensitive, you can generall refer to model classes and fields using <tt class="docutils literal"><span class="pre">snake_case</span></tt>, <tt class="docutils literal"><span class="pre">camelCase</span></tt>, or <tt class="docutils literal"><span class="pre">PascalCase</span></tt> in your code and expect Fiber ORM to be able to map the names to DB identifier names properly (though FiberORM will always use <tt class="docutils literal"><span class="pre">camelCase</span></tt> internally).</p> <p>See the <a class="reference external" href="fiber_orm/util.html#identNameToDb,string">identNameToDb</a>, <a class="reference external" href="fiber_orm/util.html#dbNameToIdent,string">dbNameToIdent</a>, <a class="reference external" href="fiber_orm/util.html#tableName,type">tableName</a> and <a class="reference external" href="fiber_orm/util.html#dbFormat,DateTime">dbFormat</a> procedures in the <a class="reference internal" href="#fiber">fiber</a> module for details.</p> <h4 id="id-field">ID Field</h4><p>Fiber ORM expects every model class to have a field named <tt class="docutils literal"><span class="pre">id</span></tt>, with a corresponding <tt class="docutils literal"><span class="pre">id</span></tt> column in the model table. This field must be either a <tt class="docutils literal"><span class="pre">string</span></tt>, <tt class="docutils literal"><span class="pre">integer</span></tt>, or <a class="reference external" href="https://github.com/pragmagic/uuids">UUID</a>.</p> <p>When creating a new record the <tt class="docutils literal"><span class="pre">id</span></tt> field will be omitted if it is empty (<a class="reference external" href="https://nim-lang.org/docs/options.html#isNone,Option[T]">Option.isNone</a>, <a class="reference external" href="https://github.com/pragmagic/uuids/blob/8cb8720b567c6bcb261bd1c0f7491bdb5209ad06/uuids.nim#L72">UUID.isZero</a>, value of <tt class="docutils literal"><span class="pre">0</span></tt>, or only whitespace). This is intended to allow for cases like the example where the database may generate an ID when a new record is inserted. If a non-zero value is provided, the create call will include the <tt class="docutils literal"><span class="pre">id</span></tt> field in the <tt class="docutils literal"><span class="pre">INSERT</span></tt> query.</p> <h3 id="supported-data-types">Supported Data Types</h3><p>The following Nim data types are supported by Fiber ORM:</p> <table border="1" class="docutils"><tr><th>Nim Type</th><th>Postgres Type</th><th>SQLite Type</th></tr> <tr><td><tt class="docutils literal"><span class="pre">string</span></tt></td><td><a class="reference external" href="https://www.postgresql.org/docs/current/datatype-character.html">varchar</a></td><td></td></tr> <tr><td><tt class="docutils literal"><span class="pre">int</span></tt></td><td><a class="reference external" href="https://www.postgresql.org/docs/current/datatype-numeric.html#DATATYPE-INT">integer</a></td><td></td></tr> <tr><td><tt class="docutils literal"><span class="pre">float</span></tt></td><td><a class="reference external" href="https://www.postgresql.org/docs/current/datatype-numeric.html#DATATYPE-FLOAT">double</a></td><td></td></tr> <tr><td><tt class="docutils literal"><span class="pre">bool</span></tt></td><td><a class="reference external" href="https://www.postgresql.org/docs/current/datatype-boolean.html">boolean</a></td><td></td></tr> <tr><td><a class="reference external" href="https://nim-lang.org/docs/times.html#DateTime">DateTime</a></td><td><a class="reference external" href="https://www.postgresql.org/docs/current/datatype-datetime.html">timestamp</a></td><td></td></tr> <tr><td><tt class="docutils literal"><span class="pre">seq[]</span></tt></td><td><a class="reference external" href="https://www.postgresql.org/docs/current/arrays.html">array</a></td><td></td></tr> <tr><td><a class="reference external" href="https://github.com/pragmagic/uuids">UUID</a></td><td><a class="reference external" href="https://www.postgresql.org/docs/current/datatype-uuid.html">uuid (pg)</a></td><td></td></tr> <tr><td><a class="reference external" href="https://nim-lang.org/docs/options.html#Option">Option</a></td><td><em>allows</em> <tt class="docutils literal"><span class="pre">NULL</span></tt> <sup><strong><a class="reference internal" href="#footnote-f1">[1]</a></strong></sup></td><td></td></tr> <tr><td><a class="reference external" href="https://nim-lang.org/docs/json.html#JsonNode">JsonNode</a></td><td><a class="reference external" href="https://www.postgresql.org/docs/current/datatype-json.html">jsonb</a></td><td></td></tr> </table><hr class="footnote"><div class="footnote-group"> <div id="footnote-f1"><div class="footnote-label"><sup><strong><a href="#footnote-f1">[1]</a></strong></sup></div>   Note that this implies that all <tt class="docutils literal"><span class="pre">NULL</span></tt>-able fields should be typed as optional using <tt class="docutils literal"><span class="pre">Option[fieldType]</span></tt>. Conversely, any fields with non-optional types should also be constrained to be <tt class="docutils literal"><span class="pre">NOT NULL</span></tt> in the database schema. </div> </div> <h2 id="database-object">Database Object</h2><p>Many of the Fiber ORM macros expect a database object type to be passed. In the example above the <a class="reference internal" href="#fiber">fiber</a> object is used as database object type (aliased as <tt class="docutils literal"><span class="pre">TodoDB</span></tt>). This is the intended usage pattern, but anything can be passed as the database object type so long as there is a defined <tt class="docutils literal"><span class="pre">withConn</span></tt> template that provides an injected <tt class="docutils literal"><span class="pre">conn: DbConn</span></tt> object to the provided statement body.</p> <p>For example, a valid database object implementation that opens a new connection for every request might look like this:</p> <pre class="listing"><span class="Keyword">import</span> <span class="Identifier">std</span><span class="Operator">/</span><span class="Identifier">db_postgres</span> <span class="Keyword">type</span> <span class="Identifier">TodoDB</span><span class="Operator">*</span> <span class="Operator">=</span> <span class="Keyword">object</span> <span class="Identifier">connString</span><span class="Punctuation">:</span> <span class="Identifier">string</span> <span class="Keyword">template</span> <span class="Identifier">withConn</span><span class="Operator">*</span><span class="Punctuation">(</span><span class="Identifier">db</span><span class="Punctuation">:</span> <span class="Identifier">TodoDB</span><span class="Punctuation">,</span> <span class="Identifier">stmt</span><span class="Punctuation">:</span> <span class="Identifier">untyped</span><span class="Punctuation">)</span><span class="Punctuation">:</span> <span class="Identifier">untyped</span> <span class="Operator">=</span> <span class="Keyword">let</span> <span class="Identifier">conn</span> <span class="Punctuation">{</span><span class="Operator">.</span><span class="Identifier">inject</span><span class="Operator">.</span><span class="Punctuation">}</span> <span class="Operator">=</span> <span class="Identifier">open</span><span class="Punctuation">(</span><span class="StringLit">""</span><span class="Punctuation">,</span> <span class="StringLit">""</span><span class="Punctuation">,</span> <span class="StringLit">""</span><span class="Punctuation">,</span> <span class="Identifier">db</span><span class="Operator">.</span><span class="Identifier">connString</span><span class="Punctuation">)</span> <span class="Keyword">try</span><span class="Punctuation">:</span> <span class="Identifier">stmt</span> <span class="Keyword">finally</span><span class="Punctuation">:</span> <span class="Identifier">close</span><span class="Punctuation">(</span><span class="Identifier">conn</span><span class="Punctuation">)</span></pre> <div class="row"> <div class="twelve-columns footer"> <span class="nim-sprite"></span> <br/> <small style="color: var(--hint);">Made with Nim. Generated: 2022-09-04 02:31:20 UTC</small> </div> </div> </div> </div> </body> </html>