Using glob in configuration
Adding files to includeFiles manually can get pretty tedious when your project scales. This guide will show you how you can use glob patterns for easier matching.
Introduction
If your project has a significant amount of schemas, it might become hard to keep track of them and add them manually. Glob is the perfect fix for this issue, we just need to define a pattern and it will add the files automatically! But how can we do this exactly? This guide will show you a step-by-step process of configuring glob patterns for your Prisma Util configuration.
During this guide, we'll use the following folder structure as a reference:
Required Knowledge
This guide assumes that you are comfortable with configuring Prisma Util and have a configuration file ready. If you don't have a configuration file, you can use the one that we've provided below this notice. We also recommend reading the Conventions & Standards page, because we'll follow the path naming recommendations.
This is the configuration file that we are going to edit in this guide:
As you can see, this file is pretty messy, but we'll fix it in the following sections. During this guide, we are going to use the built-in prisma-util/schema-creator
utility module that will help us with some cool little functions. No installations are required for Prisma Util built-in modules!
1. Editing the configuration file
During this section, we're going to use the globModels function from Schema Creator. This function requires only one parameter: the path to the base schema. We pass the base schema to this function so it can be excluded from the returned array to avoid double imports.
We'll also define the base schema using the function constantModel to make our configuration experience more enjoyable. To do this, let's remove all entries from the includeFiles
field to make room for our glob pattern, as well as migrate our base schema to use prisma-util/schema-creator
:
Now, let's use the globModels function to automatically add all of our .prisma
files. To do this, we just have to pass the globModels function to includeFiles
as such:
The configuration file is much more readable now and our files should be included properly.
2. Optimizing the glob pattern
The default glob pattern that prisma-util/schema-creator
uses is inefficient, as it searches all directories in the current folder (**/*.prisma
). However, we know that our schemas are exclusively in the models folder, so let's tell Schema Creator to use models/**/*.prisma
instead. To do it, we just need to add another parameter to our globModels function like this:
That is all! You can start running Prisma Util commands now, and everything will function exactly as it did at the beginning. When you want to include another file, you can place it under models
and give it a name ending with .prisma
.
Last updated