-
-
Notifications
You must be signed in to change notification settings - Fork 37
doc: plugin development guide #527
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule v3-doc-plugin
added at
a7b72b
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| --- | ||
| sidebar_position: 5 | ||
| description: Plugin development guide | ||
| --- | ||
|
|
||
| import StackBlitzGithub from '@site/src/components/StackBlitzGithub'; | ||
| import PackageInstall from '../_components/PackageInstall'; | ||
|
|
||
| # Plugin Development | ||
|
|
||
| This guide provides a comprehensive introduction to developing ZenStack plugins, demonstrating how to extend ZenStack’s functionality at the schema, CLI, and runtime levels. | ||
|
|
||
| ## Extending the schema language | ||
|
|
||
| Plugins can contribute new ZModel attributes and functions, and use them to extend the data modeling semantics. To do that, create a folder in your source tree with a `plugin.zmodel` file in it, and define your custom attributes and/or functions in it. | ||
|
|
||
| :::tip | ||
| You can also package a plugin as an npm package. Make sure to export the "plugin.zmodel" file in the package's `package.json` file. | ||
| ::: | ||
|
|
||
| The following example shows a sample plugin that allows you to mark fields as "password" and specify hashing algorithms to use. | ||
|
|
||
| <StackBlitzGithub repoPath="zenstackhq/v3-doc-plugin" openFile="zenstack/password-plugin/plugin.zmodel" /> | ||
|
|
||
| :::info | ||
| Model-level attributes must be prefixed with `@@`, and field-level ones with `@`. | ||
| ::: | ||
|
|
||
| You need to enable the plugin in your ZModel to use the attributes and functions defined in it: | ||
|
|
||
| ```zmodel title="schema.zmodel" | ||
| plugin password { | ||
| provider = './password-plugin' | ||
| } | ||
|
|
||
| model User { | ||
| id Int @id @default(autoincrement()) | ||
| password String @password(hasher: bcryptHasher(10)) | ||
| } | ||
| ``` | ||
|
|
||
| ## Generating custom artifacts | ||
|
|
||
| The `zen` CLI is extensible via plugins and allows you to generate custom artifacts from the ZModel schema. To continue the previous example, let's create a very simple CLI plugin that generates a markdown document listing all model fields marked as passwords. | ||
|
|
||
| To implement a plugin, first install the "@zenstackhq/sdk" package that contains type definitions and utilities for working with ZModel AST: | ||
|
|
||
| <PackageInstall devDependencies={["@zenstackhq/sdk@next"]} /> | ||
|
|
||
| A CLI plugin is an ESM module that default-exports an object that contains the following fields: | ||
|
|
||
| - `name`: the name of the plugin. | ||
| - `generate`: an async function that's invoked during the `zen generate` command run. | ||
| - `statusText` (optional): text displayed in the CLI during the plugin run. | ||
|
|
||
| The implementation looks like the following: | ||
|
|
||
| <StackBlitzGithub repoPath="zenstackhq/v3-doc-plugin" openFile="zenstack/password-plugin/index.ts" /> | ||
|
|
||
| Then, enable the reporting in ZModel with the "report" option: | ||
|
|
||
| ```zmodel title="schema.zmodel" | ||
| plugin password { | ||
| provider = './password-plugin' | ||
| report = true | ||
| } | ||
| ``` | ||
|
|
||
| Finally, run the `zen generate` command to generate the report: | ||
|
|
||
| ```bash | ||
| npx zen generate | ||
| ``` | ||
|
|
||
| You should see that the custom plugin is run during the generation process, and the markdown file is created in the output folder. | ||
|
|
||
| ```plain | ||
| % npx zen generate | ||
| ✔ Generating TypeScript schema | ||
| ✔ Running plugin Password Report | ||
| Generation completed successfully in 116ms. | ||
| ``` | ||
|
|
||
| :::info How does the CLI load plugin modules? | ||
| The CLI attempts to load the plugin module following these steps: | ||
| 1. If `provider` is resolvable as a file path (with ".js", ".mjs", ".ts", or ".mts" extensions), load it as a local file. | ||
| 2. If `provider` is resolvable as a folder containing an index file (with ".js", ".mjs", ".ts", or ".mts" extensions), load the index file. | ||
| 3. Otherwise, load it as an npm package. | ||
|
|
||
| Please note that only ESM modules are supported. TypeScript files are loaded via [jiti](https://github.com/unjs/jiti). | ||
| ::: | ||
|
|
||
| ## Extending the ORM runtime | ||
|
|
||
| The most powerful aspect of the plugin system is the ability to extend the ORM runtime behavior. ZenStack's ORM client provides a plugin system that allows you to intercept the ORM query lifecycle at different stages and abstraction levels. Please see the [ORM plugins](../orm/plugins/) documentation for detailed information. | ||
|
|
||
| In this section, we'll see how to implement automatic password hashing functionality at runtime using the [Kysely](https://kysely.dev/) query hooks mechanism. The implementation requires an understanding of Kysely's [Operation Node](https://kysely-org.github.io/kysely-apidoc/interfaces/OperationNode.html) concept (which is the SQL AST used by Kysely internally). | ||
|
|
||
| :::warning | ||
| The implementation is mostly vibe-coded with Claude Code. Please review carefully before using it in production. | ||
| ::: | ||
|
|
||
| <StackBlitzGithub repoPath="zenstackhq/v3-doc-plugin" openFile="password-hasher-plugin.ts" /> | ||
|
|
||
| With the plugin installed on the ORM client, any time you create or update a User record, the password field will be automatically hashed before being stored in the database. | ||
|
|
||
| <StackBlitzGithub repoPath="zenstackhq/v3-doc-plugin" openFile="main.ts" /> |
This file was deleted.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.