SQLite

Using the SQLite client for Deno

The third database system we will take a look at is SQLite. While PostgreSQL and MySQL provides a self-contained service that is created to support multiple concurrent connections, an SQLite database is simply stored on a binary file and is directly accessed by your code. This enables SQLite to be used for easy prototyping and is ideal for when database read/write is not heavily needed. This is not to say that SQLite is not used in production, but the use cases are different from the other database systems we have looked at; for example, in standalone applications where data needs to be stored, in a browser's local storage, or in cellphones.

There is currently an SQLite library for Deno written by dyedgreen. This library contains SQLite3 compiled to WebAssembly (WASM) and is the easiest one to start with.

There also exists an FFI implementation of the SQLite driver maintained by Deno Drivers which can be foundhere. We are not using this module in this tutorial as it requires you to have SQLite installed on your system. However, when using SQLite in production or performance is important, the FFI implementation is recommended over the WASM module.

You can find the library here.

Installation#

For this step, we need two things: an SQLite database and a Deno script.

As our SQLite library does not require us to install anything, we can simply import the module and start using it.

Writing the connection#

To get started, add a file called sqlite.ts, and import the module.

As SQLite simply reads a file, you don't need to establish any connection, but you will have to run the script with --allow-read and --allow-write due to the permissions in Deno to access the file. You can also run the database in memory, in which case you don't have to pass in a path to a file or add the flags.

By running this file, you should get an output, and exit with code 0.

 

This page is a preview of Build and deploy a REST API with Deno

Start a new discussion. All notification go to the author.