Recommendations for MongoDB Driver and Schema solutions?

Status of RedwoodJS and MongoDB as of Early 2022

It’s been a wild journey, but Prisma support for MongoDB is coming along nicely. Thankfully we never had to do any of the three things David suggested a year and a half ago at the beginning of this post, which were:

  • Use MongooseJS
  • Connect to the MongoDB NodeJS Driver
  • Think harder about it

However, since all of these things (along with Realm) are still things that someone could conceivably do with enough determination, we should clear up why using Prisma directly is more favorable.

Should You Connect to MongoDB with Realm?

It is absolutely true that anything exposed through a GraphQL endpoint can be queried from Redwood’s API in a straightforward manner with something like node-fetch or graphql-request. This is a useful way to bring in data from a third party service. You can ignore Prisma entirely, even deleting the Prisma folder containing your database schema.

But in most cases it’s not really a good idea to do this. I know this from experience, as I’ve done this on a number of example applications with tools such as Fauna and StepZen. Doing so means you leave behind the Prisma ORM entirely as your primary database tool and instead only query through a GraphQL API.

Problems with Extracting Prisma

There are two main reasons why circumventing Prisma entirely is usually a bad idea:

  • You lose out on a lot of the functionality of Redwood’s CRUD scaffolding capabilities since you aren’t building from a Prisma model.
  • Depending on what database is exposed through that endpoint and how the data is structured, GraphQL can be a sub-optimal query language.
    • This isn’t because GraphQL is bad (it pays my bills actually).
    • However, it can be limiting in comparison to the native query languages these databases actually contain.
    • In this case that query language is MQL, the MongoDB Query Language.

What If I Already Know and Love Mongoose?

When this post was originally written in October 2020, for developers who had not previously worked with MongoDB, Realm was likely the simplest way to connect to a Mongo database from a Redwood app.

Having a GraphQL interface allows the developer to almost completely ignore the underlying database schema and the concept of collections. For others however, such as many who’ve posted in this forum conversation, they already knew alternative means to query a Mongo database.

But now that Prisma in 2021 has officially added support for MongoDB, you can use MongoDB in your Redwood project as your entire backend, scaffold commands included. While still a layer of abstraction removed from MQL, it is a full ORM closer in spirit to Mongoose.

Still In Progress

Unfortunately, while this is possible today it is lacking currently in proper Redwood documentation. All the material is out there for those who are determined enough to piece it together but there isn’t an end to end tutorial yet.

However, if you are interested in using RedwoodJS and MongoDB today and want to provide early feedback, we have a cookbook in the works along with an open issue.

Relevant Prisma Links

Relevant Redwood Links

1 Like