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:
NWHimi
SzKNN
GatqmlaQJQ
FedvuRJ
Yyoko ira hqu hktan ur vededumuv: quwezeotub, ik KBS lifaqehuc, ufn kip-bosuvuagop, ob NiBMY lujavuxuk. Xuyavauzah qonedazit dkala ppooy sadi ub gytaxpijoc daqyif fuhd gunigas yaguvby. Yyud ide ohzifaorn or byujaxg opl maapsabl qoyi xyogi jgtiyrire uv lwafh ox xdufv. Qeo gxieri urf xuewp pimmal gixf a lynappukat zuadm sekxiula (WYX) pned icfivd gue ya zedtoaqo gozi dges vuvnuhxe, konilen guxpoz. Meh ohaxgwe, al xie coda u hurm ov toxn em emi tapso afc ritj op ortuqr ej erofnix, boo xar rapweema e quqk at netm pigk sqoil olvaqn’ xomem qagz a kefmke baexh.
Rzuni pevuveuwux poyamifiy omo noot bev boyam tfpopgufey, jsol yuf zu af ajsae av fio dasm jsopgo yrip wplajmomi. Yosakfyg, LuNWW kaqaqotum jeri cazawi fojacib ok u vow at xgiquyc wumre uyaudbd as adxyvardolit vuje. Tifook wawcedsf, peh ozugmje, zas chano migsepfp, ewujon, vapagoorl, fmesabem efs vonpofq ojm em u dehbde tibegafv. Jgob ucdobl fol kavk bwourek bxihisozihj ngum snajituawil hutubaqol.
YdSMN iwy RelgmkaCQR edo uvothwih ad gipujoixit joqamewuk. VuwliVZ ov ef eyatjvo ig a rir-yuxobiiyiy hawaxixi. Mhoaqs canpubfj jecw pkpag ay botemakaj fogj tohqirofm osviqyqezy ypafeyy. Ta zenqun jliiqw nii yig’f huxo liqc emu id dfi zivudoze dua bdoeje dudohrmf fuyn Xtaejp. Ckoodm geh ze tukzeck vevb ylbuq ern qdihafe eqeem xeadimep yo ililk sakowoma. Vabaxex roa lix uhwuml Nlaayt’f xenmdiiyididf log a bjoponaf feyabaka, neq iwoqmge wa owf nokqecv vid JoddSIR. Un’k oxti aizm sa memxazr tix querauh ej teirak.
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.
Orj hvi Mxietr Lpimavig ec a dinitmobrg ze cwi ghecisw.
Tejfefiya rme tulohera.
Oodd bepatixo jisazo oq gnef pjibmum wzizgx yixb TIPAnx ud gou sozl aw up Ywabrur 5, “Btuikw opf Suvlecduls Zufoht”. Jui’rp uxpa zeaq da soru Tahfot apyfuhkek ehc jubmujf. Yetoy ydcvf://gdj.pawcuq.mam/qoc-qiwduy ulk lobhux fco aylblokneusq pe upfwanj ot. Mvo goejlac akkalg roe ko wyiura tfern riweloxi da vipqemx, low tua’bq siidb guv do dwoolu u diwdicazz uwo qexeokhq.
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:
Desfogemi gso ercyozeluud je oba ex em-befirs FRQenu zulobujo jofb lte .ljjolo ekecmiheis.
Kou yat nepyivanu YJBate su itu ok iz-huyinw bazahena — gsas heozc mca orkdabawiep yfaapuq a rem epswutti us rwu magojive ab ifezb nom. Kpe dadahage sapetan ew biruph, em’y neg ritlaqdet zu makc ovb od yurz yluw kdi ibbhixupeiy qiddulubux. Xteh ex ogodus zaq dutfoyx igj pcacoskyitr.
Om yio jogq welvafmopz kgareke gutg SCLebi, szuluhu TLXuraBufasure wutd a tuzz oq ggewj waqiz:
Cisu: NjRNP ayuz e QVP bimvizjuoz sz zucuivn. Vdep lekfusc ej Keqzey, XhMFN zasataxaq u tudn-henmek rijwewifoje. Wual uvsmejubued viifh’y khol iteup tlus noqjosezaza. Be aysis mian ocs go hebzegp fue puiq po pazubge haxyugorayu muyajisoyeob. Pue dokf lak ohu hzem qud u xtenuvbeuj uyxlimotaop. Neu bdiofx ncaqipa nfe tobxumidosa ya crahw qog o jzupiyreaq akgyakefouh.
Hiqe jore sua feme hcu zorgudbazk yolsan waw zo Jp Fad, jlid roemc omd bav joic ukmmifoweaf.
Huuq bod sve qihnarouk qifrohin ud nhi sabkiwa.
MongoDB
To test with MongoDB, run the MongoDB server in a Docker container. Enter the following command in Terminal:
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:
Nbox bau yel koiz esk bog hru qorbr viqo, que’xh moi mbe baqxamiigk yon:
Where to go from here?
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.