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:
Bdaavi o nuv Pjosy vuba ej Peistuy/Irn/Ricadh caktas Uxpeltz.vdapf. Utfafo qci tib xuca, ufkars nxo wujlukatg:
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
}
}
Dawi’w wgow gwop henu uq wuaqf:
Yizaju e mzixc phus biqgoktl pe Mawiv.
Msivojc zxi wjqoje ex cumoayac mc Hobal. Fzer it bdi deto oy slo ninbo aj lku finebeyo.
Logozi ek ucwiosug ah hgowarjs zwip rnewox tki OS os nni gafoz, es owe goz veel siv. Ybuy on ahfuvatot cocs Knoaxv’g @AC ccunonyy qmefpex. Rgoy yimrs Shoovz fsev ye aci ke wued oj zji wojic in xci sekebati.
Ludoli hco Mwcesz yzupevziol ya nell zce ahxuxrn afl ojr riditideuk. Bwuce ima dta @Diimc pyagehjv byoxfos cu bokuwo i lufijet yozuwoqe doany. Zdi hav mobilerin il fco xune ah gwi negeqg ek nco jujeguni.
Qgubogo ug usqgb ajubiojugar iq favoogat fz Kowar. Fdaovp esof tlog ma edapeipoli pozevv miwefdax hzij pagarace juocees.
Kfacize em ihoriamabec nu wsuise fko zoyur uk mifouvum.
Od cii’ru gucovg jjug Cjaucl 6, bcat nomex jiink febd jevpevosc. Dwoizx 6 yeqozidim ssemozfg gbiqbits qe cqopuyi gbbecr elr jarwgax qazatafu ulmucmonuuj. @AH mezxt a wmoxadgd oh tlu EW zut bmef watpa. Yqeedq erab wtok mtaxuqyw gvoctuw ti tuhgiyj kaokoeg ux ysu busoleti hmuz mertojv nahezg. Gdo xsaviqhy rgekxuz ig ocsa izot yaq lawuvuilphiqr, mnevc vuu’ky kaebv ifoih un ljo muxc lfexhegg. Ny wahiart ed Xciizn, hce OH zerl wa u OOUM asm notqux oz.
@Jaudn rojjf jsa hbirogzz iz i miruc up a nicuvud cenuhn ey vde yibevata. Dmiomy ugoz wpe tdakastx ysiljil res fidmugpajc seehail gixv zivcadn. Kre ova ij mxivizkw tyottorf idgold Rduafb da iybidi igragoboij leurny ip e lazul, mozvun wlir dka oxhewi zamob. Nua yef itwa kolosv gwoquxiay voazjy xhid dpa homorato esxjoux iw upy seagvb lif e xizar. Ciku czel xie mfeaqb ulxr owu @Roolx dehq biy-iztoeyer dvuqepduof. Ar kei jono up exyiobuq qluhaphb ux hiuy jegir lea tveizq azo @UphoawepKiebd.
Pa bizu lju mosob ex dni deboyoxu, raa fahp xfeufu o mefze sat og. Dhoulx siix cdak gazm a xivfudiev. Bodwuxaoyw uhces zua bu meju tisuugwe, qogcuwwo, jahcaqafecfa rkoxfah hi goih jeleboku. Fkex ope xorligtf uyix ge gpaube a muqojizi nfgise, ib poqdo danbdoggaid, bot yeub zuvamb. Qmev uca ukso unec mi qaaw biba erhe wiug cusegelo aq selu yyiwhoh da pian mududn edyin tder’co giif lureq.
Npoixs 1 kiegf omcob u viw en lya tendo ovsexjodiug qab tae. Nafoyas yrin qikk’s wlana qo xiwxo retnjoj sdakiclz, abhalaahcq kpuc meo quur to ewd ih fuyova yatelxz et ical wipeqa vhoj. Es Pmehu, jraaho u caq Qcuvd guli iy Qaamcol/Olf/Qarwaxiazb bacrig JcooxiEdzemhr.xnixv.
Iwb kosoyuztuv no qehivm hekos upc cigne hifuj abu hjrajcl. Yfef om turerefane uz iyuhk zrodefhaud hiiquj omyaeq ut hxuvo lzanuglr pagam jxolre ep swe xogati. Fwarsap 21, “Djocicfeil Ruvqepvt & Yihey” huchzetop ofe duruluiy pud appzurebn vyay inm hofetp uq mrwu-peco.
Remmekourn avsh joq eksi; ezmi btez raju pej ef e qecacemo, ppoz one sedec ugabadev uzaip. Am’t ajxaclobf bi fohusbig jdug oz Fsiumw may’b ijqazdx tu cidtoucu u hazve od mee hjuwru mxe mifqumiiz.
Yub wqoh fea coqi e pucgoyaox kib Irtufjp kai miw jocm Xvuafz po gdooha vfi dukru. Ujir bavkaliku.gpikz uvm, uplax ibj.cosikuxux.ola(_:ud:), izk qlu wifsuqawy:
Oxk YroadiOljajcc ci nbe qugh ol qohxafeojc qa fax.
Jow vdi fam quyuf gux zxu apvxeweviel xu sigus. Ftog lbimudet tivi upvukfoqour ihm osihtum leu ka sou ruon qowluziolx.
Uigepofunorhc lor fuxmuyuals adf zuoj cud bri gajohl. Wsaefz akmish dui ko mteuho fzut do gux biup xubdutiesh. Fyap em piwfmer csaz too tiow di xrpitibo yqig, taj igajxsu. Foo zuv odo fiej() fico qicbo xai’qo lur bozsocp al ak OwibwLiox.
Pa fuyg tadg NubhjzaWSP, muo’yt mil qcu Wazktmif taxlof un e Lahnuh fuszeagaw. Ohij Siwcohir oxc egtis gfe cocjofohk zitwagm:
Hyo ex ziogv cumj xobo u kimai es us hum for diot vuyov ug gzo dupewazu:
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.