Skip to content

Commit

Permalink
Updated drivers fixes (#3150)
Browse files Browse the repository at this point in the history
* Fixed drivers, added isConfig util & tests, added driver-init tests, updated pglite, added new `.env` variable requirement for tests

* Restored tests

* Fixed unthrown errors

* Add release notes and bump version

---------

Co-authored-by: AndriiSherman <andreysherman11@gmail.com>
  • Loading branch information
Sukairo-02 and AndriiSherman authored Oct 18, 2024
1 parent a359214 commit 50d0264
Show file tree
Hide file tree
Showing 45 changed files with 3,582 additions and 156 deletions.
6 changes: 6 additions & 0 deletions changelogs/drizzle-orm/0.35.2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
- Fix issues with importing in several environments after updating the Drizzle driver implementation

We've added approximately 240 tests to check the ESM and CJS builds for all the drivers we have. You can check them [here](https://github.com/drizzle-team/drizzle-orm/tree/main/integration-tests/js-tests/driver-init)

- Fixed [[BUG]: Type Error in PgTransaction Missing $client Property After Upgrading to drizzle-orm@0.35.1](https://github.com/drizzle-team/drizzle-orm/issues/3140)
- Fixed [[BUG]: New critical Build error drizzle 0.35.0 deploying on Cloudflare ](https://github.com/drizzle-team/drizzle-orm/issues/3137)
2 changes: 1 addition & 1 deletion drizzle-kit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"@arethetypeswrong/cli": "^0.15.3",
"@aws-sdk/client-rds-data": "^3.556.0",
"@cloudflare/workers-types": "^4.20230518.0",
"@electric-sql/pglite": "^0.1.5",
"@electric-sql/pglite": "^0.2.12",
"@hono/node-server": "^1.9.0",
"@hono/zod-validator": "^0.2.1",
"@libsql/client": "^0.10.0",
Expand Down
4 changes: 2 additions & 2 deletions drizzle-orm/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "drizzle-orm",
"version": "0.35.1",
"version": "0.35.2",
"description": "Drizzle ORM package for SQL databases",
"type": "module",
"scripts": {
Expand Down Expand Up @@ -160,7 +160,7 @@
"devDependencies": {
"@aws-sdk/client-rds-data": "^3.549.0",
"@cloudflare/workers-types": "^4.20230904.0",
"@electric-sql/pglite": "^0.1.1",
"@electric-sql/pglite": "^0.2.12",
"@libsql/client": "^0.10.0",
"@miniflare/d1": "^2.14.4",
"@neondatabase/serverless": "^0.9.0",
Expand Down
15 changes: 7 additions & 8 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, IfNotImported, ImportTypeError } from '~/utils.ts';
import { type DrizzleConfig, type IfNotImported, type ImportTypeError, isConfig } from '~/utils.ts';
import { BetterSQLiteSession } from './session.ts';

export type DrizzleBetterSQLite3DatabaseConfig =
Expand Down Expand Up @@ -89,12 +89,13 @@ export function drizzle<
): BetterSQLite3Database<TSchema> & {
$client: Database;
} {
// eslint-disable-next-line no-instanceof/no-instanceof
if (params[0] instanceof Client) {
return construct(params[0] as Database, params[1] as DrizzleConfig<TSchema> | undefined) as any;
if (params[0] === undefined || typeof params[0] === 'string') {
const instance = params[0] === undefined ? new Client() : new Client(params[0]);

return construct(instance, params[1]) as any;
}

if (typeof params[0] === 'object') {
if (isConfig(params[0])) {
const { connection, client, ...drizzleConfig } = params[0] as
& {
connection?: DrizzleBetterSQLite3DatabaseConfig;
Expand All @@ -117,9 +118,7 @@ export function drizzle<
return construct(instance, drizzleConfig) as any;
}

const instance = new Client(params[0]);

return construct(instance, params[1]) as any;
return construct(params[0] as Database, params[1] as DrizzleConfig<TSchema> | undefined) as any;
}

export namespace drizzle {
Expand Down
15 changes: 7 additions & 8 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, IfNotImported, ImportTypeError } from '~/utils.ts';
import { type DrizzleConfig, type IfNotImported, type ImportTypeError, isConfig } from '~/utils.ts';
import { SQLiteBunSession } from './session.ts';

export class BunSQLiteDatabase<
Expand Down Expand Up @@ -111,12 +111,13 @@ export function drizzle<
): BunSQLiteDatabase<TSchema> & {
$client: TClient;
} {
// eslint-disable-next-line no-instanceof/no-instanceof
if (params[0] instanceof Database) {
return construct(params[0] as TClient, params[1] as DrizzleConfig<TSchema> | undefined) as any;
if (params[0] === undefined || typeof params[0] === 'string') {
const instance = params[0] === undefined ? new Database() : new Database(params[0]);

return construct(instance, params[1]) as any;
}

if (typeof params[0] === 'object') {
if (isConfig(params[0])) {
const { connection, client, ...drizzleConfig } = params[0] as
& ({
connection?: DrizzleBunSqliteDatabaseConfig | string;
Expand All @@ -141,9 +142,7 @@ export function drizzle<
return construct(instance, drizzleConfig) as any;
}

const instance = new Database(params[0]);

return construct(instance, params[1]) as any;
return construct(params[0] as Database, params[1] as DrizzleConfig<TSchema> | undefined) as any;
}

export namespace drizzle {
Expand Down
26 changes: 11 additions & 15 deletions drizzle-orm/src/libsql/driver.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import { type Client, type Config, createClient, type ResultSet } from '@libsql/client';
import { HttpClient } from '@libsql/client/http';
import { Sqlite3Client } from '@libsql/client/sqlite3';
import { WsClient } from '@libsql/client/ws';
import type { BatchItem, BatchResponse } from '~/batch.ts';
import { entityKind } from '~/entity.ts';
import { DefaultLogger } from '~/logger.ts';
Expand All @@ -14,7 +11,7 @@ import {
} from '~/relations.ts';
import { BaseSQLiteDatabase } from '~/sqlite-core/db.ts';
import { SQLiteAsyncDialect } from '~/sqlite-core/dialect.ts';
import type { DrizzleConfig, IfNotImported, ImportTypeError } from '~/utils.ts';
import { type DrizzleConfig, type IfNotImported, type ImportTypeError, isConfig } from '~/utils.ts';
import { LibSQLSession } from './session.ts';

export class LibSQLDatabase<
Expand Down Expand Up @@ -91,11 +88,6 @@ export function drizzle<
): LibSQLDatabase<TSchema> & {
$client: TClient;
} {
// eslint-disable-next-line no-instanceof/no-instanceof
if (params[0] instanceof WsClient || params[0] instanceof HttpClient || params[0] instanceof Sqlite3Client) {
return construct(params[0] as TClient, params[1] as DrizzleConfig<TSchema> | undefined) as any;
}

if (typeof params[0] === 'string') {
const instance = createClient({
url: params[0],
Expand All @@ -104,15 +96,19 @@ export function drizzle<
return construct(instance, params[1]) as any;
}

const { connection, client, ...drizzleConfig } = params[0] as
& { connection?: Config; client?: TClient }
& DrizzleConfig<TSchema>;
if (isConfig(params[0])) {
const { connection, client, ...drizzleConfig } = params[0] as
& { connection?: Config; client?: TClient }
& DrizzleConfig<TSchema>;

if (client) return construct(client, drizzleConfig) as any;
if (client) return construct(client, drizzleConfig) as any;

const instance = typeof connection === 'string' ? createClient({ url: connection }) : createClient(connection!);
const instance = typeof connection === 'string' ? createClient({ url: connection }) : createClient(connection!);

return construct(instance, drizzleConfig) as any;
}

return construct(instance, drizzleConfig) as any;
return construct(params[0] as TClient, params[1] as DrizzleConfig<TSchema> | undefined) as any;
}

export namespace drizzle {
Expand Down
22 changes: 10 additions & 12 deletions drizzle-orm/src/mysql2/driver.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { EventEmitter } from 'events';
import { type Connection as CallbackConnection, createPool, type Pool as CallbackPool, type PoolOptions } from 'mysql2';
import type { Connection, Pool } from 'mysql2/promise';
import { entityKind } from '~/entity.ts';
Expand All @@ -13,7 +12,7 @@ import {
type RelationalSchemaConfig,
type TablesRelationalConfig,
} from '~/relations.ts';
import type { DrizzleConfig, IfNotImported, ImportTypeError } from '~/utils.ts';
import { type DrizzleConfig, type IfNotImported, type ImportTypeError, isConfig } from '~/utils.ts';
import { DrizzleError } from '../errors.ts';
import type { MySql2Client, MySql2PreparedQueryHKT, MySql2QueryResultHKT } from './session.ts';
import { MySql2Session } from './session.ts';
Expand Down Expand Up @@ -137,12 +136,16 @@ export function drizzle<
): MySql2Database<TSchema> & {
$client: TClient;
} {
// eslint-disable-next-line no-instanceof/no-instanceof
if (params[0] instanceof EventEmitter) {
return construct(params[0] as TClient, params[1] as MySql2DrizzleConfig<TSchema> | undefined) as any;
if (typeof params[0] === 'string') {
const connectionString = params[0]!;
const instance = createPool({
uri: connectionString,
});

return construct(instance, params[1]) as any;
}

if (typeof params[0] === 'object') {
if (isConfig(params[0])) {
const { connection, client, ...drizzleConfig } = params[0] as
& { connection?: PoolOptions | string; client?: TClient }
& MySql2DrizzleConfig<TSchema>;
Expand All @@ -159,12 +162,7 @@ export function drizzle<
return db as any;
}

const connectionString = params[0]!;
const instance = createPool({
uri: connectionString,
});

return construct(instance, params[1]) as any;
return construct(params[0] as TClient, params[1] as MySql2DrizzleConfig<TSchema> | undefined) as any;
}

export namespace drizzle {
Expand Down
13 changes: 6 additions & 7 deletions drizzle-orm/src/neon-http/driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { PgDatabase } from '~/pg-core/db.ts';
import { PgDialect } from '~/pg-core/dialect.ts';
import { createTableRelationsHelpers, extractTablesRelationalConfig } from '~/relations.ts';
import type { ExtractTablesWithRelations, RelationalSchemaConfig, TablesRelationalConfig } from '~/relations.ts';
import type { DrizzleConfig, IfNotImported, ImportTypeError } from '~/utils.ts';
import { type DrizzleConfig, type IfNotImported, type ImportTypeError, isConfig } from '~/utils.ts';
import { type NeonHttpClient, type NeonHttpQueryResultHKT, NeonHttpSession } from './session.ts';

export interface NeonDriverOptions {
Expand Down Expand Up @@ -124,12 +124,12 @@ export function drizzle<
): NeonHttpDatabase<TSchema> & {
$client: TClient;
} {
// eslint-disable-next-line no-instanceof/no-instanceof
if (typeof params[0] === 'function') {
return construct(params[0] as TClient, params[1] as DrizzleConfig<TSchema> | undefined) as any;
if (typeof params[0] === 'string') {
const instance = neon(params[0] as string);
return construct(instance, params[1]) as any;
}

if (typeof params[0] === 'object') {
if (isConfig(params[0])) {
const { connection, client, ...drizzleConfig } = params[0] as
& {
connection?:
Expand All @@ -156,8 +156,7 @@ export function drizzle<
return construct(instance, drizzleConfig) as any;
}

const instance = neon(params[0] as string);
return construct(instance, params[1]) as any;
return construct(params[0] as TClient, params[1] as DrizzleConfig<TSchema> | undefined) as any;
}

export namespace drizzle {
Expand Down
19 changes: 9 additions & 10 deletions drizzle-orm/src/neon-serverless/driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
type RelationalSchemaConfig,
type TablesRelationalConfig,
} from '~/relations.ts';
import type { DrizzleConfig, IfNotImported, ImportTypeError } from '~/utils.ts';
import { type DrizzleConfig, type IfNotImported, type ImportTypeError, isConfig } from '~/utils.ts';
import type { NeonClient, NeonQueryResultHKT } from './session.ts';
import { NeonSession } from './session.ts';

Expand Down Expand Up @@ -108,12 +108,15 @@ export function drizzle<
): NeonDatabase<TSchema> & {
$client: TClient;
} {
// eslint-disable-next-line no-instanceof/no-instanceof
if (params[0] instanceof Pool) {
return construct(params[0] as TClient, params[1] as DrizzleConfig<TSchema> | undefined) as any;
if (typeof params[0] === 'string') {
const instance = new Pool({
connectionString: params[0],
});

return construct(instance, params[1]) as any;
}

if (typeof params[0] === 'object') {
if (isConfig(params[0])) {
const { connection, client, ws, ...drizzleConfig } = params[0] as {
connection?: PoolConfig | string;
ws?: any;
Expand All @@ -135,11 +138,7 @@ export function drizzle<
return construct(instance, drizzleConfig) as any;
}

const instance = new Pool({
connectionString: params[0],
});

return construct(instance, params[1]) as any;
return construct(params[0] as TClient, params[1] as DrizzleConfig<TSchema> | undefined) as any;
}

export namespace drizzle {
Expand Down
22 changes: 10 additions & 12 deletions drizzle-orm/src/node-postgres/driver.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { EventEmitter } from 'events';
import pg, { type Pool, type PoolConfig } from 'pg';
import { entityKind } from '~/entity.ts';
import type { Logger } from '~/logger.ts';
Expand All @@ -11,7 +10,7 @@ import {
type RelationalSchemaConfig,
type TablesRelationalConfig,
} from '~/relations.ts';
import type { DrizzleConfig, IfNotImported, ImportTypeError } from '~/utils.ts';
import { type DrizzleConfig, type IfNotImported, type ImportTypeError, isConfig } from '~/utils.ts';
import type { NodePgClient, NodePgQueryResultHKT } from './session.ts';
import { NodePgSession } from './session.ts';

Expand Down Expand Up @@ -86,7 +85,7 @@ export function drizzle<
>(
...params: IfNotImported<
Pool,
[ImportTypeError<'pg'>],
[ImportTypeError<'@types/pg` `pg'>],
| [
TClient | string,
]
Expand All @@ -108,12 +107,15 @@ export function drizzle<
): NodePgDatabase<TSchema> & {
$client: TClient;
} {
// eslint-disable-next-line no-instanceof/no-instanceof
if (params[0] instanceof EventEmitter) {
return construct(params[0] as TClient, params[1] as DrizzleConfig<TSchema> | undefined) as any;
if (typeof params[0] === 'string') {
const instance = new pg.Pool({
connectionString: params[0],
});

return construct(instance, params[1] as DrizzleConfig<TSchema> | undefined) as any;
}

if (typeof params[0] === 'object') {
if (isConfig(params[0])) {
const { connection, client, ...drizzleConfig } = params[0] as (
& ({ connection?: PoolConfig | string; client?: TClient })
& DrizzleConfig<TSchema>
Expand All @@ -130,11 +132,7 @@ export function drizzle<
return construct(instance, drizzleConfig) as any;
}

const instance = new pg.Pool({
connectionString: params[0],
});

return construct(instance, params[1] as DrizzleConfig<TSchema> | undefined) as any;
return construct(params[0] as TClient, params[1] as DrizzleConfig<TSchema> | undefined) as any;
}

export namespace drizzle {
Expand Down
13 changes: 6 additions & 7 deletions drizzle-orm/src/pglite/driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
type RelationalSchemaConfig,
type TablesRelationalConfig,
} from '~/relations.ts';
import type { DrizzleConfig, IfNotImported, ImportTypeError } from '~/utils.ts';
import { type DrizzleConfig, type IfNotImported, type ImportTypeError, isConfig } from '~/utils.ts';
import type { PgliteClient, PgliteQueryResultHKT } from './session.ts';
import { PgliteSession } from './session.ts';

Expand Down Expand Up @@ -105,12 +105,12 @@ export function drizzle<
): PgliteDatabase<TSchema> & {
$client: TClient;
} {
// eslint-disable-next-line no-instanceof/no-instanceof
if (params[0] instanceof PGlite) {
return construct(params[0] as TClient, params[1] as DrizzleConfig<TSchema> | undefined) as any;
if (params[0] === undefined || typeof params[0] === 'string') {
const instance = new PGlite(params[0]);
return construct(instance, params[1]) as any;
}

if (typeof params[0] === 'object') {
if (isConfig(params[0])) {
const { connection, client, ...drizzleConfig } = params[0] as {
connection?: PGliteOptions & { dataDir: string };
client?: TClient;
Expand All @@ -131,8 +131,7 @@ export function drizzle<
return construct(instance, drizzleConfig) as any;
}

const instance = new PGlite(params[0]);
return construct(instance, params[1]) as any;
return construct(params[0] as TClient, params[1] as DrizzleConfig<TSchema> | undefined) as any;
}

export namespace drizzle {
Expand Down
Loading

0 comments on commit 50d0264

Please sign in to comment.