bun:sqlite built-in module.
Let’s get started by creating a fresh project with
bun init and installing Drizzle.
terminal
Then we’ll connect to a SQLite database using the
bun:sqlite module and create the Drizzle database instance.
To see the database in action, add these lines to
index.ts.
Then run
index.ts with Bun. Bun will automatically create sqlite.db and execute the query.
terminal
Lets give our database a proper schema. Create a
schema.ts file and define a movies table.
We can use the
drizzle-kit CLI to generate an initial SQL migration.
terminal
This creates a new
drizzle directory containing a .sql migration file and meta directory.
File Tree
We can execute these migrations with a simple
migrate.ts script.
This script creates a new connection to a SQLite database that writes to sqlite.db, then executes all unexecuted migrations in the drizzle directory.
We can run this script with
bun to execute the migration.
terminal
Now that we have a database, let’s add some data to it. Create a
seed.ts file with the following contents.
Then run this file.
terminal
We finally have a database with a schema and some sample data. Let’s use Drizzle to query it. Replace the contents of
index.ts with the following.
Then run the file. You should see the three movies we inserted.
terminal
Refer to the Drizzle website for complete documentation.