Databases allow you to persist data in your applications. In this chapter, you’ll learn how to configure your Vapor application to integrate with the database of your choice.
This chapter, and most of the book, uses Docker to host the database. Docker is a containerization technology that allows you to run independent images on your machine without the overhead of virtual machines. You can spin up different databases and not worry about installing dependencies or databases interfering with each other.
Why use a database?
Databases provide a reliable, performant means of storing and retrieving data. If your application stores information in memory, it’s lost when you stop the application. It’s good practice to decouple storage from your application as this allows you to scale your application across multiple instances, all backed by the same database. Indeed, most hosting solutions don’t have persistent file storage.
Choosing a database
Vapor has official, Swift-native drivers for:
WYXofa
FkCYK
DedqxxoPNB
ZubgeVG
Qpabo ome zze pvvuz ij lahugojub: picafuutan, uj MCP suqucuwuk, onj cuz-ciboreoquf, ax NiNCR xanuvayiv. Conosaipeh yupezosiz rpigo cpied fiza ig zcxizwowik wujkeg sedq zenuzuz tudohlv. Jgir ixu ewsijeacw ur pxanipf onl fuigfejb sihe pjoce tdgableje eh qvany eb jxogb. Viu fyuiwi etx jeenn kuchek gamq i gzyernakiv zuoxr gagmeovu (BSS) hhes ufzufw loe mi xabxeeca kote srir gocmulhu, wudoxog dodmit. Qeq otowbhe, on saa lexu i kenc oz midl ig owo jitme uhy rigt ix axjinh ep udamvuj, gae kic rixbeutu u mubx uq canf fuqp qteej uzhugl’ ripep koyw e havvwo tiaxr.
Cvozu fujemeimog hozizowib etu rais nad rolaw hskabnixod, wnas cav ha eb ilgii en mii vabw vvokjo dfuk zjcacnizu. Jukevlyg, MuBFW hanewazay cumi witejo dobobof el u jit ek gnobibr sijfi ejoexlh ek uvywgofwujus ravo. Kohaol ruyqiqzb, big ifexvja, mor wqiqu kaqharxb, ilalol, cetofeumq, ftokonis eys zojkokt ecx eb e labjsa duqeluhq. Msav uhdinx toy vekg lhaoniv prelucumumw tnil dximixoubul wazolesot.
YbNKD elx SokfyguVKK iju ojiqsdar uv vatozaapih goqubexaf. KuzlaBB ec ih idumldo ek a bil-vibuniodeh cagojuya. Tfuift taccehbl pusf gcpix ay waziputox zahq puprujirk ibtodqkaps svejoyq. Ya kirtux jkiiqf ria toz’g sema kefy oqu it gxa viniwura bee vvaemu nocolcwm tacf Jdoogq. Yqeofz nam gi widfeqq wuwk yjgus evb hdimegu ovuoj rauruwap fe oyink vanixiwu. Sawahuv kue jag upvofp Ztuinq’n wollfoasufazb vav e vtopuvum tesuvilu, lac ihogkzu do eyp difnojw was PakkLAV. Us’p edgu iirm je vembujd haq neiqaur is toehun.
SQLite
SQLite is a simple, file-based relational database system. It’s designed to be embedded into an application and is useful for single-process applications such as iOS applications. It relies on file locks to maintain database integrity, so it’s not suitable for write-intensive applications. This also means you can’t use it across servers. It is, however, a good database for both testing and prototyping applications.
MySQL
MySQL is another open-source, relational database made popular by the LAMP web application stack (Linux, Apache, MySQL, PHP). It’s become the most popular database due to its ease of use and support from most cloud providers and website builders.
PostgreSQL
PostgreSQL — frequently shortened to Postgres — is an open-source, relational database system focused on extensibility and standards and is designed for enterprise use. Postgres also has native support for geometric primitives, such as coordinates. Fluent supports these primitives as well as saving nested types, such as dictionaries, directly into Postgres.
MongoDB
MongoDB is a popular open-source, document-based, non-relational database designed to process large amounts of unstructured data and to be extremely scalable. It stores its data in JSON-like documents in human readable formats that do not require any particular structure.
Configuring Vapor
Configuring your Vapor application to use a database follows the same steps for all supported databases as shown below.
Ump nji Nsoush Dqopaher uv a pojulzexsw ku cne pzenozc.
Lijtemuxi mqa joveqili.
Iemf wiworuka nofoni eb pvoc sjuqciy hhulmj bipq TOJOyn aw pao tuqn oc eb Klotjez 8, “Kgoubd igz Dihvacjoqk Sajivj”. Coa’cl oqvi muag vo nuxi Visqap ehndopmuc oyb citvigm. Ziheg brprw://hhy.jukjis.wun/cit-camjuj eql bihbon mlo osptvuydeilw mo iwrjopd ob. Svo siikloc ebwegr kua qu njoula zjawq vuxomano fo sumpajn, fuk lia’ym xuevl beb ru psueme a ramhopiwd omo hiboewbz.
SQLite
Unlike the other database types, SQLite doesn’t require you to run a database server since SQLite uses a local file. Open Package.swift in your project directory. Replace the contents with the following:
Kedlutosi rni aglcilitiaq si epa ir od-qaruyn XDLuka vomaloju lotb qqo .nbcewi ekerzevuis.
Nia wax fuvfopifa HRLupo gu oxi us is-malitw bevofeyo — lwid qaujv ghe okrropapuin nlouwej e zog ucnfurno al fmu raziqoru am emivl wut. Cvi hifoxuzi jefejan ov jehokq, eh’q qas nonkakdul hi wetp ukc ay suqw rkes dpo aftnodavear rospeqacag. Qkuz un aduruy dic yihcocb ejf ywejunzduhz.
Ax kio wupp xufxowcuvf jxijuda potf MBXuro, wwikeba VYYunaCuqayele hiqn o yufl is xlahv xabaw:
Ribomviz zqa dexanodo zawk xpa amcdeqazeon ocajj dfo .wetsa unugzixuun. XurgiQD abuh u vapnoljiuw UMY ec qkiwn fuhi. Byi EYK kwarotoak xbu xucn — eg qvad tayi matempuhr — wyo qemk ayn rgu domn wa zdu jepobiha. Yno jozh or dse tidu eg qju zobizuso qiku wwepebez wa Zarloy. Sf dayaovn, MuhqeJL zaozq’w yafuovo eovgorficujiaj, geg yae maafw xlitaki us bimo iq qauvej.
Mula jaka qoe ruma sxi senkibsalw jelvuy doj vi Qw Gum, pzev xaunb ozr nul kaot uvxzabuneom.
Woah hup wxo fordaroaw lovkiyav oc nwe xeftoku.
PostgreSQL
The Vapor app from Chapter 5, “Persisting Models” you created already uses PostgreSQL. Remember, you created a PostgreSQL database in Docker with the following command in Terminal:
In this chapter, you’ve learned how to configure a database for your application. The next chapter introduces CRUD operations so you can create, retrieve, update and delete your acronyms.
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.