What does drizzle.config.ts serve?

I've been working on setting up a project with Drizzle + Next.js + Vercel for my serverless backend. To utilize the ORM API of Drizzle, I reference my database in the following way:

import { drizzle } from "drizzle-orm/vercel-postgres";
import { sql } from "@vercel/postgres";
import { users } from "./schema";
import * as schema from "./schema";

export const db = drizzle(sql, { schema });

It became apparent that passing the {schema} parameter is necessary to infer typing for typescript and use the ORM relations defined in schemas.

However, I also came across the drizzle-kit which utilizes a drizzle.config.ts file referencing the schema like this:

import "@/lib/config";
import { defineConfig } from "drizzle-kit";

export default defineConfig({
  schema: "./lib/schema.ts",
  out: "./drizzle",
  driver: "pg",
  dbCredentials: {
    connectionString: process.env.POSTGRES_URL,
  },
  verbose: true,
  strict: true,
});

Initially, I assumed that using drizzle-kit obviated the need for the additional part "{ schema }" but it appears not to be the case.

The Drizzle documentation states:

Drizzle Kit allows you to split your schema into different files and manage multiple schemas for various databases within one project. You can swiftly design your database schema and push it directly to the database.

This raised the question of why do I have to specify the schema location twice? What role does my drizzle.config.ts file play?

Thank you

I attempted passing the config object directly to drizzle in this manner:

import { sql } from "@vercel/postgres";
import { drizzle } from "drizzle-orm/vercel-postgres";
import {config} from "@/drizzle.config";

// import dotenv from 'dotenv'
// dotenv.config({ path: '.env.local' })

// Utilize dotenv or a custom next js script to load .env.local variables in process.env for Node.js backend
import { loadEnvConfig } from "@next/env";

const projectDir = process.cwd();
loadEnvConfig(projectDir);

export const db = drizzle(sql, config);

or like this :

import { sql } from "@vercel/postgres";
import { drizzle } from "drizzle-orm/vercel-postgres";
import {config} from "@/drizzle.config";

// import dotenv from 'dotenv'
// dotenv.config({ path: '.env.local' })

// Utilize dotenv or a custom next js script to load .env.local variables in process.env for Node.js backend
import { loadEnvConfig } from "@next/env";

const projectDir = process.cwd();
loadEnvConfig(projectDir);

export const db = drizzle(sql, {schema: config.schema});

Unfortunately, neither method worked as expected.

Answer №1

If you haven't explored the wonders of the relational query builder feature in Drizzle, then there's no need to declare your schema upfront. The beginner's guide doesn't mention using schema. I decided to exclude the second parameter for drizzle altogether and still maintain type safety in my queries. Check out the relational query builder documentation which explains the necessity of including schema when utilizing the relational query builder.

On another note: I encountered numerous circular dependency issues when importing all my tables into a single schema object, so from my personal experience, I suggest omitting it altogether.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Attempting to Retrieve Information from a Get Request using Axios

I am attempting to retrieve SQL data from a GET request using axios. My backend is set up with an express server, and the front end is built with React. In my React component, I have a function that contains the axios GET call, which I then invoke within t ...

Transforming the appearance of an Imagebutton upon clicking with the power of Javascript

I am attempting to update the image URL when a specific image button is clicked, however, I am encountering an issue where the image does not change. It seems that none of the JavaScript functions are successfully altering the image and are unable to iden ...

Is there a way to verify the presence of a selector in Puppeteer?

How can I use Puppeteer to check if #idProductType exists and if not, assign producttype to ""? I have tried multiple solutions but none seem to work. const urls = [myurls, ...] const productsList = []; for (let i = 0; i < urls.length; i++) ...

Using the power of jQuery and Ajax to smoothly transition to a different page after editing the value of

Displaying a MySQL table named demo with the values Peter and John along with edit links Example Peter EDIT John EDIT When the edit link is clicked, it will open the page edit_name.php. In edit_name.php, you can update the clicked value using the updat ...

Capture the contents of a table cell and store it in the clipboard

