Introspection (MongoDB)
Overview
When connecting Prisma to an existing database that already contains some data, it can be tedious to manually write the datamodel from scratch while ensuring it matches the structure of the already existing data.
To automate this process, you can use the prisma1 introspect
command from the Prisma CLI to generate the datamodel based on the actual structure of the existing data.
The generated SDL serves as a foundation for your Prisma service, but you can easily make modifications afterwards as you see fit. Some common modifications include hiding a table from the Prisma API or giving a column to a different name.
You can learn more about how introspection is implemented for MongoDB connector here.
Introspecting a MongoDB database
MonogDB uses the following model to organize data internally:
There are two ways you can use the CLI to introspect a MongoDB database:
- Using the interactive
prisma1 init
wizard - Using the dedicated
prisma1 introspect
command
In both cases, you need to provide the connection details for the running MongoDB instance. This includes the following:
- The MongoDB connection string that can connect to your MongoDB server.
- The name of the MongoDB database you want to connect to.
Using the prisma1 init
wizard
During the interactive prisma1 init
flow you can choose to connect to an existing database with data. The CLI will ask for database connection details (as mentioned above) and verify that it can establish a successfully connection.
If the connection details are valid, the CLI will introspect the database and show you a summary.
When prisma1 init
terminates, the CLI has created the following files for you which you can now use to deploy a new Prisma service:
datamodel.prisma
: Contains the datamodel (in SDL) that was generated based on your existing database.docker-compose.yml
: The Docker Compose file containing the configuration of your Prisma server, including details about how to connect to your databaseprisma.yml
: The root configuration file for your service
To be able to query your MongoDB database using Prisma you now need to deploy the service and open a GraphQL Playground:
prisma1 deploy
prisma1 playground
Using prisma1 introspect
prisma1 introspect
works in a similar way as the prisma1 init
wizard in that you need to provide the database connection information.
While prisma1 init
wizard generates an entire service configuration, prisma1 introspect
only generates the datamodel file:
datamodel-[TIMESTAMP].prisma
: The timestamp component allows you to use the introspect command for an existing Prisma service without overriding your existing datamodel.