From 6d0c2c094a49735215a12efd03ed6483c0ee70c3 Mon Sep 17 00:00:00 2001 From: AndriiSherman Date: Sun, 5 Nov 2023 16:23:28 +0200 Subject: [PATCH 1/4] Fix cycle imports --- changelogs/drizzle-orm/0.29.0.md | 259 +++++++++++++++--- drizzle-orm/src/alias.ts | 4 +- drizzle-orm/src/aws-data-api/common/index.ts | 2 +- drizzle-orm/src/aws-data-api/pg/session.ts | 2 +- drizzle-orm/src/better-sqlite3/session.ts | 2 +- drizzle-orm/src/bun-sqlite/session.ts | 2 +- drizzle-orm/src/column-builder.ts | 2 +- drizzle-orm/src/column.ts | 2 +- drizzle-orm/src/d1/session.ts | 3 +- drizzle-orm/src/index.ts | 1 - drizzle-orm/src/libsql/session.ts | 2 +- drizzle-orm/src/mysql-core/alias.ts | 2 +- drizzle-orm/src/mysql-core/checks.ts | 2 +- drizzle-orm/src/mysql-core/columns/custom.ts | 2 +- .../src/mysql-core/columns/date.common.ts | 2 +- drizzle-orm/src/mysql-core/db.ts | 6 +- drizzle-orm/src/mysql-core/dialect.ts | 15 +- drizzle-orm/src/mysql-core/expressions.ts | 4 +- drizzle-orm/src/mysql-core/indexes.ts | 2 +- .../src/mysql-core/query-builders/delete.ts | 2 +- .../src/mysql-core/query-builders/insert.ts | 4 +- .../query-builders/query-builder.ts | 5 +- .../src/mysql-core/query-builders/query.ts | 2 +- .../src/mysql-core/query-builders/select.ts | 9 +- .../mysql-core/query-builders/select.types.ts | 6 +- .../src/mysql-core/query-builders/update.ts | 2 +- drizzle-orm/src/mysql-core/session.ts | 2 +- drizzle-orm/src/mysql-core/subquery.ts | 2 +- drizzle-orm/src/mysql-core/view-base.ts | 15 + drizzle-orm/src/mysql-core/view.ts | 20 +- drizzle-orm/src/mysql-proxy/migrator.ts | 2 +- drizzle-orm/src/mysql-proxy/session.ts | 4 +- drizzle-orm/src/mysql2/session.ts | 2 +- drizzle-orm/src/neon-http/migrator.ts | 2 +- drizzle-orm/src/neon-http/session.ts | 2 +- drizzle-orm/src/neon-serverless/session.ts | 2 +- drizzle-orm/src/node-postgres/session.ts | 2 +- drizzle-orm/src/operations.ts | 2 +- drizzle-orm/src/pg-core/alias.ts | 2 +- drizzle-orm/src/pg-core/columns/common.ts | 3 +- drizzle-orm/src/pg-core/columns/custom.ts | 2 +- .../src/pg-core/columns/date.common.ts | 2 +- drizzle-orm/src/pg-core/columns/uuid.ts | 2 +- drizzle-orm/src/pg-core/db.ts | 6 +- drizzle-orm/src/pg-core/dialect.ts | 9 +- drizzle-orm/src/pg-core/expressions.ts | 4 +- drizzle-orm/src/pg-core/indexes.ts | 2 +- .../src/pg-core/query-builders/delete.ts | 2 +- .../src/pg-core/query-builders/insert.ts | 4 +- .../pg-core/query-builders/query-builder.ts | 6 +- .../src/pg-core/query-builders/query.ts | 2 +- .../refresh-materialized-view.ts | 2 +- .../src/pg-core/query-builders/select.ts | 9 +- .../pg-core/query-builders/select.types.ts | 6 +- .../src/pg-core/query-builders/update.ts | 2 +- drizzle-orm/src/pg-core/subquery.ts | 2 +- drizzle-orm/src/pg-core/view-base.ts | 14 + drizzle-orm/src/pg-core/view.ts | 20 +- drizzle-orm/src/pg-proxy/migrator.ts | 2 +- drizzle-orm/src/pg-proxy/session.ts | 2 +- .../src/planetscale-serverless/session.ts | 2 +- drizzle-orm/src/postgres-js/session.ts | 2 +- .../src/query-builders/select.types.ts | 3 +- drizzle-orm/src/relations.ts | 8 +- drizzle-orm/src/selection-proxy.ts | 121 ++++++++ drizzle-orm/src/sql-js/session.ts | 2 +- drizzle-orm/src/sql/expressions/conditions.ts | 2 +- drizzle-orm/src/sql/sql.ts | 59 +++- drizzle-orm/src/sqlite-core/alias.ts | 2 +- drizzle-orm/src/sqlite-core/checks.ts | 2 +- drizzle-orm/src/sqlite-core/columns/custom.ts | 2 +- .../src/sqlite-core/columns/integer.ts | 2 +- drizzle-orm/src/sqlite-core/db.ts | 6 +- drizzle-orm/src/sqlite-core/dialect.ts | 5 +- drizzle-orm/src/sqlite-core/expressions.ts | 4 +- drizzle-orm/src/sqlite-core/indexes.ts | 2 +- .../src/sqlite-core/query-builders/delete.ts | 2 +- .../src/sqlite-core/query-builders/insert.ts | 4 +- .../query-builders/query-builder.ts | 5 +- .../src/sqlite-core/query-builders/query.ts | 2 +- .../src/sqlite-core/query-builders/select.ts | 9 +- .../query-builders/select.types.ts | 6 +- .../src/sqlite-core/query-builders/update.ts | 2 +- drizzle-orm/src/sqlite-core/session.ts | 2 +- drizzle-orm/src/sqlite-core/subquery.ts | 2 +- drizzle-orm/src/sqlite-core/view-base.ts | 15 + drizzle-orm/src/sqlite-core/view.ts | 18 +- drizzle-orm/src/sqlite-proxy/migrator.ts | 2 +- drizzle-orm/src/sqlite-proxy/session.ts | 2 +- drizzle-orm/src/subquery.ts | 135 +-------- drizzle-orm/src/tracing-utils.ts | 3 + drizzle-orm/src/tracing.ts | 2 +- drizzle-orm/src/utils.ts | 9 +- drizzle-orm/src/vercel-postgres/session.ts | 2 +- drizzle-orm/src/view.ts | 58 ---- drizzle-orm/type-tests/mysql/db-rel.ts | 2 +- drizzle-orm/type-tests/mysql/delete.ts | 2 +- drizzle-orm/type-tests/mysql/insert.ts | 2 +- drizzle-orm/type-tests/mysql/select.ts | 2 +- drizzle-orm/type-tests/mysql/subquery.ts | 2 +- drizzle-orm/type-tests/mysql/tables.ts | 2 +- drizzle-orm/type-tests/mysql/update.ts | 2 +- drizzle-orm/type-tests/mysql/with.ts | 2 +- drizzle-orm/type-tests/pg/db-rel.ts | 2 +- drizzle-orm/type-tests/pg/delete.ts | 2 +- drizzle-orm/type-tests/pg/insert.ts | 2 +- drizzle-orm/type-tests/pg/other.ts | 2 +- drizzle-orm/type-tests/pg/select.ts | 2 +- drizzle-orm/type-tests/pg/subquery.ts | 2 +- drizzle-orm/type-tests/pg/tables.ts | 2 +- drizzle-orm/type-tests/pg/update.ts | 2 +- drizzle-orm/type-tests/pg/with.ts | 2 +- drizzle-orm/type-tests/sqlite/delete.ts | 2 +- drizzle-orm/type-tests/sqlite/insert.ts | 2 +- drizzle-orm/type-tests/sqlite/other.ts | 2 +- drizzle-orm/type-tests/sqlite/select.ts | 2 +- drizzle-orm/type-tests/sqlite/subquery.ts | 2 +- drizzle-orm/type-tests/sqlite/tables.ts | 2 +- drizzle-orm/type-tests/sqlite/update.ts | 2 +- drizzle-orm/type-tests/sqlite/with.ts | 2 +- integration-tests/.gitignore | 2 + integration-tests/package.json | 12 +- integration-tests/tests/imports/index.test.ts | 55 ++++ integration-tests/vitest.config.ts | 2 +- pnpm-lock.yaml | 25 +- 125 files changed, 708 insertions(+), 450 deletions(-) create mode 100644 drizzle-orm/src/mysql-core/view-base.ts create mode 100644 drizzle-orm/src/pg-core/view-base.ts create mode 100644 drizzle-orm/src/selection-proxy.ts create mode 100644 drizzle-orm/src/sqlite-core/view-base.ts create mode 100644 drizzle-orm/src/tracing-utils.ts delete mode 100644 drizzle-orm/src/view.ts create mode 100644 integration-tests/tests/imports/index.test.ts diff --git a/changelogs/drizzle-orm/0.29.0.md b/changelogs/drizzle-orm/0.29.0.md index daa86e5db..518c23e33 100644 --- a/changelogs/drizzle-orm/0.29.0.md +++ b/changelogs/drizzle-orm/0.29.0.md @@ -1,28 +1,213 @@ ## New Features -### MySQL `unsigned` option for bigint +### 🎉 MySQL `unsigned` option for bigint You can now specify `bigint unsigned` type ```ts const table = mysqlTable('table', { - id: bigint('id', { mode: 'number', unsigned: true }), + id: bigint('id', { mode: 'number', unsigned: true }), }); ``` -### Improved query builder types +### 🎉 Improved query builder types Starting from `0.29.0` by default, as all the query builders in Drizzle try to conform to SQL as much as possible, you can only invoke most of the methods once. For example, in a SELECT statement there might only be one WHERE clause, so you can only invoke .where() once: ```ts const query = db - .select() - .from(users) - .where(eq(users.id, 1)) - .where(eq(users.name, 'John')); // ❌ Type error - where() can only be invoked once + .select() + .from(users) + .where(eq(users.id, 1)) + .where(eq(users.name, 'John')); // ❌ Type error - where() can only be invoked once ``` -### Possibility to specify name for primary keys and foreign keys +This behavior is useful for conventional query building, i.e. when you create the whole query at once. However, it becomes a problem when you want to build a query dynamically, i.e. if you have a shared function that takes a query builder and enhances it. To solve this problem, Drizzle provides a special 'dynamic' mode for query builders, which removes the restriction of invoking methods only once. To enable it, you need to call .$dynamic() on a query builder. + +Let's see how it works by implementing a simple withPagination function that adds LIMIT and OFFSET clauses to a query based on the provided page number and an optional page size: + +```ts +function withPagination( + qb: T, + page: number, + pageSize: number = 10, +) { + return qb.limit(pageSize).offset(page * pageSize); +} + +const query = db.select().from(users).where(eq(users.id, 1)); +withPagination(query, 1); // ❌ Type error - the query builder is not in dynamic mode + +const dynamicQuery = query.$dynamic(); +withPagination(dynamicQuery, 1); // ✅ OK +``` + +Note that the withPagination function is generic, which allows you to modify the result type of the query builder inside it, for example by adding a join: + +```ts +function withFriends(qb: T) { + return qb.leftJoin(friends, eq(friends.userId, users.id)); +} + +let query = db.select().from(users).where(eq(users.id, 1)).$dynamic(); +query = withFriends(query); +``` + +### 🎉 Possibility to specify name for primary keys and foreign keys + +There is an issue when constraint names exceed the 64-character limit of the database. This causes the database engine to truncate the name, potentially leading to issues. Starting from `0.29.0`, you have the option to specify custom names for both `primaryKey()` and `foreignKey()`. We have also deprecated the old `primaryKey()` syntax, which can still be used but will be removed in future releases + +```ts +const table = pgTable('table', { + id: integer('id'), + name: text('name'), +}, (table) => ({ + cpk: primaryKey({ name: 'composite_key', columns: [table.id, table.name] }), + cfk: foreignKey({ + name: 'fkName', + columns: [table.id], + foreignColumns: [table.name], + }), +})); +``` + +### 🎉 Read Replicas Support + +You can now use the Drizzle `withReplica` function to specify different database connections for read replicas and the main instance for write operations. By default, `withReplicas` will use a random read replica for read operations and the main instance for all other data modification operations. You can also specify custom logic for choosing which read replica connection to use. You have the freedom to make any weighted, custom decision for that. Here are some usage examples: + +```ts +const primaryDb = drizzle(client); +const read1 = drizzle(client); +const read2 = drizzle(client); + +const db = withReplicas(primaryDb, [read1, read2]); + +// read from primary +db.$primary.select().from(usersTable); + +// read from either read1 connection or read2 connection +db.select().from(usersTable) + +// use primary database for delete operation +db.delete(usersTable).where(eq(usersTable.id, 1)) +``` + +Implementation example of custom logic for selecting read replicas, where the first replica has a 70% chance of being chosen, and the second replica has a 30% chance of being chosen. Note that you can implement any type of random selection for read replicas + +```ts +const db = withReplicas(primaryDb, [read1, read2], (replicas) => { + const weight = [0.7, 0.3]; + let cumulativeProbability = 0; + const rand = Math.random(); + + for (const [i, replica] of replicas.entries()) { + cumulativeProbability += weight[i]!; + if (rand < cumulativeProbability) return replica; + } + return replicas[0]! +}); +``` + +`withReplicas` function is available for all dialects in Drizzle ORM + +### 🎉 New MySQL Proxy Driver + +A new driver has been released, allowing you to create your own implementation for an HTTP driver using a MySQL database. You can find usage examples in the `./examples/mysql-proxy` folder + +You need to implement two endpoints on your server that will be used for queries and migrations(Migrate endpoint is optional and only if you want to use drizzle migrations). Both the server and driver implementation are up to you, so you are not restricted in any way. You can add custom mappings, logging, and much more + +You can find both server and driver implementation examples in the `./examples/mysql-proxy` folder + +```ts +// Driver +import axios from 'axios'; +import { eq } from 'drizzle-orm/expressions'; +import { drizzle } from 'drizzle-orm/mysql-proxy'; +import { migrate } from 'drizzle-orm/mysql-proxy/migrator'; +import { cities, users } from './schema'; + +async function main() { + const db = drizzle(async (sql, params, method) => { + try { + const rows = await axios.post(`${process.env.REMOTE_DRIVER}/query`, { + sql, + params, + method, + }); + + return { rows: rows.data }; + } catch (e: any) { + console.error('Error from pg proxy server:', e.response.data); + return { rows: [] }; + } + }); + + await migrate(db, async (queries) => { + try { + await axios.post(`${process.env.REMOTE_DRIVER}/migrate`, { queries }); + } catch (e) { + console.log(e); + throw new Error('Proxy server cannot run migrations'); + } + }, { migrationsFolder: 'drizzle' }); + + await db.insert(cities).values({ id: 1, name: 'name' }); + + await db.insert(users).values({ + id: 1, + name: 'name', + email: 'email', + cityId: 1, + }); + + const usersToCityResponse = await db.select().from(users).leftJoin( + cities, + eq(users.cityId, cities.id), + ); +} +``` + +### 🎉 New PostgreSQL Proxy Driver + +Same as MySQL you can now implement your own http driver for PostgreSQL database. You can find usage examples in the `./examples/pg-proxy` folder + +You need to implement two endpoints on your server that will be used for queries and migrations (Migrate endpoint is optional and only if you want to use drizzle migrations). Both the server and driver implementation are up to you, so you are not restricted in any way. You can add custom mappings, logging, and much more + +You can find both server and driver implementation examples in the `./examples/pg-proxy` folder + +```ts +import axios from 'axios'; +import { eq } from 'drizzle-orm/expressions'; +import { drizzle } from 'drizzle-orm/pg-proxy'; +import { migrate } from 'drizzle-orm/pg-proxy/migrator'; +import { cities, users } from './schema'; + +async function main() { + const db = drizzle(async (sql, params, method) => { + try { + const rows = await axios.post(`${process.env.REMOTE_DRIVER}/query`, { sql, params, method }); + + return { rows: rows.data }; + } catch (e: any) { + console.error('Error from pg proxy server:', e.response.data); + return { rows: [] }; + } + }); + + await migrate(db, async (queries) => { + try { + await axios.post(`${process.env.REMOTE_DRIVER}/query`, { queries }); + } catch (e) { + console.log(e); + throw new Error('Proxy server cannot run migrations'); + } + }, { migrationsFolder: 'drizzle' }); + + const insertedCity = await db.insert(cities).values({ id: 1, name: 'name' }).returning(); + const insertedUser = await db.insert(users).values({ id: 1, name: 'name', email: 'email', cityId: 1 }); + const usersToCityResponse = await db.select().from(users).leftJoin(cities, eq(users.cityId, cities.id)); +} +``` ### 🎉 `D1` batch api support @@ -32,15 +217,15 @@ Batch API usage example: ```ts const batchResponse = await db.batch([ - db.insert(usersTable).values({ id: 1, name: 'John' }).returning({ - id: usersTable.id, - }), - db.update(usersTable).set({ name: 'Dan' }).where(eq(usersTable.id, 1)), - db.query.usersTable.findMany({}), - db.select().from(usersTable).where(eq(usersTable.id, 1)), - db.select({ id: usersTable.id, invitedBy: usersTable.invitedBy }).from( - usersTable, - ), + db.insert(usersTable).values({ id: 1, name: 'John' }).returning({ + id: usersTable.id, + }), + db.update(usersTable).set({ name: 'Dan' }).where(eq(usersTable.id, 1)), + db.query.usersTable.findMany({}), + db.select().from(usersTable).where(eq(usersTable.id, 1)), + db.select({ id: usersTable.id, invitedBy: usersTable.invitedBy }).from( + usersTable, + ), ]); ``` @@ -48,26 +233,26 @@ Type for `batchResponse` in this example would be: ```ts type BatchResponse = [ - { - id: number; - }[], - D1Result, - { - id: number; - name: string; - verified: number; - invitedBy: number | null; - }[], - { - id: number; - name: string; - verified: number; - invitedBy: number | null; - }[], - { - id: number; - invitedBy: number | null; - }[], + { + id: number; + }[], + D1Result, + { + id: number; + name: string; + verified: number; + invitedBy: number | null; + }[], + { + id: number; + name: string; + verified: number; + invitedBy: number | null; + }[], + { + id: number; + invitedBy: number | null; + }[], ]; ``` diff --git a/drizzle-orm/src/alias.ts b/drizzle-orm/src/alias.ts index 06269921e..ecbc2dc56 100644 --- a/drizzle-orm/src/alias.ts +++ b/drizzle-orm/src/alias.ts @@ -2,10 +2,10 @@ import type { AnyColumn } from './column.ts'; import { Column } from './column.ts'; import { entityKind, is } from './entity.ts'; import type { Relation } from './relations.ts'; -import { SQL, sql } from './sql/index.ts'; +import type { View} from './sql/sql.ts'; +import { SQL, sql } from './sql/sql.ts'; import { Table } from './table.ts'; import { ViewBaseConfig } from './view-common.ts'; -import type { View } from './view.ts'; export class ColumnAliasProxyHandler implements ProxyHandler { static readonly [entityKind]: string = 'ColumnAliasProxyHandler'; diff --git a/drizzle-orm/src/aws-data-api/common/index.ts b/drizzle-orm/src/aws-data-api/common/index.ts index 805011941..9a657ba6c 100644 --- a/drizzle-orm/src/aws-data-api/common/index.ts +++ b/drizzle-orm/src/aws-data-api/common/index.ts @@ -1,6 +1,6 @@ import type { Field } from '@aws-sdk/client-rds-data'; import { TypeHint } from '@aws-sdk/client-rds-data'; -import type { QueryTypingsValue } from '~/sql/index.ts'; +import type { QueryTypingsValue } from '~/sql/sql.ts'; export function getValueFromDataApi(field: Field) { if (field.stringValue !== undefined) { diff --git a/drizzle-orm/src/aws-data-api/pg/session.ts b/drizzle-orm/src/aws-data-api/pg/session.ts index f52f4d0b3..53519e6b9 100644 --- a/drizzle-orm/src/aws-data-api/pg/session.ts +++ b/drizzle-orm/src/aws-data-api/pg/session.ts @@ -18,7 +18,7 @@ import { } from '~/pg-core/index.ts'; import type { SelectedFieldsOrdered } from '~/pg-core/query-builders/select.types.ts'; import type { RelationalSchemaConfig, TablesRelationalConfig } from '~/relations.ts'; -import { fillPlaceholders, type QueryTypingsValue, type QueryWithTypings, type SQL, sql } from '~/sql/index.ts'; +import { fillPlaceholders, type QueryTypingsValue, type QueryWithTypings, type SQL, sql } from '~/sql/sql.ts'; import { mapResultRow } from '~/utils.ts'; import { getValueFromDataApi, toValueParam } from '../common/index.ts'; diff --git a/drizzle-orm/src/better-sqlite3/session.ts b/drizzle-orm/src/better-sqlite3/session.ts index 64cf7b9dd..20d1612c2 100644 --- a/drizzle-orm/src/better-sqlite3/session.ts +++ b/drizzle-orm/src/better-sqlite3/session.ts @@ -3,7 +3,7 @@ import { entityKind } from '~/entity.ts'; import type { Logger } from '~/logger.ts'; import { NoopLogger } from '~/logger.ts'; import type { RelationalSchemaConfig, TablesRelationalConfig } from '~/relations.ts'; -import { fillPlaceholders, type Query, sql } from '~/sql/index.ts'; +import { fillPlaceholders, type Query, sql } from '~/sql/sql.ts'; import type { SQLiteSyncDialect } from '~/sqlite-core/dialect.ts'; import { SQLiteTransaction } from '~/sqlite-core/index.ts'; import type { SelectedFieldsOrdered } from '~/sqlite-core/query-builders/select.types.ts'; diff --git a/drizzle-orm/src/bun-sqlite/session.ts b/drizzle-orm/src/bun-sqlite/session.ts index 3f8ac11de..612350a47 100644 --- a/drizzle-orm/src/bun-sqlite/session.ts +++ b/drizzle-orm/src/bun-sqlite/session.ts @@ -5,7 +5,7 @@ import { entityKind } from '~/entity.ts'; import type { Logger } from '~/logger.ts'; import { NoopLogger } from '~/logger.ts'; import type { RelationalSchemaConfig, TablesRelationalConfig } from '~/relations.ts'; -import { fillPlaceholders, type Query, sql } from '~/sql/index.ts'; +import { fillPlaceholders, type Query, sql } from '~/sql/sql.ts'; import type { SQLiteSyncDialect } from '~/sqlite-core/dialect.ts'; import { SQLiteTransaction } from '~/sqlite-core/index.ts'; import type { SelectedFieldsOrdered } from '~/sqlite-core/query-builders/select.types.ts'; diff --git a/drizzle-orm/src/column-builder.ts b/drizzle-orm/src/column-builder.ts index 5adce6e73..7ef9b6d14 100644 --- a/drizzle-orm/src/column-builder.ts +++ b/drizzle-orm/src/column-builder.ts @@ -2,7 +2,7 @@ import { entityKind } from '~/entity.ts'; import type { Column } from './column.ts'; import type { MySqlColumn } from './mysql-core/index.ts'; import type { PgColumn } from './pg-core/index.ts'; -import type { SQL } from './sql/index.ts'; +import type { SQL } from './sql/sql.ts'; import type { SQLiteColumn } from './sqlite-core/index.ts'; import type { Simplify } from './utils.ts'; diff --git a/drizzle-orm/src/column.ts b/drizzle-orm/src/column.ts index 1582ba825..deacc073a 100644 --- a/drizzle-orm/src/column.ts +++ b/drizzle-orm/src/column.ts @@ -1,6 +1,6 @@ import type { ColumnBuilderBaseConfig, ColumnBuilderRuntimeConfig, ColumnDataType } from './column-builder.ts'; import { entityKind } from './entity.ts'; -import type { DriverValueMapper, SQL, SQLWrapper } from './sql/index.ts'; +import type { DriverValueMapper, SQL, SQLWrapper } from './sql/sql.ts'; import type { Table } from './table.ts'; import type { Update } from './utils.ts'; diff --git a/drizzle-orm/src/d1/session.ts b/drizzle-orm/src/d1/session.ts index 1de446f2c..da2040ec7 100644 --- a/drizzle-orm/src/d1/session.ts +++ b/drizzle-orm/src/d1/session.ts @@ -6,8 +6,7 @@ import type { Logger } from '~/logger.ts'; import { NoopLogger } from '~/logger.ts'; import type { RelationalSchemaConfig, TablesRelationalConfig } from '~/relations.ts'; import type { PreparedQuery } from '~/session.ts'; -import { type Query, sql } from '~/sql/index.ts'; -import { fillPlaceholders } from '~/sql/index.ts'; +import { type Query, sql, fillPlaceholders } from '~/sql/sql.ts'; import type { SQLiteAsyncDialect } from '~/sqlite-core/dialect.ts'; import { SQLiteTransaction } from '~/sqlite-core/index.ts'; import type { SelectedFieldsOrdered } from '~/sqlite-core/query-builders/select.types.ts'; diff --git a/drizzle-orm/src/index.ts b/drizzle-orm/src/index.ts index 66b45c585..bc72260b9 100644 --- a/drizzle-orm/src/index.ts +++ b/drizzle-orm/src/index.ts @@ -13,4 +13,3 @@ export * from './subquery.ts'; export * from './table.ts'; export * from './utils.ts'; export * from './view-common.ts'; -export * from './view.ts'; diff --git a/drizzle-orm/src/libsql/session.ts b/drizzle-orm/src/libsql/session.ts index e8fb7e24a..76155f041 100644 --- a/drizzle-orm/src/libsql/session.ts +++ b/drizzle-orm/src/libsql/session.ts @@ -5,7 +5,7 @@ import type { Logger } from '~/logger.ts'; import { NoopLogger } from '~/logger.ts'; import type { RelationalSchemaConfig, TablesRelationalConfig } from '~/relations.ts'; import type { PreparedQuery } from '~/session.ts'; -import { fillPlaceholders, type Query, sql } from '~/sql/index.ts'; +import { fillPlaceholders, type Query, sql } from '~/sql/sql.ts'; import type { SQLiteAsyncDialect } from '~/sqlite-core/dialect.ts'; import { SQLiteTransaction } from '~/sqlite-core/index.ts'; import type { SelectedFieldsOrdered } from '~/sqlite-core/query-builders/select.types.ts'; diff --git a/drizzle-orm/src/mysql-core/alias.ts b/drizzle-orm/src/mysql-core/alias.ts index 59ab93dbb..8320c5533 100644 --- a/drizzle-orm/src/mysql-core/alias.ts +++ b/drizzle-orm/src/mysql-core/alias.ts @@ -1,7 +1,7 @@ import { TableAliasProxyHandler } from '~/alias.ts'; import type { BuildAliasTable } from './query-builders/select.types.ts'; import type { MySqlTable } from './table.ts'; -import type { MySqlViewBase } from './view.ts'; +import type { MySqlViewBase } from './view-base.ts'; export function alias( table: TTable, diff --git a/drizzle-orm/src/mysql-core/checks.ts b/drizzle-orm/src/mysql-core/checks.ts index fa76b4537..af9a29f6a 100644 --- a/drizzle-orm/src/mysql-core/checks.ts +++ b/drizzle-orm/src/mysql-core/checks.ts @@ -1,5 +1,5 @@ import { entityKind } from '~/entity.ts'; -import type { SQL } from '~/sql/index.ts'; +import type { SQL } from '~/sql/sql.ts'; import type { MySqlTable } from './table.ts'; export class CheckBuilder { diff --git a/drizzle-orm/src/mysql-core/columns/custom.ts b/drizzle-orm/src/mysql-core/columns/custom.ts index 02d7b9c68..135bc8c09 100644 --- a/drizzle-orm/src/mysql-core/columns/custom.ts +++ b/drizzle-orm/src/mysql-core/columns/custom.ts @@ -2,7 +2,7 @@ import type { ColumnBuilderBaseConfig, ColumnBuilderRuntimeConfig, MakeColumnCon import type { ColumnBaseConfig } from '~/column.ts'; import { entityKind } from '~/entity.ts'; import type { AnyMySqlTable } from '~/mysql-core/table.ts'; -import type { SQL } from '~/sql/index.ts'; +import type { SQL } from '~/sql/sql.ts'; import type { Equal } from '~/utils.ts'; import { MySqlColumn, MySqlColumnBuilder } from './common.ts'; diff --git a/drizzle-orm/src/mysql-core/columns/date.common.ts b/drizzle-orm/src/mysql-core/columns/date.common.ts index 9800a3d90..3fd8aa612 100644 --- a/drizzle-orm/src/mysql-core/columns/date.common.ts +++ b/drizzle-orm/src/mysql-core/columns/date.common.ts @@ -6,7 +6,7 @@ import type { } from '~/column-builder.ts'; import type { ColumnBaseConfig } from '~/column.ts'; import { entityKind } from '~/entity.ts'; -import { sql } from '~/sql/index.ts'; +import { sql } from '~/sql/sql.ts'; import { MySqlColumn, MySqlColumnBuilder } from './common.ts'; export interface MySqlDateColumnBaseConfig { diff --git a/drizzle-orm/src/mysql-core/db.ts b/drizzle-orm/src/mysql-core/db.ts index f7bd76e0c..e3a07cee3 100644 --- a/drizzle-orm/src/mysql-core/db.ts +++ b/drizzle-orm/src/mysql-core/db.ts @@ -2,10 +2,8 @@ import type { ResultSetHeader } from 'mysql2/promise'; import { entityKind } from '~/entity.ts'; import type { TypedQueryBuilder } from '~/query-builders/query-builder.ts'; import type { ExtractTablesWithRelations, RelationalSchemaConfig, TablesRelationalConfig } from '~/relations.ts'; -import type { SQLWrapper } from '~/sql/index.ts'; -import { SelectionProxyHandler, WithSubquery } from '~/subquery.ts'; +import type { ColumnsSelection, SQLWrapper } from '~/sql/sql.ts'; import type { DrizzleTypeError } from '~/utils.ts'; -import type { ColumnsSelection } from '~/view.ts'; import type { MySqlDialect } from './dialect.ts'; import { MySqlDeleteBase, @@ -27,6 +25,8 @@ import type { } from './session.ts'; import type { WithSubqueryWithSelection } from './subquery.ts'; import type { MySqlTable } from './table.ts'; +import { WithSubquery } from '~/subquery.ts'; +import { SelectionProxyHandler } from '~/selection-proxy.ts'; export class MySqlDatabase< TQueryResult extends QueryResultHKT, diff --git a/drizzle-orm/src/mysql-core/dialect.ts b/drizzle-orm/src/mysql-core/dialect.ts index 03c567069..967016e1d 100644 --- a/drizzle-orm/src/mysql-core/dialect.ts +++ b/drizzle-orm/src/mysql-core/dialect.ts @@ -14,12 +14,11 @@ import { type TableRelationalConfig, type TablesRelationalConfig, } from '~/relations.ts'; -import { and, eq, Param, type QueryWithTypings, SQL, sql, type SQLChunk } from '~/sql/index.ts'; +import { Param, type QueryWithTypings, SQL, sql, type SQLChunk, View } from '~/sql/sql.ts'; import { Subquery, SubqueryConfig } from '~/subquery.ts'; import { getTableName, Table } from '~/table.ts'; import { orderSelectedFields, type UpdateSet } from '~/utils.ts'; -import { View } from '~/view.ts'; -import { DrizzleError, ViewBaseConfig } from '../index.ts'; +import { DrizzleError, ViewBaseConfig, and, eq } from '../index.ts'; import { MySqlColumn } from './columns/common.ts'; import type { MySqlDeleteConfig } from './query-builders/delete.ts'; import type { MySqlInsertConfig } from './query-builders/insert.ts'; @@ -27,15 +26,7 @@ import type { MySqlSelectConfig, MySqlSelectJoinConfig, SelectedFieldsOrdered } import type { MySqlUpdateConfig } from './query-builders/update.ts'; import type { MySqlSession } from './session.ts'; import { MySqlTable } from './table.ts'; -import { MySqlViewBase } from './view.ts'; - -// TODO find out how to use all/values. Seems like I need those functions -// Build project -// copy runtime tests to be sure it's working - -// Add mysql to drizzle-kit - -// Add Planetscale Driver and create example repo +import { MySqlViewBase } from './view-base.ts'; export class MySqlDialect { static readonly [entityKind]: string = 'MySqlDialect'; diff --git a/drizzle-orm/src/mysql-core/expressions.ts b/drizzle-orm/src/mysql-core/expressions.ts index 76db28551..a61f77786 100644 --- a/drizzle-orm/src/mysql-core/expressions.ts +++ b/drizzle-orm/src/mysql-core/expressions.ts @@ -1,6 +1,6 @@ import { bindIfParam } from '~/expressions.ts'; -import type { Placeholder, SQL, SQLChunk, SQLWrapper } from '~/sql/index.ts'; -import { sql } from '~/sql/index.ts'; +import type { Placeholder, SQL, SQLChunk, SQLWrapper } from '~/sql/sql.ts'; +import { sql } from '~/sql/sql.ts'; import type { MySqlColumn } from './columns/index.ts'; export * from '~/expressions.ts'; diff --git a/drizzle-orm/src/mysql-core/indexes.ts b/drizzle-orm/src/mysql-core/indexes.ts index 8a6139e1e..5b73b1d30 100644 --- a/drizzle-orm/src/mysql-core/indexes.ts +++ b/drizzle-orm/src/mysql-core/indexes.ts @@ -1,5 +1,5 @@ import { entityKind } from '~/entity.ts'; -import type { SQL } from '~/sql/index.ts'; +import type { SQL } from '~/sql/sql.ts'; import type { AnyMySqlColumn, MySqlColumn } from './columns/index.ts'; import type { MySqlTable } from './table.ts'; diff --git a/drizzle-orm/src/mysql-core/query-builders/delete.ts b/drizzle-orm/src/mysql-core/query-builders/delete.ts index b8f5a36fd..33588fd16 100644 --- a/drizzle-orm/src/mysql-core/query-builders/delete.ts +++ b/drizzle-orm/src/mysql-core/query-builders/delete.ts @@ -11,7 +11,7 @@ import type { } from '~/mysql-core/session.ts'; import type { MySqlTable } from '~/mysql-core/table.ts'; import { QueryPromise } from '~/query-promise.ts'; -import type { Query, SQL, SQLWrapper } from '~/sql/index.ts'; +import type { Query, SQL, SQLWrapper } from '~/sql/sql.ts'; import type { SelectedFieldsOrdered } from './select.types.ts'; export type MySqlDeleteWithout< diff --git a/drizzle-orm/src/mysql-core/query-builders/insert.ts b/drizzle-orm/src/mysql-core/query-builders/insert.ts index ddb593aa7..4a007d06d 100644 --- a/drizzle-orm/src/mysql-core/query-builders/insert.ts +++ b/drizzle-orm/src/mysql-core/query-builders/insert.ts @@ -11,8 +11,8 @@ import type { } from '~/mysql-core/session.ts'; import type { MySqlTable } from '~/mysql-core/table.ts'; import { QueryPromise } from '~/query-promise.ts'; -import type { Placeholder, Query, SQLWrapper } from '~/sql/index.ts'; -import { Param, SQL, sql } from '~/sql/index.ts'; +import type { Placeholder, Query, SQLWrapper } from '~/sql/sql.ts'; +import { Param, SQL, sql } from '~/sql/sql.ts'; import { Table } from '~/table.ts'; import { mapUpdateSet } from '~/utils.ts'; import type { MySqlUpdateSetSource } from './update.ts'; diff --git a/drizzle-orm/src/mysql-core/query-builders/query-builder.ts b/drizzle-orm/src/mysql-core/query-builders/query-builder.ts index a145f65b3..9cb5ca9e2 100644 --- a/drizzle-orm/src/mysql-core/query-builders/query-builder.ts +++ b/drizzle-orm/src/mysql-core/query-builders/query-builder.ts @@ -2,10 +2,11 @@ import { entityKind } from '~/entity.ts'; import { MySqlDialect } from '~/mysql-core/dialect.ts'; import type { WithSubqueryWithSelection } from '~/mysql-core/subquery.ts'; import type { TypedQueryBuilder } from '~/query-builders/query-builder.ts'; -import { SelectionProxyHandler, WithSubquery } from '~/subquery.ts'; -import type { ColumnsSelection } from '~/view.ts'; import { MySqlSelectBuilder } from './select.ts'; import type { SelectedFields } from './select.types.ts'; +import { WithSubquery } from '~/subquery.ts'; +import { SelectionProxyHandler } from '~/selection-proxy.ts'; +import type { ColumnsSelection } from '~/sql/sql.ts'; export class QueryBuilder { static readonly [entityKind]: string = 'MySqlQueryBuilder'; diff --git a/drizzle-orm/src/mysql-core/query-builders/query.ts b/drizzle-orm/src/mysql-core/query-builders/query.ts index 3b28b1888..f14a5a74e 100644 --- a/drizzle-orm/src/mysql-core/query-builders/query.ts +++ b/drizzle-orm/src/mysql-core/query-builders/query.ts @@ -8,7 +8,7 @@ import { type TableRelationalConfig, type TablesRelationalConfig, } from '~/relations.ts'; -import type { Query, QueryWithTypings, SQL } from '~/sql/index.ts'; +import type { Query, QueryWithTypings, SQL } from '~/sql/sql.ts'; import type { KnownKeysOnly } from '~/utils.ts'; import type { MySqlDialect } from '../dialect.ts'; import type { Mode, MySqlSession, PreparedQueryConfig, PreparedQueryHKTBase, PreparedQueryKind } from '../session.ts'; diff --git a/drizzle-orm/src/mysql-core/query-builders/select.ts b/drizzle-orm/src/mysql-core/query-builders/select.ts index 4dd4f5cda..ff2736fca 100644 --- a/drizzle-orm/src/mysql-core/query-builders/select.ts +++ b/drizzle-orm/src/mysql-core/query-builders/select.ts @@ -4,7 +4,6 @@ import type { MySqlDialect } from '~/mysql-core/dialect.ts'; import type { MySqlSession, PreparedQueryConfig, PreparedQueryHKTBase } from '~/mysql-core/session.ts'; import type { SubqueryWithSelection } from '~/mysql-core/subquery.ts'; import type { MySqlTable } from '~/mysql-core/table.ts'; -import { MySqlViewBase } from '~/mysql-core/view.ts'; import { TypedQueryBuilder } from '~/query-builders/query-builder.ts'; import type { BuildSubquerySelection, @@ -16,13 +15,15 @@ import type { SelectResult, } from '~/query-builders/select.types.ts'; import { QueryPromise } from '~/query-promise.ts'; -import { type Query, SQL } from '~/sql/index.ts'; -import { SelectionProxyHandler, Subquery, SubqueryConfig } from '~/subquery.ts'; +import { SelectionProxyHandler } from '~/selection-proxy.ts'; +import type { ColumnsSelection, Query } from '~/sql/sql.ts'; +import { SQL, View } from '~/sql/sql.ts'; +import { Subquery, SubqueryConfig } from '~/subquery.ts'; import { Table } from '~/table.ts'; import { applyMixins, getTableColumns, getTableLikeName, type ValueOrArray } from '~/utils.ts'; import { orderSelectedFields } from '~/utils.ts'; import { ViewBaseConfig } from '~/view-common.ts'; -import { type ColumnsSelection, View } from '~/view.ts'; +import { MySqlViewBase } from '../view-base.ts'; import type { CreateMySqlSelectFromBuilderMode, LockConfig, diff --git a/drizzle-orm/src/mysql-core/query-builders/select.types.ts b/drizzle-orm/src/mysql-core/query-builders/select.types.ts index 529ce0e5d..ddb22758a 100644 --- a/drizzle-orm/src/mysql-core/query-builders/select.types.ts +++ b/drizzle-orm/src/mysql-core/query-builders/select.types.ts @@ -1,6 +1,5 @@ import type { MySqlColumn } from '~/mysql-core/columns/index.ts'; import type { MySqlTable, MySqlTableWithColumns } from '~/mysql-core/table.ts'; -import type { MySqlViewBase, MySqlViewWithSelection } from '~/mysql-core/view.ts'; import type { SelectedFields as SelectedFieldsBase, SelectedFieldsFlat as SelectedFieldsFlatBase, @@ -17,13 +16,14 @@ import type { SelectMode, SelectResult, } from '~/query-builders/select.types.ts'; -import type { Placeholder, SQL } from '~/sql/index.ts'; +import type { ColumnsSelection, Placeholder, SQL, View } from '~/sql/sql.ts'; import type { Subquery } from '~/subquery.ts'; import type { Table, UpdateTableConfig } from '~/table.ts'; import type { Assume } from '~/utils.ts'; -import type { ColumnsSelection, View } from '~/view.ts'; import type { PreparedQueryConfig, PreparedQueryHKTBase, PreparedQueryKind } from '../session.ts'; import type { MySqlSelectBase, MySqlSelectQueryBuilderBase } from './select.ts'; +import type { MySqlViewBase } from '../view-base.ts'; +import type { MySqlViewWithSelection } from '../view.ts'; export interface MySqlSelectJoinConfig { on: SQL | undefined; diff --git a/drizzle-orm/src/mysql-core/query-builders/update.ts b/drizzle-orm/src/mysql-core/query-builders/update.ts index 5bedea7b9..94b884058 100644 --- a/drizzle-orm/src/mysql-core/query-builders/update.ts +++ b/drizzle-orm/src/mysql-core/query-builders/update.ts @@ -12,7 +12,7 @@ import type { } from '~/mysql-core/session.ts'; import type { MySqlTable } from '~/mysql-core/table.ts'; import { QueryPromise } from '~/query-promise.ts'; -import type { Query, SQL, SQLWrapper } from '~/sql/index.ts'; +import type { Query, SQL, SQLWrapper } from '~/sql/sql.ts'; import { mapUpdateSet, type UpdateSet } from '~/utils.ts'; import type { SelectedFieldsOrdered } from './select.types.ts'; diff --git a/drizzle-orm/src/mysql-core/session.ts b/drizzle-orm/src/mysql-core/session.ts index 82320e0e1..528782d7b 100644 --- a/drizzle-orm/src/mysql-core/session.ts +++ b/drizzle-orm/src/mysql-core/session.ts @@ -1,7 +1,7 @@ import { entityKind } from '~/entity.ts'; import { TransactionRollbackError } from '~/errors.ts'; import type { RelationalSchemaConfig, TablesRelationalConfig } from '~/relations.ts'; -import { type Query, type SQL, sql } from '~/sql/index.ts'; +import { type Query, type SQL, sql } from '~/sql/sql.ts'; import type { Assume, Equal } from '~/utils.ts'; import { MySqlDatabase } from './db.ts'; import type { MySqlDialect } from './dialect.ts'; diff --git a/drizzle-orm/src/mysql-core/subquery.ts b/drizzle-orm/src/mysql-core/subquery.ts index 3a7378ba2..9d2c1828c 100644 --- a/drizzle-orm/src/mysql-core/subquery.ts +++ b/drizzle-orm/src/mysql-core/subquery.ts @@ -1,6 +1,6 @@ import type { AddAliasToSelection } from '~/query-builders/select.types.ts'; +import type { ColumnsSelection } from '~/sql/sql.ts'; import type { Subquery, WithSubquery } from '~/subquery.ts'; -import type { ColumnsSelection } from '~/view.ts'; export type SubqueryWithSelection< TSelection extends ColumnsSelection, diff --git a/drizzle-orm/src/mysql-core/view-base.ts b/drizzle-orm/src/mysql-core/view-base.ts new file mode 100644 index 000000000..53c9e38c2 --- /dev/null +++ b/drizzle-orm/src/mysql-core/view-base.ts @@ -0,0 +1,15 @@ +import { entityKind } from '~/entity.ts'; +import type { ColumnsSelection} from '~/sql/sql.ts'; +import { View } from '~/sql/sql.ts'; + +export abstract class MySqlViewBase< + TName extends string = string, + TExisting extends boolean = boolean, + TSelectedFields extends ColumnsSelection = ColumnsSelection, +> extends View { + static readonly [entityKind]: string = 'MySqlViewBase'; + + declare readonly _: View['_'] & { + readonly viewBrand: 'MySqlViewBase'; + }; +} diff --git a/drizzle-orm/src/mysql-core/view.ts b/drizzle-orm/src/mysql-core/view.ts index fda47b231..dd7d2f82d 100644 --- a/drizzle-orm/src/mysql-core/view.ts +++ b/drizzle-orm/src/mysql-core/view.ts @@ -2,15 +2,15 @@ import type { BuildColumns } from '~/column-builder.ts'; import { entityKind } from '~/entity.ts'; import type { TypedQueryBuilder } from '~/query-builders/query-builder.ts'; import type { AddAliasToSelection } from '~/query-builders/select.types.ts'; -import type { SQL } from '~/sql/index.ts'; -import { SelectionProxyHandler } from '~/subquery.ts'; +import type { ColumnsSelection, SQL } from '~/sql/sql.ts'; import { getTableColumns } from '~/utils.ts'; -import { type ColumnsSelection, View } from '~/view.ts'; import type { MySqlColumn, MySqlColumnBuilderBase } from './columns/index.ts'; -import { QueryBuilder } from './query-builders/index.ts'; +import { QueryBuilder } from './query-builders/query-builder.ts'; import type { SelectedFields } from './query-builders/select.types.ts'; import { mysqlTable } from './table.ts'; import { MySqlViewConfig } from './view-common.ts'; +import { MySqlViewBase } from './view-base.ts'; +import { SelectionProxyHandler } from '~/selection-proxy.ts'; export interface ViewBuilderConfig { algorithm?: 'undefined' | 'merge' | 'temptable'; @@ -152,18 +152,6 @@ export class ManualViewBuilder< } } -export abstract class MySqlViewBase< - TName extends string = string, - TExisting extends boolean = boolean, - TSelectedFields extends ColumnsSelection = ColumnsSelection, -> extends View { - static readonly [entityKind]: string = 'MySqlViewBase'; - - declare readonly _: View['_'] & { - readonly viewBrand: 'MySqlViewBase'; - }; -} - export class MySqlView< TName extends string = string, TExisting extends boolean = boolean, diff --git a/drizzle-orm/src/mysql-proxy/migrator.ts b/drizzle-orm/src/mysql-proxy/migrator.ts index f071888c7..b75184934 100644 --- a/drizzle-orm/src/mysql-proxy/migrator.ts +++ b/drizzle-orm/src/mysql-proxy/migrator.ts @@ -1,6 +1,6 @@ import type { MigrationConfig } from '~/migrator.ts'; import { readMigrationFiles } from '~/migrator.ts'; -import { sql } from '~/sql/index.ts'; +import { sql } from '~/sql/sql.ts'; import type { MySqlRemoteDatabase } from './driver.ts'; export type ProxyMigrator = (migrationQueries: string[]) => Promise; diff --git a/drizzle-orm/src/mysql-proxy/session.ts b/drizzle-orm/src/mysql-proxy/session.ts index 4d9a7b16a..973cb2393 100644 --- a/drizzle-orm/src/mysql-proxy/session.ts +++ b/drizzle-orm/src/mysql-proxy/session.ts @@ -14,8 +14,8 @@ import type { } from '~/mysql-core/session.ts'; import { MySqlSession, PreparedQuery as PreparedQueryBase } from '~/mysql-core/session.ts'; import type { RelationalSchemaConfig, TablesRelationalConfig } from '~/relations.ts'; -import { fillPlaceholders } from '~/sql/index.ts'; -import type { Query, SQL } from '~/sql/index.ts'; +import { fillPlaceholders } from '~/sql/sql.ts'; +import type { Query, SQL } from '~/sql/sql.ts'; import { type Assume, mapResultRow } from '~/utils.ts'; import type { RemoteCallback } from './driver.ts'; diff --git a/drizzle-orm/src/mysql2/session.ts b/drizzle-orm/src/mysql2/session.ts index 13e25c812..3d3e535d6 100644 --- a/drizzle-orm/src/mysql2/session.ts +++ b/drizzle-orm/src/mysql2/session.ts @@ -27,7 +27,7 @@ import { type QueryResultHKT, } from '~/mysql-core/session.ts'; import type { RelationalSchemaConfig, TablesRelationalConfig } from '~/relations.ts'; -import { fillPlaceholders, type Query, type SQL, sql } from '~/sql/index.ts'; +import { fillPlaceholders, type Query, type SQL, sql } from '~/sql/sql.ts'; import { type Assume, mapResultRow } from '~/utils.ts'; export type MySql2Client = Pool | Connection; diff --git a/drizzle-orm/src/neon-http/migrator.ts b/drizzle-orm/src/neon-http/migrator.ts index e4b00d27a..ffda77760 100644 --- a/drizzle-orm/src/neon-http/migrator.ts +++ b/drizzle-orm/src/neon-http/migrator.ts @@ -1,6 +1,6 @@ import type { MigrationConfig } from '~/migrator.ts'; import { readMigrationFiles } from '~/migrator.ts'; -import { type SQL, sql } from '~/sql/index.ts'; +import { type SQL, sql } from '~/sql/sql.ts'; import type { NeonHttpDatabase } from './driver.ts'; /** diff --git a/drizzle-orm/src/neon-http/session.ts b/drizzle-orm/src/neon-http/session.ts index 9609412e4..9b73ab98c 100644 --- a/drizzle-orm/src/neon-http/session.ts +++ b/drizzle-orm/src/neon-http/session.ts @@ -8,7 +8,7 @@ import type { SelectedFieldsOrdered } from '~/pg-core/query-builders/select.type import type { PgTransactionConfig, PreparedQueryConfig, QueryResultHKT } from '~/pg-core/session.ts'; import { PgSession, PreparedQuery } from '~/pg-core/session.ts'; import type { RelationalSchemaConfig, TablesRelationalConfig } from '~/relations.ts'; -import { fillPlaceholders, type Query } from '~/sql/index.ts'; +import { fillPlaceholders, type Query } from '~/sql/sql.ts'; import { type Assume, mapResultRow } from '~/utils.ts'; export type NeonHttpClient = { diff --git a/drizzle-orm/src/neon-serverless/session.ts b/drizzle-orm/src/neon-serverless/session.ts index c49319bb4..891835259 100644 --- a/drizzle-orm/src/neon-serverless/session.ts +++ b/drizzle-orm/src/neon-serverless/session.ts @@ -16,7 +16,7 @@ import type { SelectedFieldsOrdered } from '~/pg-core/query-builders/select.type import type { PgTransactionConfig, PreparedQueryConfig, QueryResultHKT } from '~/pg-core/session.ts'; import { PgSession, PreparedQuery } from '~/pg-core/session.ts'; import type { RelationalSchemaConfig, TablesRelationalConfig } from '~/relations.ts'; -import { fillPlaceholders, type Query, sql } from '~/sql/index.ts'; +import { fillPlaceholders, type Query, sql } from '~/sql/sql.ts'; import { type Assume, mapResultRow } from '~/utils.ts'; export type NeonClient = Pool | PoolClient | Client; diff --git a/drizzle-orm/src/node-postgres/session.ts b/drizzle-orm/src/node-postgres/session.ts index 6167ee16b..01ad7cac8 100644 --- a/drizzle-orm/src/node-postgres/session.ts +++ b/drizzle-orm/src/node-postgres/session.ts @@ -8,7 +8,7 @@ import type { SelectedFieldsOrdered } from '~/pg-core/query-builders/select.type import type { PgTransactionConfig, PreparedQueryConfig, QueryResultHKT } from '~/pg-core/session.ts'; import { PgSession, PreparedQuery } from '~/pg-core/session.ts'; import type { RelationalSchemaConfig, TablesRelationalConfig } from '~/relations.ts'; -import { fillPlaceholders, type Query, sql } from '~/sql/index.ts'; +import { fillPlaceholders, type Query, sql } from '~/sql/sql.ts'; import { tracer } from '~/tracing.ts'; import { type Assume, mapResultRow } from '~/utils.ts'; diff --git a/drizzle-orm/src/operations.ts b/drizzle-orm/src/operations.ts index 19927cd94..09cf41b8a 100644 --- a/drizzle-orm/src/operations.ts +++ b/drizzle-orm/src/operations.ts @@ -1,5 +1,5 @@ import type { AnyColumn, Column } from './column.ts'; -import type { SQL } from './sql/index.ts'; +import type { SQL } from './sql/sql.ts'; import type { Table } from './table.ts'; export type RequiredKeyOnly = T extends AnyColumn<{ diff --git a/drizzle-orm/src/pg-core/alias.ts b/drizzle-orm/src/pg-core/alias.ts index 89e8255c1..53bbf5d43 100644 --- a/drizzle-orm/src/pg-core/alias.ts +++ b/drizzle-orm/src/pg-core/alias.ts @@ -2,7 +2,7 @@ import { TableAliasProxyHandler } from '~/alias.ts'; import type { BuildAliasTable } from './query-builders/select.types.ts'; import type { PgTable } from './table.ts'; -import type { PgViewBase } from './view.ts'; +import type { PgViewBase } from './view-base.ts'; export function alias( table: TTable, diff --git a/drizzle-orm/src/pg-core/columns/common.ts b/drizzle-orm/src/pg-core/columns/common.ts index 94f7d2310..17ba6b929 100644 --- a/drizzle-orm/src/pg-core/columns/common.ts +++ b/drizzle-orm/src/pg-core/columns/common.ts @@ -10,13 +10,14 @@ import { ColumnBuilder } from '~/column-builder.ts'; import type { ColumnBaseConfig } from '~/column.ts'; import { Column } from '~/column.ts'; import { entityKind, is } from '~/entity.ts'; -import { iife, type Update } from '~/utils.ts'; +import type { Update } from '~/utils.ts'; import type { ForeignKey, UpdateDeleteAction } from '~/pg-core/foreign-keys.ts'; import { ForeignKeyBuilder } from '~/pg-core/foreign-keys.ts'; import type { AnyPgTable, PgTable } from '~/pg-core/table.ts'; import { uniqueKeyName } from '../unique-constraint.ts'; import { makePgArray, parsePgArray } from '../utils/array.ts'; +import { iife } from '~/tracing-utils.ts'; export interface ReferenceConfig { ref: () => PgColumn; diff --git a/drizzle-orm/src/pg-core/columns/custom.ts b/drizzle-orm/src/pg-core/columns/custom.ts index 43492669a..7af6c73d1 100644 --- a/drizzle-orm/src/pg-core/columns/custom.ts +++ b/drizzle-orm/src/pg-core/columns/custom.ts @@ -2,7 +2,7 @@ import type { ColumnBuilderBaseConfig, ColumnBuilderRuntimeConfig, MakeColumnCon import type { ColumnBaseConfig } from '~/column.ts'; import { entityKind } from '~/entity.ts'; import type { AnyPgTable } from '~/pg-core/table.ts'; -import type { SQL } from '~/sql/index.ts'; +import type { SQL } from '~/sql/sql.ts'; import type { Equal } from '~/utils.ts'; import { PgColumn, PgColumnBuilder } from './common.ts'; diff --git a/drizzle-orm/src/pg-core/columns/date.common.ts b/drizzle-orm/src/pg-core/columns/date.common.ts index afae5bb67..c2a46d1ce 100644 --- a/drizzle-orm/src/pg-core/columns/date.common.ts +++ b/drizzle-orm/src/pg-core/columns/date.common.ts @@ -1,6 +1,6 @@ import type { ColumnBuilderBaseConfig, ColumnDataType } from '~/column-builder.ts'; import { entityKind } from '~/entity.ts'; -import { sql } from '~/sql/index.ts'; +import { sql } from '~/sql/sql.ts'; import { PgColumnBuilder } from './common.ts'; export abstract class PgDateColumnBaseBuilder< diff --git a/drizzle-orm/src/pg-core/columns/uuid.ts b/drizzle-orm/src/pg-core/columns/uuid.ts index f9fe9e808..4c9ba04ed 100644 --- a/drizzle-orm/src/pg-core/columns/uuid.ts +++ b/drizzle-orm/src/pg-core/columns/uuid.ts @@ -2,7 +2,7 @@ import type { ColumnBuilderBaseConfig, ColumnBuilderRuntimeConfig, MakeColumnCon import type { ColumnBaseConfig } from '~/column.ts'; import { entityKind } from '~/entity.ts'; import type { AnyPgTable } from '~/pg-core/table.ts'; -import { sql } from '~/sql/index.ts'; +import { sql } from '~/sql/sql.ts'; import { PgColumn, PgColumnBuilder } from './common.ts'; export type PgUUIDBuilderInitial = PgUUIDBuilder<{ diff --git a/drizzle-orm/src/pg-core/db.ts b/drizzle-orm/src/pg-core/db.ts index 6e66e8ca7..8a1be58de 100644 --- a/drizzle-orm/src/pg-core/db.ts +++ b/drizzle-orm/src/pg-core/db.ts @@ -17,16 +17,16 @@ import type { import type { PgTable } from '~/pg-core/table.ts'; import type { TypedQueryBuilder } from '~/query-builders/query-builder.ts'; import type { ExtractTablesWithRelations, RelationalSchemaConfig, TablesRelationalConfig } from '~/relations.ts'; -import type { SQLWrapper } from '~/sql/index.ts'; -import { SelectionProxyHandler, WithSubquery } from '~/subquery.ts'; +import type { ColumnsSelection, SQLWrapper } from '~/sql/sql.ts'; +import { WithSubquery } from '~/subquery.ts'; import type { DrizzleTypeError } from '~/utils.ts'; -import type { ColumnsSelection } from '~/view.ts'; import type { PgColumn } from './columns/index.ts'; import { RelationalQueryBuilder } from './query-builders/query.ts'; import { PgRefreshMaterializedView } from './query-builders/refresh-materialized-view.ts'; import type { SelectedFields } from './query-builders/select.types.ts'; import type { WithSubqueryWithSelection } from './subquery.ts'; import type { PgMaterializedView } from './view.ts'; +import { SelectionProxyHandler } from '~/selection-proxy.ts'; export class PgDatabase< TQueryResult extends QueryResultHKT, diff --git a/drizzle-orm/src/pg-core/dialect.ts b/drizzle-orm/src/pg-core/dialect.ts index 6f5305568..651ac0d39 100644 --- a/drizzle-orm/src/pg-core/dialect.ts +++ b/drizzle-orm/src/pg-core/dialect.ts @@ -24,23 +24,22 @@ import { type TablesRelationalConfig, } from '~/relations.ts'; import { - and, type DriverValueEncoder, - eq, Param, type QueryTypingsValue, type QueryWithTypings, SQL, sql, type SQLChunk, -} from '~/sql/index.ts'; +} from '~/sql/sql.ts'; import { Subquery, SubqueryConfig } from '~/subquery.ts'; import { getTableName, Table } from '~/table.ts'; import { orderSelectedFields, type UpdateSet } from '~/utils.ts'; import { ViewBaseConfig } from '~/view-common.ts'; -import { View } from '~/view.ts'; import type { PgSession } from './session.ts'; -import { type PgMaterializedView, PgViewBase } from './view.ts'; +import type { PgMaterializedView } from './view.ts'; +import { View, and, eq } from '~/sql/index.ts'; +import { PgViewBase } from './view-base.ts'; export class PgDialect { static readonly [entityKind]: string = 'PgDialect'; diff --git a/drizzle-orm/src/pg-core/expressions.ts b/drizzle-orm/src/pg-core/expressions.ts index e410587b4..88f494633 100644 --- a/drizzle-orm/src/pg-core/expressions.ts +++ b/drizzle-orm/src/pg-core/expressions.ts @@ -1,7 +1,7 @@ import { bindIfParam } from '~/expressions.ts'; import type { PgColumn } from '~/pg-core/columns/index.ts'; -import type { Placeholder, SQL, SQLChunk, SQLWrapper } from '~/sql/index.ts'; -import { sql } from '~/sql/index.ts'; +import type { Placeholder, SQL, SQLChunk, SQLWrapper } from '~/sql/sql.ts'; +import { sql } from '~/sql/sql.ts'; export * from '~/expressions.ts'; diff --git a/drizzle-orm/src/pg-core/indexes.ts b/drizzle-orm/src/pg-core/indexes.ts index d4ecaf535..a719d44fe 100644 --- a/drizzle-orm/src/pg-core/indexes.ts +++ b/drizzle-orm/src/pg-core/indexes.ts @@ -1,4 +1,4 @@ -import type { SQL } from '~/sql/index.ts'; +import type { SQL } from '~/sql/sql.ts'; import { entityKind } from '~/entity.ts'; import type { PgColumn } from './columns/index.ts'; diff --git a/drizzle-orm/src/pg-core/query-builders/delete.ts b/drizzle-orm/src/pg-core/query-builders/delete.ts index 28dfaba09..5f446e03f 100644 --- a/drizzle-orm/src/pg-core/query-builders/delete.ts +++ b/drizzle-orm/src/pg-core/query-builders/delete.ts @@ -10,7 +10,7 @@ import type { import type { PgTable } from '~/pg-core/table.ts'; import type { SelectResultFields } from '~/query-builders/select.types.ts'; import { QueryPromise } from '~/query-promise.ts'; -import type { Query, SQL, SQLWrapper } from '~/sql/index.ts'; +import type { Query, SQL, SQLWrapper } from '~/sql/sql.ts'; import { Table } from '~/table.ts'; import { tracer } from '~/tracing.ts'; import { orderSelectedFields } from '~/utils.ts'; diff --git a/drizzle-orm/src/pg-core/query-builders/insert.ts b/drizzle-orm/src/pg-core/query-builders/insert.ts index 93bf3aa43..b886426be 100644 --- a/drizzle-orm/src/pg-core/query-builders/insert.ts +++ b/drizzle-orm/src/pg-core/query-builders/insert.ts @@ -11,8 +11,8 @@ import type { import type { PgTable } from '~/pg-core/table.ts'; import type { SelectResultFields } from '~/query-builders/select.types.ts'; import { QueryPromise } from '~/query-promise.ts'; -import type { Placeholder, Query, SQLWrapper } from '~/sql/index.ts'; -import { Param, SQL, sql } from '~/sql/index.ts'; +import type { Placeholder, Query, SQLWrapper } from '~/sql/sql.ts'; +import { Param, SQL, sql } from '~/sql/sql.ts'; import { Table } from '~/table.ts'; import { tracer } from '~/tracing.ts'; import { mapUpdateSet, orderSelectedFields } from '~/utils.ts'; diff --git a/drizzle-orm/src/pg-core/query-builders/query-builder.ts b/drizzle-orm/src/pg-core/query-builders/query-builder.ts index f1d208574..755f54251 100644 --- a/drizzle-orm/src/pg-core/query-builders/query-builder.ts +++ b/drizzle-orm/src/pg-core/query-builders/query-builder.ts @@ -1,13 +1,13 @@ import { entityKind } from '~/entity.ts'; import { PgDialect } from '~/pg-core/dialect.ts'; import type { TypedQueryBuilder } from '~/query-builders/query-builder.ts'; -import type { SQLWrapper } from '~/sql/index.ts'; -import { SelectionProxyHandler, WithSubquery } from '~/subquery.ts'; -import type { ColumnsSelection } from '~/view.ts'; +import type { ColumnsSelection, SQLWrapper } from '~/sql/sql.ts'; import type { PgColumn } from '../columns/index.ts'; import type { WithSubqueryWithSelection } from '../subquery.ts'; import { PgSelectBuilder } from './select.ts'; import type { SelectedFields } from './select.types.ts'; +import { WithSubquery } from '~/subquery.ts'; +import { SelectionProxyHandler } from '~/selection-proxy.ts'; export class QueryBuilder { static readonly [entityKind]: string = 'PgQueryBuilder'; diff --git a/drizzle-orm/src/pg-core/query-builders/query.ts b/drizzle-orm/src/pg-core/query-builders/query.ts index 70272f982..ab0a99839 100644 --- a/drizzle-orm/src/pg-core/query-builders/query.ts +++ b/drizzle-orm/src/pg-core/query-builders/query.ts @@ -8,7 +8,7 @@ import { type TableRelationalConfig, type TablesRelationalConfig, } from '~/relations.ts'; -import type { Query, QueryWithTypings, SQL } from '~/sql/index.ts'; +import type { Query, QueryWithTypings, SQL } from '~/sql/sql.ts'; import { tracer } from '~/tracing.ts'; import type { KnownKeysOnly } from '~/utils.ts'; import type { PgDialect } from '../dialect.ts'; diff --git a/drizzle-orm/src/pg-core/query-builders/refresh-materialized-view.ts b/drizzle-orm/src/pg-core/query-builders/refresh-materialized-view.ts index 48127360f..3ce015793 100644 --- a/drizzle-orm/src/pg-core/query-builders/refresh-materialized-view.ts +++ b/drizzle-orm/src/pg-core/query-builders/refresh-materialized-view.ts @@ -9,7 +9,7 @@ import type { } from '~/pg-core/session.ts'; import type { PgMaterializedView } from '~/pg-core/view.ts'; import { QueryPromise } from '~/query-promise.ts'; -import type { Query, SQL } from '~/sql/index.ts'; +import type { Query, SQL } from '~/sql/sql.ts'; import { tracer } from '~/tracing.ts'; // eslint-disable-next-line @typescript-eslint/no-empty-interface diff --git a/drizzle-orm/src/pg-core/query-builders/select.ts b/drizzle-orm/src/pg-core/query-builders/select.ts index f954f3340..a1d7caec5 100644 --- a/drizzle-orm/src/pg-core/query-builders/select.ts +++ b/drizzle-orm/src/pg-core/query-builders/select.ts @@ -4,7 +4,7 @@ import type { PgDialect } from '~/pg-core/dialect.ts'; import type { PgSession, PreparedQueryConfig } from '~/pg-core/session.ts'; import type { SubqueryWithSelection } from '~/pg-core/subquery.ts'; import type { PgTable } from '~/pg-core/table.ts'; -import { PgViewBase } from '~/pg-core/view.ts'; +import { PgViewBase } from '~/pg-core/view-base.ts'; import { TypedQueryBuilder } from '~/query-builders/query-builder.ts'; import type { BuildSubquerySelection, @@ -16,14 +16,15 @@ import type { SelectResult, } from '~/query-builders/select.types.ts'; import { QueryPromise } from '~/query-promise.ts'; -import { type Placeholder, type Query, SQL, type SQLWrapper } from '~/sql/index.ts'; -import { SelectionProxyHandler, Subquery, SubqueryConfig } from '~/subquery.ts'; +import { SelectionProxyHandler } from '~/selection-proxy.ts'; +import { SQL, View } from '~/sql/sql.ts'; +import type { ColumnsSelection, Placeholder, Query, SQLWrapper } from '~/sql/sql.ts'; +import { Subquery, SubqueryConfig } from '~/subquery.ts'; import { Table } from '~/table.ts'; import { tracer } from '~/tracing.ts'; import { applyMixins, getTableColumns, getTableLikeName, type ValueOrArray } from '~/utils.ts'; import { orderSelectedFields } from '~/utils.ts'; import { ViewBaseConfig } from '~/view-common.ts'; -import { type ColumnsSelection, View } from '~/view.ts'; import type { CreatePgSelectFromBuilderMode, LockConfig, diff --git a/drizzle-orm/src/pg-core/query-builders/select.types.ts b/drizzle-orm/src/pg-core/query-builders/select.types.ts index 949736a3a..6877f2be5 100644 --- a/drizzle-orm/src/pg-core/query-builders/select.types.ts +++ b/drizzle-orm/src/pg-core/query-builders/select.types.ts @@ -5,7 +5,8 @@ import type { } from '~/operations.ts'; import type { PgColumn } from '~/pg-core/columns/index.ts'; import type { PgTable, PgTableWithColumns } from '~/pg-core/table.ts'; -import type { PgViewBase, PgViewWithSelection } from '~/pg-core/view.ts'; +import type { PgViewBase } from '~/pg-core/view-base.ts'; +import type { PgViewWithSelection } from '~/pg-core/view.ts'; import type { AppendToNullabilityMap, AppendToResult, @@ -17,11 +18,10 @@ import type { SelectMode, SelectResult, } from '~/query-builders/select.types.ts'; -import type { Placeholder, SQL, SQLWrapper } from '~/sql/index.ts'; +import type { ColumnsSelection, Placeholder, SQL, SQLWrapper, View } from '~/sql/sql.ts'; import type { Subquery } from '~/subquery.ts'; import type { Table, UpdateTableConfig } from '~/table.ts'; import type { Assume, ValueOrArray } from '~/utils.ts'; -import type { ColumnsSelection, View } from '~/view.ts'; import type { PreparedQuery, PreparedQueryConfig } from '../session.ts'; import type { PgSelectBase, PgSelectQueryBuilderBase } from './select.ts'; diff --git a/drizzle-orm/src/pg-core/query-builders/update.ts b/drizzle-orm/src/pg-core/query-builders/update.ts index 6b7b24d92..449f99149 100644 --- a/drizzle-orm/src/pg-core/query-builders/update.ts +++ b/drizzle-orm/src/pg-core/query-builders/update.ts @@ -11,7 +11,7 @@ import type { import type { PgTable } from '~/pg-core/table.ts'; import type { SelectResultFields } from '~/query-builders/select.types.ts'; import { QueryPromise } from '~/query-promise.ts'; -import type { Query, SQL, SQLWrapper } from '~/sql/index.ts'; +import type { Query, SQL, SQLWrapper } from '~/sql/sql.ts'; import { Table } from '~/table.ts'; import { mapUpdateSet, orderSelectedFields, type UpdateSet } from '~/utils.ts'; import type { SelectedFields, SelectedFieldsOrdered } from './select.types.ts'; diff --git a/drizzle-orm/src/pg-core/subquery.ts b/drizzle-orm/src/pg-core/subquery.ts index fefb08cac..02d78dee1 100644 --- a/drizzle-orm/src/pg-core/subquery.ts +++ b/drizzle-orm/src/pg-core/subquery.ts @@ -1,6 +1,6 @@ import type { AddAliasToSelection } from '~/query-builders/select.types.ts'; +import type { ColumnsSelection } from '~/sql/sql.ts'; import type { Subquery, WithSubquery } from '~/subquery.ts'; -import type { ColumnsSelection } from '~/view.ts'; export type SubqueryWithSelection = & Subquery> diff --git a/drizzle-orm/src/pg-core/view-base.ts b/drizzle-orm/src/pg-core/view-base.ts new file mode 100644 index 000000000..87f76ac24 --- /dev/null +++ b/drizzle-orm/src/pg-core/view-base.ts @@ -0,0 +1,14 @@ +import { entityKind } from '~/entity.ts'; +import { type ColumnsSelection, View } from '~/sql/sql.ts'; + +export abstract class PgViewBase< + TName extends string = string, + TExisting extends boolean = boolean, + TSelectedFields extends ColumnsSelection = ColumnsSelection, +> extends View { + static readonly [entityKind]: string = 'PgViewBase'; + + declare readonly _: View['_'] & { + readonly viewBrand: 'PgViewBase'; + }; +} diff --git a/drizzle-orm/src/pg-core/view.ts b/drizzle-orm/src/pg-core/view.ts index 4e4eed3ce..99a06036d 100644 --- a/drizzle-orm/src/pg-core/view.ts +++ b/drizzle-orm/src/pg-core/view.ts @@ -2,15 +2,15 @@ import type { BuildColumns } from '~/column-builder.ts'; import { entityKind } from '~/entity.ts'; import type { TypedQueryBuilder } from '~/query-builders/query-builder.ts'; import type { AddAliasToSelection } from '~/query-builders/select.types.ts'; -import type { SQL } from '~/sql/index.ts'; -import { SelectionProxyHandler } from '~/subquery.ts'; +import type { SQL, ColumnsSelection } from '~/sql/sql.ts'; import { getTableColumns } from '~/utils.ts'; -import { type ColumnsSelection, View } from '~/view.ts'; import type { PgColumn, PgColumnBuilderBase } from './columns/common.ts'; -import { QueryBuilder } from './query-builders/index.ts'; +import { QueryBuilder } from './query-builders/query-builder.ts'; import type { SelectedFields } from './query-builders/select.types.ts'; import { pgTable } from './table.ts'; import { PgViewConfig } from './view-common.ts'; +import { PgViewBase } from './view-base.ts'; +import { SelectionProxyHandler } from '~/selection-proxy.ts'; export interface ViewWithConfig { checkOption: 'local' | 'cascaded'; @@ -271,18 +271,6 @@ export class ManualMaterializedViewBuilder< } } -export abstract class PgViewBase< - TName extends string = string, - TExisting extends boolean = boolean, - TSelectedFields extends ColumnsSelection = ColumnsSelection, -> extends View { - static readonly [entityKind]: string = 'PgViewBase'; - - declare readonly _: View['_'] & { - readonly viewBrand: 'PgViewBase'; - }; -} - export class PgView< TName extends string = string, TExisting extends boolean = boolean, diff --git a/drizzle-orm/src/pg-proxy/migrator.ts b/drizzle-orm/src/pg-proxy/migrator.ts index 70b8c8615..f1b3fb107 100644 --- a/drizzle-orm/src/pg-proxy/migrator.ts +++ b/drizzle-orm/src/pg-proxy/migrator.ts @@ -1,6 +1,6 @@ import type { MigrationConfig } from '~/migrator.ts'; import { readMigrationFiles } from '~/migrator.ts'; -import { sql } from '~/sql/index.ts'; +import { sql } from '~/sql/sql.ts'; import type { PgRemoteDatabase } from './driver.ts'; export type ProxyMigrator = (migrationQueries: string[]) => Promise; diff --git a/drizzle-orm/src/pg-proxy/session.ts b/drizzle-orm/src/pg-proxy/session.ts index 444decb6b..cdff2beca 100644 --- a/drizzle-orm/src/pg-proxy/session.ts +++ b/drizzle-orm/src/pg-proxy/session.ts @@ -7,7 +7,7 @@ import type { SelectedFieldsOrdered } from '~/pg-core/query-builders/select.type import type { PgTransactionConfig, PreparedQueryConfig, QueryResultHKT } from '~/pg-core/session.ts'; import { PgSession, PreparedQuery as PreparedQueryBase } from '~/pg-core/session.ts'; import type { RelationalSchemaConfig, TablesRelationalConfig } from '~/relations.ts'; -import { fillPlaceholders, type Query } from '~/sql/index.ts'; +import { fillPlaceholders, type Query } from '~/sql/sql.ts'; import { tracer } from '~/tracing.ts'; import { type Assume, mapResultRow } from '~/utils.ts'; import type { RemoteCallback } from './driver.ts'; diff --git a/drizzle-orm/src/planetscale-serverless/session.ts b/drizzle-orm/src/planetscale-serverless/session.ts index 2f235af41..913008c95 100644 --- a/drizzle-orm/src/planetscale-serverless/session.ts +++ b/drizzle-orm/src/planetscale-serverless/session.ts @@ -13,7 +13,7 @@ import { type QueryResultHKT, } from '~/mysql-core/session.ts'; import type { RelationalSchemaConfig, TablesRelationalConfig } from '~/relations.ts'; -import { fillPlaceholders, type Query, type SQL, sql } from '~/sql/index.ts'; +import { fillPlaceholders, type Query, type SQL, sql } from '~/sql/sql.ts'; import { type Assume, mapResultRow } from '~/utils.ts'; export type PlanetScaleConnection = Connection; diff --git a/drizzle-orm/src/postgres-js/session.ts b/drizzle-orm/src/postgres-js/session.ts index a430f2415..f861b260f 100644 --- a/drizzle-orm/src/postgres-js/session.ts +++ b/drizzle-orm/src/postgres-js/session.ts @@ -8,7 +8,7 @@ import type { SelectedFieldsOrdered } from '~/pg-core/query-builders/select.type import type { PgTransactionConfig, PreparedQueryConfig, QueryResultHKT } from '~/pg-core/session.ts'; import { PgSession, PreparedQuery } from '~/pg-core/session.ts'; import type { RelationalSchemaConfig, TablesRelationalConfig } from '~/relations.ts'; -import { fillPlaceholders, type Query } from '~/sql/index.ts'; +import { fillPlaceholders, type Query } from '~/sql/sql.ts'; import { tracer } from '~/tracing.ts'; import { type Assume, mapResultRow } from '~/utils.ts'; diff --git a/drizzle-orm/src/query-builders/select.types.ts b/drizzle-orm/src/query-builders/select.types.ts index 2477ba0ba..9cda2ab8d 100644 --- a/drizzle-orm/src/query-builders/select.types.ts +++ b/drizzle-orm/src/query-builders/select.types.ts @@ -1,11 +1,10 @@ import type { ChangeColumnTableName, Dialect } from '~/column-builder.ts'; import type { AnyColumn, Column, GetColumnData, UpdateColConfig } from '~/column.ts'; import type { SelectedFields } from '~/operations.ts'; -import type { SQL } from '~/sql/index.ts'; +import type { ColumnsSelection, SQL, View } from '~/sql/sql.ts'; import type { Subquery } from '~/subquery.ts'; import type { Table } from '~/table.ts'; import type { Assume, DrizzleTypeError, Equal, IsAny, Simplify } from '~/utils.ts'; -import type { ColumnsSelection, View } from '~/view.ts'; export type JoinType = 'inner' | 'left' | 'right' | 'full'; diff --git a/drizzle-orm/src/relations.ts b/drizzle-orm/src/relations.ts index 5bd0d6398..b4750f794 100644 --- a/drizzle-orm/src/relations.ts +++ b/drizzle-orm/src/relations.ts @@ -1,7 +1,7 @@ import { type AnyTable, type InferModelFromColumns, isTable, Table } from '~/table.ts'; import { type AnyColumn, Column } from './column.ts'; import { entityKind, is } from './entity.ts'; -import { PrimaryKeyBuilder } from './pg-core/index.ts'; +import { PrimaryKeyBuilder } from './pg-core/primary-keys.ts'; import { and, asc, @@ -26,10 +26,8 @@ import { notInArray, notLike, or, - type Placeholder, - SQL, - sql, -} from './sql/index.ts'; +} from './sql/expressions/index.ts'; +import { type Placeholder, SQL, sql } from './sql/sql.ts'; import type { Assume, ColumnsWithTable, Equal, Simplify, ValueOrArray } from './utils.ts'; export abstract class Relation { diff --git a/drizzle-orm/src/selection-proxy.ts b/drizzle-orm/src/selection-proxy.ts new file mode 100644 index 000000000..9127793ac --- /dev/null +++ b/drizzle-orm/src/selection-proxy.ts @@ -0,0 +1,121 @@ +import { ColumnAliasProxyHandler, TableAliasProxyHandler } from './alias.ts'; +import { Column } from './column.ts'; +import { entityKind, is } from './entity.ts'; +import { SQL, View } from './sql/sql.ts'; +import { Subquery, SubqueryConfig } from './subquery.ts'; +import { ViewBaseConfig } from './view-common.ts'; + +export class SelectionProxyHandler | View> + implements ProxyHandler | View> +{ + static readonly [entityKind]: string = 'SelectionProxyHandler'; + + private config: { + /** + * Table alias for the columns + */ + alias?: string; + /** + * What to do when a field is an instance of `SQL.Aliased` and it's not a selection field (from a subquery) + * + * `sql` - return the underlying SQL expression + * + * `alias` - return the field alias + */ + sqlAliasedBehavior: 'sql' | 'alias'; + /** + * What to do when a field is an instance of `SQL` and it doesn't have an alias declared + * + * `sql` - return the underlying SQL expression + * + * `error` - return a DrizzleTypeError on type level and throw an error on runtime + */ + sqlBehavior: 'sql' | 'error'; + + /** + * Whether to replace the original name of the column with the alias + * Should be set to `true` for views creation + * @default false + */ + replaceOriginalName?: boolean; + }; + + constructor(config: SelectionProxyHandler['config']) { + this.config = { ...config }; + } + + get(subquery: T, prop: string | symbol): any { + if (prop === SubqueryConfig) { + return { + ...subquery[SubqueryConfig as keyof typeof subquery], + selection: new Proxy( + (subquery as Subquery)[SubqueryConfig].selection, + this as ProxyHandler>, + ), + }; + } + + if (prop === ViewBaseConfig) { + return { + ...subquery[ViewBaseConfig as keyof typeof subquery], + selectedFields: new Proxy( + (subquery as View)[ViewBaseConfig].selectedFields, + this as ProxyHandler>, + ), + }; + } + + if (typeof prop === 'symbol') { + return subquery[prop as keyof typeof subquery]; + } + + const columns = is(subquery, Subquery) + ? subquery[SubqueryConfig].selection + : is(subquery, View) + ? subquery[ViewBaseConfig].selectedFields + : subquery; + const value: unknown = columns[prop as keyof typeof columns]; + + if (is(value, SQL.Aliased)) { + // Never return the underlying SQL expression for a field previously selected in a subquery + if (this.config.sqlAliasedBehavior === 'sql' && !value.isSelectionField) { + return value.sql; + } + + const newValue = value.clone(); + newValue.isSelectionField = true; + return newValue; + } + + if (is(value, SQL)) { + if (this.config.sqlBehavior === 'sql') { + return value; + } + + throw new Error( + `You tried to reference "${prop}" field from a subquery, which is a raw SQL field, but it doesn't have an alias declared. Please add an alias to the field using ".as('alias')" method.`, + ); + } + + if (is(value, Column)) { + if (this.config.alias) { + return new Proxy( + value, + new ColumnAliasProxyHandler( + new Proxy( + value.table, + new TableAliasProxyHandler(this.config.alias, this.config.replaceOriginalName ?? false), + ), + ), + ); + } + return value; + } + + if (typeof value !== 'object' || value === null) { + return value; + } + + return new Proxy(value, new SelectionProxyHandler(this.config)); + } +} diff --git a/drizzle-orm/src/sql-js/session.ts b/drizzle-orm/src/sql-js/session.ts index 24d173857..978de6e3e 100644 --- a/drizzle-orm/src/sql-js/session.ts +++ b/drizzle-orm/src/sql-js/session.ts @@ -3,7 +3,7 @@ import { entityKind } from '~/entity.ts'; import type { Logger } from '~/logger.ts'; import { NoopLogger } from '~/logger.ts'; import type { RelationalSchemaConfig, TablesRelationalConfig } from '~/relations.ts'; -import { fillPlaceholders, type Query, sql } from '~/sql/index.ts'; +import { fillPlaceholders, type Query, sql } from '~/sql/sql.ts'; import type { SQLiteSyncDialect } from '~/sqlite-core/dialect.ts'; import { SQLiteTransaction } from '~/sqlite-core/index.ts'; import type { SelectedFieldsOrdered } from '~/sqlite-core/query-builders/select.types.ts'; diff --git a/drizzle-orm/src/sql/expressions/conditions.ts b/drizzle-orm/src/sql/expressions/conditions.ts index 3a2f78bfd..d1fa377ce 100644 --- a/drizzle-orm/src/sql/expressions/conditions.ts +++ b/drizzle-orm/src/sql/expressions/conditions.ts @@ -1,7 +1,6 @@ import { type AnyColumn, Column, type GetColumnData } from '~/column.ts'; import { is } from '~/entity.ts'; import { Table } from '~/table.ts'; -import { View } from '~/view.ts'; import { isDriverValueEncoder, isSQLWrapper, @@ -12,6 +11,7 @@ import { type SQLChunk, type SQLWrapper, StringChunk, + View, } from '../sql.ts'; export function bindIfParam(value: unknown, column: SQLWrapper): SQLChunk { diff --git a/drizzle-orm/src/sql/sql.ts b/drizzle-orm/src/sql/sql.ts index b4ec5fca2..a40eb079d 100644 --- a/drizzle-orm/src/sql/sql.ts +++ b/drizzle-orm/src/sql/sql.ts @@ -3,10 +3,10 @@ import { Relation } from '~/relations.ts'; import { Subquery, SubqueryConfig } from '~/subquery.ts'; import { tracer } from '~/tracing.ts'; import { ViewBaseConfig } from '~/view-common.ts'; -import { View } from '~/view.ts'; import type { AnyColumn } from '../column.ts'; import { Column } from '../column.ts'; import { Table } from '../table.ts'; +import type { SelectedFields } from '~/operations.ts'; /** * This class is used to indicate a primitive param value that is used in `sql` tag. @@ -578,6 +578,58 @@ export function fillPlaceholders(params: unknown[], values: Record; + +export abstract class View< + TName extends string = string, + TExisting extends boolean = boolean, + TSelection extends ColumnsSelection = ColumnsSelection, +> implements SQLWrapper { + static readonly [entityKind]: string = 'View'; + + declare _: { + brand: 'View'; + viewBrand: string; + name: TName; + existing: TExisting; + selectedFields: TSelection; + }; + + /** @internal */ + [ViewBaseConfig]: { + name: TName; + originalName: TName; + schema: string | undefined; + selectedFields: SelectedFields; + isExisting: TExisting; + query: TExisting extends true ? undefined : SQL; + isAlias: boolean; + }; + + constructor( + { name, schema, selectedFields, query }: { + name: TName; + schema: string | undefined; + selectedFields: SelectedFields; + query: SQL | undefined; + }, + ) { + this[ViewBaseConfig] = { + name, + originalName: name, + schema, + selectedFields, + query: query as (TExisting extends true ? undefined : SQL), + isExisting: !query as TExisting, + isAlias: false, + }; + } + + getSQL(): SQL { + return new SQL([this]); + } +} + // Defined separately from the Column class to resolve circular dependency Column.prototype.getSQL = function() { return new SQL([this]); @@ -587,3 +639,8 @@ Column.prototype.getSQL = function() { Table.prototype.getSQL = function() { return new SQL([this]); }; + +// Defined separately from the Column class to resolve circular dependency +Subquery.prototype.getSQL = function() { + return new SQL([this]); +}; \ No newline at end of file diff --git a/drizzle-orm/src/sqlite-core/alias.ts b/drizzle-orm/src/sqlite-core/alias.ts index bb873baa9..caf7ac9bb 100644 --- a/drizzle-orm/src/sqlite-core/alias.ts +++ b/drizzle-orm/src/sqlite-core/alias.ts @@ -2,7 +2,7 @@ import { TableAliasProxyHandler } from '~/alias.ts'; import type { BuildAliasTable } from './query-builders/select.types.ts'; import type { SQLiteTable } from './table.ts'; -import type { SQLiteViewBase } from './view.ts'; +import type { SQLiteViewBase } from './view-base.ts'; export function alias( table: TTable, diff --git a/drizzle-orm/src/sqlite-core/checks.ts b/drizzle-orm/src/sqlite-core/checks.ts index df992f7c5..8204a54f0 100644 --- a/drizzle-orm/src/sqlite-core/checks.ts +++ b/drizzle-orm/src/sqlite-core/checks.ts @@ -1,5 +1,5 @@ import { entityKind } from '~/entity.ts'; -import type { SQL } from '~/sql/index.ts'; +import type { SQL } from '~/sql/sql.ts'; import type { SQLiteTable } from './table.ts'; export class CheckBuilder { diff --git a/drizzle-orm/src/sqlite-core/columns/custom.ts b/drizzle-orm/src/sqlite-core/columns/custom.ts index 5e0ff5732..e8b765ffc 100644 --- a/drizzle-orm/src/sqlite-core/columns/custom.ts +++ b/drizzle-orm/src/sqlite-core/columns/custom.ts @@ -1,7 +1,7 @@ import type { ColumnBuilderBaseConfig, ColumnBuilderRuntimeConfig, MakeColumnConfig } from '~/column-builder.ts'; import type { ColumnBaseConfig } from '~/column.ts'; import { entityKind } from '~/entity.ts'; -import type { SQL } from '~/sql/index.ts'; +import type { SQL } from '~/sql/sql.ts'; import type { AnySQLiteTable } from '~/sqlite-core/table.ts'; import type { Equal } from '~/utils.ts'; import { SQLiteColumn, SQLiteColumnBuilder } from './common.ts'; diff --git a/drizzle-orm/src/sqlite-core/columns/integer.ts b/drizzle-orm/src/sqlite-core/columns/integer.ts index 86db7b08a..77f64069c 100644 --- a/drizzle-orm/src/sqlite-core/columns/integer.ts +++ b/drizzle-orm/src/sqlite-core/columns/integer.ts @@ -8,7 +8,7 @@ import type { } from '~/column-builder.ts'; import type { ColumnBaseConfig } from '~/column.ts'; import { entityKind } from '~/entity.ts'; -import { sql } from '~/sql/index.ts'; +import { sql } from '~/sql/sql.ts'; import type { OnConflict } from '~/sqlite-core/utils.ts'; import type { Equal, Or } from '~/utils.ts'; import type { AnySQLiteTable } from '../table.ts'; diff --git a/drizzle-orm/src/sqlite-core/db.ts b/drizzle-orm/src/sqlite-core/db.ts index 2f36a4d43..18594a3ba 100644 --- a/drizzle-orm/src/sqlite-core/db.ts +++ b/drizzle-orm/src/sqlite-core/db.ts @@ -1,7 +1,7 @@ import { entityKind } from '~/entity.ts'; import type { TypedQueryBuilder } from '~/query-builders/query-builder.ts'; import type { ExtractTablesWithRelations, RelationalSchemaConfig, TablesRelationalConfig } from '~/relations.ts'; -import type { SQLWrapper } from '~/sql/index.ts'; +import type { ColumnsSelection, SQLWrapper } from '~/sql/sql.ts'; import type { SQLiteAsyncDialect, SQLiteSyncDialect } from '~/sqlite-core/dialect.ts'; import { QueryBuilder, @@ -18,13 +18,13 @@ import type { SQLiteTransactionConfig, } from '~/sqlite-core/session.ts'; import type { SQLiteTable } from '~/sqlite-core/table.ts'; -import { SelectionProxyHandler, WithSubquery } from '~/subquery.ts'; +import { WithSubquery } from '~/subquery.ts'; import type { DrizzleTypeError } from '~/utils.ts'; -import type { ColumnsSelection } from '~/view.ts'; import { RelationalQueryBuilder } from './query-builders/query.ts'; import { SQLiteRaw } from './query-builders/raw.ts'; import type { SelectedFields } from './query-builders/select.types.ts'; import type { WithSubqueryWithSelection } from './subquery.ts'; +import { SelectionProxyHandler } from '~/selection-proxy.ts'; export class BaseSQLiteDatabase< TResultKind extends 'sync' | 'async', diff --git a/drizzle-orm/src/sqlite-core/dialect.ts b/drizzle-orm/src/sqlite-core/dialect.ts index c6e64da60..14b99b6ad 100644 --- a/drizzle-orm/src/sqlite-core/dialect.ts +++ b/drizzle-orm/src/sqlite-core/dialect.ts @@ -16,7 +16,8 @@ import { type TableRelationalConfig, type TablesRelationalConfig, } from '~/relations.ts'; -import { and, eq, Param, type QueryWithTypings, SQL, sql, type SQLChunk } from '~/sql/index.ts'; +import { Param, type QueryWithTypings, SQL, sql, type SQLChunk } from '~/sql/sql.ts'; +import { and, eq } from '~/sql/index.ts' import { SQLiteColumn } from '~/sqlite-core/columns/index.ts'; import type { SQLiteDeleteConfig, SQLiteInsertConfig, SQLiteUpdateConfig } from '~/sqlite-core/query-builders/index.ts'; import { SQLiteTable } from '~/sqlite-core/table.ts'; @@ -30,7 +31,7 @@ import type { SQLiteSelectJoinConfig, } from './query-builders/select.types.ts'; import type { SQLiteSession } from './session.ts'; -import { SQLiteViewBase } from './view.ts'; +import { SQLiteViewBase } from './view-base.ts'; export abstract class SQLiteDialect { static readonly [entityKind]: string = 'SQLiteDialect'; diff --git a/drizzle-orm/src/sqlite-core/expressions.ts b/drizzle-orm/src/sqlite-core/expressions.ts index 5153c2bfa..c04db49d1 100644 --- a/drizzle-orm/src/sqlite-core/expressions.ts +++ b/drizzle-orm/src/sqlite-core/expressions.ts @@ -1,6 +1,6 @@ import { bindIfParam } from '~/expressions.ts'; -import type { SQL, SQLChunk, SQLWrapper } from '~/sql/index.ts'; -import { sql } from '~/sql/index.ts'; +import type { SQL, SQLChunk, SQLWrapper } from '~/sql/sql.ts'; +import { sql } from '~/sql/sql.ts'; import type { SQLiteColumn } from '~/sqlite-core/columns/index.ts'; export * from '~/expressions.ts'; diff --git a/drizzle-orm/src/sqlite-core/indexes.ts b/drizzle-orm/src/sqlite-core/indexes.ts index 8bb004940..70ca62a8c 100644 --- a/drizzle-orm/src/sqlite-core/indexes.ts +++ b/drizzle-orm/src/sqlite-core/indexes.ts @@ -1,5 +1,5 @@ import { entityKind } from '~/entity.ts'; -import type { SQL } from '~/sql/index.ts'; +import type { SQL } from '~/sql/sql.ts'; import type { SQLiteColumn } from './columns/index.ts'; import type { SQLiteTable } from './table.ts'; diff --git a/drizzle-orm/src/sqlite-core/query-builders/delete.ts b/drizzle-orm/src/sqlite-core/query-builders/delete.ts index 746bb28fb..2296310f5 100644 --- a/drizzle-orm/src/sqlite-core/query-builders/delete.ts +++ b/drizzle-orm/src/sqlite-core/query-builders/delete.ts @@ -2,7 +2,7 @@ import { entityKind } from '~/entity.ts'; import type { SelectResultFields } from '~/query-builders/select.types.ts'; import { QueryPromise } from '~/query-promise.ts'; import type { RunnableQuery } from '~/runnable-query.ts'; -import type { Query, SQL, SQLWrapper } from '~/sql/index.ts'; +import type { Query, SQL, SQLWrapper } from '~/sql/sql.ts'; import type { SQLiteDialect } from '~/sqlite-core/dialect.ts'; import type { SQLitePreparedQuery, SQLiteSession } from '~/sqlite-core/session.ts'; import { SQLiteTable } from '~/sqlite-core/table.ts'; diff --git a/drizzle-orm/src/sqlite-core/query-builders/insert.ts b/drizzle-orm/src/sqlite-core/query-builders/insert.ts index 669ee5ac5..1190fe79e 100644 --- a/drizzle-orm/src/sqlite-core/query-builders/insert.ts +++ b/drizzle-orm/src/sqlite-core/query-builders/insert.ts @@ -2,8 +2,8 @@ import { entityKind, is } from '~/entity.ts'; import type { SelectResultFields } from '~/query-builders/select.types.ts'; import { QueryPromise } from '~/query-promise.ts'; import type { RunnableQuery } from '~/runnable-query.ts'; -import type { Placeholder, Query, SQLWrapper } from '~/sql/index.ts'; -import { Param, SQL, sql } from '~/sql/index.ts'; +import type { Placeholder, Query, SQLWrapper } from '~/sql/sql.ts'; +import { Param, SQL, sql } from '~/sql/sql.ts'; import type { SQLiteDialect } from '~/sqlite-core/dialect.ts'; import type { IndexColumn } from '~/sqlite-core/indexes.ts'; import type { SQLitePreparedQuery, SQLiteSession } from '~/sqlite-core/session.ts'; diff --git a/drizzle-orm/src/sqlite-core/query-builders/query-builder.ts b/drizzle-orm/src/sqlite-core/query-builders/query-builder.ts index 1e30669bd..54b615b4d 100644 --- a/drizzle-orm/src/sqlite-core/query-builders/query-builder.ts +++ b/drizzle-orm/src/sqlite-core/query-builders/query-builder.ts @@ -2,10 +2,11 @@ import { entityKind } from '~/entity.ts'; import type { TypedQueryBuilder } from '~/query-builders/query-builder.ts'; import { SQLiteSyncDialect } from '~/sqlite-core/dialect.ts'; import type { WithSubqueryWithSelection } from '~/sqlite-core/subquery.ts'; -import { SelectionProxyHandler, WithSubquery } from '~/subquery.ts'; -import type { ColumnsSelection } from '~/view.ts'; import { SQLiteSelectBuilder } from './select.ts'; import type { SelectedFields } from './select.types.ts'; +import type { ColumnsSelection } from '~/sql/sql.ts'; +import { WithSubquery } from '~/subquery.ts'; +import { SelectionProxyHandler } from '~/selection-proxy.ts'; export class QueryBuilder { static readonly [entityKind]: string = 'SQLiteQueryBuilder'; diff --git a/drizzle-orm/src/sqlite-core/query-builders/query.ts b/drizzle-orm/src/sqlite-core/query-builders/query.ts index b301acdb2..a8f04b764 100644 --- a/drizzle-orm/src/sqlite-core/query-builders/query.ts +++ b/drizzle-orm/src/sqlite-core/query-builders/query.ts @@ -9,7 +9,7 @@ import { type TablesRelationalConfig, } from '~/relations.ts'; import type { RunnableQuery } from '~/runnable-query.ts'; -import type { Query, QueryWithTypings, SQL, SQLWrapper } from '~/sql/index.ts'; +import type { Query, QueryWithTypings, SQL, SQLWrapper } from '~/sql/sql.ts'; import type { KnownKeysOnly } from '~/utils.ts'; import type { SQLiteDialect } from '../dialect.ts'; import type { PreparedQueryConfig, SQLitePreparedQuery, SQLiteSession } from '../session.ts'; diff --git a/drizzle-orm/src/sqlite-core/query-builders/select.ts b/drizzle-orm/src/sqlite-core/query-builders/select.ts index a8e0f72bf..02f30316c 100644 --- a/drizzle-orm/src/sqlite-core/query-builders/select.ts +++ b/drizzle-orm/src/sqlite-core/query-builders/select.ts @@ -11,18 +11,16 @@ import type { } from '~/query-builders/select.types.ts'; import { QueryPromise } from '~/query-promise.ts'; import type { RunnableQuery } from '~/runnable-query.ts'; -import { type Placeholder, type Query, SQL } from '~/sql/index.ts'; +import { SQL, View } from '~/sql/sql.ts'; +import type { ColumnsSelection, Placeholder, Query } from '~/sql/sql.ts'; import type { SQLiteColumn } from '~/sqlite-core/columns/index.ts'; import type { SQLiteDialect } from '~/sqlite-core/dialect.ts'; import type { SQLiteSession } from '~/sqlite-core/session.ts'; import type { SubqueryWithSelection } from '~/sqlite-core/subquery.ts'; import type { SQLiteTable } from '~/sqlite-core/table.ts'; -import { SelectionProxyHandler, Subquery, SubqueryConfig } from '~/subquery.ts'; import { Table } from '~/table.ts'; import { applyMixins, getTableColumns, getTableLikeName, orderSelectedFields, type ValueOrArray } from '~/utils.ts'; import { ViewBaseConfig } from '~/view-common.ts'; -import { type ColumnsSelection, View } from '~/view.ts'; -import { SQLiteViewBase } from '../view.ts'; import type { CreateSQLiteSelectFromBuilderMode, SelectedFields, @@ -35,6 +33,9 @@ import type { SQLiteSelectPrepare, SQLiteSelectWithout, } from './select.types.ts'; +import { Subquery, SubqueryConfig } from '~/subquery.ts'; +import { SQLiteViewBase } from '../view-base.ts'; +import { SelectionProxyHandler } from '~/selection-proxy.ts'; export class SQLiteSelectBuilder< TSelection extends SelectedFields | undefined, diff --git a/drizzle-orm/src/sqlite-core/query-builders/select.types.ts b/drizzle-orm/src/sqlite-core/query-builders/select.types.ts index d2cb53df8..8e51819d4 100644 --- a/drizzle-orm/src/sqlite-core/query-builders/select.types.ts +++ b/drizzle-orm/src/sqlite-core/query-builders/select.types.ts @@ -1,4 +1,4 @@ -import type { Placeholder, SQL } from '~/sql/index.ts'; +import type { ColumnsSelection, Placeholder, SQL, View } from '~/sql/sql.ts'; import type { Assume } from '~/utils.ts'; import type { SQLiteColumn } from '~/sqlite-core/columns/index.ts'; @@ -22,10 +22,10 @@ import type { } from '~/query-builders/select.types.ts'; import type { Subquery } from '~/subquery.ts'; import type { Table, UpdateTableConfig } from '~/table.ts'; -import type { ColumnsSelection, View } from '~/view.ts'; import type { SQLitePreparedQuery } from '../session.ts'; -import type { SQLiteViewBase, SQLiteViewWithSelection } from '../view.ts'; import type { SQLiteSelectBase, SQLiteSelectQueryBuilderBase } from './select.ts'; +import type { SQLiteViewBase } from '../view-base.ts'; +import type { SQLiteViewWithSelection } from '../view.ts'; export interface SQLiteSelectJoinConfig { on: SQL | undefined; diff --git a/drizzle-orm/src/sqlite-core/query-builders/update.ts b/drizzle-orm/src/sqlite-core/query-builders/update.ts index aaafada7e..857a944d8 100644 --- a/drizzle-orm/src/sqlite-core/query-builders/update.ts +++ b/drizzle-orm/src/sqlite-core/query-builders/update.ts @@ -3,7 +3,7 @@ import { entityKind } from '~/entity.ts'; import type { SelectResultFields } from '~/query-builders/select.types.ts'; import { QueryPromise } from '~/query-promise.ts'; import type { RunnableQuery } from '~/runnable-query.ts'; -import type { Query, SQL, SQLWrapper } from '~/sql/index.ts'; +import type { Query, SQL, SQLWrapper } from '~/sql/sql.ts'; import type { SQLiteDialect } from '~/sqlite-core/dialect.ts'; import type { SQLitePreparedQuery, SQLiteSession } from '~/sqlite-core/session.ts'; import { SQLiteTable } from '~/sqlite-core/table.ts'; diff --git a/drizzle-orm/src/sqlite-core/session.ts b/drizzle-orm/src/sqlite-core/session.ts index d4afb4f9d..0dcfebe38 100644 --- a/drizzle-orm/src/sqlite-core/session.ts +++ b/drizzle-orm/src/sqlite-core/session.ts @@ -2,7 +2,7 @@ import { entityKind } from '~/entity.ts'; import { DrizzleError, TransactionRollbackError } from '~/errors.ts'; import type { TablesRelationalConfig } from '~/relations.ts'; import type { PreparedQuery } from '~/session.ts'; -import type { Query, SQL } from '~/sql/index.ts'; +import type { Query, SQL } from '~/sql/sql.ts'; import type { SQLiteAsyncDialect, SQLiteSyncDialect } from '~/sqlite-core/dialect.ts'; import { QueryPromise } from '../index.ts'; import { BaseSQLiteDatabase } from './db.ts'; diff --git a/drizzle-orm/src/sqlite-core/subquery.ts b/drizzle-orm/src/sqlite-core/subquery.ts index be16261a6..587f53e7d 100644 --- a/drizzle-orm/src/sqlite-core/subquery.ts +++ b/drizzle-orm/src/sqlite-core/subquery.ts @@ -1,6 +1,6 @@ import type { AddAliasToSelection } from '~/query-builders/select.types.ts'; import type { Subquery, WithSubquery } from '~/subquery.ts'; -import type { ColumnsSelection } from '~/view.ts'; +import type { ColumnsSelection } from '~/sql/sql.ts'; export type SubqueryWithSelection = & Subquery> diff --git a/drizzle-orm/src/sqlite-core/view-base.ts b/drizzle-orm/src/sqlite-core/view-base.ts new file mode 100644 index 000000000..64f6529d4 --- /dev/null +++ b/drizzle-orm/src/sqlite-core/view-base.ts @@ -0,0 +1,15 @@ +import { entityKind } from '~/entity.ts'; +import type { ColumnsSelection} from '~/sql/sql.ts'; +import { View } from '~/sql/sql.ts'; + +export abstract class SQLiteViewBase< + TName extends string = string, + TExisting extends boolean = boolean, + TSelection extends ColumnsSelection = ColumnsSelection, +> extends View { + static readonly [entityKind]: string = 'SQLiteViewBase'; + + declare _: View['_'] & { + viewBrand: 'SQLiteView'; + }; +} diff --git a/drizzle-orm/src/sqlite-core/view.ts b/drizzle-orm/src/sqlite-core/view.ts index 2a51c2ffa..d07a36ba7 100644 --- a/drizzle-orm/src/sqlite-core/view.ts +++ b/drizzle-orm/src/sqlite-core/view.ts @@ -2,15 +2,15 @@ import type { BuildColumns } from '~/column-builder.ts'; import { entityKind } from '~/entity.ts'; import type { TypedQueryBuilder } from '~/query-builders/query-builder.ts'; import type { AddAliasToSelection } from '~/query-builders/select.types.ts'; -import type { SQL } from '~/sql/index.ts'; -import { SelectionProxyHandler } from '~/subquery.ts'; +import type { ColumnsSelection, SQL } from '~/sql/sql.ts'; import { getTableColumns } from '~/utils.ts'; -import { type ColumnsSelection, View } from '~/view.ts'; import type { SQLiteColumn, SQLiteColumnBuilderBase } from './columns/common.ts'; import { QueryBuilder } from './query-builders/query-builder.ts'; import type { SelectedFields } from './query-builders/select.types.ts'; import { sqliteTable } from './table.ts'; import { SQLiteViewConfig } from './view-common.ts'; +import { SQLiteViewBase } from './view-base.ts'; +import { SelectionProxyHandler } from '~/selection-proxy.ts'; export interface ViewBuilderConfig { algorithm?: 'undefined' | 'merge' | 'temptable'; @@ -127,18 +127,6 @@ export class ManualViewBuilder< } } -export abstract class SQLiteViewBase< - TName extends string = string, - TExisting extends boolean = boolean, - TSelection extends ColumnsSelection = ColumnsSelection, -> extends View { - static readonly [entityKind]: string = 'SQLiteViewBase'; - - declare _: View['_'] & { - viewBrand: 'SQLiteView'; - }; -} - export class SQLiteView< TName extends string = string, TExisting extends boolean = boolean, diff --git a/drizzle-orm/src/sqlite-proxy/migrator.ts b/drizzle-orm/src/sqlite-proxy/migrator.ts index c5822a8c8..29448e01a 100644 --- a/drizzle-orm/src/sqlite-proxy/migrator.ts +++ b/drizzle-orm/src/sqlite-proxy/migrator.ts @@ -1,6 +1,6 @@ import type { MigrationConfig } from '~/migrator.ts'; import { readMigrationFiles } from '~/migrator.ts'; -import { sql } from '~/sql/index.ts'; +import { sql } from '~/sql/sql.ts'; import type { SqliteRemoteDatabase } from './driver.ts'; export type ProxyMigrator = (migrationQueries: string[]) => Promise; diff --git a/drizzle-orm/src/sqlite-proxy/session.ts b/drizzle-orm/src/sqlite-proxy/session.ts index b27c6c492..c8f35050c 100644 --- a/drizzle-orm/src/sqlite-proxy/session.ts +++ b/drizzle-orm/src/sqlite-proxy/session.ts @@ -2,7 +2,7 @@ import { entityKind } from '~/entity.ts'; import type { Logger } from '~/logger.ts'; import { NoopLogger } from '~/logger.ts'; import type { RelationalSchemaConfig, TablesRelationalConfig } from '~/relations.ts'; -import { fillPlaceholders, type Query, sql } from '~/sql/index.ts'; +import { fillPlaceholders, type Query, sql } from '~/sql/sql.ts'; import type { SQLiteAsyncDialect } from '~/sqlite-core/dialect.ts'; import { SQLiteTransaction } from '~/sqlite-core/index.ts'; import type { SelectedFieldsOrdered } from '~/sqlite-core/query-builders/select.types.ts'; diff --git a/drizzle-orm/src/subquery.ts b/drizzle-orm/src/subquery.ts index 8e1413528..2f292cf06 100644 --- a/drizzle-orm/src/subquery.ts +++ b/drizzle-orm/src/subquery.ts @@ -1,12 +1,12 @@ -import { ColumnAliasProxyHandler, TableAliasProxyHandler } from './alias.ts'; -import { Column } from './column.ts'; -import { entityKind, is } from './entity.ts'; -import { SQL, type SQLWrapper } from './sql/index.ts'; -import { ViewBaseConfig } from './view-common.ts'; -import { type ColumnsSelection, View } from './view.ts'; +import { entityKind } from './entity.ts'; +import type { SQL, SQLWrapper, ColumnsSelection } from './sql/sql.ts'; export const SubqueryConfig = Symbol.for('drizzle:SubqueryConfig'); +// eslint-disable-next-line @typescript-eslint/no-unused-vars +export interface Subquery extends SQLWrapper { + // SQLWrapper runtime implementation is defined in 'sql/sql.ts' +} export class Subquery implements SQLWrapper { static readonly [entityKind]: string = 'Subquery'; @@ -33,126 +33,11 @@ export class Subquery }; } - getSQL(): SQL { - return new SQL([this]); - } + // getSQL(): SQL { + // return new SQL([this]); + // } } export class WithSubquery extends Subquery { static readonly [entityKind]: string = 'WithSubquery'; -} - -export class SelectionProxyHandler | View> - implements ProxyHandler | View> -{ - static readonly [entityKind]: string = 'SelectionProxyHandler'; - - private config: { - /** - * Table alias for the columns - */ - alias?: string; - /** - * What to do when a field is an instance of `SQL.Aliased` and it's not a selection field (from a subquery) - * - * `sql` - return the underlying SQL expression - * - * `alias` - return the field alias - */ - sqlAliasedBehavior: 'sql' | 'alias'; - /** - * What to do when a field is an instance of `SQL` and it doesn't have an alias declared - * - * `sql` - return the underlying SQL expression - * - * `error` - return a DrizzleTypeError on type level and throw an error on runtime - */ - sqlBehavior: 'sql' | 'error'; - - /** - * Whether to replace the original name of the column with the alias - * Should be set to `true` for views creation - * @default false - */ - replaceOriginalName?: boolean; - }; - - constructor(config: SelectionProxyHandler['config']) { - this.config = { ...config }; - } - - get(subquery: T, prop: string | symbol): any { - if (prop === SubqueryConfig) { - return { - ...subquery[SubqueryConfig as keyof typeof subquery], - selection: new Proxy( - (subquery as Subquery)[SubqueryConfig].selection, - this as ProxyHandler>, - ), - }; - } - - if (prop === ViewBaseConfig) { - return { - ...subquery[ViewBaseConfig as keyof typeof subquery], - selectedFields: new Proxy( - (subquery as View)[ViewBaseConfig].selectedFields, - this as ProxyHandler>, - ), - }; - } - - if (typeof prop === 'symbol') { - return subquery[prop as keyof typeof subquery]; - } - - const columns = is(subquery, Subquery) - ? subquery[SubqueryConfig].selection - : is(subquery, View) - ? subquery[ViewBaseConfig].selectedFields - : subquery; - const value: unknown = columns[prop as keyof typeof columns]; - - if (is(value, SQL.Aliased)) { - // Never return the underlying SQL expression for a field previously selected in a subquery - if (this.config.sqlAliasedBehavior === 'sql' && !value.isSelectionField) { - return value.sql; - } - - const newValue = value.clone(); - newValue.isSelectionField = true; - return newValue; - } - - if (is(value, SQL)) { - if (this.config.sqlBehavior === 'sql') { - return value; - } - - throw new Error( - `You tried to reference "${prop}" field from a subquery, which is a raw SQL field, but it doesn't have an alias declared. Please add an alias to the field using ".as('alias')" method.`, - ); - } - - if (is(value, Column)) { - if (this.config.alias) { - return new Proxy( - value, - new ColumnAliasProxyHandler( - new Proxy( - value.table, - new TableAliasProxyHandler(this.config.alias, this.config.replaceOriginalName ?? false), - ), - ), - ); - } - return value; - } - - if (typeof value !== 'object' || value === null) { - return value; - } - - return new Proxy(value, new SelectionProxyHandler(this.config)); - } -} +} \ No newline at end of file diff --git a/drizzle-orm/src/tracing-utils.ts b/drizzle-orm/src/tracing-utils.ts new file mode 100644 index 000000000..b4809af25 --- /dev/null +++ b/drizzle-orm/src/tracing-utils.ts @@ -0,0 +1,3 @@ +export function iife(fn: (...args: T) => U, ...args: T): U { + return fn(...args); +} \ No newline at end of file diff --git a/drizzle-orm/src/tracing.ts b/drizzle-orm/src/tracing.ts index d18c2832e..7d5fd165b 100644 --- a/drizzle-orm/src/tracing.ts +++ b/drizzle-orm/src/tracing.ts @@ -1,5 +1,5 @@ import type { Span, Tracer } from '@opentelemetry/api'; -import { iife } from '~/utils.ts'; +import { iife } from '~/tracing-utils.ts'; import { npmVersion } from '~/version.ts'; let otel: typeof import('@opentelemetry/api') | undefined; diff --git a/drizzle-orm/src/utils.ts b/drizzle-orm/src/utils.ts index b92a58465..afb750c9d 100644 --- a/drizzle-orm/src/utils.ts +++ b/drizzle-orm/src/utils.ts @@ -4,12 +4,11 @@ import { is } from './entity.ts'; import type { Logger } from './logger.ts'; import type { SelectedFieldsOrdered } from './operations.ts'; import type { TableLike } from './query-builders/select.types.ts'; -import { Param, SQL } from './sql/index.ts'; -import type { DriverValueDecoder } from './sql/index.ts'; +import { Param, SQL, View } from './sql/sql.ts'; +import type { DriverValueDecoder } from './sql/sql.ts'; import { Subquery, SubqueryConfig } from './subquery.ts'; import { getTableName, Table } from './table.ts'; import { ViewBaseConfig } from './view-common.ts'; -import { View } from './view.ts'; /** @internal */ export function mapResultRow( @@ -199,8 +198,4 @@ export type KnownKeysOnly = { [K in keyof T]: K extends keyof U ? T[K] : never; }; -export function iife(fn: (...args: T) => U, ...args: T): U { - return fn(...args); -} - export type IsAny = 0 extends (1 & T) ? true : false; diff --git a/drizzle-orm/src/vercel-postgres/session.ts b/drizzle-orm/src/vercel-postgres/session.ts index 2dde91f6d..f85748b46 100644 --- a/drizzle-orm/src/vercel-postgres/session.ts +++ b/drizzle-orm/src/vercel-postgres/session.ts @@ -14,7 +14,7 @@ import type { SelectedFieldsOrdered } from '~/pg-core/query-builders/select.type import type { PgTransactionConfig, PreparedQueryConfig, QueryResultHKT } from '~/pg-core/session.ts'; import { PgSession, PreparedQuery } from '~/pg-core/session.ts'; import type { RelationalSchemaConfig, TablesRelationalConfig } from '~/relations.ts'; -import { fillPlaceholders, type Query, sql } from '~/sql/index.ts'; +import { fillPlaceholders, type Query, sql } from '~/sql/sql.ts'; import { type Assume, mapResultRow } from '~/utils.ts'; export type VercelPgClient = VercelPool | VercelClient | VercelPoolClient; diff --git a/drizzle-orm/src/view.ts b/drizzle-orm/src/view.ts deleted file mode 100644 index bd1485bda..000000000 --- a/drizzle-orm/src/view.ts +++ /dev/null @@ -1,58 +0,0 @@ -import type { AnyColumn } from './column.ts'; -import { entityKind } from './entity.ts'; -import type { SelectedFields } from './operations.ts'; -import { SQL, type SQLWrapper } from './sql/index.ts'; -import type { Table } from './table.ts'; -import { ViewBaseConfig } from './view-common.ts'; - -export type ColumnsSelection = Record; - -export abstract class View< - TName extends string = string, - TExisting extends boolean = boolean, - TSelection extends ColumnsSelection = ColumnsSelection, -> implements SQLWrapper { - static readonly [entityKind]: string = 'View'; - - declare _: { - brand: 'View'; - viewBrand: string; - name: TName; - existing: TExisting; - selectedFields: TSelection; - }; - - /** @internal */ - [ViewBaseConfig]: { - name: TName; - originalName: TName; - schema: string | undefined; - selectedFields: SelectedFields; - isExisting: TExisting; - query: TExisting extends true ? undefined : SQL; - isAlias: boolean; - }; - - constructor( - { name, schema, selectedFields, query }: { - name: TName; - schema: string | undefined; - selectedFields: SelectedFields; - query: SQL | undefined; - }, - ) { - this[ViewBaseConfig] = { - name, - originalName: name, - schema, - selectedFields, - query: query as (TExisting extends true ? undefined : SQL), - isExisting: !query as TExisting, - isAlias: false, - }; - } - - getSQL(): SQL { - return new SQL([this]); - } -} diff --git a/drizzle-orm/type-tests/mysql/db-rel.ts b/drizzle-orm/type-tests/mysql/db-rel.ts index 0d528b186..957e6c9ce 100644 --- a/drizzle-orm/type-tests/mysql/db-rel.ts +++ b/drizzle-orm/type-tests/mysql/db-rel.ts @@ -1,7 +1,7 @@ import pg from 'pg'; import { type Equal, Expect } from 'type-tests/utils.ts'; import { drizzle } from '~/node-postgres/index.ts'; -import { placeholder, sql } from '~/sql/index.ts'; +import { placeholder, sql } from '~/sql/sql.ts'; import * as schema from './tables-rel.ts'; const { Pool } = pg; diff --git a/drizzle-orm/type-tests/mysql/delete.ts b/drizzle-orm/type-tests/mysql/delete.ts index 314fd2ed5..c3e5afbb2 100644 --- a/drizzle-orm/type-tests/mysql/delete.ts +++ b/drizzle-orm/type-tests/mysql/delete.ts @@ -3,7 +3,7 @@ import { Expect } from 'type-tests/utils.ts'; import { eq } from '~/expressions.ts'; import type { MySqlDelete } from '~/mysql-core/index.ts'; import type { MySqlRawQueryResult } from '~/mysql2/index.ts'; -import { sql } from '~/sql/index.ts'; +import { sql } from '~/sql/sql.ts'; import { db } from './db.ts'; import { users } from './tables.ts'; diff --git a/drizzle-orm/type-tests/mysql/insert.ts b/drizzle-orm/type-tests/mysql/insert.ts index c5d9814ab..edc48d5a2 100644 --- a/drizzle-orm/type-tests/mysql/insert.ts +++ b/drizzle-orm/type-tests/mysql/insert.ts @@ -2,7 +2,7 @@ import type { Equal } from 'type-tests/utils.ts'; import { Expect } from 'type-tests/utils.ts'; import { int, type MySqlInsert, mysqlTable, text } from '~/mysql-core/index.ts'; import type { MySqlRawQueryResult } from '~/mysql2/index.ts'; -import { sql } from '~/sql/index.ts'; +import { sql } from '~/sql/sql.ts'; import { db } from './db.ts'; import { users } from './tables.ts'; diff --git a/drizzle-orm/type-tests/mysql/select.ts b/drizzle-orm/type-tests/mysql/select.ts index fd28ce598..0a6af743b 100644 --- a/drizzle-orm/type-tests/mysql/select.ts +++ b/drizzle-orm/type-tests/mysql/select.ts @@ -22,7 +22,7 @@ import { or, } from '~/expressions.ts'; import { alias } from '~/mysql-core/alias.ts'; -import { param, sql } from '~/sql/index.ts'; +import { param, sql } from '~/sql/sql.ts'; import type { Equal } from 'type-tests/utils.ts'; import { Expect } from 'type-tests/utils.ts'; diff --git a/drizzle-orm/type-tests/mysql/subquery.ts b/drizzle-orm/type-tests/mysql/subquery.ts index 920520e45..a76cd1919 100644 --- a/drizzle-orm/type-tests/mysql/subquery.ts +++ b/drizzle-orm/type-tests/mysql/subquery.ts @@ -1,7 +1,7 @@ import { Expect } from 'type-tests/utils.ts'; import { and, eq } from '~/expressions.ts'; import { alias, int, mysqlTable, serial, text } from '~/mysql-core/index.ts'; -import { sql } from '~/sql/index.ts'; +import { sql } from '~/sql/sql.ts'; import type { DrizzleTypeError, Equal } from '~/utils.ts'; import { db } from './db.ts'; diff --git a/drizzle-orm/type-tests/mysql/tables.ts b/drizzle-orm/type-tests/mysql/tables.ts index aad863086..6eac879da 100644 --- a/drizzle-orm/type-tests/mysql/tables.ts +++ b/drizzle-orm/type-tests/mysql/tables.ts @@ -27,7 +27,7 @@ import { } from '~/mysql-core/index.ts'; import { mysqlSchema } from '~/mysql-core/schema.ts'; import { mysqlView, type MySqlViewWithSelection } from '~/mysql-core/view.ts'; -import { sql } from '~/sql/index.ts'; +import { sql } from '~/sql/sql.ts'; import { db } from './db.ts'; export const users = mysqlTable( diff --git a/drizzle-orm/type-tests/mysql/update.ts b/drizzle-orm/type-tests/mysql/update.ts index 31139ee7a..dc6967f44 100644 --- a/drizzle-orm/type-tests/mysql/update.ts +++ b/drizzle-orm/type-tests/mysql/update.ts @@ -1,7 +1,7 @@ import { type Equal, Expect } from 'type-tests/utils.ts'; import type { MySqlUpdate } from '~/mysql-core/index.ts'; import type { MySqlRawQueryResult } from '~/mysql2/session.ts'; -import { sql } from '~/sql/index.ts'; +import { sql } from '~/sql/sql.ts'; import { db } from './db.ts'; import { users } from './tables.ts'; diff --git a/drizzle-orm/type-tests/mysql/with.ts b/drizzle-orm/type-tests/mysql/with.ts index eb36e520f..338f9c43b 100644 --- a/drizzle-orm/type-tests/mysql/with.ts +++ b/drizzle-orm/type-tests/mysql/with.ts @@ -2,7 +2,7 @@ import type { Equal } from 'type-tests/utils.ts'; import { Expect } from 'type-tests/utils.ts'; import { gt, inArray } from '~/expressions.ts'; import { int, mysqlTable, serial, text } from '~/mysql-core/index.ts'; -import { sql } from '~/sql/index.ts'; +import { sql } from '~/sql/sql.ts'; import { db } from './db.ts'; const orders = mysqlTable('orders', { diff --git a/drizzle-orm/type-tests/pg/db-rel.ts b/drizzle-orm/type-tests/pg/db-rel.ts index 3e90fbb8e..c8cc63c7f 100644 --- a/drizzle-orm/type-tests/pg/db-rel.ts +++ b/drizzle-orm/type-tests/pg/db-rel.ts @@ -1,7 +1,7 @@ import pg from 'pg'; import { type Equal, Expect } from 'type-tests/utils.ts'; import { drizzle } from '~/node-postgres/index.ts'; -import { sql } from '~/sql/index.ts'; +import { sql } from '~/sql/sql.ts'; import * as schema from './tables-rel.ts'; const { Pool } = pg; diff --git a/drizzle-orm/type-tests/pg/delete.ts b/drizzle-orm/type-tests/pg/delete.ts index 7fd9c3dc6..6421db090 100644 --- a/drizzle-orm/type-tests/pg/delete.ts +++ b/drizzle-orm/type-tests/pg/delete.ts @@ -3,7 +3,7 @@ import type { Equal } from 'type-tests/utils.ts'; import { Expect } from 'type-tests/utils.ts'; import { eq } from '~/expressions.ts'; import type { PgDelete } from '~/pg-core/index.ts'; -import { sql } from '~/sql/index.ts'; +import { sql } from '~/sql/sql.ts'; import { db } from './db.ts'; import { users } from './tables.ts'; diff --git a/drizzle-orm/type-tests/pg/insert.ts b/drizzle-orm/type-tests/pg/insert.ts index 1ea9e8bd4..6a5179804 100644 --- a/drizzle-orm/type-tests/pg/insert.ts +++ b/drizzle-orm/type-tests/pg/insert.ts @@ -2,7 +2,7 @@ import type { QueryResult } from 'pg'; import type { Equal } from 'type-tests/utils.ts'; import { Expect } from 'type-tests/utils.ts'; import type { PgInsert } from '~/pg-core/query-builders/insert.ts'; -import { sql } from '~/sql/index.ts'; +import { sql } from '~/sql/sql.ts'; import { db } from './db.ts'; import { users } from './tables.ts'; diff --git a/drizzle-orm/type-tests/pg/other.ts b/drizzle-orm/type-tests/pg/other.ts index e1e781402..38d41f113 100644 --- a/drizzle-orm/type-tests/pg/other.ts +++ b/drizzle-orm/type-tests/pg/other.ts @@ -1,6 +1,6 @@ import type { QueryResult } from 'pg'; import { eq, inArray } from '~/expressions.ts'; -import { sql } from '~/sql/index.ts'; +import { sql } from '~/sql/sql.ts'; import type { Equal } from 'type-tests/utils.ts'; import { Expect } from 'type-tests/utils.ts'; diff --git a/drizzle-orm/type-tests/pg/select.ts b/drizzle-orm/type-tests/pg/select.ts index 9f22782f8..0fde90a71 100644 --- a/drizzle-orm/type-tests/pg/select.ts +++ b/drizzle-orm/type-tests/pg/select.ts @@ -37,7 +37,7 @@ import { QueryBuilder, text, } from '~/pg-core/index.ts'; -import { type SQL, sql } from '~/sql/index.ts'; +import { type SQL, sql } from '~/sql/sql.ts'; import { db } from './db.ts'; import { cities, classes, newYorkers, newYorkers2, users } from './tables.ts'; diff --git a/drizzle-orm/type-tests/pg/subquery.ts b/drizzle-orm/type-tests/pg/subquery.ts index 0af17ccf8..82e30ec86 100644 --- a/drizzle-orm/type-tests/pg/subquery.ts +++ b/drizzle-orm/type-tests/pg/subquery.ts @@ -1,7 +1,7 @@ import { Expect } from 'type-tests/utils.ts'; import { and, eq } from '~/expressions.ts'; import { alias, integer, pgTable, serial, text } from '~/pg-core/index.ts'; -import { sql } from '~/sql/index.ts'; +import { sql } from '~/sql/sql.ts'; import type { DrizzleTypeError, Equal } from '~/utils.ts'; import { db } from './db.ts'; diff --git a/drizzle-orm/type-tests/pg/tables.ts b/drizzle-orm/type-tests/pg/tables.ts index 26699bcaf..4a940ebcb 100644 --- a/drizzle-orm/type-tests/pg/tables.ts +++ b/drizzle-orm/type-tests/pg/tables.ts @@ -45,7 +45,7 @@ import { pgView, type PgViewWithSelection, } from '~/pg-core/view.ts'; -import { sql } from '~/sql/index.ts'; +import { sql } from '~/sql/sql.ts'; import type { InferInsertModel, InferSelectModel } from '~/table.ts'; import { db } from './db.ts'; diff --git a/drizzle-orm/type-tests/pg/update.ts b/drizzle-orm/type-tests/pg/update.ts index 187afa147..a667825e0 100644 --- a/drizzle-orm/type-tests/pg/update.ts +++ b/drizzle-orm/type-tests/pg/update.ts @@ -3,7 +3,7 @@ import type { Equal } from 'type-tests/utils.ts'; import { Expect } from 'type-tests/utils.ts'; import { eq } from '~/expressions.ts'; import type { PgUpdate } from '~/pg-core/index.ts'; -import { sql } from '~/sql/index.ts'; +import { sql } from '~/sql/sql.ts'; import { db } from './db.ts'; import { users } from './tables.ts'; diff --git a/drizzle-orm/type-tests/pg/with.ts b/drizzle-orm/type-tests/pg/with.ts index 49fc10f3b..1e4dfda99 100644 --- a/drizzle-orm/type-tests/pg/with.ts +++ b/drizzle-orm/type-tests/pg/with.ts @@ -2,7 +2,7 @@ import type { Equal } from 'type-tests/utils.ts'; import { Expect } from 'type-tests/utils.ts'; import { gt, inArray } from '~/expressions.ts'; import { integer, pgTable, serial, text } from '~/pg-core/index.ts'; -import { sql } from '~/sql/index.ts'; +import { sql } from '~/sql/sql.ts'; import { db } from './db.ts'; const orders = pgTable('orders', { diff --git a/drizzle-orm/type-tests/sqlite/delete.ts b/drizzle-orm/type-tests/sqlite/delete.ts index 91f0376c9..fcc754740 100644 --- a/drizzle-orm/type-tests/sqlite/delete.ts +++ b/drizzle-orm/type-tests/sqlite/delete.ts @@ -3,7 +3,7 @@ import { eq } from '~/expressions.ts'; import type { Equal } from 'type-tests/utils.ts'; import { Expect } from 'type-tests/utils.ts'; -import { sql } from '~/sql/index.ts'; +import { sql } from '~/sql/sql.ts'; import type { SQLiteDelete } from '~/sqlite-core/index.ts'; import type { DrizzleTypeError } from '~/utils.ts'; import { bunDb, db } from './db.ts'; diff --git a/drizzle-orm/type-tests/sqlite/insert.ts b/drizzle-orm/type-tests/sqlite/insert.ts index 17f89b600..c4566ae8f 100644 --- a/drizzle-orm/type-tests/sqlite/insert.ts +++ b/drizzle-orm/type-tests/sqlite/insert.ts @@ -2,7 +2,7 @@ import type { RunResult } from 'better-sqlite3'; import type { Equal } from 'type-tests/utils.ts'; import { Expect } from 'type-tests/utils.ts'; import { and, eq } from '~/expressions.ts'; -import { sql } from '~/sql/index.ts'; +import { sql } from '~/sql/sql.ts'; import type { SQLiteInsert } from '~/sqlite-core/query-builders/insert.ts'; import type { DrizzleTypeError } from '~/utils.ts'; import { bunDb, db } from './db.ts'; diff --git a/drizzle-orm/type-tests/sqlite/other.ts b/drizzle-orm/type-tests/sqlite/other.ts index a544e73af..6dfe8a4c9 100644 --- a/drizzle-orm/type-tests/sqlite/other.ts +++ b/drizzle-orm/type-tests/sqlite/other.ts @@ -1,6 +1,6 @@ import type { RunResult } from 'better-sqlite3'; import { eq, inArray } from '~/expressions.ts'; -import { sql } from '~/sql/index.ts'; +import { sql } from '~/sql/sql.ts'; import type { Equal } from 'type-tests/utils.ts'; import { Expect } from 'type-tests/utils.ts'; diff --git a/drizzle-orm/type-tests/sqlite/select.ts b/drizzle-orm/type-tests/sqlite/select.ts index 6693b8e25..196dbc4de 100644 --- a/drizzle-orm/type-tests/sqlite/select.ts +++ b/drizzle-orm/type-tests/sqlite/select.ts @@ -21,7 +21,7 @@ import { notLike, or, } from '~/expressions.ts'; -import { param, sql } from '~/sql/index.ts'; +import { param, sql } from '~/sql/sql.ts'; import { alias } from '~/sqlite-core/alias.ts'; import type { Equal } from 'type-tests/utils.ts'; diff --git a/drizzle-orm/type-tests/sqlite/subquery.ts b/drizzle-orm/type-tests/sqlite/subquery.ts index 87926864e..72b4c165a 100644 --- a/drizzle-orm/type-tests/sqlite/subquery.ts +++ b/drizzle-orm/type-tests/sqlite/subquery.ts @@ -1,6 +1,6 @@ import { Expect } from 'type-tests/utils.ts'; import { and, eq } from '~/expressions.ts'; -import { sql } from '~/sql/index.ts'; +import { sql } from '~/sql/sql.ts'; import { alias, integer, sqliteTable, text } from '~/sqlite-core/index.ts'; import type { DrizzleTypeError, Equal } from '~/utils.ts'; import { db } from './db.ts'; diff --git a/drizzle-orm/type-tests/sqlite/tables.ts b/drizzle-orm/type-tests/sqlite/tables.ts index 0c7269031..d56b5fe09 100644 --- a/drizzle-orm/type-tests/sqlite/tables.ts +++ b/drizzle-orm/type-tests/sqlite/tables.ts @@ -1,7 +1,7 @@ import type { Equal } from 'type-tests/utils.ts'; import { Expect } from 'type-tests/utils.ts'; import { eq, gt } from '~/expressions.ts'; -import { sql } from '~/sql/index.ts'; +import { sql } from '~/sql/sql.ts'; import { alias, check, diff --git a/drizzle-orm/type-tests/sqlite/update.ts b/drizzle-orm/type-tests/sqlite/update.ts index 6686c836d..aa1f8051f 100644 --- a/drizzle-orm/type-tests/sqlite/update.ts +++ b/drizzle-orm/type-tests/sqlite/update.ts @@ -2,7 +2,7 @@ import type { RunResult } from 'better-sqlite3'; import type { Equal } from 'type-tests/utils.ts'; import { Expect } from 'type-tests/utils.ts'; import { eq } from '~/expressions.ts'; -import { sql } from '~/sql/index.ts'; +import { sql } from '~/sql/sql.ts'; import type { SQLiteUpdate } from '~/sqlite-core/query-builders/update.ts'; import type { DrizzleTypeError } from '~/utils.ts'; import { bunDb, db } from './db.ts'; diff --git a/drizzle-orm/type-tests/sqlite/with.ts b/drizzle-orm/type-tests/sqlite/with.ts index db8b33d75..94a969dac 100644 --- a/drizzle-orm/type-tests/sqlite/with.ts +++ b/drizzle-orm/type-tests/sqlite/with.ts @@ -1,7 +1,7 @@ import type { Equal } from 'type-tests/utils.ts'; import { Expect } from 'type-tests/utils.ts'; import { gt, inArray } from '~/expressions.ts'; -import { sql } from '~/sql/index.ts'; +import { sql } from '~/sql/sql.ts'; import { integer, sqliteTable, text } from '~/sqlite-core/index.ts'; import { db } from './db.ts'; diff --git a/integration-tests/.gitignore b/integration-tests/.gitignore index 045655f19..eeff14916 100644 --- a/integration-tests/.gitignore +++ b/integration-tests/.gitignore @@ -1,3 +1,5 @@ .env *.db trace +tests/imports/imports.cjs +tests/imports/imports.mjs diff --git a/integration-tests/package.json b/integration-tests/package.json index b14575401..745d1cbe0 100644 --- a/integration-tests/package.json +++ b/integration-tests/package.json @@ -23,7 +23,8 @@ "!tests/relational/**/*", "!tests/libsql-batch.test.ts", "!tests/d1-batch.test.ts", - "!tests/replicas/**/*" + "!tests/replicas/**/*", + "!tests/imports/**/*" ], "extensions": { "ts": "module" @@ -52,25 +53,26 @@ "axios": "^1.4.0", "tsx": "^3.12.7", "vite": "^4.3.9", - "vite-tsconfig-paths": "^4.2.0" + "vite-tsconfig-paths": "^4.2.0", + "zx": "^7.2.2" }, "dependencies": { "@aws-sdk/client-rds-data": "^3.345.0", "@aws-sdk/credential-providers": "^3.345.0", "@libsql/client": "^0.1.6", + "@miniflare/d1": "^2.14.0", + "@miniflare/shared": "^2.14.0", "@planetscale/database": "^1.7.0", "@typescript/analyze-trace": "^0.10.0", "@vercel/postgres": "^0.3.0", "better-sqlite3": "^8.4.0", "dockerode": "^3.3.4", "dotenv": "^16.1.4", - "drizzle-zod": "workspace:../drizzle-zod/dist", "drizzle-typebox": "workspace:../drizzle-typebox/dist", "drizzle-valibot": "workspace:../drizzle-valibot/dist", + "drizzle-zod": "workspace:../drizzle-zod/dist", "express": "^4.18.2", "get-port": "^7.0.0", - "@miniflare/shared": "^2.14.0", - "@miniflare/d1": "^2.14.0", "mysql2": "^3.3.3", "pg": "^8.11.0", "postgres": "^3.3.5", diff --git a/integration-tests/tests/imports/index.test.ts b/integration-tests/tests/imports/index.test.ts new file mode 100644 index 000000000..f40420b66 --- /dev/null +++ b/integration-tests/tests/imports/index.test.ts @@ -0,0 +1,55 @@ +import { afterAll, expect, it } from 'vitest'; +import 'zx/globals'; +import * as fs from 'fs'; +import path from 'path'; + +const IMPORTS_FOLDER = 'tests/imports/files'; + +const folderPath = '../drizzle-orm/dist/package.json'; +const pj = JSON.parse(fs.readFileSync(folderPath, 'utf8')); + +if (!fs.existsSync(IMPORTS_FOLDER)) { + fs.mkdirSync(IMPORTS_FOLDER); +} + +it('dynamic imports check for cjs and mjs', async () => { + const promises: ProcessPromise[] = []; + for (const [i, key] of Object.keys(pj['exports']).entries()) { + const o1 = path.join('drizzle-orm', key); + fs.writeFileSync(`${IMPORTS_FOLDER}/imports_${i}.cjs`, 'requ'); + fs.appendFileSync(`${IMPORTS_FOLDER}/imports_${i}.cjs`, 'ire("' + o1 + '");\n', {}); + + fs.writeFileSync(`${IMPORTS_FOLDER}/imports_${i}.mjs`, 'imp'); + fs.appendFileSync(`${IMPORTS_FOLDER}/imports_${i}.mjs`, 'ort "' + o1 + '"\n', {}); + + promises.push( + $`node ${IMPORTS_FOLDER}/imports_${i}.cjs`.nothrow(), + // $`node ${IMPORTS_FOLDER}/imports_${i}.mjs`.nothrow(), + ); + } + const results = await Promise.all(promises); + + for (const result of results) { + expect(result.exitCode, result.message).toBe(0); + } +}); + +// it('dynamic imports check for mjs', async () => { +// const promises: ProcessPromise[] = []; +// for (const [i, key] of Object.keys(pj['exports']).entries()) { +// const o1 = path.join('drizzle-orm', key); +// fs.writeFileSync(`${IMPORTS_FOLDER}/imports_${i}.mjs`, 'imp'); +// fs.appendFileSync(`${IMPORTS_FOLDER}/imports_${i}.mjs`, 'ort "' + o1 + '"\n', {}); +// promises.push($`node ${IMPORTS_FOLDER}/imports_${i}.mjs`.nothrow()); +// } + +// const results = await Promise.all(promises); + +// for (const result of results) { +// expect(result.exitCode, result.message).toBe(0) +// } +// }); + +afterAll(() => { + fs.rmdirSync(IMPORTS_FOLDER, { recursive: true }); +}); diff --git a/integration-tests/vitest.config.ts b/integration-tests/vitest.config.ts index 35b0c9b76..22ffdab10 100644 --- a/integration-tests/vitest.config.ts +++ b/integration-tests/vitest.config.ts @@ -4,7 +4,7 @@ import { defineConfig } from 'vitest/config'; export default defineConfig({ test: { - include: ['tests/relational/**/*.test.ts', 'tests/libsql-batch.test.ts', 'tests/d1-batch.test.ts', 'tests/replicas/**/*'], + include: ['tests/relational/**/*.test.ts', 'tests/libsql-batch.test.ts', 'tests/d1-batch.test.ts', 'tests/replicas/**/*', 'tests/imports/**/*'], exclude: [ ...(process.env.SKIP_PLANETSCALE_TESTS ? ['tests/relational/mysql.planetscale.test.ts'] : []), 'tests/relational/vercel.test.ts', diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index d6f587854..15af08a5e 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1,4 +1,4 @@ -lockfileVersion: '6.1' +lockfileVersion: '6.0' settings: autoInstallPeers: true @@ -413,6 +413,9 @@ importers: vite-tsconfig-paths: specifier: ^4.2.0 version: 4.2.0(typescript@5.2.2)(vite@4.3.9) + zx: + specifier: ^7.2.2 + version: 7.2.2 packages: @@ -2605,7 +2608,7 @@ packages: resolution: {integrity: sha512-MxObHvNl4A69ofaTRU8DFqvgzzv8s9yRtaPPm5gud9HDNvpB3GPQFvNuTWAI59B9huVGV5jXYJwbCsmBsOGYWA==} dependencies: '@types/jsonfile': 6.1.1 - '@types/node': 20.2.5 + '@types/node': 20.8.7 dev: true /@types/json-schema@7.0.12: @@ -2623,7 +2626,7 @@ packages: /@types/jsonfile@6.1.1: resolution: {integrity: sha512-GSgiRCVeapDN+3pqA35IkQwasaCh/0YFH5dEF6S88iDvEn901DjOeH3/QPY+XYP1DFzDZPvIvfeEgk+7br5png==} dependencies: - '@types/node': 20.2.5 + '@types/node': 20.8.7 dev: true /@types/mime@3.0.1: @@ -2637,7 +2640,7 @@ packages: /@types/node-fetch@2.6.4: resolution: {integrity: sha512-1ZX9fcN4Rvkvgv4E6PAY5WXUFWFcRWxZa3EW83UjycOB9ljJCedb2CupIP4RZMEwF/M3eTcCihbBRgwtGbg5Rg==} dependencies: - '@types/node': 20.2.5 + '@types/node': 20.8.7 form-data: 3.0.1 /@types/node@18.15.10: @@ -2651,6 +2654,11 @@ packages: /@types/node@20.2.5: resolution: {integrity: sha512-JJulVEQXmiY9Px5axXHeYGLSjhkZEnD+MDPDGbCbIAbMslkKwmygtZFy1X6s/075Yo94sf8GuSlFfPzysQrWZQ==} + /@types/node@20.8.7: + resolution: {integrity: sha512-21TKHHh3eUHIi2MloeptJWALuCu5H7HQTdTrWIFReA8ad+aggoX+lRes3ex7/FtpC+sVUpFMQ+QTfYr74mruiQ==} + dependencies: + undici-types: 5.25.3 + /@types/normalize-package-data@2.4.1: resolution: {integrity: sha512-Gj7cI7z+98M282Tqmp2K5EIsoouUEzbBJhQQzDE3jSIRk6r9gsz0oUokqIUR4u1R3dMHo0pDHM7sNOHyhulypw==} dev: true @@ -2721,7 +2729,7 @@ packages: /@types/ws@8.5.4: resolution: {integrity: sha512-zdQDHKUgcX/zBc4GrwsE/7dVdAD8JR4EuiAXiiUhhfyIJXXb2+PrGshFyeXWQPMmmZ2XxgaqclgpIC7eTXc1mg==} dependencies: - '@types/node': 20.2.5 + '@types/node': 20.8.7 /@typescript-eslint/eslint-plugin@6.7.3(@typescript-eslint/parser@6.7.3)(eslint@8.50.0)(typescript@5.2.2): resolution: {integrity: sha512-vntq452UHNltxsaaN+L9WyuMch8bMd9CqJ3zhzTPXXidwbf5mqqKCVXEuvRZUqLJSTLeWE65lQwyXsRGnXkCTA==} @@ -5541,7 +5549,7 @@ packages: engines: {node: ^12.20.0 || ^14.13.1 || >=16.0.0} dependencies: dir-glob: 3.0.1 - fast-glob: 3.2.12 + fast-glob: 3.3.1 ignore: 5.2.4 merge2: 1.4.1 slash: 4.0.0 @@ -8579,6 +8587,9 @@ packages: which-boxed-primitive: 1.0.2 dev: true + /undici-types@5.25.3: + resolution: {integrity: sha512-Ga1jfYwRn7+cP9v8auvEXN1rX3sWqlayd4HP7OKk4mZWylEmu3KzXDUGrQUN6Ol7qo1gPvB2e5gX6udnyEPgdA==} + /undici@5.20.0: resolution: {integrity: sha512-J3j60dYzuo6Eevbawwp1sdg16k5Tf768bxYK4TUJRH7cBM4kFCbf3mOnM/0E3vQYXvpxITbbWmBafaDbxLDz3g==} engines: {node: '>=12.18'} @@ -9059,7 +9070,7 @@ packages: '@types/node': 18.16.16 '@types/ps-tree': 1.1.2 '@types/which': 3.0.0 - chalk: 5.2.0 + chalk: 5.3.0 fs-extra: 11.1.1 fx: 28.0.0 globby: 13.1.4 From 3822b1c7d9049c247d2e59e7de7ecc6042276583 Mon Sep 17 00:00:00 2001 From: AndriiSherman Date: Sun, 5 Nov 2023 17:01:10 +0200 Subject: [PATCH 2/4] Add schema to extractTablesRelationalConfig function --- drizzle-orm/src/relations.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/drizzle-orm/src/relations.ts b/drizzle-orm/src/relations.ts index b4750f794..cc87cf787 100644 --- a/drizzle-orm/src/relations.ts +++ b/drizzle-orm/src/relations.ts @@ -275,6 +275,7 @@ export interface TableRelationalConfig { columns: Record; relations: Record; primaryKey: AnyColumn[]; + schema?: string; } export type TablesRelationalConfig = Record; @@ -435,6 +436,7 @@ export function extractTablesRelationalConfig< tablesConfig[key] = { tsName: key, dbName: value[Table.Symbol.Name], + schema: value[Table.Symbol.Schema], columns: value[Table.Symbol.Columns], relations: bufferedRelations?.relations ?? {}, primaryKey: bufferedRelations?.primaryKey ?? [], From f6d20a115e4e40d749a5373759d73270f61a920b Mon Sep 17 00:00:00 2001 From: AndriiSherman Date: Sun, 5 Nov 2023 17:17:19 +0200 Subject: [PATCH 3/4] Add 60 sec timeout for vitest --- integration-tests/tests/imports/index.test.ts | 4 ++-- integration-tests/vitest.config.ts | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/integration-tests/tests/imports/index.test.ts b/integration-tests/tests/imports/index.test.ts index f40420b66..f89efe00f 100644 --- a/integration-tests/tests/imports/index.test.ts +++ b/integration-tests/tests/imports/index.test.ts @@ -19,8 +19,8 @@ it('dynamic imports check for cjs and mjs', async () => { fs.writeFileSync(`${IMPORTS_FOLDER}/imports_${i}.cjs`, 'requ'); fs.appendFileSync(`${IMPORTS_FOLDER}/imports_${i}.cjs`, 'ire("' + o1 + '");\n', {}); - fs.writeFileSync(`${IMPORTS_FOLDER}/imports_${i}.mjs`, 'imp'); - fs.appendFileSync(`${IMPORTS_FOLDER}/imports_${i}.mjs`, 'ort "' + o1 + '"\n', {}); + // fs.writeFileSync(`${IMPORTS_FOLDER}/imports_${i}.mjs`, 'imp'); + // fs.appendFileSync(`${IMPORTS_FOLDER}/imports_${i}.mjs`, 'ort "' + o1 + '"\n', {}); promises.push( $`node ${IMPORTS_FOLDER}/imports_${i}.cjs`.nothrow(), diff --git a/integration-tests/vitest.config.ts b/integration-tests/vitest.config.ts index 22ffdab10..a238ac877 100644 --- a/integration-tests/vitest.config.ts +++ b/integration-tests/vitest.config.ts @@ -12,8 +12,8 @@ export default defineConfig({ typecheck: { tsconfig: 'tsconfig.json', }, - testTimeout: 40000, - hookTimeout: 40000, + testTimeout: 60000, + hookTimeout: 60000, // deps: { // inline: true, // }, From 99ce57780ce755cbcc3c0758be740d462f842d75 Mon Sep 17 00:00:00 2001 From: AndriiSherman Date: Sun, 5 Nov 2023 17:25:51 +0200 Subject: [PATCH 4/4] timeout to 100 sec in vitest --- integration-tests/vitest.config.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/integration-tests/vitest.config.ts b/integration-tests/vitest.config.ts index a238ac877..261754895 100644 --- a/integration-tests/vitest.config.ts +++ b/integration-tests/vitest.config.ts @@ -12,8 +12,8 @@ export default defineConfig({ typecheck: { tsconfig: 'tsconfig.json', }, - testTimeout: 60000, - hookTimeout: 60000, + testTimeout: 100000, + hookTimeout: 100000, // deps: { // inline: true, // },