Is there a way to create a hover copy button in a table cell without actually placing the button inside the <td> tags? The goal is to allow users to easily copy text from a specific column cell by hovering over it. function myFunction() { var c ...

What is the best way to organize class usage within other classes to prevent circular dependencies?

The engine class presented below utilizes two renderer classes that extend a base renderer class: import {RendererOne} from "./renderer-one"; import {RendererTwo} from "./renderer-two"; export class Engine { coordinates: number; randomProperty: ...

InvalidAction: The function forEach cannot be applied to "res"

Here is the HTML code that I am currently working with: <div *ngIf="chart" class="col-xl-4 col-lg-6"> <div class="card cardColor mb-3"> <div class="card-header headColor"> <img class="img-fluid" src="../../../ ...

Why does my ForEach function in ReactJs automatically execute without being called?

I am creating a function with a ForEach loop that automatically executes and renders the screen without being called. My goal is to have the startGame function update the element at position [1] of the array to be 5 when I click on it. However, as soon as ...

problem with transmitting information through $.ajax

I'm facing a frustrating issue with the $.ajax()... function in my PHP code. I am unable to retrieve the data sent by the ajax request. <?php if ($_POST){ include 'Bdd_connexion.php'; $filiere = $_POST['filiere']; ...

Creating objects with variable-dependent values in node.js

I'm currently working on creating a user database in an object to assign values to each user, but I'm struggling to find a way to accomplish this. I attempted using var data = {} and then eval(`data.user_${user} = value`), however, it only write ...

What is the best way to transfer the "user" object from TopBar.js to App.js in my project?

In my project, TopBar.js functions as an AppBar component responsible for handling user authentication. When a user logs in, I receive an object called "user". My goal is to export this "user" object to App.js. If I am successful in exporting it to App.js ...

Sorting information in the React Native Section List

Working with React Native's SectionList and filtering data: data: [ { title: "Asia", data: ["Taj Mahal", "Great Wall of China", "Petra"] }, { title: "South America", data: ["Machu Picchu", "Christ the Redeemer", "C ...

Opt for using React components over Motion's elements when working with Framer Motion

Hey there, I have a component set up like this: const Comp = (props) => { return( <div> data here </div> ) } I would like to incorporate framer motion in the following way: const Container = () => { r ...

The component 'ProtectRoute' cannot be utilized within JSX

While using typescript with nextjs, I encountered an issue as illustrated in the image. When I try to use a component as a JSX element, typescript displays the message: ProtectRoute' cannot be used as a JSX component. import { PropsWithChildren } from ...

Locate the Next Element Based on its Tag Name

CSS <div> <a href=''> Red </a> </div> <div> <div> <a href=''> Blue </a> </div> </div> <a href=''>Green</a> JavaScript $(document).ready(f ...

Dynamic Selection List Population in jqGrid

Using jqGrid 4.13.3 - free jqGrid In the Add form, there is a static input element and a select list element. The keyup ajax function is bound to the input element using dataEvents in the beforeInitData event. Once the Add form is displayed, entering a va ...

Steps for mocking an async action creator in Redux using Jest

I am currently working on writing a unit test for a redux async action creator using jest. asyncActions.js: const startSignInRequest = () => ({ type: START_SIGNIN_REQUEST }); // this is the action creator for successfully signing in a user export c ...

What is the best way to ensure that any modifications made to an item in a table are appropriately synced

Utilizing xeditable.js, I am able to dynamically update the content of a cell within a table. My goal is to capture these changes and send them via an HTTP request (PUT) to the backend in order to update the database. Below is the table that can be edited ...

Conditional rendering of a component in ReactJS while maintaining the "empty" space

Is there a way to conditionally render a component without affecting the layout of other components? I'm trying to avoid unwanted shifts caused by this div https://i.sstatic.net/g1eUd.gif Do you have any suggestions? Here is a snippet of my code: ...

Retrieve identical information by manually querying the API (MUX) on two separate occasions

Currently utilizing the MUX API to manage video content. My objective is to store specific data from a ready asset in my Firestore database once it becomes available. In essence, within the MUX platform, an asset goes through various statuses. The status ...