Drizzle-seed and sequential dates

Say I have a schema with two DateTime fields, started_at and ended_at

How would I use drizzle-seed and its refine function to seed my database with these values differening not more than say, 5 days?

Aka if started_at seeds on 01-05-2025 then the minimum should be the same and the maximum for ended_at should be 05-05-2025

If not possible with the refine function, what would be best practice for this?

Here's an example seed script I was working on when I ran into this problem:

import { reset, seed } from "drizzle-seed";
import { drizzle } from "drizzle-orm/mysql2";
import mysql from "mysql2/promise";
import *  as schema from "../src/lib/server/db/schema";
import "dotenv/config";

async function reseed_db() {
    if (!process.env.DATABASE_URL) throw new Error('DATABASE_URL is not set');

    const client = mysql.createPool(process.env.DATABASE_URL);
    const db = drizzle(client, { schema, mode: 'default' });
    console.log("Resetting database...");
    await reset(db, schema);
    console.log("Reseeding database...");
    await seed(db, schema).refine((f) => ({
        user: {
            count: 20,
            columns: {
                name: f.firstName(),
                createdAt: f.date({ minDate: "2025-01-01", maxDate: "2025-04-01" }),
                dateOfBirth: f.date({ minDate: "1990-01-01", maxDate: "2000-01-01" }),
            },
        },
        session: {
            count: 100,
            columns: {
                score: f.int({ minValue: 0, maxValue: 1500 }),
                accuracy: f.number({ minValue: 0, maxValue: 1, precision: 100 }),
         // How would I add sequential created_at and ended_at datetimes here?
            },

        },
    }));
    console.log("Database reseeded successfully");
    client.end();
}

async function main() {
    await reseed_db();
}

main()
Was this page helpful?