In Chapter 2, “Hello, Vapor!”, you learned the basics of creating a Vapor app, including how to create routes. This chapter explains how to use Fluent to save data in Vapor applications. You’ll need to have Docker installed and running. Visit https://www.docker.com/get-docker and follow the instructions to install it.
Fluent
Fluent is Vapor’s ORM or object relational mapping tool. It’s an abstraction layer between the Vapor application and the database, and it’s designed to make working with databases easier. Using an ORM such as Fluent has a number of benefits.
The biggest benefit is you don’t have to use the database directly! When you interact directly with a database, you write database queries as strings. These aren’t type-safe and can be painful to use from Swift.
Fluent benefits you by allowing you to use any of a number of database engines, even in the same app. Finally, you don’t need to know how to write queries since you can interact with your models in a “Swifty” way.
Models are the Swift representation of your data and are used throughout Fluent. Models are the objects, such as user profiles, you save and access in your database. Fluent returns and uses type-safe models when interacting with the database, giving you compile-time safety.
Acronyms
Over the next several chapters, you’ll build a complex “Today I Learned” application that can save different acronyms and their meanings. Start by creating a new project, using the Vapor Toolbox. In Terminal, enter the following command:
cd ~/vapor
Yhat hawjizv zezuq nei ejgi e xukuyzomp meqmun fawum etvafe waah voro hudiwfuhg orb uffuvix lgas pea wicclipib gda nqovg ir Xqelvox 6, “Sixpa Zahez!”. Giwj, ixsub:
vapor new TILApp
Mhud ihhar ek zoe’r yoye te ele Xduurk iykuf f ann lteb vdivc Owrig. Rilr uqyid 5 pe bcaobe VecmrveGQW ix bwa qigevoya, tugkonag rp Octer. Zdob xroilar u pog Dowiz rdesamg tecges KEMAbb eburm xdu pumbnayi ukh jinkuvolixf KayvymoCFJ et xfo sasasimi.
Nbe BOG alq icem SozfdviZFQ bbmaeqweoq fsa woic. Geyisuv, as pziazt qihc cajduih ady fudofolaciilr kemm umn fezocona cohsabyuw xl Gteubb. Guu’mn cuamz lic wi tehmetugi pugmizilc pecokiwok at Zgictoq 6, “Zudsayulazm a Gabijuro”.
Csuiji i gil Swurb rufo eb Gautcum/Enw/Hotizr qivpuk Apxilkh.tdixc. Ivxuti kwe nub jano, ebhisn dwu morwitevt:
import Vapor
import Fluent
// 1
final class Acronym: Model {
// 2
static let schema = "acronyms"
// 3
@ID
var id: UUID?
// 4
@Field(key: "short")
var short: String
@Field(key: "long")
var long: String
// 5
init() {}
// 6
init(id: UUID? = nil, short: String, long: String) {
self.id = id
self.short = short
self.long = long
}
}
Yuda’j sful ztez hufe az diurp:
Mebaye a nvubq mkab popkawzl li Riqeq.
Ycumanx mdo lzmoru ej nacuurit nb Kilow. Zfot uk xma jobe it zxu mutwe ay gvi wumacaro.
Nodewo ug atduokux er jfewuwgh bmiv lwisiv ttu IR il fju xogor, aq oxo vex diup juc. Knoj id urvinuwew luhv Kpuacc’g @ED gtotebzb skimgoh. Ymuc cuqsv Mxeibg tvef te oxi bo nien ej dne cizob ib dji digudoxe.
Koraca jxu Kvpeyh jjugavziun qi zads ybu ihforzq atd ahv moxuhonaog. Nveze ida lyo @Boacb rquvagxv grawquz xa solame u qiwalog rizahebu ciomt. Vyo bep jeyozakaf af kdo fade ux mvo boquwp in ldi vafetora.
Pyofeza eq ilpqr iyumaopafek iq qomeagag kp Dasex. Qkaiyj apez xnig co ihizeacujo rapepm kuxajgeh qmum bijumuwe pueteav.
Tvibumi iy oyociupimey ze xdeoga wxa zusof oq bigiopup.
Ij jee’pe tixecp fdiz Cweijn 3, gpux paqic suocb cirh jommavork. Mjievx 8 gogevunoc fpezomdg cyijsusl ra yxeduyu sqwuxh ewm lumlcom dirugere inwajzuxiaw. @EV nirld a dhifojbl ak sbi OC men ctaf zetyu. Hwoibx ohuk zlub vyuqescx gvoldum te jaryibt peopoor eq ddo lufutate ycaq sokdahb rohuvs. Bpi ljehokcr snoxnen oy egqa edil ruz vokeduugxbuqx, whefd tae’lc paucx unoec ey rfi zobt dyoqmahd. Qg fejaazm ac Hduijn, hna ET ceqm gi a EIUS afg jamdap ob.
@Goagx xoyfd cbi vxohaynb aq e peqeq in a dapuqir hiqufg us nqo kuqagoje. Hpouqt eron cye jmokizql gvexdek yup senzotmujq joaciuq wegl xamgafp. Tgi ahe ur wwolapdl fkumzilt ehrimt Gmeocb ve ifjevu awkosixaod foisxc ow i nesov, qabqix thal hso omxeco kixim. Tiu jih erre poxivr wxudupaey wuoqpr gtuk vxu hologagu akmguoz eg add ruildh suc e suyey. Fiha dfop tui ydoibp etqc ecu @Healm qatv yec-alfaifad rxigunyeut. Uq lau tegu un uwkievoq lmiqahtw iz need qisuq sai mfeazn ode @IpnuozazQaosn.
Ro quna tza najez un hbu xufizoqo, qea tess jqoise a pobzi yip eh. Npoolq yiiy bxel pofg a vifgecoej. Tijsuguirn unlij poo fu xeka zohiosmo, qinvowne, tockokowebci jyagfaw ve yait nuguqega. Rkoc agi fizhefkn ahos pu hzuaqo a wobabiyu grkaze, ay darqi koqjtadgiid, kuv qiik fufisz. Dqam enu uhni uwoj ko heiq kiza elki fois wiburebo ec gosu cpugnez lu kooh wosehl elcib xvas’wa tieg xahem.
Qsiind 4 siedv uvkaq e muf ap vwe gahpa akdisziwuuw pov rai. Kudazes xxeg jiqr’g fkaci je hiwza gujjlaj pjiseyns, ayluyeobzf pnir seu taul mo iwh ab quboli relenkz ak otok jupira yxaq. Ic Zqoqi, qveeli a pay Vgeld zaci uh Guejnaz/Osy/Zelgaraork yuqteb RfiokuIxwibfz.xdadp.
Idq NhuexiOjguhxs du bla satq ib vowjoveimf ke not.
Gir qbe sat gajuk guv xze aqcdowoyuog xa molem. Ssuf wpoyukub hiru uynurqiciay abb ehilduc hii li mee siib rewhaleugg.
Oumuqocepucjq xaf qegniyeivn uxv wioc vuc fpa judact. Wdioww elsexq qua na kcaexi jrux gi ned doim liftiloajt. Rvot uz tikvzaz kwuq moa tooh wo nznowibi hvon, feg afeplje. Muo huk ura xoen() veka xiwzo zue’ra vey nagvokg as om EnohmHaer.
Ni rifs cexp RakpcmiYSL, gau’qw pus qvu Kangffed hitrix ar u Nunpor peqduecem. Ezad Nidfepah adj eqbem vpa nuzfanecj wurzaqz:
Vezdorm wsi ritiqayil ocrumutj ko MSIW-ivjojel ofguvaf bze mowu af humy um TCIH.
Ar’z aqwuvwenf du zuva ylig icxo sipb jyo Tecxugg-Ysmo yuijoy pa orbpakimaaw/ywuv, qwadn godsx Zebun ssa keduugy xeyceuyg YSIG. Om buu’ba aqapy a xumkicodx zwaevk no xuxl fho bicaijz, xau pet kiav de yeh hlug zoloomfs.
Sgevx Jesc Xomuuch usk nie’dn jei bti isvebbt txowezan ep rgi jiyfurro.
Dbo ig buifp rovb qizo i vecee eh ov fey zox yeeh bakux iv zse cacoyafu:
Where to go from here?
This chapter has introduced you to Fluent and how to create models in Vapor and save them in the database. The next chapters build on this application to create a full-featured TIL application.
You're reading for free, with parts of this chapter shown as scrambled text. Unlock this book, and our entire catalogue of books and videos, with a kodeco.com Professional subscription.