fiber-orm-nim/docs/index.html

620 lines
70 KiB
HTML

<?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>src/fiber_orm</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">src/fiber_orm</h1>
<div class="row">
<div class="three columns">
<div class="theme-switch-wrapper">
<label class="theme-switch" for="checkbox">
<input type="checkbox" id="checkbox" />
<div class="slider round"></div>
</label>
&nbsp;&nbsp;&nbsp; <em>Dark Mode</em>
</div>
<div id="global-links">
<ul class="simple">
<li>
<a href="theindex.html">Index</a>
</li>
</ul>
</div>
<div id="searchInputDiv">
Search: <input type="text" id="searchInput"
onkeyup="search()" />
</div>
<div>
Group by:
<select onchange="groupBy(this.value)">
<option value="section">Section</option>
<option value="type">Type</option>
</select>
</div>
<ul class="simple simple-toc" id="toc-list">
<li><a class="reference" id="basic-usage_toc" href="#basic-usage">Basic Usage</a></li>
<ul class="simple"><li><a class="reference" id="basic-usage-example-db-schema_toc" href="#basic-usage-example-db-schema">Example DB Schema</a></li>
<li><a class="reference" id="basic-usage-example-model-definitions_toc" href="#basic-usage-example-model-definitions">Example Model Definitions</a></li>
<li><a class="reference" id="basic-usage-example-fiber-orm-usage_toc" href="#basic-usage-example-fiber-orm-usage">Example Fiber ORM Usage</a></li>
</ul><li><a class="reference" id="objectminusrelational-modeling_toc" href="#objectminusrelational-modeling">Object-Relational Modeling</a></li>
<ul class="simple"><li><a class="reference" id="objectminusrelational-modeling-model-class_toc" href="#objectminusrelational-modeling-model-class">Model Class</a></li>
<ul class="simple"><li><a class="reference" id="model-class-name-mapping_toc" href="#model-class-name-mapping">Name Mapping</a></li>
<li><a class="reference" id="model-class-id-field_toc" href="#model-class-id-field">ID Field</a></li>
</ul><li><a class="reference" id="objectminusrelational-modeling-supported-data-types_toc" href="#objectminusrelational-modeling-supported-data-types">Supported Data Types</a></li>
</ul><li><a class="reference" id="database-object_toc" href="#database-object">Database Object</a></li>
<li><a class="reference" id="see-also_toc" href="#see-also">See Also</a></li>
<li>
<a class="reference reference-toplevel" href="#6" id="56">Imports</a>
<ul class="simple simple-toc-section">
</ul>
</li>
<li>
<a class="reference reference-toplevel" href="#7" id="57">Types</a>
<ul class="simple simple-toc-section">
<li><a class="reference" href="#NotFoundError"
title="NotFoundError = object of CatchableError">NotFoundError</a></li>
</ul>
</li>
<li>
<a class="reference reference-toplevel" href="#12" id="62">Procs</a>
<ul class="simple simple-toc-section">
<ul class="simple nested-toc-section">createRecord
<li><a class="reference" href="#createRecord%2CDbConn%2CT"
title="createRecord[T](db: DbConn; rec: T): T">createRecord[T](db: DbConn; rec: T): T</a></li>
</ul>
<ul class="simple nested-toc-section">deleteRecord
<li><a class="reference" href="#deleteRecord%2CDbConn%2CT"
title="deleteRecord[T](db: DbConn; rec: T): bool">deleteRecord[T](db: DbConn; rec: T): bool</a></li>
</ul>
<ul class="simple nested-toc-section">initPool
<li><a class="reference" href="#initPool%2Cproc%29%2Cint%2Cstring"
title="initPool(connect: proc (): DbConn; poolSize = 10; hardCap = false;
healthCheckQuery = &quot;SELECT \'true\' AS alive&quot;): DbConnPool">initPool(connect: proc (): DbConn; poolSize = 10; hardCap = false;
healthCheckQuery = &quot;SELECT \'true\' AS alive&quot;): DbConnPool</a></li>
</ul>
<ul class="simple nested-toc-section">updateRecord
<li><a class="reference" href="#updateRecord%2CDbConn%2CT"
title="updateRecord[T](db: DbConn; rec: T): bool">updateRecord[T](db: DbConn; rec: T): bool</a></li>
</ul>
</ul>
</li>
<li>
<a class="reference reference-toplevel" href="#17" id="67">Macros</a>
<ul class="simple simple-toc-section">
<ul class="simple nested-toc-section">generateLookup
<li><a class="reference" href="#generateLookup.m%2Ctype%2Ctype%2Cseq%5Bstring%5D"
title="generateLookup(dbType: type; modelType: type; fields: seq[string]): untyped">generateLookup(dbType: type; modelType: type; fields: seq[string]): untyped</a></li>
</ul>
<ul class="simple nested-toc-section">generateProcsForFieldLookups
<li><a class="reference" href="#generateProcsForFieldLookups.m%2Ctype%2CopenArray%5Btuple%5Btype%2Cseq%5Bstring%5D%5D%5D"
title="generateProcsForFieldLookups(dbType: type; modelsAndFields: openArray[
tuple[t: type, fields: seq[string]]]): untyped">generateProcsForFieldLookups(dbType: type; modelsAndFields: openArray[
tuple[t: type, fields: seq[string]]]): untyped</a></li>
</ul>
<ul class="simple nested-toc-section">generateProcsForModels
<li><a class="reference" href="#generateProcsForModels.m%2Ctype%2CopenArray%5Btype%5D"
title="generateProcsForModels(dbType: type; modelTypes: openArray[type]): untyped">generateProcsForModels(dbType: type; modelTypes: openArray[type]): untyped</a></li>
</ul>
</ul>
</li>
<li>
<a class="reference reference-toplevel" href="#18" id="68">Templates</a>
<ul class="simple simple-toc-section">
<ul class="simple nested-toc-section">deleteRecord
<li><a class="reference" href="#deleteRecord.t%2CDbConn%2Ctype%2Ctyped"
title="deleteRecord(db: DbConn; modelType: type; id: typed): untyped">deleteRecord(db: DbConn; modelType: type; id: typed): untyped</a></li>
</ul>
<ul class="simple nested-toc-section">findRecordsBy
<li><a class="reference" href="#findRecordsBy.t%2CDbConn%2Ctype%2Cseq%5Btuple%5Bstring%2Cstring%5D%5D"
title="findRecordsBy(db: DbConn; modelType: type;
lookups: seq[tuple[field: string, value: string]]): untyped">findRecordsBy(db: DbConn; modelType: type;
lookups: seq[tuple[field: string, value: string]]): untyped</a></li>
</ul>
<ul class="simple nested-toc-section">findRecordsWhere
<li><a class="reference" href="#findRecordsWhere.t%2CDbConn%2Ctype%2Cstring%2Cvarargs%5Bstring%2CdbFormat%5D"
title="findRecordsWhere(db: DbConn; modelType: type; whereClause: string;
values: varargs[string, dbFormat]): untyped">findRecordsWhere(db: DbConn; modelType: type; whereClause: string;
values: varargs[string, dbFormat]): untyped</a></li>
</ul>
<ul class="simple nested-toc-section">getAllRecords
<li><a class="reference" href="#getAllRecords.t%2CDbConn%2Ctype"
title="getAllRecords(db: DbConn; modelType: type): untyped">getAllRecords(db: DbConn; modelType: type): untyped</a></li>
</ul>
<ul class="simple nested-toc-section">getRecord
<li><a class="reference" href="#getRecord.t%2CDbConn%2Ctype%2Ctyped"
title="getRecord(db: DbConn; modelType: type; id: typed): untyped">getRecord(db: DbConn; modelType: type; id: typed): untyped</a></li>
</ul>
<ul class="simple nested-toc-section">inTransaction
<li><a class="reference" href="#inTransaction.t%2CDbConnPool%2Cuntyped"
title="inTransaction(db: DbConnPool; body: untyped)">inTransaction(db: DbConnPool; body: untyped)</a></li>
</ul>
</ul>
</li>
<li>
<a class="reference reference-toplevel" href="#19" id="69">Exports</a>
<ul class="simple simple-toc-section">
</ul>
</li>
</ul>
</div>
&nbsp;&nbsp;<a
href="https://github.com/jdbernard/fiber-orm/tree/version-1-6/src/fiber_orm.nim#L1"
class="link-seesrc" target="_blank">Source</a>
&nbsp;&nbsp;<a href="https://github.com/jdbernard/fiber-orm/edit/devel/src/fiber_orm.nim#L1" class="link-seesrc" target="_blank" >Edit</a>
<div class="nine columns" id="content">
<div id="tocRoot"></div>
<p class="module-desc"><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>
<h1><a class="toc-backref" id="basic-usage" href="#basic-usage">Basic Usage</a></h1><p>Consider a simple TODO list application that keeps track of TODO items as well as time logged against those items.</p>
<h2><a class="toc-backref" id="basic-usage-example-db-schema" href="#basic-usage-example-db-schema">Example DB Schema</a></h2><p>You might have a schema such as:</p>
<pre class="listing">create extension if not exists &quot;pgcrypto&quot;;
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>
<h2><a class="toc-backref" id="basic-usage-example-model-definitions" href="#basic-usage-example-model-definitions">Example Model Definitions</a></h2><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>
<h2><a class="toc-backref" id="basic-usage-example-fiber-orm-usage" href="#basic-usage-example-fiber-orm-usage">Example Fiber ORM Usage</a></h2><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">result</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">&quot;&quot;</span><span class="Punctuation">,</span> <span class="StringLit">&quot;&quot;</span><span class="Punctuation">,</span> <span class="StringLit">&quot;&quot;</span><span class="Punctuation">,</span> <span class="Identifier">connString</span><span class="Punctuation">)</span><span class="Punctuation">,</span>
<span class="Identifier">poolSize</span> <span class="Operator">=</span> <span class="DecNumber">20</span><span class="Punctuation">,</span>
<span class="Identifier">hardCap</span> <span class="Operator">=</span> <span class="Identifier">false</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">&quot;todoItemId&quot;</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>
<h1><a class="toc-backref" id="objectminusrelational-modeling" href="#objectminusrelational-modeling">Object-Relational Modeling</a></h1>
<h2><a class="toc-backref" id="objectminusrelational-modeling-model-class" href="#objectminusrelational-modeling-model-class">Model Class</a></h2><p>Fiber ORM uses simple Nim <tt class="docutils literal"><span class="pre"><span class="Keyword">object</span></span></tt>s and <tt class="docutils literal"><span class="pre"><span class="Keyword">ref</span> <span class="Keyword">object</span></span></tt>s as model classes. Fiber ORM expects there to be one table for each model class.</p>
<h3><a class="toc-backref" id="model-class-name-mapping" href="#model-class-name-mapping">Name Mapping</a></h3><p>Fiber ORM uses <tt class="docutils literal"><span class="pre"><span class="Identifier">snake_case</span></span></tt> for database identifiers (column names, table names, etc.) and <tt class="docutils literal"><span class="pre"><span class="Identifier">camelCase</span></span></tt> for Nim identifiers. We automatically convert model names to and from table names (<tt class="docutils literal"><span class="pre"><span class="Identifier">TodoItem</span></span></tt> &lt;-&gt; <tt class="docutils literal"><span class="pre"><span class="Identifier">todo_items</span></span></tt>), as well as column names (<tt class="docutils literal"><span class="pre"><span class="Identifier">userId</span></span></tt> &lt;-&gt; <tt class="docutils literal"><span class="pre"><span class="Identifier">user_id</span></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"><span class="Identifier">snake_case</span></span></tt>, <tt class="docutils literal"><span class="pre"><span class="Identifier">camelCase</span></span></tt>, or <tt class="docutils literal"><span class="pre"><span class="Identifier">PascalCase</span></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"><span class="Identifier">camelCase</span></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>
<h3><a class="toc-backref" id="model-class-id-field" href="#model-class-id-field">ID Field</a></h3><p>Fiber ORM expects every model class to have a field named <tt class="docutils literal"><span class="pre"><span class="Identifier">id</span></span></tt>, with a corresponding <tt class="docutils literal"><span class="pre"><span class="Identifier">id</span></span></tt> column in the model table. This field must be either a <tt class="docutils literal"><span class="pre"><span class="Identifier">string</span></span></tt>, <tt class="docutils literal"><span class="pre"><span class="Identifier">integer</span></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"><span class="Identifier">id</span></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"><span class="DecNumber">0</span></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"><span class="Identifier">id</span></span></tt> field in the <tt class="docutils literal"><span class="pre"><span class="Identifier">INSERT</span></span></tt> query.</p>
<p>For example, to allow the database to create the id:</p>
<pre class="listing"><span class="Keyword">let</span> <span class="Identifier">item</span> <span class="Operator">=</span> <span class="Identifier">TodoItem</span><span class="Punctuation">(</span>
<span class="Identifier">owner</span><span class="Punctuation">:</span> <span class="StringLit">&quot;John Mann&quot;</span><span class="Punctuation">,</span>
<span class="Identifier">summary</span><span class="Punctuation">:</span> <span class="StringLit">&quot;Create a grocery list.&quot;</span><span class="Punctuation">,</span>
<span class="Identifier">details</span><span class="Punctuation">:</span> <span class="Identifier">none</span><span class="Punctuation">[</span><span class="Identifier">string</span><span class="Punctuation">]</span><span class="Punctuation">(</span><span class="Punctuation">)</span><span class="Punctuation">,</span>
<span class="Identifier">priority</span><span class="Punctuation">:</span> <span class="DecNumber">0</span><span class="Punctuation">,</span>
<span class="Identifier">relatedTodoItemIds</span><span class="Punctuation">:</span> <span class="Operator">@</span><span class="Punctuation">[</span><span class="Punctuation">]</span><span class="Punctuation">)</span>
<span class="Keyword">let</span> <span class="Identifier">itemWithId</span> <span class="Operator">=</span> <span class="Identifier">db</span><span class="Operator">.</span><span class="Identifier">createTodoItem</span><span class="Punctuation">(</span><span class="Identifier">item</span><span class="Punctuation">)</span>
<span class="Identifier">echo</span> <span class="Operator">$</span><span class="Identifier">itemWithId</span><span class="Operator">.</span><span class="Identifier">id</span> <span class="Comment"># generated in the database</span></pre><p>And to create it in code:</p>
<pre class="listing"><span class="Keyword">import</span> <span class="Identifier">uuids</span>
<span class="Keyword">let</span> <span class="Identifier">item</span> <span class="Operator">=</span> <span class="Identifier">TodoItem</span><span class="Punctuation">(</span>
<span class="Identifier">id</span><span class="Punctuation">:</span> <span class="Identifier">genUUID</span><span class="Punctuation">(</span><span class="Punctuation">)</span><span class="Punctuation">,</span>
<span class="Identifier">owner</span><span class="Punctuation">:</span> <span class="StringLit">&quot;John Mann&quot;</span><span class="Punctuation">,</span>
<span class="Identifier">summary</span><span class="Punctuation">:</span> <span class="StringLit">&quot;Create a grocery list.&quot;</span><span class="Punctuation">,</span>
<span class="Identifier">details</span><span class="Punctuation">:</span> <span class="Identifier">none</span><span class="Punctuation">[</span><span class="Identifier">string</span><span class="Punctuation">]</span><span class="Punctuation">(</span><span class="Punctuation">)</span><span class="Punctuation">,</span>
<span class="Identifier">priority</span><span class="Punctuation">:</span> <span class="DecNumber">0</span><span class="Punctuation">,</span>
<span class="Identifier">relatedTodoItemIds</span><span class="Punctuation">:</span> <span class="Operator">@</span><span class="Punctuation">[</span><span class="Punctuation">]</span><span class="Punctuation">)</span>
<span class="Keyword">let</span> <span class="Identifier">itemInDb</span> <span class="Operator">=</span> <span class="Identifier">db</span><span class="Operator">.</span><span class="Identifier">createTodoItem</span><span class="Punctuation">(</span><span class="Identifier">item</span><span class="Punctuation">)</span>
<span class="Identifier">echo</span> <span class="Operator">$</span><span class="Identifier">itemInDb</span><span class="Operator">.</span><span class="Identifier">id</span> <span class="Comment"># will be the same as what was provided</span></pre>
<h2><a class="toc-backref" id="objectminusrelational-modeling-supported-data-types" href="#objectminusrelational-modeling-supported-data-types">Supported Data Types</a></h2><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"><span class="Identifier">string</span></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"><span class="Identifier">int</span></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"><span class="Identifier">float</span></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"><span class="Identifier">bool</span></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"><span class="Identifier">seq</span><span class="Punctuation">[</span><span class="Punctuation">]</span></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"><span class="Identifier">NULL</span></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> &ensp; Note that this implies that all <tt class="docutils literal"><span class="pre"><span class="Identifier">NULL</span></span></tt>-able fields should be typed as optional using <tt class="docutils literal"><span class="pre"><span class="Identifier">Option</span><span class="Punctuation">[</span><span class="Identifier">fieldType</span><span class="Punctuation">]</span></span></tt>. Conversely, any fields with non-optional types should also be constrained to be <tt class="docutils literal"><span class="pre"><span class="Keyword">NOT</span> <span class="Identifier">NULL</span></span></tt> in the database schema.
</div>
</div>
<h1><a class="toc-backref" id="database-object" href="#database-object">Database Object</a></h1><p>Many of the Fiber ORM macros expect a database object type to be passed. In the example above the <a class="reference external" href="fiber_orm/pool.html#DbConnPool">pool.DbConnPool</a> object is used as database object type (aliased as <tt class="docutils literal"><span class="pre"><span class="Identifier">TodoDB</span></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"><span class="Identifier">withConn</span></span></tt> template that provides an injected <tt class="docutils literal"><span class="pre"><span class="Identifier">conn</span><span class="Punctuation">:</span> <span class="Identifier">DbConn</span></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">&quot;&quot;</span><span class="Punctuation">,</span> <span class="StringLit">&quot;&quot;</span><span class="Punctuation">,</span> <span class="StringLit">&quot;&quot;</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>
<h1><a class="toc-backref" id="see-also" href="#see-also">See Also</a></h1><p><a class="reference external" href="fiber_orm/pool.html">fiber_orm/pool</a></p>
</p>
<div class="section" id="6">
<h1><a class="toc-backref" href="#6">Imports</a></h1>
<dl class="item">
<a class="reference external" href="fiber_orm/pool.html">fiber_orm/pool</a>, <a class="reference external" href="fiber_orm/util.html">fiber_orm/util</a>
</dl></div>
<div class="section" id="7">
<h1><a class="toc-backref" href="#7">Types</a></h1>
<dl class="item">
<div id="NotFoundError">
<dt><pre><a href="fiber_orm.html#NotFoundError"><span class="Identifier">NotFoundError</span></a> <span class="Other">=</span> <span class="Keyword">object</span> <span class="Keyword">of</span> <span class="Identifier">CatchableError</span></pre></dt>
<dd>
Error type raised when no record matches a given ID
&nbsp;&nbsp;<a
href="https://github.com/jdbernard/fiber-orm/tree/version-1-6/src/fiber_orm.nim#L299"
class="link-seesrc" target="_blank">Source</a>
&nbsp;&nbsp;<a href="https://github.com/jdbernard/fiber-orm/edit/devel/src/fiber_orm.nim#L299" class="link-seesrc" target="_blank" >Edit</a>
</dd>
</div>
</dl></div>
<div class="section" id="12">
<h1><a class="toc-backref" href="#12">Procs</a></h1>
<dl class="item">
<div id="createRecord,DbConn,T">
<dt><pre><span class="Keyword">proc</span> <a href="#createRecord%2CDbConn%2CT"><span class="Identifier">createRecord</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">db</span><span class="Other">:</span> <span class="Identifier">DbConn</span><span class="Other">;</span> <span class="Identifier">rec</span><span class="Other">:</span> <span class="Identifier">T</span><span class="Other">)</span><span class="Other">:</span> <span class="Identifier">T</span></pre></dt>
<dd>
<p>Create a new record. <tt class="docutils literal"><span class="pre"><span class="Identifier">rec</span></span></tt> is expected to be a <a class="reference external" href="#objectminusrelational-modeling-model-class">model class</a>. The <tt class="docutils literal"><span class="pre"><span class="Identifier">id</span></span></tt> field is only set if it is non-empty (see <a class="reference external" href="#model-class-id-field">ID Field</a> for details).</p>
<p>Returns the newly created record.</p>
&nbsp;&nbsp;<a
href="https://github.com/jdbernard/fiber-orm/tree/version-1-6/src/fiber_orm.nim#L314"
class="link-seesrc" target="_blank">Source</a>
&nbsp;&nbsp;<a href="https://github.com/jdbernard/fiber-orm/edit/devel/src/fiber_orm.nim#L314" class="link-seesrc" target="_blank" >Edit</a>
</dd>
</div>
<div id="deleteRecord,DbConn,T">
<dt><pre><span class="Keyword">proc</span> <a href="#deleteRecord%2CDbConn%2CT"><span class="Identifier">deleteRecord</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">db</span><span class="Other">:</span> <span class="Identifier">DbConn</span><span class="Other">;</span> <span class="Identifier">rec</span><span class="Other">:</span> <span class="Identifier">T</span><span class="Other">)</span><span class="Other">:</span> <span class="Identifier">bool</span></pre></dt>
<dd>
Delete a record by <a class="reference external" href="#model-class-id-field">id</a>.
&nbsp;&nbsp;<a
href="https://github.com/jdbernard/fiber-orm/tree/version-1-6/src/fiber_orm.nim#L359"
class="link-seesrc" target="_blank">Source</a>
&nbsp;&nbsp;<a href="https://github.com/jdbernard/fiber-orm/edit/devel/src/fiber_orm.nim#L359" class="link-seesrc" target="_blank" >Edit</a>
</dd>
</div>
<div id="initPool,proc),int,string">
<dt><pre><span class="Keyword">proc</span> <a href="#initPool%2Cproc%29%2Cint%2Cstring"><span class="Identifier">initPool</span></a><span class="Other">(</span><span class="Identifier">connect</span><span class="Other">:</span> <span class="Keyword">proc</span> <span class="Other">(</span><span class="Other">)</span><span class="Other">:</span> <span class="Identifier">DbConn</span><span class="Other">;</span> <span class="Identifier">poolSize</span> <span class="Other">=</span> <span class="DecNumber">10</span><span class="Other">;</span> <span class="Identifier">hardCap</span> <span class="Other">=</span> <span class="Identifier">false</span><span class="Other">;</span>
<span class="Identifier">healthCheckQuery</span> <span class="Other">=</span> <span class="StringLit">&quot;SELECT \'true\' AS alive&quot;</span><span class="Other">)</span><span class="Other">:</span> <a href="fiber_orm/pool.html#DbConnPool"><span class="Identifier">DbConnPool</span></a> {.
<span><span class="Other pragmadots">...</span></span><span class="pragmawrap"><span class="Identifier">raises</span><span class="Other">:</span> <span class="Other">[</span><span class="Identifier">Exception</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">tags</span><span class="Other">:</span> <span class="Other">[</span><span class="Identifier">RootEffect</span><span class="Other">]</span></span>.}</pre></dt>
<dd>
Initialize a new DbConnPool. See the <tt class="docutils literal"><span class="pre"><span class="Identifier">initDb</span></span></tt> procedure in the <a class="reference external" href="#basic-usage-example-fiber-orm-usage">Example Fiber ORM Usage</a> for an example<ul class="simple"><li><tt class="docutils literal"><span class="pre"><span class="Identifier">connect</span></span></tt> must be a factory which creates a new <tt class="docutils literal"><span class="pre"><span class="Identifier">DbConn</span></span></tt>.</li>
<li><tt class="docutils literal"><span class="pre"><span class="Identifier">poolSize</span></span></tt> sets the desired capacity of the connection pool.</li>
<li><p><tt class="docutils literal"><span class="pre"><span class="Identifier">hardCap</span></span></tt> defaults to <tt class="docutils literal"><span class="pre"><span class="Identifier">false</span></span></tt>. When <tt class="docutils literal"><span class="pre"><span class="Identifier">false</span></span></tt>, the pool can grow beyond the configured capacity, but will release connections down to the its capacity (no less than <tt class="docutils literal"><span class="pre"><span class="Identifier">poolSize</span></span></tt>).</p>
<p>When <tt class="docutils literal"><span class="pre"><span class="Identifier">true</span></span></tt> the pool will not create more than its configured capacity. It a connection is requested, none are free, and the pool is at capacity, this will result in an Error being raised.</p>
</li>
<li><tt class="docutils literal"><span class="pre"><span class="Identifier">healthCheckQuery</span></span></tt> should be a simple and fast SQL query that the pool can use to test the liveliness of pooled connections.</li>
</ul>
&nbsp;&nbsp;<a
href="https://github.com/jdbernard/fiber-orm/tree/version-1-6/src/fiber_orm.nim#L539"
class="link-seesrc" target="_blank">Source</a>
&nbsp;&nbsp;<a href="https://github.com/jdbernard/fiber-orm/edit/devel/src/fiber_orm.nim#L539" class="link-seesrc" target="_blank" >Edit</a>
</dd>
</div>
<div id="updateRecord,DbConn,T">
<dt><pre><span class="Keyword">proc</span> <a href="#updateRecord%2CDbConn%2CT"><span class="Identifier">updateRecord</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">db</span><span class="Other">:</span> <span class="Identifier">DbConn</span><span class="Other">;</span> <span class="Identifier">rec</span><span class="Other">:</span> <span class="Identifier">T</span><span class="Other">)</span><span class="Other">:</span> <span class="Identifier">bool</span></pre></dt>
<dd>
Update a record by id. <tt class="docutils literal"><span class="pre"><span class="Identifier">rec</span></span></tt> is expected to be a <a class="reference external" href="#objectminusrelational-modeling-model-class">model class</a>.
&nbsp;&nbsp;<a
href="https://github.com/jdbernard/fiber-orm/tree/version-1-6/src/fiber_orm.nim#L337"
class="link-seesrc" target="_blank">Source</a>
&nbsp;&nbsp;<a href="https://github.com/jdbernard/fiber-orm/edit/devel/src/fiber_orm.nim#L337" class="link-seesrc" target="_blank" >Edit</a>
</dd>
</div>
</dl></div>
<div class="section" id="17">
<h1><a class="toc-backref" href="#17">Macros</a></h1>
<dl class="item">
<div id="generateLookup.m,type,type,seq[string]">
<dt><pre><span class="Keyword">macro</span> <a href="#generateLookup.m%2Ctype%2Ctype%2Cseq%5Bstring%5D"><span class="Identifier">generateLookup</span></a><span class="Other">(</span><span class="Identifier">dbType</span><span class="Other">:</span> <span class="Identifier">type</span><span class="Other">;</span> <span class="Identifier">modelType</span><span class="Other">:</span> <span class="Identifier">type</span><span class="Other">;</span> <span class="Identifier">fields</span><span class="Other">:</span> <span class="Identifier">seq</span><span class="Other">[</span><span class="Identifier">string</span><span class="Other">]</span><span class="Other">)</span><span class="Other">:</span> <span class="Identifier">untyped</span></pre></dt>
<dd>
Create a lookup procedure for a given set of field names. For example, given the TODO database demostrated above,<pre class="listing"><span class="Identifier">generateLookup</span><span class="Punctuation">(</span><span class="Identifier">TodoDB</span><span class="Punctuation">,</span> <span class="Identifier">TodoItem</span><span class="Punctuation">,</span> <span class="Punctuation">[</span><span class="StringLit">&quot;owner&quot;</span><span class="Punctuation">,</span> <span class="StringLit">&quot;priority&quot;</span><span class="Punctuation">]</span><span class="Punctuation">)</span></pre><p>will generate the following procedure:</p>
<pre class="listing"><span class="Keyword">proc</span> <span class="Identifier">findTodoItemsByOwnerAndPriority</span><span class="Operator">*</span><span class="Punctuation">(</span><span class="Identifier">db</span><span class="Punctuation">:</span> <span class="Identifier">SampleDB</span><span class="Punctuation">,</span>
<span class="Identifier">owner</span><span class="Punctuation">:</span> <span class="Identifier">string</span><span class="Punctuation">,</span> <span class="Identifier">priority</span><span class="Punctuation">:</span> <span class="Identifier">int</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></pre>
&nbsp;&nbsp;<a
href="https://github.com/jdbernard/fiber-orm/tree/version-1-6/src/fiber_orm.nim#L468"
class="link-seesrc" target="_blank">Source</a>
&nbsp;&nbsp;<a href="https://github.com/jdbernard/fiber-orm/edit/devel/src/fiber_orm.nim#L468" class="link-seesrc" target="_blank" >Edit</a>
</dd>
</div>
<div id="generateProcsForFieldLookups.m,type,openArray[tuple[type,seq[string]]]">
<dt><pre><span class="Keyword">macro</span> <a href="#generateProcsForFieldLookups.m%2Ctype%2CopenArray%5Btuple%5Btype%2Cseq%5Bstring%5D%5D%5D"><span class="Identifier">generateProcsForFieldLookups</span></a><span class="Other">(</span><span class="Identifier">dbType</span><span class="Other">:</span> <span class="Identifier">type</span><span class="Other">;</span> <span class="Identifier">modelsAndFields</span><span class="Other">:</span> <span class="Identifier">openArray</span><span class="Other">[</span>
<span class="Keyword">tuple</span><span class="Other">[</span><span class="Identifier">t</span><span class="Other">:</span> <span class="Identifier">type</span><span class="Other">,</span> <span class="Identifier">fields</span><span class="Other">:</span> <span class="Identifier">seq</span><span class="Other">[</span><span class="Identifier">string</span><span class="Other">]</span><span class="Other">]</span><span class="Other">]</span><span class="Other">)</span><span class="Other">:</span> <span class="Identifier">untyped</span></pre></dt>
<dd>
&nbsp;&nbsp;<a
href="https://github.com/jdbernard/fiber-orm/tree/version-1-6/src/fiber_orm.nim#L510"
class="link-seesrc" target="_blank">Source</a>
&nbsp;&nbsp;<a href="https://github.com/jdbernard/fiber-orm/edit/devel/src/fiber_orm.nim#L510" class="link-seesrc" target="_blank" >Edit</a>
</dd>
</div>
<div id="generateProcsForModels.m,type,openArray[type]">
<dt><pre><span class="Keyword">macro</span> <a href="#generateProcsForModels.m%2Ctype%2CopenArray%5Btype%5D"><span class="Identifier">generateProcsForModels</span></a><span class="Other">(</span><span class="Identifier">dbType</span><span class="Other">:</span> <span class="Identifier">type</span><span class="Other">;</span> <span class="Identifier">modelTypes</span><span class="Other">:</span> <span class="Identifier">openArray</span><span class="Other">[</span><span class="Identifier">type</span><span class="Other">]</span><span class="Other">)</span><span class="Other">:</span> <span class="Identifier">untyped</span></pre></dt>
<dd>
Generate all standard access procedures for the given model types. For a <a class="reference external" href="#objectminusrelational-modeling-model-class">model class</a> named <tt class="docutils literal"><span class="pre"><span class="Identifier">TodoItem</span></span></tt>, this will generate the following procedures:<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">idType</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">TodoItem</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">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">idType</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">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">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="Punctuation">)</span><span class="Punctuation">:</span> <span class="Identifier">TodoItem</span><span class="Punctuation">;</span></pre><p><tt class="docutils literal"><span class="pre"><span class="Identifier">dbType</span></span></tt> is expected to be some type that has a defined <tt class="docutils literal"><span class="pre"><span class="Identifier">withConn</span></span></tt> procedure (see <a class="reference external" href="#database-object">Database Object</a> for details).</p>
&nbsp;&nbsp;<a
href="https://github.com/jdbernard/fiber-orm/tree/version-1-6/src/fiber_orm.nim#L414"
class="link-seesrc" target="_blank">Source</a>
&nbsp;&nbsp;<a href="https://github.com/jdbernard/fiber-orm/edit/devel/src/fiber_orm.nim#L414" class="link-seesrc" target="_blank" >Edit</a>
</dd>
</div>
</dl></div>
<div class="section" id="18">
<h1><a class="toc-backref" href="#18">Templates</a></h1>
<dl class="item">
<div id="deleteRecord.t,DbConn,type,typed">
<dt><pre><span class="Keyword">template</span> <a href="#deleteRecord.t%2CDbConn%2Ctype%2Ctyped"><span class="Identifier">deleteRecord</span></a><span class="Other">(</span><span class="Identifier">db</span><span class="Other">:</span> <span class="Identifier">DbConn</span><span class="Other">;</span> <span class="Identifier">modelType</span><span class="Other">:</span> <span class="Identifier">type</span><span class="Other">;</span> <span class="Identifier">id</span><span class="Other">:</span> <span class="Identifier">typed</span><span class="Other">)</span><span class="Other">:</span> <span class="Identifier">untyped</span></pre></dt>
<dd>
Delete a record by id.
&nbsp;&nbsp;<a
href="https://github.com/jdbernard/fiber-orm/tree/version-1-6/src/fiber_orm.nim#L353"
class="link-seesrc" target="_blank">Source</a>
&nbsp;&nbsp;<a href="https://github.com/jdbernard/fiber-orm/edit/devel/src/fiber_orm.nim#L353" class="link-seesrc" target="_blank" >Edit</a>
</dd>
</div>
<div id="findRecordsBy.t,DbConn,type,seq[tuple[string,string]]">
<dt><pre><span class="Keyword">template</span> <a href="#findRecordsBy.t%2CDbConn%2Ctype%2Cseq%5Btuple%5Bstring%2Cstring%5D%5D"><span class="Identifier">findRecordsBy</span></a><span class="Other">(</span><span class="Identifier">db</span><span class="Other">:</span> <span class="Identifier">DbConn</span><span class="Other">;</span> <span class="Identifier">modelType</span><span class="Other">:</span> <span class="Identifier">type</span><span class="Other">;</span>
<span class="Identifier">lookups</span><span class="Other">:</span> <span class="Identifier">seq</span><span class="Other">[</span><span class="Keyword">tuple</span><span class="Other">[</span><span class="Identifier">field</span><span class="Other">:</span> <span class="Identifier">string</span><span class="Other">,</span> <span class="Identifier">value</span><span class="Other">:</span> <span class="Identifier">string</span><span class="Other">]</span><span class="Other">]</span><span class="Other">)</span><span class="Other">:</span> <span class="Identifier">untyped</span></pre></dt>
<dd>
Find all records matching the provided lookup values.
&nbsp;&nbsp;<a
href="https://github.com/jdbernard/fiber-orm/tree/version-1-6/src/fiber_orm.nim#L403"
class="link-seesrc" target="_blank">Source</a>
&nbsp;&nbsp;<a href="https://github.com/jdbernard/fiber-orm/edit/devel/src/fiber_orm.nim#L403" class="link-seesrc" target="_blank" >Edit</a>
</dd>
</div>
<div id="findRecordsWhere.t,DbConn,type,string,varargs[string,dbFormat]">
<dt><pre><span class="Keyword">template</span> <a href="#findRecordsWhere.t%2CDbConn%2Ctype%2Cstring%2Cvarargs%5Bstring%2CdbFormat%5D"><span class="Identifier">findRecordsWhere</span></a><span class="Other">(</span><span class="Identifier">db</span><span class="Other">:</span> <span class="Identifier">DbConn</span><span class="Other">;</span> <span class="Identifier">modelType</span><span class="Other">:</span> <span class="Identifier">type</span><span class="Other">;</span> <span class="Identifier">whereClause</span><span class="Other">:</span> <span class="Identifier">string</span><span class="Other">;</span>
<span class="Identifier">values</span><span class="Other">:</span> <span class="Identifier">varargs</span><span class="Other">[</span><span class="Identifier">string</span><span class="Other">,</span> <span class="Identifier">dbFormat</span><span class="Other">]</span><span class="Other">)</span><span class="Other">:</span> <span class="Identifier">untyped</span></pre></dt>
<dd>
Find all records matching a given <tt class="docutils literal"><span class="pre"><span class="Identifier">WHERE</span></span></tt> clause. The number of elements in the <tt class="docutils literal"><span class="pre"><span class="Identifier">values</span></span></tt> array must match the number of placeholders (<tt class="docutils literal"><span class="pre"><span class="Operator">?</span></span></tt>) in the provided <tt class="docutils literal"><span class="pre"><span class="Identifier">WHERE</span></span></tt> clause.
&nbsp;&nbsp;<a
href="https://github.com/jdbernard/fiber-orm/tree/version-1-6/src/fiber_orm.nim#L382"
class="link-seesrc" target="_blank">Source</a>
&nbsp;&nbsp;<a href="https://github.com/jdbernard/fiber-orm/edit/devel/src/fiber_orm.nim#L382" class="link-seesrc" target="_blank" >Edit</a>
</dd>
</div>
<div id="getAllRecords.t,DbConn,type">
<dt><pre><span class="Keyword">template</span> <a href="#getAllRecords.t%2CDbConn%2Ctype"><span class="Identifier">getAllRecords</span></a><span class="Other">(</span><span class="Identifier">db</span><span class="Other">:</span> <span class="Identifier">DbConn</span><span class="Other">;</span> <span class="Identifier">modelType</span><span class="Other">:</span> <span class="Identifier">type</span><span class="Other">)</span><span class="Other">:</span> <span class="Identifier">untyped</span></pre></dt>
<dd>
Fetch all records of the given type.
&nbsp;&nbsp;<a
href="https://github.com/jdbernard/fiber-orm/tree/version-1-6/src/fiber_orm.nim#L394"
class="link-seesrc" target="_blank">Source</a>
&nbsp;&nbsp;<a href="https://github.com/jdbernard/fiber-orm/edit/devel/src/fiber_orm.nim#L394" class="link-seesrc" target="_blank" >Edit</a>
</dd>
</div>
<div id="getRecord.t,DbConn,type,typed">
<dt><pre><span class="Keyword">template</span> <a href="#getRecord.t%2CDbConn%2Ctype%2Ctyped"><span class="Identifier">getRecord</span></a><span class="Other">(</span><span class="Identifier">db</span><span class="Other">:</span> <span class="Identifier">DbConn</span><span class="Other">;</span> <span class="Identifier">modelType</span><span class="Other">:</span> <span class="Identifier">type</span><span class="Other">;</span> <span class="Identifier">id</span><span class="Other">:</span> <span class="Identifier">typed</span><span class="Other">)</span><span class="Other">:</span> <span class="Identifier">untyped</span></pre></dt>
<dd>
Fetch a record by id.
&nbsp;&nbsp;<a
href="https://github.com/jdbernard/fiber-orm/tree/version-1-6/src/fiber_orm.nim#L367"
class="link-seesrc" target="_blank">Source</a>
&nbsp;&nbsp;<a href="https://github.com/jdbernard/fiber-orm/edit/devel/src/fiber_orm.nim#L367" class="link-seesrc" target="_blank" >Edit</a>
</dd>
</div>
<div id="inTransaction.t,DbConnPool,untyped">
<dt><pre><span class="Keyword">template</span> <a href="#inTransaction.t%2CDbConnPool%2Cuntyped"><span class="Identifier">inTransaction</span></a><span class="Other">(</span><span class="Identifier">db</span><span class="Other">:</span> <a href="fiber_orm/pool.html#DbConnPool"><span class="Identifier">DbConnPool</span></a><span class="Other">;</span> <span class="Identifier">body</span><span class="Other">:</span> <span class="Identifier">untyped</span><span class="Other">)</span></pre></dt>
<dd>
&nbsp;&nbsp;<a
href="https://github.com/jdbernard/fiber-orm/tree/version-1-6/src/fiber_orm.nim#L567"
class="link-seesrc" target="_blank">Source</a>
&nbsp;&nbsp;<a href="https://github.com/jdbernard/fiber-orm/edit/devel/src/fiber_orm.nim#L567" class="link-seesrc" target="_blank" >Edit</a>
</dd>
</div>
</dl></div>
<div class="section" id="19">
<h1><a class="toc-backref" href="#19">Exports</a></h1>
<dl class="item">
<a href="fiber_orm/pool.html#DbConnPoolConfig"><span class="Identifier">DbConnPoolConfig</span></a>, <a href="fiber_orm/pool.html#initDbConnPool,DbConnPoolConfig"><span class="Identifier">initDbConnPool</span></a>, <a href="fiber_orm/pool.html#withConn.t,DbConnPool,untyped"><span class="Identifier">withConn</span></a>, <a href="fiber_orm/pool.html#take,DbConnPool"><span class="Identifier">take</span></a>, <a href="fiber_orm/pool.html#DbConnPool"><span class="Identifier">DbConnPool</span></a>, <a href="fiber_orm/pool.html#release,DbConnPool,int"><span class="Identifier">release</span></a>, <a href="fiber_orm/util.html#columnNamesForModel.m,typed"><span class="Identifier">columnNamesForModel</span></a>, <a href="fiber_orm/util.html#dbFormat,DateTime"><span class="Identifier">dbFormat</span></a>, <a href="fiber_orm/util.html#dbFormat,seq[T]"><span class="Identifier">dbFormat</span></a>, <a href="fiber_orm/util.html#dbFormat,string"><span class="Identifier">dbFormat</span></a>, <a href="fiber_orm/util.html#dbFormat,T"><span class="Identifier">dbFormat</span></a>, <a href="fiber_orm/util.html#dbNameToIdent,string"><span class="Identifier">dbNameToIdent</span></a>, <a href="fiber_orm/util.html#identNameToDb,string"><span class="Identifier">identNameToDb</span></a>, <a href="fiber_orm/util.html#modelName.m,type"><span class="Identifier">modelName</span></a>, <a href="fiber_orm/util.html#modelName.m"><span class="Identifier">modelName</span></a>, <a href="fiber_orm/util.html#rowToModel.m,typed,seq[string]"><span class="Identifier">rowToModel</span></a>, <a href="fiber_orm/util.html#tableName,type"><span class="Identifier">tableName</span></a>, <a href="fiber_orm/util.html#tableName,T"><span class="Identifier">tableName</span></a>
</dl></div>
</div>
</div>
<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>