Database Introspection
Database introspection loads your database's schema, calculates the difference between it and your current ZModel schema, and then make necessary updates to the ZModel schema to bring it in sync with the database. This is useful when you have a pre-existing database and want to start using ZenStack with it, or when you need to keep your ZModel schema in sync with changes made directly to the database.
This feature is initially contributed by @svetch.
Use the zen db pull CLI command to run introspection. Refer to the CLI reference for more details and options.
The "db pull" command is NOT based on Prisma's introspection engine and has its own implementation.
Common Workflows​
Starting a new ZenStack project from an existing database​
If you already have a database and want to adopt ZenStack:
- Initialize a ZenStack project:
- npm
- pnpm
- bun
- yarn
npx zen initpnpm zen initbunx zen initnpx zen init - Configure the
datasourceblock in your ZModel schema with the correct database connection URL. - Run introspection to generate the schema:
- npm
- pnpm
- bun
- yarn
npx zen db pullpnpm zen db pullbunx zen db pullnpx zen db pull - Review and refine the generated ZModel schema (e.g., add access control rules, computed fields, etc.).
- Run code generation:
- npm
- pnpm
- bun
- yarn
npx zen generatepnpm zen generatebunx zen generatenpx zen generate
Syncing after direct database changes​
If changes were made directly to the database (outside of the ZenStack migration workflow), you can re-introspect to update your schema:
- Run introspection:
zen db pull - Review the ZModel schema changes and make necessary adjustments
Running zen db pull may overwrite an existing schema file. Make sure to use source control or manually back up your schema file before running the command.