Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Beta #3592

Merged
merged 12 commits into from
Nov 22, 2024
Merged

Beta #3592

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/release-feature-branch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ jobs:
- drizzle-orm
- drizzle-kit
- drizzle-zod
- drizzle-seed
- drizzle-typebox
- drizzle-valibot
- eslint-plugin-drizzle
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/release-latest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ jobs:
- drizzle-orm
- drizzle-kit
- drizzle-zod
- drizzle-seed
- drizzle-typebox
- drizzle-valibot
- eslint-plugin-drizzle
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ dist-dts
rollup.config-*.mjs
*.log
.DS_Store
drizzle-seed/src/test.ts
drizzle-seed/src/schemaTest.ts
84 changes: 84 additions & 0 deletions changelogs/drizzle-orm/0.36.4.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# 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](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)
52 changes: 52 additions & 0 deletions changelogs/drizzle-seed/0.1.1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Initial Release

> [!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](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)
6 changes: 3 additions & 3 deletions drizzle-orm/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "drizzle-orm",
"version": "0.36.3",
"version": "0.36.4",
"description": "Drizzle ORM package for SQL databases",
"type": "module",
"scripts": {
Expand Down Expand Up @@ -49,7 +49,7 @@
"@electric-sql/pglite": ">=0.2.0",
"@libsql/client": ">=0.10.0",
"@libsql/client-wasm": ">=0.10.0",
"@neondatabase/serverless": ">=0.1",
"@neondatabase/serverless": ">=0.10.0",
"@op-engineering/op-sqlite": ">=2",
"@opentelemetry/api": "^1.4.1",
"@planetscale/database": ">=1",
Expand Down Expand Up @@ -169,7 +169,7 @@
"@libsql/client": "^0.10.0",
"@libsql/client-wasm": "^0.10.0",
"@miniflare/d1": "^2.14.4",
"@neondatabase/serverless": "^0.9.0",
"@neondatabase/serverless": "^0.10.0",
"@op-engineering/op-sqlite": "^2.0.16",
"@opentelemetry/api": "^1.4.1",
"@originjs/vite-plugin-commonjs": "^1.0.3",
Expand Down
44 changes: 20 additions & 24 deletions drizzle-orm/src/aws-data-api/pg/driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
} from '~/relations.ts';
import { Param, type SQL, sql, type SQLWrapper } from '~/sql/sql.ts';
import { Table } from '~/table.ts';
import type { DrizzleConfig, IfNotImported, ImportTypeError, UpdateSet } from '~/utils.ts';
import type { DrizzleConfig, UpdateSet } from '~/utils.ts';
import type { AwsDataApiClient, AwsDataApiPgQueryResult, AwsDataApiPgQueryResultHKT } from './session.ts';
import { AwsDataApiSession } from './session.ts';

Expand Down Expand Up @@ -129,29 +129,25 @@ export function drizzle<
TSchema extends Record<string, unknown> = Record<string, never>,
TClient extends AwsDataApiClient = RDSDataClient,
>(
...params: IfNotImported<
RDSDataClientConfig,
[ImportTypeError<'@aws-sdk/client-rds-data'>],
[
TClient,
DrizzleAwsDataApiPgConfig<TSchema>,
] | [
(
| (
& DrizzleConfig<TSchema>
& {
connection: RDSDataClientConfig & Omit<DrizzleAwsDataApiPgConfig, keyof DrizzleConfig>;
}
)
| (
& DrizzleAwsDataApiPgConfig<TSchema>
& {
client: TClient;
}
)
),
]
>
...params: [
TClient,
DrizzleAwsDataApiPgConfig<TSchema>,
] | [
(
| (
& DrizzleConfig<TSchema>
& {
connection: RDSDataClientConfig & Omit<DrizzleAwsDataApiPgConfig, keyof DrizzleConfig>;
}
)
| (
& DrizzleAwsDataApiPgConfig<TSchema>
& {
client: TClient;
}
)
),
]
): AwsDataApiPgDatabase<TSchema> & {
$client: TClient;
} {
Expand Down
7 changes: 2 additions & 5 deletions drizzle-orm/src/better-sqlite3/driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
} from '~/relations.ts';
import { BaseSQLiteDatabase } from '~/sqlite-core/db.ts';
import { SQLiteSyncDialect } from '~/sqlite-core/dialect.ts';
import { type DrizzleConfig, type IfNotImported, type ImportTypeError, isConfig } from '~/utils.ts';
import { type DrizzleConfig, isConfig } from '~/utils.ts';
import { BetterSQLiteSession } from './session.ts';

