This chapter covers
- Interacting with SQL databases
- Understanding the driver pattern
- Integration-testing external services
- Using the consumer-driven interface approach
In earlier chapters, we built a link service for Bite. Now another team needs persistent storage so that links survive HTTP server restarts. We asked them to implement it and offered help with integration. This chapter shows how the team added persistence to the project.
We’ll start with the standard library’s sql
package and use SQLite because it’s lightweight and needs no separate installation. The concepts apply equally to other SQL databases, such as PostgreSQL, because the sql
package abstracts database interactions.
We’ll add a new Shortener
service backed by SQLite and verify it using a test database. Finally, we’ll decouple HTTP handlers from this service using consumer-driven interfaces, an approach that requires a different mindset than traditional object-oriented languages. By the end of the chapter, we’ll know how to work with SQL databases using the sql
package and use interfaces effectively to decouple implementations from the code that uses them.