drizzle-orm 0.36.2 → 0.36.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (79) hide show
  1. package/aws-data-api/pg/driver.cjs +9 -7
  2. package/aws-data-api/pg/driver.cjs.map +1 -1
  3. package/aws-data-api/pg/driver.d.cts +1 -1
  4. package/aws-data-api/pg/driver.d.ts +1 -1
  5. package/aws-data-api/pg/driver.js +9 -7
  6. package/aws-data-api/pg/driver.js.map +1 -1
  7. package/mysql-core/dialect.cjs +39 -28
  8. package/mysql-core/dialect.cjs.map +1 -1
  9. package/mysql-core/dialect.d.cts +1 -1
  10. package/mysql-core/dialect.d.ts +1 -1
  11. package/mysql-core/dialect.js +39 -28
  12. package/mysql-core/dialect.js.map +1 -1
  13. package/mysql-core/query-builders/insert.cjs +12 -2
  14. package/mysql-core/query-builders/insert.cjs.map +1 -1
  15. package/mysql-core/query-builders/insert.d.cts +12 -2
  16. package/mysql-core/query-builders/insert.d.ts +12 -2
  17. package/mysql-core/query-builders/insert.js +14 -4
  18. package/mysql-core/query-builders/insert.js.map +1 -1
  19. package/package.json +49 -49
  20. package/pg-core/dialect.cjs +93 -70
  21. package/pg-core/dialect.cjs.map +1 -1
  22. package/pg-core/dialect.d.cts +4 -2
  23. package/pg-core/dialect.d.ts +4 -2
  24. package/pg-core/dialect.js +93 -70
  25. package/pg-core/dialect.js.map +1 -1
  26. package/pg-core/query-builders/insert.cjs +12 -2
  27. package/pg-core/query-builders/insert.cjs.map +1 -1
  28. package/pg-core/query-builders/insert.d.cts +13 -2
  29. package/pg-core/query-builders/insert.d.ts +13 -2
  30. package/pg-core/query-builders/insert.js +14 -4
  31. package/pg-core/query-builders/insert.js.map +1 -1
  32. package/pg-core/query-builders/select.cjs.map +1 -1
  33. package/pg-core/query-builders/select.d.cts +5 -5
  34. package/pg-core/query-builders/select.d.ts +5 -5
  35. package/pg-core/query-builders/select.js.map +1 -1
  36. package/pg-core/query-builders/select.types.cjs.map +1 -1
  37. package/pg-core/query-builders/select.types.d.cts +2 -2
  38. package/pg-core/query-builders/select.types.d.ts +2 -2
  39. package/pg-core/query-builders/update.cjs +101 -4
  40. package/pg-core/query-builders/update.cjs.map +1 -1
  41. package/pg-core/query-builders/update.d.cts +56 -16
  42. package/pg-core/query-builders/update.d.ts +56 -16
  43. package/pg-core/query-builders/update.js +102 -5
  44. package/pg-core/query-builders/update.js.map +1 -1
  45. package/sqlite-core/dialect.cjs +78 -58
  46. package/sqlite-core/dialect.cjs.map +1 -1
  47. package/sqlite-core/dialect.d.cts +4 -2
  48. package/sqlite-core/dialect.d.ts +4 -2
  49. package/sqlite-core/dialect.js +78 -58
  50. package/sqlite-core/dialect.js.map +1 -1
  51. package/sqlite-core/query-builders/insert.cjs +12 -2
  52. package/sqlite-core/query-builders/insert.cjs.map +1 -1
  53. package/sqlite-core/query-builders/insert.d.cts +13 -2
  54. package/sqlite-core/query-builders/insert.d.ts +13 -2
  55. package/sqlite-core/query-builders/insert.js +14 -4
  56. package/sqlite-core/query-builders/insert.js.map +1 -1
  57. package/sqlite-core/query-builders/select.cjs.map +1 -1
  58. package/sqlite-core/query-builders/select.d.cts +5 -5
  59. package/sqlite-core/query-builders/select.d.ts +5 -5
  60. package/sqlite-core/query-builders/select.js.map +1 -1
  61. package/sqlite-core/query-builders/select.types.cjs.map +1 -1
  62. package/sqlite-core/query-builders/select.types.d.cts +2 -2
  63. package/sqlite-core/query-builders/select.types.d.ts +2 -2
  64. package/sqlite-core/query-builders/update.cjs +35 -1
  65. package/sqlite-core/query-builders/update.cjs.map +1 -1
  66. package/sqlite-core/query-builders/update.d.cts +23 -11
  67. package/sqlite-core/query-builders/update.d.ts +23 -11
  68. package/sqlite-core/query-builders/update.js +37 -2
  69. package/sqlite-core/query-builders/update.js.map +1 -1
  70. package/utils.cjs +1 -1
  71. package/utils.cjs.map +1 -1
  72. package/utils.d.cts +1 -1
  73. package/utils.d.ts +1 -1
  74. package/utils.js +1 -1
  75. package/utils.js.map +1 -1
  76. package/version.cjs +1 -1
  77. package/version.d.cts +1 -1
  78. package/version.d.ts +1 -1
  79. package/version.js +1 -1
@@ -1,8 +1,9 @@
1
1
  import { entityKind, is } from "../../entity.js";
2
2
  import { QueryPromise } from "../../query-promise.js";
3
3
  import { Param, SQL, sql } from "../../sql/sql.js";
4
- import { Table } from "../../table.js";
5
- import { mapUpdateSet } from "../../utils.js";
4
+ import { Columns, Table } from "../../table.js";
5
+ import { haveSameKeys, mapUpdateSet } from "../../utils.js";
6
+ import { QueryBuilder } from "./query-builder.js";
6
7
  class MySqlInsertBuilder {
7
8
  constructor(table, session, dialect) {
8
9
  this.table = table;
@@ -31,13 +32,22 @@ class MySqlInsertBuilder {
31
32
  });
32
33
  return new MySqlInsertBase(this.table, mappedValues, this.shouldIgnore, this.session, this.dialect);
33
34
  }
35
+ select(selectQuery) {
36
+ const select = typeof selectQuery === "function" ? selectQuery(new QueryBuilder()) : selectQuery;
37
+ if (!is(select, SQL) && !haveSameKeys(this.table[Columns], select._.selectedFields)) {
38
+ throw new Error(
39
+ "Insert select error: selected fields are not the same or are in a different order compared to the table definition"
40
+ );
41
+ }
42
+ return new MySqlInsertBase(this.table, select, this.shouldIgnore, this.session, this.dialect, true);
43
+ }
34
44
  }
