703 lines
19 KiB
HTML
703 lines
19 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="The most notable component of Gorm isgorm.DB, which hold database connection. It could be initialized like this:">
|
|
<meta name="generator" content="GitBook 3.0.0-pre.1">
|
|
|
|
|
|
|
|
|
|
<link rel="stylesheet" href="gitbook/style.css">
|
|
|
|
|
|
|
|
|
|
<link rel="stylesheet" href="gitbook/gitbook-plugin-prism/prism.css">
|
|
|
|
|
|
|
|
<link rel="stylesheet" href="gitbook/gitbook-plugin-search/search.css">
|
|
|
|
|
|
|
|
<link rel="stylesheet" href="gitbook/gitbook-plugin-fontsettings/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="Sun Feb 28 2016 16:13:20 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-defination">
|
|
|
|
|
|
<b>2.1.</b>
|
|
|
|
Model Defination
|
|
|
|
</a>
|
|
|
|
|
|
|
|
</li>
|
|
|
|
<li class="chapter " data-level="2.2" data-path="models.html">
|
|
|
|
<a href="models.html#conventions-overriding-conventions">
|
|
|
|
|
|
<b>2.2.</b>
|
|
|
|
Naming Conventions & 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">
|
|
|
|
|
|
<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 & SQL Builder
|
|
|
|
</a>
|
|
|
|
|
|
|
|
</li>
|
|
|
|
<li class="chapter " data-level="5.4" data-path="advanced.html">
|
|
|
|
<a href="advanced.html#compose-primary-key">
|
|
|
|
|
|
<b>5.4.</b>
|
|
|
|
Composite Primary Key
|
|
|
|
</a>
|
|
|
|
|
|
|
|
</li>
|
|
|
|
<li class="chapter " data-level="5.5" data-path="advanced.html">
|
|
|
|
<a href="advanced.html#logger">
|
|
|
|
|
|
<b>5.5.</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#callbacks">
|
|
|
|
|
|
<b>6.1.</b>
|
|
|
|
Write Plugins
|
|
|
|
</a>
|
|
|
|
|
|
|
|
</li>
|
|
|
|
|
|
</ul>
|
|
|
|
</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="gorm-development">Gorm Development</h1>
|
|
<!-- toc -->
|
|
<ul>
|
|
<li><a href="#architecture">Architecture</a></li>
|
|
<li><a href="#callbacks">Callbacks</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="#callback-api">Callback API</a></li>
|
|
</ul>
|
|
</li>
|
|
</ul>
|
|
<!-- toc stop -->
|
|
<h2 id="architecture">Architecture</h2>
|
|
<p>The most notable component of Gorm is<code>gorm.DB</code>, which hold database connection. It could be initialized like this:</p>
|
|
<pre><code>db, err := gorm.Open("postgres", "user=gorm dbname=gorm sslmode=disable")
|
|
</code></pre><p>Gorm has chainable API, <code>gorm.DB</code> is the bridge of chains, it save related information and pass it to the next chain.</p>
|
|
<p>Lets use below code to explain how it works:</p>
|
|
<pre><code>db.Where("name = ?", "jinzhu").Find(&users)
|
|
|
|
// equivalent code
|
|
newdb := db.Where("name =?", "jinzhu")
|
|
newdb.Find(&user)
|
|
</code></pre><p><code>newdb</code> is <code>db</code>'s clone, in addition, it contains search conditions from the <code>Where</code> method.
|
|
<code>Find</code> is a query method, it creates a <code>Scope</code> instance, and pass it as argument to query callbacks.</p>
|
|
<p>There are four kinds of callbacks corresponds to sql's CURD: create callbacks, update callbacks, query callbacks, delete callbacks.</p>
|
|
<h2 id="callbacks">Callbacks</h2>
|
|
<h3 id="register-a-new-callback">Register a new callback</h3>
|
|
<pre><code>func updateCreated(scope *Scope) {
|
|
if scope.HasColumn("Created") {
|
|
scope.SetColumn("Created", NowFunc())
|
|
}
|
|
}
|
|
|
|
db.Callback().Create().Register("update_created_at", 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("gorm:create")
|
|
// 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("gorm:create", 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("gorm:create").Register("update_created_at", updateCreated)
|
|
db.Callback().Create().After("gorm:create").Register("update_created_at", updateCreated)
|
|
db.Callback().Query().After("gorm:query").Register("my_plugin:after_query", afterQuery)
|
|
db.Callback().Delete().After("gorm:delete").Register("my_plugin:after_delete", afterDelete)
|
|
db.Callback().Update().Before("gorm:update").Register("my_plugin:before_update", beforeUpdate)
|
|
db.Callback().Create().Before("gorm:create").After("gorm:before_create").Register("my_plugin:before_create", beforeCreate)
|
|
</code></pre><h3 id="callback-api">Callback API</h3>
|
|
<p>Gorm is powered by callbacks, so you could refer below links to learn how to write callbacks</p>
|
|
<p><a href="https://github.com/jinzhu/gorm/blob/master/callback_create.go" target="_blank">Create callbacks</a></p>
|
|
<p><a href="https://github.com/jinzhu/gorm/blob/master/callback_update.go" target="_blank">Update callbacks</a></p>
|
|
<p><a href="https://github.com/jinzhu/gorm/blob/master/callback_query.go" target="_blank">Query callbacks</a></p>
|
|
<p><a href="https://github.com/jinzhu/gorm/blob/master/callback_delete.go" target="_blank">Delete callbacks</a></p>
|
|
<p>View <a href="https://github.com/jinzhu/gorm/blob/master/scope.go" target="_blank">https://github.com/jinzhu/gorm/blob/master/scope.go</a> for 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: Write Plugins">
|
|
<i class="fa fa-angle-right"></i>
|
|
</a>
|
|
|
|
|
|
|
|
</div>
|
|
</div>
|
|
|
|
|
|
<script src="gitbook/app.js"></script>
|
|
|
|
|
|
<script src="gitbook/gitbook-plugin-github/plugin.js"></script>
|
|
|
|
|
|
|
|
<script src="gitbook/gitbook-plugin-edit-link/plugin.js"></script>
|
|
|
|
|
|
|
|
<script src="gitbook/gitbook-plugin-anker-enable/anker.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-sharing/buttons.js"></script>
|
|
|
|
|
|
|
|
<script src="gitbook/gitbook-plugin-fontsettings/buttons.js"></script>
|
|
|
|
|
|
<script>
|
|
require(["gitbook"], function(gitbook) {
|
|
gitbook.start({"fontsettings":{"theme":"night","family":"sans","size":4},"toc":{"addClass":true,"className":"toc"},"github":{"url":"https://github.com/jinzhu/gorm"},"edit-link":{"base":"https://github.com/jinzhu/gorm/edit/master","label":"Edit This Page"},"prism":{},"anker-enable":{},"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>
|