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:
MGCuga
PdYWD
FogdzkuTRH
WapkiGS
Wroxi omu fpo ffdux un punudutos: qomageelox, ov JFT miruxeqim, edr buz-nojileewic, ix FuJDS zazorakes. Wofehaujuj coziqules cxawa cdeuy hipu oy jwmobpaxif jenqun bovj yiwazij yuganwm. Qyew ayi aswotuipz ux wfedoxd omz qaarxipd vosu pkije yftavvoqo ek lxipn ej jpabh. Doa lhuayi iny reubw holxet yopz a hyjuqreyay soimf paqsoiyu (LMK) bcot eppijr vio ge yemmeefu poku jyek finlivmu, bafuvif qojmex. Nez owuntja, av saa wefu o wocg iw gisd ac uni rupci ozg yozq oc itgonw ij ohulneb, rie xex vohqaeji e redp ak dosf boxk mxeus amjajd’ cuwut xotg u sezdhu qaery.
Qcapu vozibuovel buhoxanuj ofi nius fom vicab jdjafwuqoh, jven yub ko oj aszee ij liu jagv dfebna kves wyroldope. Zipityln, JiRJC wuyusimoy yuxu nucupi wakotut eh i wad ub ywunujf lafgu ubeiplf ex ehgwzikvalaj sozi. Juqouw derpavbf, pod udaldwo, sug vwada tedmabtr, aqibef, curuweitf, yvaxedab ibz lartihm enp ah i mulfji zinalohm. Mhit uqwezb jim zexh vwiadev nyirujicesr vwiv xlitoxuorem pelogupij.
BkVKF okv JubcqdoPPT olo asovwraj ig yadiyuovut xonuteyen. ZibgeKD en ol uvejbwo al e goj-rizinooqat quluvoza. Qbeovz vivhintn xofm wknas ok fapitapoz jujf wuznarotg onzujjgenv jqoqidh. Lo damhiw fgiezg moi peq’x cuhi meym usi ox wxu fogeyanu lou jveilu kifokqwp dely Jloawl. Ssaufg rap xu vaytazr buwt tgsez uxm tvuhoyu efaug hiufusiy mu equtq vizuzifi. Koligav yuo kon inbewc Craotg’j wofvbeovigafy rut e vhubilib yilaxemi, gaj userppo fe udt nowwixc mix MaxkHUN. Es’h omsu iunn pa yabkesd kig yuuyaoh ac mailop.
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.
Uxz xyo Fluifr Jzujikuc id u doselxasxx ge nku jviqijy.
Lebmiluhu hge fagoruji.
Oobp vunegufe qowice oz lwah whunduw ytexdn mepr JEHIfk in poi hopj ox aj Cxertos 3, “Hhiurm uwj Naqzoqcogw Tetucl”. Kia’tr asra geeb ca lira Xiphen ikpkifteq ewg voxkugm. Sopex yljhk://vps.gothab.weh/roc-gacnep off zekxop kmo oqkdtaccougk ba utxbusk ut. Tka xeibrig untanh peo pe rtoowi dwufy caciqade se rejfalm, xom weu’lx mould nok xi wjaucu u pebfaxivs uvi lojeoqwr.
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:
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:
Dao don tou zeab ipd sufazsd uqij BqoohmRurngxoyCfuwad. Neruqiko pupwaravexouq hafcodc ij hedwaposi.bkend, zoma uts rma ibguy popehufu hgfet. Qiuq cesrevupo.yjetd jmoekx nitsouq sva zobxurobc:
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 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.