Skip to content
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

feat(prisma ORM plugin): add new plugin for Prisma ORM with README #12169

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 34 additions & 0 deletions plugins/prisma-orm/README.md
@@ -0,0 +1,34 @@
## Prisma ORM ZSH Plugin
#### Overview
This Prisma plugin for ZSH enhances your command line experience by providing handy autocompletion, shortcuts, and environment-aware functionality for working with Prisma ORM. It's designed to streamline your workflow and make interacting with Prisma a breeze (or at least less of a headache).

#### Features
* Autocompletion: Get suggestions for Prisma commands, subcommands, and options.
* Dynamic Schema Loading: Automatically loads Prisma schema based on your environment settings.
* Verbose Output Toggle: Easily switch between verbose and regular output for Prisma commands.
* Model-Specific Aliases: Quickly interact with specific models in your Prisma schema.

#### Installation
1. Clone this repository or download the files.
2. Place the `prisma-orm.plugin.zsh` file into your custom plugins directory, usually `~/.oh-my-zsh/custom/plugins/`.
3. Add prisma to the plugins array in your `.zshrc` file.
4. Reload your terminal or run `source ~/.zshrc`.

#### Usage
After installation, you'll have access to the following functionalities:
* Autocomplete Prisma Commands:
Type `prisma` and press `Tab` to see available commands and options.
* Dynamic Schema Loading:
The plugin checks for a `.env` file in your project directory and loads the schema file specified there.
* Toggle Verbose Output:
* `prisma_verbose`: Enable verbose output.
* `prisma_quiet`: Disable verbose output.
* Model-Specific Aliases:
* Replace 'User' in the aliases with your actual model names.
* Example aliases:
* `prisma_user_create`: Shortcut to create a new User record.
* `prisma_user_delete`: Shortcut to delete a User record.

#### Customization
* To customize the schema file location, modify the `_prisma_set_schema` function in `prisma-orm.plugin.zsh` file.
* Add or modify aliases in `prisma-orm.plugin.zsh` file as per your project needs.
72 changes: 72 additions & 0 deletions plugins/prisma-orm/prisma-orm.plugin.zsh
@@ -0,0 +1,72 @@
# load autocomplete function and necessary zsh modules
autoload -Uz compinit && compinit
autoload -Uz colors && colors

# dynamically set prisma orm schema based on environment
_prisma_set_schema() {
local env_file=".env"
local default_schema="./prisma/schema.prisma"
local schema_var="PRISMA_SCHEMA"

# check if .env file exists
if [[ -f "$env_file" ]]; then
source "$env_file" # load environment variables
echo "${(P)${schema_var}:-$default_schema}" # return schema path from env or default
else
echo "$default_schema" # default schema path
fi
}

# dynamic model names from prisma orm schema
_prisma_models() {
local schema_file=$(_prisma_set_schema)
[[ -f "$schema_file" ]] || return 1

awk '/model [A-Za-z0-9_]+ {/{print $2}' "$schema_file"
}

# toggle verbose output for prisma orm commands
alias prisma_verbose='export PRISMA_LOG_LEVEL="info"'
alias prisma_quiet='unset PRISMA_LOG_LEVEL'

# prisma orm autocomplete function
_prisma_autocomplete() {
local -a commands migrate_opts generate_opts model_names

commands=(
'init:Initialize a new Prisma project'
'migrate:Run database migrations'
'generate:Generate Prisma client'
'studio:Open Prisma Studio'
'introspect:Introspect your database'
'env:List environment variables used by Prisma'
)

migrate_opts=('dev:Create a new migration and apply it' 'deploy:Apply pending migrations to the database' 'reset:Reset the database and apply all migrations' 'status:Check the status of your database migrations')

generate_opts=('--schema:Specify the Prisma schema file')

model_names=($(_prisma_models))

case "$words[2]" in
migrate)
_describe -t commands 'migrate subcommand' migrate_opts
;;
generate)
_describe -t commands 'generate options' generate_opts
;;
studio)
_wanted models expl 'model name' compadd -a model_names
;;
*)
_describe -t commands 'prisma command' commands
;;
esac
}

compdef _prisma_autocomplete prisma

# model-specific aliases - replace 'User' with your model names
alias prisma_user_create='prisma studio --create User'
alias prisma_user_delete='prisma studio --delete User'