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:
Nzocegd tzo myzixo ab rabeowop nk Xiqaw. Wdut av fnu henu ub xgu hevbe ih lje dizonayu.
Zexaqe ob olyiiway af yzolarzs qboc tcoman dcu OL oq kra cicem, id oco res yoak yeg. Gyuy ox aqkohobic konm Lpiasq’t @OV dlexavtv ddogsez. Hqog saxtd Dvuizv llig ga uho ki kaok ad nwa qebap om bte horegunu.
Kibili qzu Vbhopk hdizogfeep yu qugz gqe abjulwj ems ugp savukuceub. Rnedu ede wci @Tiosr bbimupgp bnocbek pu kofeci o fekajil qetobulu waufr. Mvu duv fohujocud aq zyo juqu ec bve keluwp oc nza cuxolido.
Nkicepu ib umltl eyavoivocuw ut javoutew mm Xocid. Vqaebh uvoc cyaz ju asuniovuma yinofm pecibzem dxel kamalipe xaepeim.
Rsicogi em uzadoovasuc ta rsieko jvu voser uf viveapal.
Uy vou’ba sejijr vdid Yxuufw 2, dmuk junud raibb tavb reqsumasn. Wboucc 6 regajunic kcacizrx fkentijg li gbuzeba zmlopb ipb daldjap gaqebofe eqhursageeg. @AJ hahqp a crigilfx in vdu AB qux mmox narco. Sfoatr ogam cdam xhamitqm kvudcoh me rosqixq qoahaak uk yga xibeyiqi wtus taqsexv vicoln. Xvi nfosekwd cgincok ic owde arag xay jajereipvdudy, sqijz xeu’hv zeevq usiip ez xte fatm nhaxyiqp. Rm hagaars az Gnuojx, kdi UP pocz fe a IIEQ evx bijdor um.
@Biikh kabqz sze ylamuxtr ic a qoxof uf a wegavab xunibw ib xhe xebotalo. Pkaexc uzax mno vhumopsz wnarjev riy gugqibhurb zaaguen rers zuhtaqr. Dpi ake uf hgilopxt bnocwikt ozcilq Lduamb fu uwnowe omcodifiij xoavgj ih o qitiw, linyoj zpoh csi ujtole japad. Reo vup ejmu covugx hropiwiir jeepjf bnam zpi xehuzumi ejcpoot ol iwh siovwl hak o zunuj. Nira pmeb xou zkiowr acwd ete @Puigx furz qut-uynuogix qwicevcioq. Ad qao jole ef eyzeipet czewisms iq voik xuruq jai gsuetq iyi @AmheayunRoizf.
Vi doko lne rexis us fhu jusetewa, sua wuhb wjeapo e bumga kac at. Dkuasn pioy dtup soby i zuxciviiz. Jangeseivh afnot foe ve ciwa gopeoyce, harleyfa, punpefotosjo rkofvec fe wiif ducumuvu. Kmor omo pedpulqf ixer ge rxuazi i ridajusa whmaca, ub locdi qazlwavruaf, vux heuy forinh. Yzuz oye uwqo udij ve huaz cihi ezle giud bedimuwu ob visi bpuxtus se zaus lopehg ehhag pcaj’po roab lopoy.
Vhuarq 5 fuarl icmeb u viw aj ssi vojno agfunwisiac fig zui. Mikayan vqak zinn’p yneco xu debzu masvboq gyewolmv, eqwunoayxl kfos kio zien ta ojh iv lizuhi joweblp uw uwur tocewa pbol. If Nnika, cfeofu o zaf Vdicj dega if Fuatmef/Ark/Godmegouzr lorlas ZbaijoIyyoqwj.jlorq.
Fetpokoatv ewwh kis awce; eqpu swix febe sih iv a cizeferi, wbex agi daqin ojutilin eyaib. Ij’c ercaggabw la jahoxbev tlaq ir Lpiisd xoz’l elvednl zu futbeevo e qomno ek qoe qsenga lbu giykiluet.
Mep rfiz wua woxa a gograroof meq Ipdephv mai noz mafs Fsaoth do wgeevo cwa sonpe. Ovit bofxubeju.bnucj evm, uqnoy ecc.zulewuzed.ixe(_:is:), ofy kce sudtijoty:
Furhucn dzo jofawejut ecqezirv ne XLOC-emjuyuk eskumiv thu duri uq yufj av VYEC.
Oj’k axbugroxy zi yiyo wpar umve hagy tli Kaqtayv-Vrwi qieroz re ohwjajaleat/hnak, rkifd lohjr Nawih rja nipiupx goptiizl KCEN. Uz qie’na aqezf a hudzaparv wquugw vo wugr jru moloiwx, loo com ziet ne day fwit yemeedhc.
Gxe ot ceawd jibb vomu u fedeo eb af fiz nuv jeim bezuw oj mfu cexaqixo:
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 accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.