sb-prisma
About The Project
This is a library that uses REST API of supabase directly from the Prisma client to process databases.
Prisma is a very useful library, but it cannot be used with non-Node runtimes such as browsers or edge workers; using Prisma Cloud Data Proxy solves this problem, but it does cause delays due to cold starts and round trips.
Also, if you use subapabse-js without Prisma, you can process the database regardless of runtime, but you will throw away the very good types of Prisma clients.
This project retains the type benefits of the Prisma client, but allows runtime-independent data access by using subabase's REST API.
Install & Setup
- Install
@sb-prisma/client
# npm
npm install @sb-prisma/client
# yarn
yarn add @sb-prisma/client
- Add the sb-prisma generator to your schema.prisma
generator sb {
provider = "sb-prisma"
}
generator client {
provider = "prisma-client-js"
previewFeatures = ["dataProxy"] // must specify dataProxy
}
- Run
PRISMA_CLIENT_ENGINE_TYPE=dataproxy npx prisma generate
orPRISMA_CLIENT_ENGINE_TYPE=dataproxy yarn prisma generate
to generate your initialize code.
This library is based on the Data Proxy client. Therefore, please set the environment variable PRISMA_CLIENT_ENGINE_TYPE
to dataproxy
. It is recommended to register it as an alias in the scripts of package.json.
{
"generate": "PRISMA_CLIENT_ENGINE_TYPE=proxy npx prisma generate"
}
Usage
Simply create the middleware with makeMiddleware
and set it with .$use()
. Now, before the prisma client communicates with the DB, it will intercept it and proxy to the REST API of supabase.
import { PrismaClient } from '@prisma/client'
import { makeMiddleware } from '@sb-prisma/client'
const db = new PrismaClient()
const sbMiddleware = makeMiddleware(
process.env.SUPABASE_URL ?? '',
process.env.SUPABASE_ANON_KEY ?? '',
)
db.$use(sbMiddleware)
const main = async () => {
const users = await db.user.findMany({
select: {
id: true,
name: true,
email: true,
Team: true
},
where: {
age: { gte: 20 },
OR: [
{ email: { startsWith: 'foo', mode: 'insensitive' } },
{ name: { contains: 'bar', mode: 'insensitive' } },
]
},
take: 10,
orderBy: { name: 'asc' }
})
console.log(users)
return users
}
main()
Supported APIs
See APIs.md
Contribution
Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.
License
This project is licensed under the MIT License - see the LICENSE file for details
This generator was bootstraped using create-prisma-generator