iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🙌

Trying Out Prisma Postgres Early Access

に公開

Prisma Postgres

Prisma has launched a service providing PostgreSQL functionality. I wish they had given it a more unique name, as it's a bit inconvenient to search for.

https://www.prisma.io/blog/announcing-prisma-postgres-early-access

The specific pricing table can be viewed after logging in.

Access to the database itself is free, and costs are incurred based on the use of Prisma Accelerate.

Creating a Project

You can choose a region when creating a project. The Tokyo region is also available.

When you create a project, a URL in the Prisma Accelerate format is issued.

DATABASE_URL="prisma+postgres://accelerate.prisma-data.net/?api_key=xxxxx"

Since this is a Prisma-specific format, it cannot be accessed from other tools. You must go through Prisma Accelerate.

Migration

The DATABASE_URL is now prisma+postgres:, and prisma migrate dev works with prisma@5.21.1. It seems to be a dedicated format for integrating with this service. It didn't work with Prisma Accelerate URLs before, but it seems to have been improved.

Trying it out

I will try running it on Cloudflare Workers.

  • .env
DATABASE_URL="prisma+postgres://accelerate.prisma-data.net/?api_key=xxxx"
  • .dev.vars
DATABASE_URL="prisma+postgres://accelerate.prisma-data.net/?api_key=xxxx"
  • wrangler.toml
#:schema node_modules/wrangler/config-schema.json
name = "prisma-postgres"
compatibility_date = "2024-09-25"
compatibility_flags = ["nodejs_compat"]
main = "./src/index.ts"

[observability]
enabled = true
  • prisma/prisma.schema
generator client {
  provider        = "prisma-client-js"
  previewFeatures = ["driverAdapters"]
}

datasource db {
  provider  = "postgresql"
  url       = env("DATABASE_URL")
}

model Test {
  id String @id @default(uuid())
}
  • src/index.ts
import { PrismaClient } from "@prisma/client/edge";

export interface Env {
  DATABASE_URL: string;
}

export default {
  async fetch(
    request: Request,
    env: Env,
    _ctx: ExecutionContext
  ): Promise<Response> {
    const url = new URL(request.url);
    if (url.pathname !== "/") return new Response("Not Found", { status: 404 });

    const prisma = new PrismaClient({ datasourceUrl: env.DATABASE_URL });

    await prisma.test.create({ data: {} });
    const result = await prisma.test
      .findMany()
      .then((r) => r.map(({ id }) => id));
    return new Response(JSON.stringify(result, undefined, "  "), {
      headers: { "content-type": "application/json" },
    });
  },
};
pnpm run prisma migrate dev
pnpm run prisma generate --no-engine
pnpm wrangler dev
  • Execution results

Summary

Another option for using a database with a free tier has been added. However, since it is a service accessible exclusively via Prisma Accelerate, you cannot choose to access PostgreSQL directly using other tools. Also, considering the pricing of Prisma Accelerate, if you have a certain scale of traffic, using other services might be more cost-effective.

When using it with Cloudflare Workers, the heavy PrismaEngine is no longer necessary, allowing you to avoid the 1MB size limit issue.

In my case, when running Prisma on Cloudflare Workers, I use Supabase for PostgreSQL and separate the PrismaEngine using prisma-accelerate-local instead of Prisma Accelerate.

If I were to use this service, it would likely be for cases where I want to quickly build experiments or sample programs.

GitHubで編集を提案

Discussion