Hey MongoDB community! I work at Prisma where we are building a new kind of ORM/ODM for the Node.js ecosystem (with a special focus on TypeScript).
Our MongoDB connector is currently running in Preview and today we released support for embedded documents, making it pretty much feature-complete — so from now on we are looking for feedback that helps us stabilize the connector and iron out that last rough edges before we release it for production.
Let me spare a few words on why a tool like Prisma would be useful when using MongoDB in a Node.js application.
In my opinion, the biggest benefit Prisma provides is that it enforces a schema for the data that you store in your MongoDB database. You declare this schema using an intuitive and human-readable modeling language that looks as follows:
model Product {
id String @id
name String
color Color
photos Photo[]
}
model Order {
id String @id
product Product @relation(fields: [productId], references: [id])
productId String
shippingAddress Address
billingAddress Address?
}
enum Color {
Red
Green
Blue
}
type Photo {
height Int
width Int
url String
}
type Address {
street String
city String
zip String
}
Once you have your schema in place, Prisma will generate a type-safe database client (called Prisma Client) for you that is aware of your schema and provides powerful queries that are tailored to your schema. If a certain query is not available in the native Prisma Client API, you can also fallback to using raw MongoDB queries that can still be sent via Prisma Client.
Here are a few example queries:
const newOrder = await prisma.order.create({
data: {
// Relation (via reference ID)
product: { connect: { id: 'some-object-id' } },
color: 'Red',
// Embedded document
shippingAddress: {
street: '1084 Candycane Lane',
city: 'Silverlake',
zip: '84323',
},
},
})
const updatedOrder = await prisma.order.update({
where: {
id: 'some-object-id',
},
data: {
shippingAddress: {
// Update just the zip field
update: {
zip: '41232',
},
},
},
})
You can find more API examples in our docs.
Notice that all query results, even the ones where you retrieve only a subset of fields or include a relation (via a reference or an embedded document) will be strongly typed if you are using TypeScript. This means you will never accidentally access a field that wasn’t actually retrieved from the DB because the TypeScript compiler won’t allow you to do this.
Prisma also provides full auto-completion for all of your queries (and naturally for accessing the data in your query results as well). This benefit comes even if you’re using plain JavaScript because modern code editors will still pick up Prisma Client’s generated types.
You can follow our Getting started guide or check out the ready-to-run MongoDB example (in the prisma/prisma-examples
repo) to get started.
Prisma also works nicely with your existing MongoDB instance! We have an introspection feature (invoked via the prisma db pull
command) that allows you to generate your Prisma models instead of typing them up manually, in short, the workflow to get started with your existing MongoDB looks as follows:
-
npm install prisma
to install the Prisma CLI -
npx prisma init
to bootstrap yourschema.prisma
file - Configure your DB connection in
schema.prisma
-
npx prisma db pull
to introspect your MongoDB and generate Prisam models -
npx prisma generate
to generate Prisma Client - Import
PrismaClient
in your Node.js app and instantiate it like so:const prisma = new PrismaClient()
- You’re ready to send queries to your MongoDB
The Preview of the MongoDB connector has been seeing great adoption already and people really seem to like it so far
If you are currently a MongoDB user, we would love to hear your opinions on what we’ve built! Feel free to share your feedback with me here in the community, find me in our public Slack community or directly drop your thoughts in the open #mongodb channel there.