Matt Mueller (Prisma Client PM)
mongoDb
preview feature flag:
prisma
datasource db {
provider = "mongodb"
url = env("DATABASE_URL")
}
generator client {
provider = "prisma-client-js"
previewFeatures = ["mongoDb"]
}
Then you'll have a new type
keyword in your Prisma Schema, for example:
prisma
model Order {
id String @id @default(auto()) @map("_id") @db.ObjectId
product Product @relation(fields: [productId], references: [id])
color Color
size Size
shippingAddress Address
billingAddress Address?
productId String @db.ObjectId
}
// New composite type!
type Address {
street String
city String
zip String
}
Once you run prisma generate
, you'll have a new type-safe API for working with these types.
For example, here's how you'd create a new Order with an embedded shipping address:
ts
const order = await prisma.order.create({
data: {
// Normal relation
product: { connect: { id: 'some-object-id' } },
color: 'Red',
size: 'Large',
// Composite type
shippingAddress: {
street: '1084 Candycane Lane',
city: 'Silverlake',
zip: '84323',
},
},
})
This is just the tip of the iceberg. Dive deeper in our documentation.
---
If you have any feedback for us, I'd love to jump on a call with you.
We're actively working towards getting MongoDB production ready, so now's the time to give this a try and share your thoughts!PinkiePie
02/28/2022, 9:51 PMsudo docker run --name mongo -d -p 27017:27017 mongo:latest
, but now I need to find and copy some docker file from the Prisma repo and add some additional scripts to run it... It would be just better to have a published image.
2. numeric keys are not supported for no reason. Prisma can generate perfectly fine types for them, but the runtime throws while trying to query such a type. To make it worse, the error message is super unclear. Like there is no validation error in the schema, there is no error during client generation, there is no error in TS, but some weird issue about having some 0[Int]
(don't have it anymore, so don't remember the exact error). It was so hard to understand it that I had to create a new schema using introspection and trying to comment on all the fields one by one. It actually took more than all of the other issues combined. In the end, I throw away a schema generated by introspection and just changed my type to JSON. Still, I don't see any reason to restrict type keys to start from the letter. Having a leading number is totally fine, and a mandatory leading letter feels like a purely artificial restriction.
3. Bad typings for optionals in embedded types. If some field is mandatory but skipped in the code, TS gives a wrong error message. It complains not about missing fields, but about to Type {...some fields here...} is not assignable to type 'undefined'
. So it's not that convenient to debug it while writing the code, the only 2 ways are either triggering autocomplete to see which mandatory fields are missing, or running the code and checking runtime errorsPinkiePie
02/28/2022, 9:56 PMjanpio
Matt Mueller (Prisma Client PM)
PinkiePie
03/08/2022, 7:52 AM