0.36.4
drizzle-team/drizzle-orm0.36.4Nov 22, 2024by github-actions[bot]
AI Summary
This release introduces `drizzle-seed`, a new package for seeding databases with test data, along with PostgreSQL support for `OVERRIDING SYSTEM VALUE` in inserts and a new `.$withAuth()` API for the Neon HTTP driver. The release also fixes a TypeScript error related to the Neon serverless package.
Key Highlights
- New `drizzle-seed` package for database seeding with customizable count and seed options
- Added `OVERRIDING SYSTEM VALUE` API for PostgreSQL GENERATED ALWAYS AS IDENTITY columns
- New `.$withAuth()` API for Neon HTTP driver to authorize queries with auth tokens
- Fixed TypeScript error regarding missing '@neondatabase/serverless' package
New Features
- New `drizzle-seed` package for seeding databases with random test data
- `OVERRIDING SYSTEM VALUE` API for db.insert() to force custom values on IDENTITY columns
- `.$withAuth()` API for Neon HTTP driver to authenticate specific queries
Full Release Notes
# New Package: `drizzle-seed`
> [!NOTE]
> `drizzle-seed` can only be used with `drizzle-orm@0.36.4` 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 `drizzle-orm@0.36.4`
## Full Reference
The full API reference and package overview can be found in our [official documentation](https://orm.drizzle.team/docs/seed-overview)
## Basic Usage
In this example we will create 10 users with random names and ids
```ts {12}
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
```ts
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
```ts
await seed(db, schema, { seed: 12345 });
```
The full API reference and package overview can be found in our [official documentation](https://orm.drizzle.team/docs/seed-overview)
# 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
```ts
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
```ts
const token = 'HdncFj1Nm'
await db.$withAuth(token).select().from(usersTable);
await db.$withAuth(token).update(usersTable).set({ name: 'CHANGED' }).where(eq(usersTable.name, 'TARGET'))
```
# Bug Fixes
- [[BUG]: TypeScript error Please install '@neondatabase/serverless' to allow Drizzle ORM to connect to the database](https://github.com/drizzle-team/drizzle-orm/issues/3521)