Plugin Overview
ZenStack's plugin system aims to provide a more flexible extensibility solution than Prisma Client Extensions, allowing you to tap into the ORM runtime at different levels. Some parts of the plugin design resemble client extensions, but overall it's not meant to be compatible.
As you go deeper using an ORM, you'll find the need to tap into its engine for different purposes. For example, you may want to:
- Log the time cost of each query operation.
- Block certain CRUD operations.
- Execute code when an entity is created, updated, or deleted.
- Alter the SQL query before it's sent to the database.
- ...
ZenStack ORM provides three ways for you to tap into its runtime:
-
Query API hooks
Query API hooks allow you to intercept ORM query operations (
create
,findUnique
, etc.). You can execute arbitrary code before or after the query operation, or even block the operation altogether. See Query API Hooks for details. -
Entity mutation hooks
Entity mutation hooks allow you to execute code before or after an entity is created, updated, or deleted. This is very useful when you only care about entity changes instead of what triggered the changes. See Entity Mutation Hooks for details.
-
Kysely query hooks
Kysely query hooks give you the ultimate power to inspect and alter the SQL query (in AST form) before it's sent to the database. This is a very powerful low-level extensibility that should be used with care. See Kysely Query Hooks for details.
All three types of plugins are installed via the unified $use
method on the ORM client. The $use
method returns a new ORM client with the plugin applied, without modifying the original client. You can use the $unuse
or $unuseAll
methods to remove plugin(s) from a client.
const db = new ZenStackClient({ ... });
const withPlugin = $db.use({ ... });
const noPlugin = withPlugin.$unuseAll();