gorm/development.html
2016-05-04 21:26:42 +08:00

774 lines
21 KiB
HTML

<!DOCTYPE HTML>
<html lang="" >
<head>
<title>Development · GORM Guide</title>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<meta name="description" content="Gorm use chainable API, *gorm.DB is the bridge of chains, for each chain API, it will create a new relation.">
<meta name="generator" content="GitBook 3.0.0-pre.5">
<link rel="stylesheet" href="gitbook/style.css">
<link rel="stylesheet" href="gitbook/gitbook-plugin-fontsettings/website.css">
<link rel="stylesheet" href="gitbook/gitbook-plugin-search/search.css">
<link rel="stylesheet" href="gitbook/gitbook-plugin-highlight/website.css">
<meta name="HandheldFriendly" content="true"/>
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
<link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">
<link rel="next" href="development.html" />
<link rel="prev" href="advanced.html" />
</head>
<body>
<div class="book"
data-level="6"
data-chapter-title="Development"
data-filepath="development.md"
data-basepath="."
data-revision="Wed May 04 2016 21:23:42 GMT+0800 (CST)"
data-innerlanguage="">
<div class="book-summary">
<nav role="navigation">
<ul class="summary">
<li class="chapter " data-level="0" data-path="./">
<a href="./">
Getting Started with GORM
</a>
</li>
<li class="chapter " data-level="1" data-path="database.html">
<a href="database.html">
<b>1.</b>
Database
</a>
<ul class="articles">
<li class="chapter " data-level="1.1" data-path="database.html">
<a href="database.html#connecting-to-a-database">
<b>1.1.</b>
Database Connection
</a>
</li>
<li class="chapter " data-level="1.2" data-path="database.html">
<a href="database.html#migration">
<b>1.2.</b>
Migration
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="2" data-path="models.html">
<a href="models.html">
<b>2.</b>
Models
</a>
<ul class="articles">
<li class="chapter " data-level="2.1" data-path="models.html">
<a href="models.html#model-definition">
<b>2.1.</b>
Model Definition
</a>
</li>
<li class="chapter " data-level="2.2" data-path="models.html">
<a href="models.html#conventions">
<b>2.2.</b>
Conventions &amp; Overriding
</a>
</li>
<li class="chapter " data-level="2.3" data-path="associations.html">
<a href="associations.html">
<b>2.3.</b>
Associations
</a>
<ul class="articles">
<li class="chapter " data-level="2.3.1" data-path="associations.html">
<a href="associations.html#belongs-to">
<b>2.3.1.</b>
Belongs To
</a>
</li>
<li class="chapter " data-level="2.3.2" data-path="associations.html">
<a href="associations.html#has-one">
<b>2.3.2.</b>
Has One
</a>
</li>
<li class="chapter " data-level="2.3.3" data-path="associations.html">
<a href="associations.html#has-many">
<b>2.3.3.</b>
Has Many
</a>
</li>
<li class="chapter " data-level="2.3.4" data-path="associations.html">
<a href="associations.html#many-to-many">
<b>2.3.4.</b>
Many To Many
</a>
</li>
<li class="chapter " data-level="2.3.5" data-path="associations.html">
<a href="associations.html#polymorphism">
<b>2.3.5.</b>
Polymorphism
</a>
</li>
<li class="chapter " data-level="2.3.6" data-path="associations.html">
<a href="associations.html#association-mode">
<b>2.3.6.</b>
Association Mode
</a>
</li>
</ul>
</li>
</ul>
</li>
<li class="chapter " data-level="3" data-path="curd.html">
<a href="curd.html">
<b>3.</b>
CRUD: Reading and Writing Data
</a>
<ul class="articles">
<li class="chapter " data-level="3.1" data-path="curd.html">
<a href="curd.html#create">
<b>3.1.</b>
Create
</a>
</li>
<li class="chapter " data-level="3.2" data-path="curd.html">
<a href="curd.html#query">
<b>3.2.</b>
Query
</a>
</li>
<li class="chapter " data-level="3.3" data-path="curd.html">
<a href="curd.html#preloading-eager-loading">
<b>3.3.</b>
Preloading (Eager Loading)
</a>
</li>
<li class="chapter " data-level="3.4" data-path="curd.html">
<a href="curd.html#update">
<b>3.4.</b>
Update
</a>
</li>
<li class="chapter " data-level="3.5" data-path="curd.html">
<a href="curd.html#delete">
<b>3.5.</b>
Delete / Soft Delete
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="4" data-path="callbacks.html">
<a href="callbacks.html">
<b>4.</b>
Callbacks
</a>
</li>
<li class="chapter " data-level="5" data-path="advanced.html">
<a href="advanced.html">
<b>5.</b>
Advanced Usage
</a>
<ul class="articles">
<li class="chapter " data-level="5.1" data-path="advanced.html">
<a href="advanced.html#error-handling">
<b>5.1.</b>
Error Handling
</a>
</li>
<li class="chapter " data-level="5.2" data-path="advanced.html">
<a href="advanced.html#transactions">
<b>5.2.</b>
Transactions
</a>
</li>
<li class="chapter " data-level="5.3" data-path="advanced.html">
<a href="advanced.html#sql-builder">
<b>5.3.</b>
Raw SQL &amp; SQL Builder
</a>
</li>
<li class="chapter " data-level="5.4" data-path="advanced.html">
<a href="advanced.html#generic-database-interface-sqldb">
<b>5.4.</b>
Generic database interface sql.DB
</a>
</li>
<li class="chapter " data-level="5.5" data-path="advanced.html">
<a href="advanced.html#compose-primary-key">
<b>5.5.</b>
Composite Primary Key
</a>
</li>
<li class="chapter " data-level="5.6" data-path="advanced.html">
<a href="advanced.html#logger">
<b>5.6.</b>
Overriding Logger
</a>
</li>
</ul>
</li>
<li class="chapter active" data-level="6" data-path="development.html">
<a href="development.html">
<b>6.</b>
Development
</a>
<ul class="articles">
<li class="chapter " data-level="6.1" data-path="development.html">
<a href="development.html#architecture">
<b>6.1.</b>
Architecture
</a>
</li>
<li class="chapter " data-level="6.2" data-path="development.html">
<a href="development.html#write-plugins">
<b>6.2.</b>
Write Plugins
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="7" data-path="changelog.html">
<a href="changelog.html">
<b>7.</b>
Change Log
</a>
</li>
<li class="divider"></li>
</ul>
</nav>
</div>
<div class="book-body">
<div class="body-inner">
<div class="book-header" role="navigation">
<!-- Title -->
<h1>
<i class="fa fa-circle-o-notch fa-spin"></i>
<a href="." >Development</a>
</h1>
</div>
<div class="page-wrapper" tabindex="-1" role="main">
<div class="page-inner">
<section class="normal markdown-section">
<h1 id="development">Development</h1>
<!-- toc -->
<ul>
<li><a href="#architecture">Architecture</a></li>
<li><a href="#write-plugins">Write Plugins</a><ul>
<li><a href="#register-a-new-callback">Register a new callback</a></li>
<li><a href="#delete-an-existing-callback">Delete an existing callback</a></li>
<li><a href="#replace-an-existing-callback">Replace an existing callback</a></li>
<li><a href="#register-callback-orders">Register callback orders</a></li>
<li><a href="#pre-defined-callbacks">Pre-Defined Callbacks</a></li>
</ul>
</li>
</ul>
<!-- toc stop -->
<h2 id="architecture">Architecture</h2>
<p>Gorm use chainable API, <code>*gorm.DB</code> is the bridge of chains, for each chain API, it will create a new relation.</p>
<pre><code class="lang-go">db, err := gorm.Open(<span class="hljs-string">&quot;postgres&quot;</span>, <span class="hljs-string">&quot;user=gorm dbname=gorm sslmode=disable&quot;</span>)
<span class="hljs-comment">// create a new relation</span>
db = db.Where(<span class="hljs-string">&quot;name = ?&quot;</span>, <span class="hljs-string">&quot;jinzhu&quot;</span>)
<span class="hljs-comment">// filter even more</span>
<span class="hljs-keyword">if</span> SomeCondition {
db = db.Where(<span class="hljs-string">&quot;age = ?&quot;</span>, <span class="hljs-number">20</span>)
} <span class="hljs-keyword">else</span> {
db = db.Where(<span class="hljs-string">&quot;age = ?&quot;</span>, <span class="hljs-number">30</span>)
}
<span class="hljs-keyword">if</span> YetAnotherCondition {
db = db.Where(<span class="hljs-string">&quot;active = ?&quot;</span>, <span class="hljs-number">1</span>)
}
</code></pre>
<p>When we start to perform any operations, GROM will create a new <code>*gorm.Scope</code> instance based on current <code>*gorm.DB</code></p>
<pre><code class="lang-go"><span class="hljs-comment">// perform a querying operation</span>
db.First(&amp;user)
</code></pre>
<p>And based on current operation&apos;s type, it will call registered <code>creating</code>, <code>updating</code>, <code>querying</code>, <code>deleting</code> or <code>row_querying</code> callbacks to run the operation.</p>
<p>For above example, will call <code>querying</code> callbacks, refer <a href="callbacks.html#querying-an-object">Querying Callbacks</a></p>
<h2 id="write-plugins">Write Plugins</h2>
<p>GORM itself is powered by <code>Callbacks</code>, so you could fully customize GORM as you want</p>
<h3 id="register-a-new-callback">Register a new callback</h3>
<pre><code>func updateCreated(scope *Scope) {
if scope.HasColumn(&quot;Created&quot;) {
scope.SetColumn(&quot;Created&quot;, NowFunc())
}
}
db.Callback().Create().Register(&quot;update_created_at&quot;, updateCreated)
// register a callback for Create process
</code></pre><h3 id="delete-an-existing-callback">Delete an existing callback</h3>
<pre><code>db.Callback().Create().Remove(&quot;gorm:create&quot;)
// delete callback `gorm:create` from Create callbacks
</code></pre><h3 id="replace-an-existing-callback">Replace an existing callback</h3>
<pre><code>db.Callback().Create().Replace(&quot;gorm:create&quot;, newCreateFunction)
// replace callback `gorm:create` with new function `newCreateFunction` for Create process
</code></pre><h3 id="register-callback-orders">Register callback orders</h3>
<pre><code>db.Callback().Create().Before(&quot;gorm:create&quot;).Register(&quot;update_created_at&quot;, updateCreated)
db.Callback().Create().After(&quot;gorm:create&quot;).Register(&quot;update_created_at&quot;, updateCreated)
db.Callback().Query().After(&quot;gorm:query&quot;).Register(&quot;my_plugin:after_query&quot;, afterQuery)
db.Callback().Delete().After(&quot;gorm:delete&quot;).Register(&quot;my_plugin:after_delete&quot;, afterDelete)
db.Callback().Update().Before(&quot;gorm:update&quot;).Register(&quot;my_plugin:before_update&quot;, beforeUpdate)
db.Callback().Create().Before(&quot;gorm:create&quot;).After(&quot;gorm:before_create&quot;).Register(&quot;my_plugin:before_create&quot;, beforeCreate)
</code></pre><h3 id="predefined-callbacks">Pre-Defined Callbacks</h3>
<p>GORM has defiend callbacks to perform its CRUD operations, check them out before start write your plugins</p>
<ul>
<li><p><a href="https://github.com/jinzhu/gorm/blob/master/callback_create.go" target="_blank">Create callbacks</a></p>
</li>
<li><p><a href="https://github.com/jinzhu/gorm/blob/master/callback_update.go" target="_blank">Update callbacks</a></p>
</li>
<li><p><a href="https://github.com/jinzhu/gorm/blob/master/callback_query.go" target="_blank">Query callbacks</a></p>
</li>
<li><p><a href="https://github.com/jinzhu/gorm/blob/master/callback_delete.go" target="_blank">Delete callbacks</a></p>
</li>
<li><p>Row Query callbacks</p>
</li>
</ul>
<p>Row Query callbacks will be called when run <code>Row</code> or <code>Rows</code>, by default there is no registered callbacks for it, you could register a new one like:</p>
<pre><code class="lang-go"><span class="hljs-keyword">func</span> updateTableName(scope *gorm.Scope) {
scope.Search.Table(scope.TableName() + <span class="hljs-string">&quot;_draft&quot;</span>) <span class="hljs-comment">// append `_draft` to table name</span>
}
db.Callback().RowQuery().Register(<span class="hljs-string">&quot;publish:update_table_name&quot;</span>, updateTableName)
</code></pre>
<p>View <a href="https://godoc.org/github.com/jinzhu/gorm" target="_blank">https://godoc.org/github.com/jinzhu/gorm</a> to view all available API</p>
</section>
</div>
</div>
</div>
<a href="advanced.html" class="navigation navigation-prev " aria-label="Previous page: Overriding Logger">
<i class="fa fa-angle-left"></i>
</a>
<a href="development.html" class="navigation navigation-next " aria-label="Next page: Architecture">
<i class="fa fa-angle-right"></i>
</a>
</div>
</div>
<script src="gitbook/app.js"></script>
<script src="gitbook/gitbook-plugin-fontsettings/buttons.js"></script>
<script src="gitbook/gitbook-plugin-sharing/buttons.js"></script>
<script src="gitbook/gitbook-plugin-search/lunr.min.js"></script>
<script src="gitbook/gitbook-plugin-search/search.js"></script>
<script src="gitbook/gitbook-plugin-anker-enable/anker.js"></script>
<script src="gitbook/gitbook-plugin-edit-link/plugin.js"></script>
<script src="gitbook/gitbook-plugin-github/plugin.js"></script>
<script>
require(["gitbook"], function(gitbook) {
gitbook.start({"fontsettings":{"theme":"night","size":2,"family":"sans"},"toc":{"addClass":true,"className":"toc"},"github":{"url":"https://github.com/jinzhu/gorm"},"edit-link":{"base":"https://github.com/jinzhu/gorm/edit/gh-pages/documents/","label":"Edit This Page"},"anker-enable":{},"highlight":{},"search":{"maxIndexSize":1000000},"sharing":{"all":["facebook","google","instapaper","twitter","weibo"],"facebook":true,"google":false,"instapaper":false,"twitter":true,"vk":false,"weibo":false},"theme-default":{"styles":{"ebook":"styles/ebook.css","epub":"styles/epub.css","mobi":"styles/mobi.css","pdf":"styles/pdf.css","print":"styles/print.css","website":"styles/website.css"}}});
});
</script>
</body>
</html>