Hey all, I just wanted to share that Embedded Docu...
# mongodb
m
Hey all, I just wanted to share that Embedded Document support for MongoDB is now in Preview! It's easy to get started. First, install the latest Prisma (3.10.0) and add the
mongoDb
preview feature flag:
Copy code
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:
Copy code
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:
Copy code
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!
👍 1
prisma rainbow 1
p
Yesterday I migrated from my custom connector to a proper Prisma implementation (more details in the tread above: https://prisma.slack.com/archives/C0281DB57HQ/p1645604221475539). it was mostly a smooth transition! I had a few issues though which I described above. 1. reliance on a replica set, without a published Docker image. That makes testing harder, I didn't manage to migrate the tests yet. I run them on the remote DB and spent a lot of time, so I know that they works fine, but it would be really nice to have an ability to test it locally. With my own custom connector, it was enough just to run
sudo 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 errors
👍 2
to sum up, the current MongoDB implementation in Prisma is good enough for most of my use cases, so I already replaced my custom connector with it and will finally drop it from the codebase (it was such a nightmare to maintain it!). There is a reason though I won't be able to completely drop a NodeJS MongoDB driver - Prisma doesn't support changeStream thing, so there is no way to implement a graphql subscriptions on top of it. But that's a minor thing and subscriptions server is like 5% of my overall codebase.
💯 2
j
(Similar and repeated from your two previous messages, if you create issues at https://github.com/prisma/prisma/issues/new/choose we will look at these as soon as possible and hopefully be able to fix or properly document them.)
plus one +1 1
m
Thanks for your feedback @PinkiePie! I'm working on reproducing this issue: https://github.com/prisma/prisma/issues/7368 Did you already create an issue for "publish a mongodb docker image" and "numeric keys are not supported for no reason"?
p
@Matt Mueller (Prisma Client PM) issue about docker https://github.com/prisma/prisma/issues/12192 and issue about numeric keys https://github.com/prisma/prisma/issues/12193
👍 1