"use strict";(self.webpackChunkgoffee_site=self.webpackChunkgoffee_site||[]).push([[6248],{5500:(e,t,n)=>{n.r(t),n.d(t,{assets:()=>d,contentTitle:()=>a,default:()=>m,frontMatter:()=>s,metadata:()=>i,toc:()=>c});var o=n(4848),r=n(8453);const s={title:"Models"},a=void 0,i={id:"database-models",title:"Models",description:"Models are located in the directory models",source:"@site/docs/database-models.md",sourceDirName:".",slug:"/database-models",permalink:"/goffee/docs/database-models",draft:!1,unlisted:!1,tags:[],version:"current",frontMatter:{title:"Models"},sidebar:"docsSidebar",previous:{title:"Database Configuration",permalink:"/goffee/docs/database-config"},next:{title:"CRUD Operations",permalink:"/goffee/docs/database-crud"}},d={},c=[{value:"Database auto-migration",id:"database-auto-migration",level:4}];function l(e){const t={a:"a",code:"code",h4:"h4",p:"p",pre:"pre",...(0,r.R)(),...e.components};return(0,o.jsxs)(o.Fragment,{children:[(0,o.jsxs)(t.p,{children:["Models are located in the directory ",(0,o.jsx)(t.code,{children:"models"}),"\nBelow an example of a ",(0,o.jsx)(t.code,{children:"users"})," model"]}),"\n",(0,o.jsx)(t.pre,{children:(0,o.jsx)(t.code,{className:"language-go",metastring:'title="models/user.go"',children:'package models\n\nimport "gorm.io/gorm"\n\ntype User struct {\n    gorm.Model\n    Name     string\n    Email    string\n    Password string\n}\n\n// Override the table name\nfunc (User) TableName() string {\n    return "users"\n}\n'})}),"\n",(0,o.jsx)(t.h4,{id:"database-auto-migration",children:"Database auto-migration"}),"\n",(0,o.jsxs)(t.p,{children:["After creating your models you need to add them to the ",(0,o.jsx)(t.code,{children:"runAutoMigrations()"})," function which located in the file ",(0,o.jsx)(t.code,{children:"run-auto-migrations.go"})," in the root directory\nHere is how:"]}),"\n",(0,o.jsx)(t.pre,{children:(0,o.jsx)(t.code,{className:"language-go",children:'package main\n\nimport (\n    "git.smarteching.com/goffee/core"\n    "git.smarteching.com/goffee/[my-project]/models"\n)\n\nfunc runAutoMigrations() {\n    db := core.ResolveGorm()\n    //##############################\n    //# Models auto migration  #####\n    //##############################\n\n    // Add models to be auto-migrated here...\n    db.AutoMigrate(&models.User{})\n}\n'})}),"\n",(0,o.jsxs)(t.p,{children:["For more information about the models check ",(0,o.jsx)(t.a,{href:"https://gorm.io/docs/models.html",children:"GORM Models Docs"})]})]})}function m(e={}){const{wrapper:t}={...(0,r.R)(),...e.components};return t?(0,o.jsx)(t,{...e,children:(0,o.jsx)(l,{...e})}):l(e)}},8453:(e,t,n)=>{n.d(t,{R:()=>a,x:()=>i});var o=n(6540);const r={},s=o.createContext(r);function a(e){const t=o.useContext(s);return o.useMemo((function(){return"function"==typeof e?e(t):{...t,...e}}),[t,e])}function i(e){let t;return t=e.disableParentContext?"function"==typeof e.components?e.components(r):e.components||r:a(e.components),o.createElement(s.Provider,{value:t},e.children)}}}]);