Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@prisma/adapter-d1

Package Overview
Dependencies
Maintainers
0
Versions
1377
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@prisma/adapter-d1

Prisma's driver adapter for Cloudflare D1

  • 6.1.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
3.3K
decreased by-33.6%
Maintainers
0
Weekly downloads
 
Created
Source

Prisma driver adapter for Cloudflare D1

Prisma driver adapter for Cloudflare's D1 database.

[!NOTE] The adapter is currently in Preview, we are looking for feedback before moving to General Availability.

Refer to the announcement blog post and our docs for more details.

Getting started

To get started, enable the driverAdapters Preview feature flag in your Prisma schema:

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

datasource db {
  provider = "sqlite"
  url      = "file:./dev.db"
}

Install Prisma CLI, Prisma Client, the Prisma adapter for Cloudflare D1, the TypeScript types for Cloudflare Workers, and Wrangler CLI packages:

npm install @prisma/client
npm install @prisma/adapter-d1
npm install --save-dev prisma
npm install --save-dev @cloudflare/workers-types
npm install --save-dev wrangler

Generate Prisma Client:

npx prisma generate

Update your Prisma Client instance to use PrismaD1:

// Import needed packages
import { PrismaClient } from '@prisma/client'
import { PrismaD1 } from '@prisma/adapter-d1'

export interface Env {
  // This must match the binding name defined in your wrangler.toml configuration
  MY_DATABASE: D1Database
}

export default {
  async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {
    // Initialize Prisma Client with the D1 adapter
    const adapter = new PrismaD1(env.MY_DATABASE)
    const prisma = new PrismaClient({ adapter })

    // Execute a Prisma Client query
    const usersCount = await prisma.user.count()

    // Return result
    return new Response(usersCount)
  },
}
For JavaScript users
// Import needed packages
import { PrismaClient } from '@prisma/client'
import { PrismaD1 } from '@prisma/adapter-d1'

export default {
  async fetch(request, env, ctx) {
    // Setup Prisma Client with the adapter
    const adapter = new PrismaD1(env.MY_DATABASE)
    const prisma = new PrismaClient({ adapter })

    // Execute a Prisma Client query
    const usersCount = await prisma.user.count()

    // Return result
    return new Response(usersCount)
  },
}

Note: Make sure your D1 database is setup in your wrangler.toml. Refer to Cloudflare's docs to learn how to set up your database binding.

[[d1_databases]]
binding = "MY_DATABASE"    # i.e. available in the Worker on env.MY_DATABASE
database_name = "database_name"
database_id = "<unique-ID-for-your-database>"

Migrations

Please refer to our docs to learn how to manage schema changes when using Prisma and Cloudflare D1.

Feedback

We encourage you to create an issue if you find something missing or run into a bug.

If you have any feedback, leave a comment in this GitHub discussion.

FAQs

Package last updated on 17 Dec 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc