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:

File Structure
using-glob (Project Root)
    @node_modules
    prisma
        base.prisma
    models
        user
            user.prisma
            account.prisma
        post
            post.prisma
            comment.prisma
    .env
    package.json
    prisma-util.config.mjs

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:

prisma-util.config.mjs
// The JSDocs are stripped from this code block to make it cleaner
// and easier to follow.
export default {
    optionalFeatures: [],
    baseSchema: "prisma/base.prisma",
    includeFiles: ["models/user/user.prisma", "models/user/account.prisma", 
    "models/post/post.prisma", "models/post/comment.prisma"]
}

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:

prisma-util.config.mjs
// The JSDocs are stripped from this code block to make it cleaner
// and easier to follow.

// We'll use globModels later in this guide.
import { constantModel, globModels } from "prisma-util/schema-creator";

// This is just good practice, we recommend defining reusable paths using
// constantModel to make sure that you don't get configuration inconsistencies.
const BaseSchema = constantModel("prisma/base.prisma");

export default {
    optionalFeatures: [],
    // Update our base schema to use the created constant model.
    baseSchema: BaseSchema,
    // We've emptied this array to make room for globModels.
    includeFiles: []
}

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:

prisma-util.config.mjs
// The JSDocs are stripped from this code block to make it cleaner
// and easier to follow.
import { constantModel, globModels } from "prisma-util/schema-creator";

const BaseSchema = constantModel("prisma/base.prisma");

export default {
    optionalFeatures: [],
    baseSchema: BaseSchema,
    // Use globModels to get all of the files to include.
    // We are passing the BaseSchema as a parameter so it is excluded from the array.
    includeFiles: [...globModels(BaseSchema)]
}

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:

prisma-util.config.mjs
// The JSDocs are stripped from this code block to make it cleaner
// and easier to follow.
import { constantModel, globModels } from "prisma-util/schema-creator";

const BaseSchema = constantModel("prisma/base.prisma");

export default {
    optionalFeatures: [],
    baseSchema: BaseSchema,
    // Add the glob pattern as the second parameter.
    includeFiles: [...globModels(BaseSchema, "models/**/*.prisma")]
}

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