Skip to content

Commit

Permalink
Reverted aliasedTable type improvements in favor of dialect-specifi…
Browse files Browse the repository at this point in the history
…c `alias` functions
  • Loading branch information
Sukairo-02 committed Jan 29, 2025
1 parent 6b6aca7 commit a42bb03
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 19 deletions.
15 changes: 3 additions & 12 deletions drizzle-orm/src/alias.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import type { AnyColumn } from './column.ts';
import { Column } from './column.ts';
import { entityKind, is } from './entity.ts';
import type { AnyMySqlTable, BuildAliasTable as BuildAliasMySqlTable, MySqlView } from './mysql-core/index.ts';
import type { AnyPgTable, BuildAliasTable as BuildAliasPgTable, PgView } from './pg-core/index.ts';
import type { Relation } from './relations.ts';
import type { View } from './sql/sql.ts';
import { SQL, sql } from './sql/sql.ts';
import type { BuildAliasTable as BuildAliasSQLiteTable } from './sqlite-core/index.ts';
import type { AnySQLiteTable } from './sqlite-core/table.ts';
import type { SQLiteView } from './sqlite-core/view.ts';
import { Table } from './table.ts';
import { ViewBaseConfig } from './view-common.ts';

Expand Down Expand Up @@ -93,14 +88,10 @@ export class RelationTableAliasProxyHandler<T extends Relation> implements Proxy
}
}

export function aliasedTable<T extends Table | View, TAlias extends string>(
export function aliasedTable<T extends Table | View>(
table: T,
tableAlias: TAlias,
): T extends AnyPgTable | PgView ? BuildAliasPgTable<T, TAlias>
: T extends AnySQLiteTable | SQLiteView ? BuildAliasSQLiteTable<T, TAlias>
: T extends AnyMySqlTable | MySqlView ? BuildAliasMySqlTable<T, TAlias>
: never
{
tableAlias: string,
): T {
return new Proxy(table, new TableAliasProxyHandler(tableAlias, false)) as any;
}

Expand Down
17 changes: 10 additions & 7 deletions drizzle-orm/type-tests/common/aliased-table.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { type Equal, Expect } from 'type-tests/utils.ts';
import { aliasedTable, eq } from '~/index.ts';
import { eq } from '~/index.ts';
import { drizzle as sqlited } from '~/libsql/index.ts';
import { alias as mysqlAliasFn } from '~/mysql-core/alias.ts';
import { mysqlView } from '~/mysql-core/view.ts';
import { drizzle as mysqld } from '~/mysql2/index.ts';
import { alias as pgAliasFn } from '~/pg-core/alias.ts';
import { pgView } from '~/pg-core/view.ts';
import { drizzle as pgd } from '~/postgres-js/index.ts';
import { alias as sqliteAliasFn } from '~/sqlite-core/alias.ts';
import { sqliteView } from '~/sqlite-core/view.ts';
import { users as mysqlUsers } from '../mysql/tables.ts';
import { users as pgUsers } from '../pg/tables.ts';
Expand All @@ -18,13 +21,13 @@ const pgvUsers = pgView('users_view').as((qb) => qb.select().from(pgUsers));
const sqlitevUsers = sqliteView('users_view').as((qb) => qb.select().from(sqliteUsers));
const mysqlvUsers = mysqlView('users_view').as((qb) => qb.select().from(mysqlUsers));

const pgAlias = aliasedTable(pgUsers, 'usersAlias');
const sqliteAlias = aliasedTable(sqliteUsers, 'usersAlias');
const mysqlAlias = aliasedTable(mysqlUsers, 'usersAlias');
const pgAlias = pgAliasFn(pgUsers, 'usersAlias');
const sqliteAlias = sqliteAliasFn(sqliteUsers, 'usersAlias');
const mysqlAlias = mysqlAliasFn(mysqlUsers, 'usersAlias');

const pgvAlias = aliasedTable(pgvUsers, 'usersvAlias');
const sqlitevAlias = aliasedTable(sqlitevUsers, 'usersvAlias');
const mysqlvAlias = aliasedTable(mysqlvUsers, 'usersvAlias');
const pgvAlias = pgAliasFn(pgvUsers, 'usersvAlias');
const sqlitevAlias = sqliteAliasFn(sqlitevUsers, 'usersvAlias');
const mysqlvAlias = mysqlAliasFn(mysqlvUsers, 'usersvAlias');

const pgRes = await pg.select().from(pgUsers).leftJoin(pgAlias, eq(pgAlias.id, pgUsers.id));
const sqliteRes = await sqlite.select().from(sqliteUsers).leftJoin(sqliteAlias, eq(sqliteAlias.id, sqliteUsers.id));
Expand Down

0 comments on commit a42bb03

Please sign in to comment.