MongoDB provider for Prisma 2 and Keystone Next

Gautam Singh
2 min readApr 18, 2021

Many of us are anxiously waiting for MongoDB support in Prisma 2 (prisma.io) which is in development and being pushed every time the version is due release.

I wanted to use MongoDB in keystone-next (v6 you can say) which has removed all support for MongoDB (mongoose) in recent development. This iteration of Keystone is still in development and not released for production yet. Keystone is going all Prisma and dropping support for MongoDB and Knex adapters in next release. While it has advantages but also has major drawback that it can not support MongoDB. Their idea is to wait for Mongo support in Prisma and then enable the provider in Keystone. There are many new features like Document field which is going to change the way we are managing content.

I have played with Prisma 2.21 version which has this support for Mongo database provider (not very well documented yet).

All you need is version ^2.21.0 of all Prisma libs ( prisma, @prismma/client) and change following in your schema.prisma file.

datasource mongodb {
url = env("DATABASE_URL")
// provider value changes
provider = "mongodb"
}
generator client {
provider = "prisma-client-js"
// next line is added
previewFeatures = ["mongodb"]
output = "node_modules/.prisma/client"
}

you can run it with Prisma Studio, use the npx way of running otherwise this preview feature wont work. do not forget to change url or add .env file with DATABASE_URL=mongodb://localhost/db-name value.

try installing Prisma locally if it does not work with npx

npx prisma studio

see the example schema running with Prisma Studio in browser.

prisma studio in browser `npx prisma studio`

I have created a sample repo which updates the original GraphQL example from Prisma, it has two commits only, original code and changes needed to be done for MongoDB provider.

Since I was interested in MongoDB with Keystone-next, I have made a proof of concept with Keystone-Next which required significant changes for Prisma with Mongo. There are several caveat due to this being undocumented preview. see the PR at https://github.com/keystonejs/keystone/pull/5480. Add your comments there for PR.

--

--