export type DrizzleBetterSQLite3DatabaseConfig =
Expand Down Expand Up @@ -64,9 +64,7 @@ function construct<TSchema extends Record<string, unknown> = Record<string, neve
export function drizzle<
TSchema extends Record<string, unknown> = Record<string, never>,
>(
...params: IfNotImported<
Database,
[ImportTypeError<'better-sqlite3'>],
...params:
| []
| [
Database | string,
Expand All @@ -85,7 +83,6 @@ export function drizzle<
})
),
]
>
): BetterSQLite3Database<TSchema> & {
$client: Database;
} {
Expand Down
7 changes: 2 additions & 5 deletions drizzle-orm/src/bun-sqlite/driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
} from '~/relations.ts';
import { BaseSQLiteDatabase } from '~/sqlite-core/db.ts';
import { SQLiteSyncDialect } from '~/sqlite-core/dialect.ts';
import { type DrizzleConfig, type IfNotImported, type ImportTypeError, isConfig } from '~/utils.ts';
import { type DrizzleConfig, isConfig } from '~/utils.ts';
import { SQLiteBunSession } from './session.ts';

export class BunSQLiteDatabase<
Expand Down Expand Up @@ -86,9 +86,7 @@ export function drizzle<
TSchema extends Record<string, unknown> = Record<string, never>,
TClient extends Database = Database,
>(
...params: IfNotImported<
Database,
[ImportTypeError<'bun-types'>],
...params:
| []
| [
TClient | string,
Expand All @@ -107,7 +105,6 @@ export function drizzle<
})
),
]
>
): BunSQLiteDatabase<TSchema> & {
$client: TClient;
} {
Expand Down
7 changes: 4 additions & 3 deletions drizzle-orm/src/column-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export type MakeColumnConfig<
enumValues: T['enumValues'];
baseColumn: T extends { baseBuilder: infer U extends ColumnBuilderBase } ? BuildColumn<TTableName, U, 'common'>
: never;
identity: T extends { identity: 'always' } ? 'always' : T extends { identity: 'byDefault' } ? 'byDefault' : undefined;
generated: T extends { generated: infer G } ? unknown extends G ? undefined
: G extends undefined ? undefined
: G
Expand All @@ -84,6 +85,7 @@ export type ColumnBuilderTypeConfig<
notNull: T extends { notNull: infer U } ? U : boolean;
hasDefault: T extends { hasDefault: infer U } ? U : boolean;
enumValues: T['enumValues'];
identity: T extends { identity: infer U } ? U : unknown;
generated: T extends { generated: infer G } ? G extends undefined ? unknown : G : unknown;
}
& TTypeConfig
Expand Down Expand Up @@ -154,17 +156,16 @@ export type HasGenerated<T extends ColumnBuilderBase, TGenerated extends {} = {}
};
};

export type IsIdentityByDefault<
export type IsIdentity<
T extends ColumnBuilderBase,
TType extends 'always' | 'byDefault',
> = T & {
_: {
notNull: true;
hasDefault: true;
generated: { as: any; type: TType };
identity: TType;
};
};

export interface ColumnBuilderBase<
T extends ColumnBuilderBaseConfig<ColumnDataType, string> = ColumnBuilderBaseConfig<ColumnDataType, string>,
TTypeConfig extends object = object,
Expand Down
1 change: 1 addition & 0 deletions drizzle-orm/src/column.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export type ColumnTypeConfig<T extends ColumnBaseConfig<ColumnDataType, string>,
enumValues: T['enumValues'];
baseColumn: T extends { baseColumn: infer U } ? U : unknown;
generated: GeneratedColumnConfig<T['data']> | undefined;
identity: undefined | 'always' | 'byDefault';
} & TTypeConfig;

export type ColumnRuntimeConfig<TData, TRuntimeConfig extends object> = ColumnBuilderRuntimeConfig<
Expand Down
36 changes: 16 additions & 20 deletions drizzle-orm/src/libsql/driver.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { type Client, type Config, createClient } from '@libsql/client';
import { type DrizzleConfig, type IfNotImported, type ImportTypeError, isConfig } from '~/utils.ts';
import { type DrizzleConfig, isConfig } from '~/utils.ts';
import { construct as construct, type LibSQLDatabase } from './driver-core.ts';

export { LibSQLDatabase } from './driver-core.ts';
Expand All @@ -8,25 +8,21 @@ export function drizzle<
TSchema extends Record<string, unknown> = Record<string, never>,
TClient extends Client = Client,
>(
...params: IfNotImported<
Client,
[ImportTypeError<'@libsql/client'>],
[
TClient | string,
] | [
TClient | string,
DrizzleConfig<TSchema>,
] | [
(
& DrizzleConfig<TSchema>
& ({
connection: string | Config;
} | {
client: TClient;
})
),
]
>
...params: [
TClient | string,
] | [
TClient | string,
DrizzleConfig<TSchema>,
] | [
(
& DrizzleConfig<TSchema>
& ({
connection: string | Config;
} | {
client: TClient;
})
),
]
): LibSQLDatabase<TSchema> & {
$client: TClient;
} {
Expand Down
Loading
Loading