New Package: drizzle-seed
Note
drizzle-seed
can only be used with [email protected]
or higher. Versions lower than this may work at runtime but could have type issues and identity column issues, as this patch was introduced in [email protected]
Full Reference
The full API reference and package overview can be found in our official documentation
Basic Usage
In this example we will create 10 users with random names and ids
import { pgTable, integer, text } 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(),
});
async function main() {
const db = drizzle(process.env.DATABASE_URL!);
await seed(db, { users });
}
main();
Options
count
By default, the seed
function will create 10 entities.
However, if you need more for your tests, you can specify this in the seed options object
await seed(db, schema, { count: 1000 });
seed
If you need a seed to generate a different set of values for all subsequent runs, you can define a different number
in the seed
option. Any new number will generate a unique set of values
await seed(db, schema, { seed: 12345 });
The full API reference and package overview can be found in our official documentation
Features
Added OVERRIDING SYSTEM VALUE
api to db.insert()
If you want to force you own values for GENERATED ALWAYS AS IDENTITY
columns, you can use OVERRIDING SYSTEM VALUE
As PostgreSQL docs mentions
In an INSERT command, if ALWAYS is selected, a user-specified value is only accepted if the INSERT statement specifies OVERRIDING SYSTEM VALUE. If BY DEFAULT is selected, then the user-specified value takes precedence
await db.insert(identityColumnsTable).overridingSystemValue().values([
{ alwaysAsIdentity: 2 },
]);
Added .$withAuth()
API for Neon HTTP driver
Using this API, Drizzle will send you an auth token to authorize your query. It can be used with any query available in Drizzle by simply adding .$withAuth()
before it. This token will be used for a specific query
Examples
const token = 'HdncFj1Nm'
await db.$withAuth(token).select().from(usersTable);
await db.$withAuth(token).update(usersTable).set({ name: 'CHANGED' }).where(eq(usersTable.name, 'TARGET'))