Prisma
Prisma provides a database client and migration tool for TypeScript and Node.js. You can use this to access and perform migrations on your bit.io databases.
This guide will show how to connect to bit.io via Prisma. It largely follows this guide on the Prisma website, but includes some modifications for working with bit.io.
Getting Started and Configuration
First, make sure you have Node.js installed on your machine. Next, create and navigate into a project directory, initialize a TypeScript project, and add the Prisma CLI as a dependency:
mkdir bitdotio-prisma
cd bitdotio-prisma
# initialize project and specify dependency
npm init -y
npm install prisma typescript ts-node @types/node --save-dev
Next, create a tsconfig.json
file with the following:
{
"compilerOptions": {
"sourceMap": true,
"outDir": "dist",
"strict": true,
"lib": ["esnext"],
"esModuleInterop": true
}
}
Next, initialize the Prisma project with:
npx prisma init
npx prisma
invokes the Prisma CLI.
At this point, we can connect to our bit.io database.
Connecting to bit.io with Prisma
We will follow the instructions in the Prisma documentation with some modifications.
npx prisma init
creatd a directory called prisma
with a file called schema.prisma
. We want to update the datasource
section with our bit.io database string. You can find the database string (and all other necessary connection details) in the Connect Tab on your bit.io database page.
Note that you will need to modify your database name. It is usually formatted as username/dbname
; you must substitute /
with .
.
Replace
/
with.
in the database name in your connection stringFor example, if your connection string looks like this:
postgresql://bitdotio:<PASSWORD>@db.bit.io/
bitdotio/prisma_example`You should update such that it looks like this:
postgresql://bitdotio:<PASSWORD>@db.bit.io/bitdotio.prisma_example
We changed
bitdotio/prisma_example
tobitdotio.prisma_example
Furthermore, note that we need two databases, not just one. If you only have one database, create another on bit.io with the "+New DB" button and include its connection string value as indicated below. This "shadow database" is used in a number of migration-related operations conducted by Prisma.
datasource db {
provider = "postgresql"
url = "<BITDOTIO_PG_STRING>"
shadowDatabaseUrl = "<BITDOTIO_PG_STRING_2>"
}
Using Prisma Migrate with bit.io
We will continue to follow the Prisma docs and show how to use our connection to conduct a migration with Prisma.
First, we add the following to our schema.prisma file:
model Post {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
title String @db.VarChar(255)
content String?
published Boolean @default(false)
author User @relation(fields: [authorId], references: [id])
authorId Int
}
model Profile {
id Int @id @default(autoincrement())
bio String?
user User @relation(fields: [userId], references: [id])
userId Int @unique
}
model User {
id Int @id @default(autoincrement())
email String @unique
name String?
posts Post[]
profile Profile?
}
Next, we run the actual migration:
npx prisma migrate dev --name init
At this point, our database (the primary database, not the shadow database) has the tables and columns specified in the schema.
You can do much more with Prisma. To get started with Prisma Client, for example, read along with this guide. For far more guides on using Prisma to work with databases, read here.
Updated 7 months ago