Skip to content

Latest commit

 

History

History
131 lines (103 loc) · 2.63 KB

File metadata and controls

131 lines (103 loc) · 2.63 KB

Bug fixes

  • #3644
  • seeding a table with columns that have .default(sql``) will result in an error

Features

  • added support for postgres uuid columns

Example

import { pgTable, uuid } from "drizzle-orm/pg-core";
import { drizzle } from "drizzle-orm/node-postgres";
import { seed } from "drizzle-seed";

const users = pgTable("users", {
  uuid: uuid("uuid"),
});

async function main() {
  const db = drizzle(process.env.DATABASE_URL!);
  //  You can let it seed automatically
  //  await seed(db, { users });

  // Alternatively, you can manually specify the generator in refine.
  await seed(db, { users }, { count: 1000 }).refine((funcs) => ({
    users: {
      columns: {
        uuid: funcs.uuid(),
      },
    },
  }));
}

main();

  • added support for postgres array columns

Example

import { pgTable, integer, text, varchar } from "drizzle-orm/pg-core";
import { drizzle } from "drizzle-orm/node-postgres";
import { seed } from "drizzle-seed";

const users = pgTable("users", {
  id: integer().primaryKey(),
  name: text().notNull(),
  phone_numbers: varchar({ length: 256 }).array(),
});

You can specify the arraySize parameter in generator options, like funcs.phoneNumber({ arraySize: 3 }), to generate 1D arrays.

async function main() {
  const db = drizzle(process.env.DATABASE_URL!);
  await seed(db, { users }, { count: 1000 }).refine((funcs) => ({
    users: {
      columns: {
        phone_numbers: funcs.phoneNumber({ arraySize: 3 }),
      },
    },
  }));
}

main();

Alternatively, you can let it seed automatically, and it will handle arrays of any dimension.

async function main() {
  const db = drizzle(process.env.DATABASE_URL!);
  await seed(db, { users });
}

main();

  • added support for cyclic tables

You can now seed tables with cyclic relations.

import type { AnyPgColumn } from "drizzle-orm/pg-core";
import {
  foreignKey,
  integer,
  pgTable,
  serial,
  varchar,
} from "drizzle-orm/pg-core";

export const modelTable = pgTable(
  "model",
  {
    id: serial().primaryKey(),
    name: varchar().notNull(),
    defaultImageId: integer(),
  },
  (t) => [
    foreignKey({
      columns: [t.defaultImageId],
      foreignColumns: [modelImageTable.id],
    }),
  ]
);

export const modelImageTable = pgTable("model_image", {
  id: serial().primaryKey(),
  url: varchar().notNull(),
  caption: varchar(),
  modelId: integer()
    .notNull()
    .references((): AnyPgColumn => modelTable.id),
});

async function main() {
  const db = drizzle(process.env.DATABASE_URL!);
  await seed(db, { modelTable, modelImageTable });
}

main();