35
45
  class MySqlInsertBase extends QueryPromise {
36
- constructor(table, values, ignore, session, dialect) {
46
+ constructor(table, values, ignore, session, dialect, select) {
37
47
  super();
38
48
  this.session = session;
39
49
  this.dialect = dialect;
40
- this.config = { table, values, ignore };
50
+ this.config = { table, values, select, ignore };
41
51
  }
42
52
  static [entityKind] = "MySqlInsert";
43
53
  config;
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/mysql-core/query-builders/insert.ts"],"sourcesContent":["import { entityKind, is } from '~/entity.ts';\nimport type { MySqlDialect } from '~/mysql-core/dialect.ts';\nimport type {\n\tAnyMySqlQueryResultHKT,\n\tMySqlPreparedQueryConfig,\n\tMySqlQueryResultHKT,\n\tMySqlQueryResultKind,\n\tMySqlSession,\n\tPreparedQueryHKTBase,\n\tPreparedQueryKind,\n} from '~/mysql-core/session.ts';\nimport type { MySqlTable } from '~/mysql-core/table.ts';\nimport { QueryPromise } from '~/query-promise.ts';\nimport type { RunnableQuery } from '~/runnable-query.ts';\nimport type { Placeholder, Query, SQLWrapper } from '~/sql/sql.ts';\nimport { Param, SQL, sql } from '~/sql/sql.ts';\nimport type { InferModelFromColumns } from '~/table.ts';\nimport { Table } from '~/table.ts';\nimport { mapUpdateSet } from '~/utils.ts';\nimport type { AnyMySqlColumn } from '../columns/common.ts';\nimport type { SelectedFieldsOrdered } from './select.types.ts';\nimport type { MySqlUpdateSetSource } from './update.ts';\n\nexport interface MySqlInsertConfig<TTable extends MySqlTable = MySqlTable> {\n\ttable: TTable;\n\tvalues: Record<string, Param | SQL>[];\n\tignore: boolean;\n\tonConflict?: SQL;\n\treturning?: SelectedFieldsOrdered;\n}\n\nexport type AnyMySqlInsertConfig = MySqlInsertConfig<MySqlTable>;\n\nexport type MySqlInsertValue<TTable extends MySqlTable> =\n\t& {\n\t\t[Key in keyof TTable['$inferInsert']]: TTable['$inferInsert'][Key] | SQL | Placeholder;\n\t}\n\t& {};\n\nexport class MySqlInsertBuilder<\n\tTTable extends MySqlTable,\n\tTQueryResult extends MySqlQueryResultHKT,\n\tTPreparedQueryHKT extends PreparedQueryHKTBase,\n> {\n\tstatic readonly [entityKind]: string = 'MySqlInsertBuilder';\n\n\tprivate shouldIgnore = false;\n\n\tconstructor(\n\t\tprivate table: TTable,\n\t\tprivate session: MySqlSession,\n\t\tprivate dialect: MySqlDialect,\n\t) {}\n\n\tignore(): this {\n\t\tthis.shouldIgnore = true;\n\t\treturn this;\n\t}\n\n\tvalues(value: MySqlInsertValue<TTable>): MySqlInsertBase<TTable, TQueryResult, TPreparedQueryHKT>;\n\tvalues(values: MySqlInsertValue<TTable>[]): MySqlInsertBase<TTable, TQueryResult, TPreparedQueryHKT>;\n\tvalues(\n\t\tvalues: MySqlInsertValue<TTable> | MySqlInsertValue<TTable>[],\n\t): MySqlInsertBase<TTable, TQueryResult, TPreparedQueryHKT> {\n\t\tvalues = Array.isArray(values) ? values : [values];\n\t\tif (values.length === 0) {\n\t\t\tthrow new Error('values() must be called with at least one value');\n\t\t}\n\t\tconst mappedValues = values.map((entry) => {\n\t\t\tconst result: Record<string, Param | SQL> = {};\n\t\t\tconst cols = this.table[Table.Symbol.Columns];\n\t\t\tfor (const colKey of Object.keys(entry)) {\n\t\t\t\tconst colValue = entry[colKey as keyof typeof entry];\n\t\t\t\tresult[colKey] = is(colValue, SQL) ? colValue : new Param(colValue, cols[colKey]);\n\t\t\t}\n\t\t\treturn result;\n\t\t});\n\n\t\treturn new MySqlInsertBase(this.table, mappedValues, this.shouldIgnore, this.session, this.dialect);\n\t}\n}\n\nexport type MySqlInsertWithout<T extends AnyMySqlInsert, TDynamic extends boolean, K extends keyof T & string> =\n\tTDynamic extends true ? T\n\t\t: Omit<\n\t\t\tMySqlInsertBase<\n\t\t\t\tT['_']['table'],\n\t\t\t\tT['_']['queryResult'],\n\t\t\t\tT['_']['preparedQueryHKT'],\n\t\t\t\tT['_']['returning'],\n\t\t\t\tTDynamic,\n\t\t\t\tT['_']['excludedMethods'] | '$returning'\n\t\t\t>,\n\t\t\tT['_']['excludedMethods'] | K\n\t\t>;\n\nexport type MySqlInsertDynamic<T extends AnyMySqlInsert> = MySqlInsert<\n\tT['_']['table'],\n\tT['_']['queryResult'],\n\tT['_']['preparedQueryHKT'],\n\tT['_']['returning']\n>;\n\nexport type MySqlInsertPrepare<\n\tT extends AnyMySqlInsert,\n\tTReturning extends Record<string, unknown> | undefined = undefined,\n> = PreparedQueryKind<\n\tT['_']['preparedQueryHKT'],\n\tMySqlPreparedQueryConfig & {\n\t\texecute: TReturning extends undefined ? MySqlQueryResultKind<T['_']['queryResult'], never> : TReturning[];\n\t\titerator: never;\n\t},\n\ttrue\n>;\n\nexport type MySqlInsertOnDuplicateKeyUpdateConfig<T extends AnyMySqlInsert> = {\n\tset: MySqlUpdateSetSource<T['_']['table']>;\n};\n\nexport type MySqlInsert<\n\tTTable extends MySqlTable = MySqlTable,\n\tTQueryResult extends MySqlQueryResultHKT = AnyMySqlQueryResultHKT,\n\tTPreparedQueryHKT extends PreparedQueryHKTBase = PreparedQueryHKTBase,\n\tTReturning extends Record<string, unknown> | undefined = Record<string, unknown> | undefined,\n> = MySqlInsertBase<TTable, TQueryResult, TPreparedQueryHKT, TReturning, true, never>;\n\nexport type MySqlInsertReturning<\n\tT extends AnyMySqlInsert,\n\tTDynamic extends boolean,\n> = MySqlInsertBase<\n\tT['_']['table'],\n\tT['_']['queryResult'],\n\tT['_']['preparedQueryHKT'],\n\tInferModelFromColumns<GetPrimarySerialOrDefaultKeys<T['_']['table']['_']['columns']>>,\n\tTDynamic,\n\tT['_']['excludedMethods'] | '$returning'\n>;\n\nexport type AnyMySqlInsert = MySqlInsertBase<any, any, any, any, any, any>;\n\nexport interface MySqlInsertBase<\n\tTTable extends MySqlTable,\n\tTQueryResult extends MySqlQueryResultHKT,\n\tTPreparedQueryHKT extends PreparedQueryHKTBase,\n\tTReturning extends Record<string, unknown> | undefined = undefined,\n\tTDynamic extends boolean = false,\n\tTExcludedMethods extends string = never,\n> extends\n\tQueryPromise<TReturning extends undefined ? MySqlQueryResultKind<TQueryResult, never> : TReturning[]>,\n\tRunnableQuery<TReturning extends undefined ? MySqlQueryResultKind<TQueryResult, never> : TReturning[], 'mysql'>,\n\tSQLWrapper\n{\n\treadonly _: {\n\t\treadonly dialect: 'mysql';\n\t\treadonly table: TTable;\n\t\treadonly queryResult: TQueryResult;\n\t\treadonly preparedQueryHKT: TPreparedQueryHKT;\n\t\treadonly dynamic: TDynamic;\n\t\treadonly excludedMethods: TExcludedMethods;\n\t\treadonly returning: TReturning;\n\t\treadonly result: TReturning extends undefined ? MySqlQueryResultKind<TQueryResult, never> : TReturning[];\n\t};\n}\n\nexport type PrimaryKeyKeys<T extends Record<string, AnyMySqlColumn>> = {\n\t[K in keyof T]: T[K]['_']['isPrimaryKey'] extends true ? T[K]['_']['isAutoincrement'] extends true ? K\n\t\t: T[K]['_']['hasRuntimeDefault'] extends true ? T[K]['_']['isPrimaryKey'] extends true ? K : never\n\t\t: never\n\t\t: T[K]['_']['hasRuntimeDefault'] extends true ? T[K]['_']['isPrimaryKey'] extends true ? K : never\n\t\t: never;\n}[keyof T];\n\nexport type GetPrimarySerialOrDefaultKeys<T extends Record<string, AnyMySqlColumn>> = {\n\t[K in PrimaryKeyKeys<T>]: T[K];\n};\n\nexport class MySqlInsertBase<\n\tTTable extends MySqlTable,\n\tTQueryResult extends MySqlQueryResultHKT,\n\t// eslint-disable-next-line @typescript-eslint/no-unused-vars\n\tTPreparedQueryHKT extends PreparedQueryHKTBase,\n\t// eslint-disable-next-line @typescript-eslint/no-unused-vars\n\tTReturning extends Record<string, unknown> | undefined = undefined,\n\t// eslint-disable-next-line @typescript-eslint/no-unused-vars\n\tTDynamic extends boolean = false,\n\t// eslint-disable-next-line @typescript-eslint/no-unused-vars\n\tTExcludedMethods extends string = never,\n> extends QueryPromise<TReturning extends undefined ? MySqlQueryResultKind<TQueryResult, never> : TReturning[]>\n\timplements\n\t\tRunnableQuery<TReturning extends undefined ? MySqlQueryResultKind<TQueryResult, never> : TReturning[], 'mysql'>,\n\t\tSQLWrapper\n{\n\tstatic override readonly [entityKind]: string = 'MySqlInsert';\n\n\tdeclare protected $table: TTable;\n\n\tprivate config: MySqlInsertConfig<TTable>;\n\n\tconstructor(\n\t\ttable: TTable,\n\t\tvalues: MySqlInsertConfig['values'],\n\t\tignore: boolean,\n\t\tprivate session: MySqlSession,\n\t\tprivate dialect: MySqlDialect,\n\t) {\n\t\tsuper();\n\t\tthis.config = { table, values, ignore };\n\t}\n\n\t/**\n\t * Adds an `on duplicate key update` clause to the query.\n\t *\n\t * Calling this method will update the row if any unique index conflicts. MySQL will automatically determine the conflict target based on the primary key and unique indexes.\n\t *\n\t * See docs: {@link https://orm.drizzle.team/docs/insert#on-duplicate-key-update}\n\t *\n\t * @param config The `set` clause\n\t *\n\t * @example\n\t * ```ts\n\t * await db.insert(cars)\n\t * .values({ id: 1, brand: 'BMW'})\n\t * .onDuplicateKeyUpdate({ set: { brand: 'Porsche' }});\n\t * ```\n\t *\n\t * While MySQL does not directly support doing nothing on conflict, you can perform a no-op by setting any column's value to itself and achieve the same effect:\n\t *\n\t * ```ts\n\t * import { sql } from 'drizzle-orm';\n\t *\n\t * await db.insert(cars)\n\t * .values({ id: 1, brand: 'BMW' })\n\t * .onDuplicateKeyUpdate({ set: { id: sql`id` } });\n\t * ```\n\t */\n\tonDuplicateKeyUpdate(\n\t\tconfig: MySqlInsertOnDuplicateKeyUpdateConfig<this>,\n\t): MySqlInsertWithout<this, TDynamic, 'onDuplicateKeyUpdate'> {\n\t\tconst setSql = this.dialect.buildUpdateSet(this.config.table, mapUpdateSet(this.config.table, config.set));\n\t\tthis.config.onConflict = sql`update ${setSql}`;\n\t\treturn this as any;\n\t}\n\n\t$returningId(): MySqlInsertWithout<\n\t\tMySqlInsertReturning<this, TDynamic>,\n\t\tTDynamic,\n\t\t'$returningId'\n\t> {\n\t\tconst returning: SelectedFieldsOrdered = [];\n\t\tfor (const [key, value] of Object.entries(this.config.table[Table.Symbol.Columns])) {\n\t\t\tif (value.primary) {\n\t\t\t\treturning.push({ field: value, path: [key] });\n\t\t\t}\n\t\t}\n\t\tthis.config.returning = returning;\n\t\treturn this as any;\n\t}\n\n\t/** @internal */\n\tgetSQL(): SQL {\n\t\treturn this.dialect.buildInsertQuery(this.config).sql;\n\t}\n\n\ttoSQL(): Query {\n\t\tconst { typings: _typings, ...rest } = this.dialect.sqlToQuery(this.getSQL());\n\t\treturn rest;\n\t}\n\n\tprepare(): MySqlInsertPrepare<this, TReturning> {\n\t\tconst { sql, generatedIds } = this.dialect.buildInsertQuery(this.config);\n\t\treturn this.session.prepareQuery(\n\t\t\tthis.dialect.sqlToQuery(sql),\n\t\t\tundefined,\n\t\t\tundefined,\n\t\t\tgeneratedIds,\n\t\t\tthis.config.returning,\n\t\t) as MySqlInsertPrepare<this, TReturning>;\n\t}\n\n\toverride execute: ReturnType<this['prepare']>['execute'] = (placeholderValues) => {\n\t\treturn this.prepare().execute(placeholderValues);\n\t};\n\n\tprivate createIterator = (): ReturnType<this['prepare']>['iterator'] => {\n\t\tconst self = this;\n\t\treturn async function*(placeholderValues) {\n\t\t\tyield* self.prepare().iterator(placeholderValues);\n\t\t};\n\t};\n\n\titerator = this.createIterator();\n\n\t$dynamic(): MySqlInsertDynamic<this> {\n\t\treturn this as any;\n\t}\n}\n"],"mappings":"AAAA,SAAS,YAAY,UAAU;AAY/B,SAAS,oBAAoB;AAG7B,SAAS,OAAO,KAAK,WAAW;AAEhC,SAAS,aAAa;AACtB,SAAS,oBAAoB;AAqBtB,MAAM,mBAIX;AAAA,EAKD,YACS,OACA,SACA,SACP;AAHO;AACA;AACA;AAAA,EACN;AAAA,EARH,QAAiB,UAAU,IAAY;AAAA,EAE/B,eAAe;AAAA,EAQvB,SAAe;AACd,SAAK,eAAe;AACpB,WAAO;AAAA,EACR;AAAA,EAIA,OACC,QAC2D;AAC3D,aAAS,MAAM,QAAQ,MAAM,IAAI,SAAS,CAAC,MAAM;AACjD,QAAI,OAAO,WAAW,GAAG;AACxB,YAAM,IAAI,MAAM,iDAAiD;AAAA,IAClE;AACA,UAAM,eAAe,OAAO,IAAI,CAAC,UAAU;AAC1C,YAAM,SAAsC,CAAC;AAC7C,YAAM,OAAO,KAAK,MAAM,MAAM,OAAO,OAAO;AAC5C,iBAAW,UAAU,OAAO,KAAK,KAAK,GAAG;AACxC,cAAM,WAAW,MAAM,MAA4B;AACnD,eAAO,MAAM,IAAI,GAAG,UAAU,GAAG,IAAI,WAAW,IAAI,MAAM,UAAU,KAAK,MAAM,CAAC;AAAA,MACjF;AACA,aAAO;AAAA,IACR,CAAC;AAED,WAAO,IAAI,gBAAgB,KAAK,OAAO,cAAc,KAAK,cAAc,K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,WAAWA,IAAG;AAAA,MAC3B;AAAA,MACA;AAAA,MACA;AAAA,MACA,KAAK,OAAO;AAAA,IACb;AAAA,EACD;AAAA,EAES,UAAkD,CAAC,sBAAsB;AACjF,WAAO,KAAK,QAAQ,EAAE,QAAQ,iBAAiB;AAAA,EAChD;AAAA,EAEQ,iBAAiB,MAA+C;AACvE,UAAM,OAAO;AACb,WAAO,iBAAgB,mBAAmB;AACzC,aAAO,KAAK,QAAQ,EAAE,SAAS,iBAAiB;AAAA,IACjD;AAAA,EACD;AAAA,EAEA,WAAW,KAAK,eAAe;AAAA,EAE/B,WAAqC;AACpC,WAAO;AAAA,EACR;AACD;","names":["sql"]}
1
+ {"version":3,"sources":["../../../src/mysql-core/query-builders/insert.ts"],"sourcesContent":["import { entityKind, is } from '~/entity.ts';\nimport type { MySqlDialect } from '~/mysql-core/dialect.ts';\nimport type {\n\tAnyMySqlQueryResultHKT,\n\tMySqlPreparedQueryConfig,\n\tMySqlQueryResultHKT,\n\tMySqlQueryResultKind,\n\tMySqlSession,\n\tPreparedQueryHKTBase,\n\tPreparedQueryKind,\n} from '~/mysql-core/session.ts';\nimport type { MySqlTable } from '~/mysql-core/table.ts';\nimport type { TypedQueryBuilder } from '~/query-builders/query-builder.ts';\nimport { QueryPromise } from '~/query-promise.ts';\nimport type { RunnableQuery } from '~/runnable-query.ts';\nimport type { Placeholder, Query, SQLWrapper } from '~/sql/sql.ts';\nimport { Param, SQL, sql } from '~/sql/sql.ts';\nimport type { InferModelFromColumns } from '~/table.ts';\nimport { Columns, Table } from '~/table.ts';\nimport { haveSameKeys, mapUpdateSet } from '~/utils.ts';\nimport type { AnyMySqlColumn } from '../columns/common.ts';\nimport { QueryBuilder } from './query-builder.ts';\nimport type { SelectedFieldsOrdered } from './select.types.ts';\nimport type { MySqlUpdateSetSource } from './update.ts';\n\nexport interface MySqlInsertConfig<TTable extends MySqlTable = MySqlTable> {\n\ttable: TTable;\n\tvalues: Record<string, Param | SQL>[] | MySqlInsertSelectQueryBuilder<TTable> | SQL;\n\tignore: boolean;\n\tonConflict?: SQL;\n\treturning?: SelectedFieldsOrdered;\n\tselect?: boolean;\n}\n\nexport type AnyMySqlInsertConfig = MySqlInsertConfig<MySqlTable>;\n\nexport type MySqlInsertValue<TTable extends MySqlTable> =\n\t& {\n\t\t[Key in keyof TTable['$inferInsert']]: TTable['$inferInsert'][Key] | SQL | Placeholder;\n\t}\n\t& {};\n\nexport type MySqlInsertSelectQueryBuilder<TTable extends MySqlTable> = TypedQueryBuilder<\n\t{ [K in keyof TTable['$inferInsert']]: AnyMySqlColumn | SQL | SQL.Aliased | TTable['$inferInsert'][K] }\n>;\n\nexport class MySqlInsertBuilder<\n\tTTable extends MySqlTable,\n\tTQueryResult extends MySqlQueryResultHKT,\n\tTPreparedQueryHKT extends PreparedQueryHKTBase,\n> {\n\tstatic readonly [entityKind]: string = 'MySqlInsertBuilder';\n\n\tprivate shouldIgnore = false;\n\n\tconstructor(\n\t\tprivate table: TTable,\n\t\tprivate session: MySqlSession,\n\t\tprivate dialect: MySqlDialect,\n\t) {}\n\n\tignore(): this {\n\t\tthis.shouldIgnore = true;\n\t\treturn this;\n\t}\n\n\tvalues(value: MySqlInsertValue<TTable>): MySqlInsertBase<TTable, TQueryResult, TPreparedQueryHKT>;\n\tvalues(values: MySqlInsertValue<TTable>[]): MySqlInsertBase<TTable, TQueryResult, TPreparedQueryHKT>;\n\tvalues(\n\t\tvalues: MySqlInsertValue<TTable> | MySqlInsertValue<TTable>[],\n\t): MySqlInsertBase<TTable, TQueryResult, TPreparedQueryHKT> {\n\t\tvalues = Array.isArray(values) ? values : [values];\n\t\tif (values.length === 0) {\n\t\t\tthrow new Error('values() must be called with at least one value');\n\t\t}\n\t\tconst mappedValues = values.map((entry) => {\n\t\t\tconst result: Record<string, Param | SQL> = {};\n\t\t\tconst cols = this.table[Table.Symbol.Columns];\n\t\t\tfor (const colKey of Object.keys(entry)) {\n\t\t\t\tconst colValue = entry[colKey as keyof typeof entry];\n\t\t\t\tresult[colKey] = is(colValue, SQL) ? colValue : new Param(colValue, cols[colKey]);\n\t\t\t}\n\t\t\treturn result;\n\t\t});\n\n\t\treturn new MySqlInsertBase(this.table, mappedValues, this.shouldIgnore, this.session, this.dialect);\n\t}\n\n\tselect(\n\t\tselectQuery: (qb: QueryBuilder) => MySqlInsertSelectQueryBuilder<TTable>,\n\t): MySqlInsertBase<TTable, TQueryResult, TPreparedQueryHKT>;\n\tselect(selectQuery: (qb: QueryBuilder) => SQL): MySqlInsertBase<TTable, TQueryResult, TPreparedQueryHKT>;\n\tselect(selectQuery: SQL): MySqlInsertBase<TTable, TQueryResult, TPreparedQueryHKT>;\n\tselect(selectQuery: MySqlInsertSelectQueryBuilder<TTable>): MySqlInsertBase<TTable, TQueryResult, TPreparedQueryHKT>;\n\tselect(\n\t\tselectQuery:\n\t\t\t| SQL\n\t\t\t| MySqlInsertSelectQueryBuilder<TTable>\n\t\t\t| ((qb: QueryBuilder) => MySqlInsertSelectQueryBuilder<TTable> | SQL),\n\t): MySqlInsertBase<TTable, TQueryResult, TPreparedQueryHKT> {\n\t\tconst select = typeof selectQuery === 'function' ? selectQuery(new QueryBuilder()) : selectQuery;\n\n\t\tif (\n\t\t\t!is(select, SQL)\n\t\t\t&& !haveSameKeys(this.table[Columns], select._.selectedFields)\n\t\t) {\n\t\t\tthrow new Error(\n\t\t\t\t'Insert select error: selected fields are not the same or are in a different order compared to the table definition',\n\t\t\t);\n\t\t}\n\n\t\treturn new MySqlInsertBase(this.table, select, this.shouldIgnore, this.session, this.dialect, true);\n\t}\n}\n\nexport type MySqlInsertWithout<T extends AnyMySqlInsert, TDynamic extends boolean, K extends keyof T & string> =\n\tTDynamic extends true ? T\n\t\t: Omit<\n\t\t\tMySqlInsertBase<\n\t\t\t\tT['_']['table'],\n\t\t\t\tT['_']['queryResult'],\n\t\t\t\tT['_']['preparedQueryHKT'],\n\t\t\t\tT['_']['returning'],\n\t\t\t\tTDynamic,\n\t\t\t\tT['_']['excludedMethods'] | '$returning'\n\t\t\t>,\n\t\t\tT['_']['excludedMethods'] | K\n\t\t>;\n\nexport type MySqlInsertDynamic<T extends AnyMySqlInsert> = MySqlInsert<\n\tT['_']['table'],\n\tT['_']['queryResult'],\n\tT['_']['preparedQueryHKT'],\n\tT['_']['returning']\n>;\n\nexport type MySqlInsertPrepare<\n\tT extends AnyMySqlInsert,\n\tTReturning extends Record<string, unknown> | undefined = undefined,\n> = PreparedQueryKind<\n\tT['_']['preparedQueryHKT'],\n\tMySqlPreparedQueryConfig & {\n\t\texecute: TReturning extends undefined ? MySqlQueryResultKind<T['_']['queryResult'], never> : TReturning[];\n\t\titerator: never;\n\t},\n\ttrue\n>;\n\nexport type MySqlInsertOnDuplicateKeyUpdateConfig<T extends AnyMySqlInsert> = {\n\tset: MySqlUpdateSetSource<T['_']['table']>;\n};\n\nexport type MySqlInsert<\n\tTTable extends MySqlTable = MySqlTable,\n\tTQueryResult extends MySqlQueryResultHKT = AnyMySqlQueryResultHKT,\n\tTPreparedQueryHKT extends PreparedQueryHKTBase = PreparedQueryHKTBase,\n\tTReturning extends Record<string, unknown> | undefined = Record<string, unknown> | undefined,\n> = MySqlInsertBase<TTable, TQueryResult, TPreparedQueryHKT, TReturning, true, never>;\n\nexport type MySqlInsertReturning<\n\tT extends AnyMySqlInsert,\n\tTDynamic extends boolean,\n> = MySqlInsertBase<\n\tT['_']['table'],\n\tT['_']['queryResult'],\n\tT['_']['preparedQueryHKT'],\n\tInferModelFromColumns<GetPrimarySerialOrDefaultKeys<T['_']['table']['_']['columns']>>,\n\tTDynamic,\n\tT['_']['excludedMethods'] | '$returning'\n>;\n\nexport type AnyMySqlInsert = MySqlInsertBase<any, any, any, any, any, any>;\n\nexport interface MySqlInsertBase<\n\tTTable extends MySqlTable,\n\tTQueryResult extends MySqlQueryResultHKT,\n\tTPreparedQueryHKT extends PreparedQueryHKTBase,\n\tTReturning extends Record<string, unknown> | undefined = undefined,\n\tTDynamic extends boolean = false,\n\tTExcludedMethods extends string = never,\n> extends\n\tQueryPromise<TReturning extends undefined ? MySqlQueryResultKind<TQueryResult, never> : TReturning[]>,\n\tRunnableQuery<TReturning extends undefined ? MySqlQueryResultKind<TQueryResult, never> : TReturning[], 'mysql'>,\n\tSQLWrapper\n{\n\treadonly _: {\n\t\treadonly dialect: 'mysql';\n\t\treadonly table: TTable;\n\t\treadonly queryResult: TQueryResult;\n\t\treadonly preparedQueryHKT: TPreparedQueryHKT;\n\t\treadonly dynamic: TDynamic;\n\t\treadonly excludedMethods: TExcludedMethods;\n\t\treadonly returning: TReturning;\n\t\treadonly result: TReturning extends undefined ? MySqlQueryResultKind<TQueryResult, never> : TReturning[];\n\t};\n}\n\nexport type PrimaryKeyKeys<T extends Record<string, AnyMySqlColumn>> = {\n\t[K in keyof T]: T[K]['_']['isPrimaryKey'] extends true ? T[K]['_']['isAutoincrement'] extends true ? K\n\t\t: T[K]['_']['hasRuntimeDefault'] extends true ? T[K]['_']['isPrimaryKey'] extends true ? K : never\n\t\t: never\n\t\t: T[K]['_']['hasRuntimeDefault'] extends true ? T[K]['_']['isPrimaryKey'] extends true ? K : never\n\t\t: never;\n}[keyof T];\n\nexport type GetPrimarySerialOrDefaultKeys<T extends Record<string, AnyMySqlColumn>> = {\n\t[K in PrimaryKeyKeys<T>]: T[K];\n};\n\nexport class MySqlInsertBase<\n\tTTable extends MySqlTable,\n\tTQueryResult extends MySqlQueryResultHKT,\n\t// eslint-disable-next-line @typescript-eslint/no-unused-vars\n\tTPreparedQueryHKT extends PreparedQueryHKTBase,\n\t// eslint-disable-next-line @typescript-eslint/no-unused-vars\n\tTReturning extends Record<string, unknown> | undefined = undefined,\n\t// eslint-disable-next-line @typescript-eslint/no-unused-vars\n\tTDynamic extends boolean = false,\n\t// eslint-disable-next-line @typescript-eslint/no-unused-vars\n\tTExcludedMethods extends string = never,\n> extends QueryPromise<TReturning extends undefined ? MySqlQueryResultKind<TQueryResult, never> : TReturning[]>\n\timplements\n\t\tRunnableQuery<TReturning extends undefined ? MySqlQueryResultKind<TQueryResult, never> : TReturning[], 'mysql'>,\n\t\tSQLWrapper\n{\n\tstatic override readonly [entityKind]: string = 'MySqlInsert';\n\n\tdeclare protected $table: TTable;\n\n\tprivate config: MySqlInsertConfig<TTable>;\n\n\tconstructor(\n\t\ttable: TTable,\n\t\tvalues: MySqlInsertConfig['values'],\n\t\tignore: boolean,\n\t\tprivate session: MySqlSession,\n\t\tprivate dialect: MySqlDialect,\n\t\tselect?: boolean,\n\t) {\n\t\tsuper();\n\t\tthis.config = { table, values: values as any, select, ignore };\n\t}\n\n\t/**\n\t * Adds an `on duplicate key update` clause to the query.\n\t *\n\t * Calling this method will update the row if any unique index conflicts. MySQL will automatically determine the conflict target based on the primary key and unique indexes.\n\t *\n\t * See docs: {@link https://orm.drizzle.team/docs/insert#on-duplicate-key-update}\n\t *\n\t * @param config The `set` clause\n\t *\n\t * @example\n\t * ```ts\n\t * await db.insert(cars)\n\t * .values({ id: 1, brand: 'BMW'})\n\t * .onDuplicateKeyUpdate({ set: { brand: 'Porsche' }});\n\t * ```\n\t *\n\t * While MySQL does not directly support doing nothing on conflict, you can perform a no-op by setting any column's value to itself and achieve the same effect:\n\t *\n\t * ```ts\n\t * import { sql } from 'drizzle-orm';\n\t *\n\t * await db.insert(cars)\n\t * .values({ id: 1, brand: 'BMW' })\n\t * .onDuplicateKeyUpdate({ set: { id: sql`id` } });\n\t * ```\n\t */\n\tonDuplicateKeyUpdate(\n\t\tconfig: MySqlInsertOnDuplicateKeyUpdateConfig<this>,\n\t): MySqlInsertWithout<this, TDynamic, 'onDuplicateKeyUpdate'> {\n\t\tconst setSql = this.dialect.buildUpdateSet(this.config.table, mapUpdateSet(this.config.table, config.set));\n\t\tthis.config.onConflict = sql`update ${setSql}`;\n\t\treturn this as any;\n\t}\n\n\t$returningId(): MySqlInsertWithout<\n\t\tMySqlInsertReturning<this, TDynamic>,\n\t\tTDynamic,\n\t\t'$returningId'\n\t> {\n\t\tconst returning: SelectedFieldsOrdered = [];\n\t\tfor (const [key, value] of Object.entries(this.config.table[Table.Symbol.Columns])) {\n\t\t\tif (value.primary) {\n\t\t\t\treturning.push({ field: value, path: [key] });\n\t\t\t}\n\t\t}\n\t\tthis.config.returning = returning;\n\t\treturn this as any;\n\t}\n\n\t/** @internal */\n\tgetSQL(): SQL {\n\t\treturn this.dialect.buildInsertQuery(this.config).sql;\n\t}\n\n\ttoSQL(): Query {\n\t\tconst { typings: _typings, ...rest } = this.dialect.sqlToQuery(this.getSQL());\n\t\treturn rest;\n\t}\n\n\tprepare(): MySqlInsertPrepare<this, TReturning> {\n\t\tconst { sql, generatedIds } = this.dialect.buildInsertQuery(this.config);\n\t\treturn this.session.prepareQuery(\n\t\t\tthis.dialect.sqlToQuery(sql),\n\t\t\tundefined,\n\t\t\tundefined,\n\t\t\tgeneratedIds,\n\t\t\tthis.config.returning,\n\t\t) as MySqlInsertPrepare<this, TReturning>;\n\t}\n\n\toverride execute: ReturnType<this['prepare']>['execute'] = (placeholderValues) => {\n\t\treturn this.prepare().execute(placeholderValues);\n\t};\n\n\tprivate createIterator = (): ReturnType<this['prepare']>['iterator'] => {\n\t\tconst self = this;\n\t\treturn async function*(placeholderValues) {\n\t\t\tyield* self.prepare().iterator(placeholderValues);\n\t\t};\n\t};\n\n\titerator = this.createIterator();\n\n\t$dynamic(): MySqlInsertDynamic<this> {\n\t\treturn this as any;\n\t}\n}\n"],"mappings":"AAAA,SAAS,YAAY,UAAU;AAa/B,SAAS,oBAAoB;AAG7B,SAAS,OAAO,KAAK,WAAW;AAEhC,SAAS,SAAS,aAAa;AAC/B,SAAS,cAAc,oBAAoB;AAE3C,SAAS,oBAAoB;AAyBtB,MAAM,mBAIX;AAAA,EAKD,YACS,OACA,SACA,SACP;AAHO;AACA;AACA;AAAA,EACN;AAAA,EARH,QAAiB,UAAU,IAAY;AAAA,EAE/B,eAAe;AAAA,EAQvB,SAAe;AACd,SAAK,eAAe;AACpB,WAAO;AAAA,EACR;A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EA,WAAW,KAAK,eAAe;AAAA,EAE/B,WAAqC;AACpC,WAAO;AAAA,EACR;AACD;","names":["sql"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "drizzle-orm",
3
- "version": "0.36.2",
3
+ "version": "0.36.3",
4
4
  "description": "Drizzle ORM package for SQL databases",
5
5
  "type": "module",
6
6
  "scripts": {
@@ -516,54 +516,6 @@
516
516
  "types": "./view-common.d.ts",
517
517
  "default": "./view-common.js"
518
518
  },
519
- "./bun-sqlite/driver": {
520
- "import": {
521
- "types": "./bun-sqlite/driver.d.ts",
522
- "default": "./bun-sqlite/driver.js"
523
- },
524
- "require": {
525
- "types": "./bun-sqlite/driver.d.cts",
526
- "default": "./bun-sqlite/driver.cjs"
527
- },
528
- "types": "./bun-sqlite/driver.d.ts",
529
- "default": "./bun-sqlite/driver.js"
530
- },
531
- "./bun-sqlite": {
532
- "import": {
533
- "types": "./bun-sqlite/index.d.ts",
534
- "default": "./bun-sqlite/index.js"
535
- },
536
- "require": {
537
- "types": "./bun-sqlite/index.d.cts",
538
- "default": "./bun-sqlite/index.cjs"
539
- },
540
- "types": "./bun-sqlite/index.d.ts",
541
- "default": "./bun-sqlite/index.js"
542
- },
543
- "./bun-sqlite/migrator": {
544
- "import": {
545
- "types": "./bun-sqlite/migrator.d.ts",
546
- "default": "./bun-sqlite/migrator.js"
547
- },
548
- "require": {
549
- "types": "./bun-sqlite/migrator.d.cts",
550
- "default": "./bun-sqlite/migrator.cjs"
551
- },
552
- "types": "./bun-sqlite/migrator.d.ts",
553
- "default": "./bun-sqlite/migrator.js"
554
- },
555
- "./bun-sqlite/session": {
556
- "import": {
557
- "types": "./bun-sqlite/session.d.ts",
558
- "default": "./bun-sqlite/session.js"
559
- },
560
- "require": {
561
- "types": "./bun-sqlite/session.d.cts",
562
- "default": "./bun-sqlite/session.cjs"
563
- },
564
- "types": "./bun-sqlite/session.d.ts",
565
- "default": "./bun-sqlite/session.js"
566
- },
567
519
  "./better-sqlite3/driver": {
568
520
  "import": {
569
521
  "types": "./better-sqlite3/driver.d.ts",
@@ -612,6 +564,54 @@
612
564
  "types": "./better-sqlite3/session.d.ts",
613
565
  "default": "./better-sqlite3/session.js"
614
566
  },
567
+ "./bun-sqlite/driver": {
568
+ "import": {
569
+ "types": "./bun-sqlite/driver.d.ts",
570
+ "default": "./bun-sqlite/driver.js"
571
+ },
572
+ "require": {
573
+ "types": "./bun-sqlite/driver.d.cts",
574
+ "default": "./bun-sqlite/driver.cjs"
575
+ },
576
+ "types": "./bun-sqlite/driver.d.ts",
577
+ "default": "./bun-sqlite/driver.js"
578
+ },
579
+ "./bun-sqlite": {
580
+ "import": {
581
+ "types": "./bun-sqlite/index.d.ts",
582
+ "default": "./bun-sqlite/index.js"
583
+ },
584
+ "require": {
585
+ "types": "./bun-sqlite/index.d.cts",
586
+ "default": "./bun-sqlite/index.cjs"
587
+ },
588
+ "types": "./bun-sqlite/index.d.ts",
589
+ "default": "./bun-sqlite/index.js"
590
+ },
591
+ "./bun-sqlite/migrator": {
592
+ "import": {
593
+ "types": "./bun-sqlite/migrator.d.ts",
594
+ "default": "./bun-sqlite/migrator.js"
595
+ },
596
+ "require": {
597
+ "types": "./bun-sqlite/migrator.d.cts",
598
+ "default": "./bun-sqlite/migrator.cjs"
599
+ },
600
+ "types": "./bun-sqlite/migrator.d.ts",
601
+ "default": "./bun-sqlite/migrator.js"
602
+ },
603
+ "./bun-sqlite/session": {
604
+ "import": {
605
+ "types": "./bun-sqlite/session.d.ts",
606
+ "default": "./bun-sqlite/session.js"
607
+ },
608
+ "require": {
609
+ "types": "./bun-sqlite/session.d.cts",
610
+ "default": "./bun-sqlite/session.cjs"
611
+ },
612
+ "types": "./bun-sqlite/session.d.ts",
613
+ "default": "./bun-sqlite/session.js"
614
+ },
615
615
  "./d1/driver": {
616
616
  "import": {
617
617
  "types": "./d1/driver.d.ts",
@@ -116,12 +116,19 @@ class PgDialect {
116
116
  return [res];
117
117
  }));
118
118
  }
119
- buildUpdateQuery({ table, set, where, returning, withList }) {
119
+ buildUpdateQuery({ table, set, where, returning, withList, from, joins }) {
120
120
  const withSql = this.buildWithCTE(withList);
121
+ const tableName = table[import_table.PgTable.Symbol.Name];
122
+ const tableSchema = table[import_table.PgTable.Symbol.Schema];
123
+ const origTableName = table[import_table.PgTable.Symbol.OriginalName];
124
+ const alias = tableName === origTableName ? void 0 : tableName;
125
+ const tableSql = import_sql2.sql`${tableSchema ? import_sql2.sql`${import_sql2.sql.identifier(tableSchema)}.` : void 0}${import_sql2.sql.identifier(origTableName)}${alias && import_sql2.sql` ${import_sql2.sql.identifier(alias)}`}`;
121
126
  const setSql = this.buildUpdateSet(table, set);
122
- const returningSql = returning ? import_sql2.sql` returning ${this.buildSelection(returning, { isSingleTable: true })}` : void 0;
127
+ const fromSql = from && import_sql2.sql.join([import_sql2.sql.raw(" from "), this.buildFromTable(from)]);
128
+ const joinsSql = this.buildJoins(joins);
129
+ const returningSql = returning ? import_sql2.sql` returning ${this.buildSelection(returning, { isSingleTable: !from })}` : void 0;
123
130
  const whereSql = where ? import_sql2.sql` where ${where}` : void 0;
124
- return import_sql2.sql`${withSql}update ${table} set ${setSql}${whereSql}${returningSql}`;
131
+ return import_sql2.sql`${withSql}update ${tableSql} set ${setSql}${fromSql}${joinsSql}${whereSql}${returningSql}`;
125
132
  }
126
133
  /**
127
134
  * Builds selection SQL with provided fields/expressions
@@ -173,6 +180,54 @@ class PgDialect {
173
180
  });
174
181
  return import_sql2.sql.join(chunks);
175
182
  }
183
+ buildJoins(joins) {
184
+ if (!joins || joins.length === 0) {
185
+ return void 0;
186
+ }
187
+ const joinsArray = [];
188
+ for (const [index, joinMeta] of joins.entries()) {
189
+ if (index === 0) {
190
+ joinsArray.push(import_sql2.sql` `);
191
+ }
192
+ const table = joinMeta.table;
193
+ const lateralSql = joinMeta.lateral ? import_sql2.sql` lateral` : void 0;
194
+ if ((0, import_entity.is)(table, import_table.PgTable)) {
195
+ const tableName = table[import_table.PgTable.Symbol.Name];
196
+ const tableSchema = table[import_table.PgTable.Symbol.Schema];
197
+ const origTableName = table[import_table.PgTable.Symbol.OriginalName];
198
+ const alias = tableName === origTableName ? void 0 : joinMeta.alias;
199
+ joinsArray.push(
200
+ import_sql2.sql`${import_sql2.sql.raw(joinMeta.joinType)} join${lateralSql} ${tableSchema ? import_sql2.sql`${import_sql2.sql.identifier(tableSchema)}.` : void 0}${import_sql2.sql.identifier(origTableName)}${alias && import_sql2.sql` ${import_sql2.sql.identifier(alias)}`} on ${joinMeta.on}`
201
+ );
202
+ } else if ((0, import_entity.is)(table, import_sql.View)) {
203
+ const viewName = table[import_view_common.ViewBaseConfig].name;
204
+ const viewSchema = table[import_view_common.ViewBaseConfig].schema;
205
+ const origViewName = table[import_view_common.ViewBaseConfig].originalName;
206
+ const alias = viewName === origViewName ? void 0 : joinMeta.alias;
207
+ joinsArray.push(
208
+ import_sql2.sql`${import_sql2.sql.raw(joinMeta.joinType)} join${lateralSql} ${viewSchema ? import_sql2.sql`${import_sql2.sql.identifier(viewSchema)}.` : void 0}${import_sql2.sql.identifier(origViewName)}${alias && import_sql2.sql` ${import_sql2.sql.identifier(alias)}`} on ${joinMeta.on}`
209
+ );
210
+ } else {
211
+ joinsArray.push(
212
+ import_sql2.sql`${import_sql2.sql.raw(joinMeta.joinType)} join${lateralSql} ${table} on ${joinMeta.on}`
213
+ );
214
+ }
215
+ if (index < joins.length - 1) {
216
+ joinsArray.push(import_sql2.sql` `);
217
+ }
218
+ }
219
+ return import_sql2.sql.join(joinsArray);
220
+ }
221
+ buildFromTable(table) {
222
+ if ((0, import_entity.is)(table, import_table2.Table) && table[import_table2.Table.Symbol.OriginalName] !== table[import_table2.Table.Symbol.Name]) {
223
+ let fullName = import_sql2.sql`${import_sql2.sql.identifier(table[import_table2.Table.Symbol.OriginalName])}`;
224
+ if (table[import_table2.Table.Symbol.Schema]) {
225
+ fullName = import_sql2.sql`${import_sql2.sql.identifier(table[import_table2.Table.Symbol.Schema])}.${fullName}`;
226
+ }
227
+ return import_sql2.sql`${fullName} ${import_sql2.sql.identifier(table[import_table2.Table.Symbol.Name])}`;
228
+ }
229
+ return table;
230
+ }
176
231
  buildSelectQuery({
177
232
  withList,
178
233
  fields,
@@ -207,51 +262,8 @@ class PgDialect {
207
262
  distinctSql = distinct === true ? import_sql2.sql` distinct` : import_sql2.sql` distinct on (${import_sql2.sql.join(distinct.on, import_sql2.sql`, `)})`;
208
263
  }
209
264
  const selection = this.buildSelection(fieldsList, { isSingleTable });
210
- const tableSql = (() => {
211
- if ((0, import_entity.is)(table, import_table2.Table) && table[import_table2.Table.Symbol.OriginalName] !== table[import_table2.Table.Symbol.Name]) {
212
- let fullName = import_sql2.sql`${import_sql2.sql.identifier(table[import_table2.Table.Symbol.OriginalName])}`;
213
- if (table[import_table2.Table.Symbol.Schema]) {
214
- fullName = import_sql2.sql`${import_sql2.sql.identifier(table[import_table2.Table.Symbol.Schema])}.${fullName}`;
215
- }
216
- return import_sql2.sql`${fullName} ${import_sql2.sql.identifier(table[import_table2.Table.Symbol.Name])}`;
217
- }
218
- return table;
219
- })();
220
- const joinsArray = [];
221
- if (joins) {
222
- for (const [index, joinMeta] of joins.entries()) {
223
- if (index === 0) {
224
- joinsArray.push(import_sql2.sql` `);
225
- }
226
- const table2 = joinMeta.table;
227
- const lateralSql = joinMeta.lateral ? import_sql2.sql` lateral` : void 0;
228
- if ((0, import_entity.is)(table2, import_table.PgTable)) {
229
- const tableName = table2[import_table.PgTable.Symbol.Name];
230
- const tableSchema = table2[import_table.PgTable.Symbol.Schema];
231
- const origTableName = table2[import_table.PgTable.Symbol.OriginalName];
232
- const alias = tableName === origTableName ? void 0 : joinMeta.alias;
233
- joinsArray.push(
234
- import_sql2.sql`${import_sql2.sql.raw(joinMeta.joinType)} join${lateralSql} ${tableSchema ? import_sql2.sql`${import_sql2.sql.identifier(tableSchema)}.` : void 0}${import_sql2.sql.identifier(origTableName)}${alias && import_sql2.sql` ${import_sql2.sql.identifier(alias)}`} on ${joinMeta.on}`
235
- );
236
- } else if ((0, import_entity.is)(table2, import_sql.View)) {
237
- const viewName = table2[import_view_common.ViewBaseConfig].name;
238
- const viewSchema = table2[import_view_common.ViewBaseConfig].schema;
239
- const origViewName = table2[import_view_common.ViewBaseConfig].originalName;
240
- const alias = viewName === origViewName ? void 0 : joinMeta.alias;
241
- joinsArray.push(
242
- import_sql2.sql`${import_sql2.sql.raw(joinMeta.joinType)} join${lateralSql} ${viewSchema ? import_sql2.sql`${import_sql2.sql.identifier(viewSchema)}.` : void 0}${import_sql2.sql.identifier(origViewName)}${alias && import_sql2.sql` ${import_sql2.sql.identifier(alias)}`} on ${joinMeta.on}`
243
- );
244
- } else {
245
- joinsArray.push(
246
- import_sql2.sql`${import_sql2.sql.raw(joinMeta.joinType)} join${lateralSql} ${table2} on ${joinMeta.on}`
247
- );
248
- }
249
- if (index < joins.length - 1) {
250
- joinsArray.push(import_sql2.sql` `);
251
- }
252
- }
253
- }
254
- const joinsSql = import_sql2.sql.join(joinsArray);
265
+ const tableSql = this.buildFromTable(table);
266
+ const joinsSql = this.buildJoins(joins);
255
267
  const whereSql = where ? import_sql2.sql` where ${where}` : void 0;
256
268
  const havingSql = having ? import_sql2.sql` having ${having}` : void 0;
257
269
  let orderBySql;
@@ -332,43 +344,54 @@ class PgDialect {
332
344
  const offsetSql = offset ? import_sql2.sql` offset ${offset}` : void 0;
333
345
  return import_sql2.sql`${leftChunk}${operatorChunk}${rightChunk}${orderBySql}${limitSql}${offsetSql}`;
334
346
  }
335
- buildInsertQuery({ table, values, onConflict, returning, withList }) {
347
+ buildInsertQuery({ table, values: valuesOrSelect, onConflict, returning, withList, select }) {
336
348
  const valuesSqlList = [];
337
349
  const columns = table[import_table2.Table.Symbol.Columns];
338
350
  const colEntries = Object.entries(columns).filter(([_, col]) => !col.shouldDisableInsert());
339
351
  const insertOrder = colEntries.map(
340
352
  ([, column]) => import_sql2.sql.identifier(this.casing.getColumnCasing(column))
341
353
  );
342
- for (const [valueIndex, value] of values.entries()) {
343
- const valueList = [];
344
- for (const [fieldName, col] of colEntries) {
345
- const colValue = value[fieldName];
346
- if (colValue === void 0 || (0, import_entity.is)(colValue, import_sql2.Param) && colValue.value === void 0) {
347
- if (col.defaultFn !== void 0) {
348
- const defaultFnResult = col.defaultFn();
349
- const defaultValue = (0, import_entity.is)(defaultFnResult, import_sql2.SQL) ? defaultFnResult : import_sql2.sql.param(defaultFnResult, col);
350
- valueList.push(defaultValue);
351
- } else if (!col.default && col.onUpdateFn !== void 0) {
352
- const onUpdateFnResult = col.onUpdateFn();
353
- const newValue = (0, import_entity.is)(onUpdateFnResult, import_sql2.SQL) ? onUpdateFnResult : import_sql2.sql.param(onUpdateFnResult, col);
354
- valueList.push(newValue);
354
+ if (select) {
355
+ const select2 = valuesOrSelect;
356
+ if ((0, import_entity.is)(select2, import_sql2.SQL)) {
357
+ valuesSqlList.push(select2);
358
+ } else {
359
+ valuesSqlList.push(select2.getSQL());
360
+ }
361
+ } else {
362
+ const values = valuesOrSelect;
363
+ valuesSqlList.push(import_sql2.sql.raw("values "));
364
+ for (const [valueIndex, value] of values.entries()) {
365
+ const valueList = [];
366
+ for (const [fieldName, col] of colEntries) {
367
+ const colValue = value[fieldName];
368
+ if (colValue === void 0 || (0, import_entity.is)(colValue, import_sql2.Param) && colValue.value === void 0) {
369
+ if (col.defaultFn !== void 0) {
370
+ const defaultFnResult = col.defaultFn();
371
+ const defaultValue = (0, import_entity.is)(defaultFnResult, import_sql2.SQL) ? defaultFnResult : import_sql2.sql.param(defaultFnResult, col);
372
+ valueList.push(defaultValue);
373
+ } else if (!col.default && col.onUpdateFn !== void 0) {
374
+ const onUpdateFnResult = col.onUpdateFn();
375
+ const newValue = (0, import_entity.is)(onUpdateFnResult, import_sql2.SQL) ? onUpdateFnResult : import_sql2.sql.param(onUpdateFnResult, col);
376
+ valueList.push(newValue);
377
+ } else {
378
+ valueList.push(import_sql2.sql`default`);
379
+ }
355
380
  } else {
356
- valueList.push(import_sql2.sql`default`);
381
+ valueList.push(colValue);
357
382
  }
358
- } else {
359
- valueList.push(colValue);
360
383
  }
361
- }
362
- valuesSqlList.push(valueList);
363
- if (valueIndex < values.length - 1) {
364
- valuesSqlList.push(import_sql2.sql`, `);
384
+ valuesSqlList.push(valueList);
385
+ if (valueIndex < values.length - 1) {
386
+ valuesSqlList.push(import_sql2.sql`, `);
387
+ }
365
388
  }
366
389
  }
367
390
  const withSql = this.buildWithCTE(withList);
368
391
  const valuesSql = import_sql2.sql.join(valuesSqlList);
369
392
  const returningSql = returning ? import_sql2.sql` returning ${this.buildSelection(returning, { isSingleTable: true })}` : void 0;
370
393
  const onConflictSql = onConflict ? import_sql2.sql` on conflict ${onConflict}` : void 0;
371
- return import_sql2.sql`${withSql}insert into ${table} ${insertOrder} values ${valuesSql}${onConflictSql}${returningSql}`;
394
+ return import_sql2.sql`${withSql}insert into ${table} ${insertOrder} ${valuesSql}${onConflictSql}${returningSql}`;
372
395
  }
373
396
  buildRefreshMaterializedViewQuery({ view, concurrently, withNoData }) {
374
397
  const concurrentlySql = concurrently ? import_sql2.sql` concurrently` : void 0;