drizzle-orm 0.27.3-e080bed → 0.28.0-5dd9190

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 (120) hide show
  1. package/{alias-5aeeb2f5.cjs → alias-3f5ac719.cjs} +267 -142
  2. package/alias-3f5ac719.cjs.map +1 -0
  3. package/{alias-6eaa2e87.mjs → alias-7164ff43.mjs} +223 -126
  4. package/alias-7164ff43.mjs.map +1 -0
  5. package/aws-data-api/pg/index.cjs +1 -1
  6. package/aws-data-api/pg/index.d.ts +4 -4
  7. package/aws-data-api/pg/index.mjs +2 -2
  8. package/aws-data-api/pg/migrator.d.ts +4 -4
  9. package/better-sqlite3/index.cjs +2 -2
  10. package/better-sqlite3/index.d.ts +5 -5
  11. package/better-sqlite3/index.mjs +2 -2
  12. package/better-sqlite3/migrator.d.ts +5 -5
  13. package/bun-sqlite/index.cjs +2 -2
  14. package/bun-sqlite/index.d.ts +5 -5
  15. package/bun-sqlite/index.mjs +2 -2
  16. package/bun-sqlite/migrator.d.ts +5 -5
  17. package/column.d-9d2f4045.d.ts +1151 -0
  18. package/d1/index.cjs +2 -2
  19. package/d1/index.d.ts +5 -5
  20. package/d1/index.mjs +2 -2
  21. package/d1/migrator.d.ts +5 -5
  22. package/{driver.d-58d095ec.d.ts → driver.d-055d4782.d.ts} +2 -2
  23. package/{driver.d-d2f034d7.d.ts → driver.d-20707624.d.ts} +2 -2
  24. package/{driver.d-7f308b1e.d.ts → driver.d-ab0f126f.d.ts} +2 -2
  25. package/{driver.d-f55c470d.d.ts → driver.d-b8dd753f.d.ts} +2 -2
  26. package/{driver.d-28b0b849.d.ts → driver.d-c4dd56f7.d.ts} +2 -2
  27. package/{driver.d-53ed595e.d.ts → driver.d-eb654ed5.d.ts} +2 -2
  28. package/index.cjs +3 -3
  29. package/index.d.ts +4 -4
  30. package/index.mjs +2 -2
  31. package/knex/index.d.ts +1 -1
  32. package/kysely/index.d.ts +1 -1
  33. package/libsql/index.cjs +2 -2
  34. package/libsql/index.d.ts +5 -5
  35. package/libsql/index.mjs +2 -2
  36. package/libsql/migrator.d.ts +5 -5
  37. package/mysql-core/index.cjs +57 -21
  38. package/mysql-core/index.cjs.map +1 -1
  39. package/mysql-core/index.d.ts +184 -344
  40. package/mysql-core/index.mjs +58 -22
  41. package/mysql-core/index.mjs.map +1 -1
  42. package/mysql2/index.cjs +14 -10
  43. package/mysql2/index.cjs.map +1 -1
  44. package/mysql2/index.d.ts +16 -12
  45. package/mysql2/index.mjs +15 -11
  46. package/mysql2/index.mjs.map +1 -1
  47. package/mysql2/migrator.d.ts +4 -4
  48. package/neon-http/index.cjs +1 -1
  49. package/neon-http/index.d.ts +4 -4
  50. package/neon-http/index.mjs +2 -2
  51. package/neon-http/migrator.cjs +1 -1
  52. package/neon-http/migrator.d.ts +4 -4
  53. package/neon-http/migrator.mjs +1 -1
  54. package/neon-serverless/index.cjs +1 -1
  55. package/neon-serverless/index.d.ts +4 -4
  56. package/neon-serverless/index.mjs +2 -2
  57. package/neon-serverless/migrator.d.ts +4 -4
  58. package/node-postgres/index.cjs +1 -1
  59. package/node-postgres/index.d.ts +4 -4
  60. package/node-postgres/index.mjs +2 -2
  61. package/node-postgres/migrator.d.ts +4 -4
  62. package/package.json +3 -3
  63. package/pg-core/index.cjs +1 -1
  64. package/pg-core/index.cjs.map +1 -1
  65. package/pg-core/index.d.ts +207 -373
  66. package/pg-core/index.mjs +48 -18
  67. package/pg-core/index.mjs.map +1 -1
  68. package/planetscale-serverless/index.cjs +5 -5
  69. package/planetscale-serverless/index.cjs.map +1 -1
  70. package/planetscale-serverless/index.d.ts +5 -5
  71. package/planetscale-serverless/index.mjs +5 -5
  72. package/planetscale-serverless/index.mjs.map +1 -1
  73. package/planetscale-serverless/migrator.d.ts +4 -4
  74. package/postgres-js/index.cjs +1 -1
  75. package/postgres-js/index.d.ts +4 -4
  76. package/postgres-js/index.mjs +2 -2
  77. package/postgres-js/migrator.d.ts +4 -4
  78. package/{query-promise.d-fd15f63a.d.ts → query-promise.d-bc96befc.d.ts} +48 -45
  79. package/{db.d-2e828d8c.d.ts → select.types.d-570dd25a.d.ts} +281 -465
  80. package/{select.types.d-35b6e089.d.ts → select.types.d-73209a67.d.ts} +47 -258
  81. package/{select.types.d-1ea8ee3b.d.ts → select.types.d-a3c4c974.d.ts} +20 -19
  82. package/{db.d-9d586c00.d.ts → select.types.d-d0a10728.d.ts} +471 -731
  83. package/{session-c47f12d9.mjs → session-0c131cde.mjs} +2 -2
  84. package/{session-c47f12d9.mjs.map → session-0c131cde.mjs.map} +1 -1
  85. package/{session-2431f9e1.mjs → session-16f863cd.mjs} +36 -64
  86. package/session-16f863cd.mjs.map +1 -0
  87. package/{session-6bfef963.cjs → session-805d2876.cjs} +64 -131
  88. package/session-805d2876.cjs.map +1 -0
  89. package/{session-fa99bfce.mjs → session-ac71392b.mjs} +67 -134
  90. package/session-ac71392b.mjs.map +1 -0
  91. package/{session-fa9720a5.cjs → session-e9306785.cjs} +61 -71
  92. package/session-e9306785.cjs.map +1 -0
  93. package/sql-js/index.cjs +2 -2
  94. package/sql-js/index.d.ts +5 -5
  95. package/sql-js/index.mjs +2 -2
  96. package/sql-js/migrator.d.ts +5 -5
  97. package/sqlite-core/index.cjs +2 -2
  98. package/sqlite-core/index.d.ts +83 -137
  99. package/sqlite-core/index.mjs +28 -10
  100. package/sqlite-core/index.mjs.map +1 -1
  101. package/sqlite-proxy/index.cjs +2 -2
  102. package/sqlite-proxy/index.d.ts +6 -6
  103. package/sqlite-proxy/index.mjs +2 -2
  104. package/sqlite-proxy/migrator.cjs +1 -1
  105. package/sqlite-proxy/migrator.d.ts +5 -5
  106. package/sqlite-proxy/migrator.mjs +1 -1
  107. package/vercel-postgres/index.cjs +1 -1
  108. package/vercel-postgres/index.d.ts +4 -4
  109. package/vercel-postgres/index.mjs +2 -2
  110. package/vercel-postgres/migrator.d.ts +4 -4
  111. package/version.cjs +1 -1
  112. package/version.d.ts +1 -1
  113. package/version.mjs +1 -1
  114. package/alias-5aeeb2f5.cjs.map +0 -1
  115. package/alias-6eaa2e87.mjs.map +0 -1
  116. package/column.d-9d74a4f3.d.ts +0 -558
  117. package/session-2431f9e1.mjs.map +0 -1
  118. package/session-6bfef963.cjs.map +0 -1
  119. package/session-fa9720a5.cjs.map +0 -1
  120. package/session-fa99bfce.mjs.map +0 -1
@@ -1,417 +1,315 @@
1
- import { a as SQL, e as entityKind, o as ColumnBuilderHKTBase, aD as Assume, l as ColumnBuilderBaseConfig, c as ColumnHKTBase, C as ColumnBaseConfig, s as ColumnBuilder, aA as Update, f as ColumnHKT, g as Column, r as ColumnBuilderRuntimeConfig, h as AnyColumnHKT, ap as TableConfig$1, T as Table, aq as UpdateTableConfig, at as AnyTableHKT, v as BuildColumns, B as BuildColumn, t as AnyColumnBuilder, aQ as ColumnsSelection, V as View, am as Subquery, an as WithSubquery, a1 as SQLWrapper, aG as ValueOrArray, ai as Placeholder, S as Simplify, Q as Query, as as AnyTable, K as SelectedFieldsFlat$1, X as SelectedFields$1, Y as SelectedFieldsOrdered$1, I as InferModel, ay as UpdateSet, G as GetColumnData, ae as Param, a8 as DriverValueEncoder, a0 as QueryTypingsValue, aN as KnownKeysOnly, aF as DrizzleTypeError } from './column.d-9d74a4f3.js';
1
+ import { e as entityKind, bm as AnyPgTable, aF as KnownKeysOnly, Q as Query, aJ as ColumnsSelection, ah as Subquery, ai as WithSubquery, ax as DrizzleTypeError, bn as AnyPgColumn, X as SQLWrapper, a as SQL, aq as UpdateSet, a3 as DriverValueEncoder, W as QueryTypingsValue, bo as PgColumn, I as InferModel, G as GetColumnData, a9 as Param, ad as Placeholder, bp as IndexColumn, ay as ValueOrArray, bq as PgColumnBuilder, o as BuildColumns, br as AnyPgColumnBuilder, V as View, bs as PgTable, T as Table, bt as PgTableWithColumns, al as UpdateTableConfig, av as Assume, y as SelectedFieldsFlat$1, E as SelectedFields$1, F as SelectedFieldsOrdered$1 } from './column.d-9d2f4045.js';
2
+ import { A as AddAliasToSelection, T as TypedQueryBuilder, f as SelectResultFields, G as GetSelectTableName, a as GetSelectTableSelection, S as SelectMode, J as JoinNullability, B as BuildSubquerySelection, b as SelectResult, c as JoinType, M as MapColumnsToTableAlias, d as AppendToResult, e as AppendToNullabilityMap } from './select.types.d-a3c4c974.js';
3
+ import { T as TablesRelationalConfig, L as TableRelationalConfig, K as DBQueryConfig, V as BuildQueryResult, Q as QueryPromise, N as ExtractTablesWithRelations, R as RelationalSchemaConfig, a as Relation, a3 as BuildRelationalQueryResult } from './query-promise.d-bc96befc.js';
2
4
  import { MigrationMeta } from './migrator.js';
3
- import { Q as QueryPromise, T as TablesRelationalConfig, K as TableRelationalConfig, J as DBQueryConfig, a as Relation, a2 as BuildRelationalQueryResult, U as BuildQueryResult, L as ExtractTablesWithRelations, R as RelationalSchemaConfig } from './query-promise.d-fd15f63a.js';
4
- import { T as TypedQueryBuilder, A as AddAliasToSelection, G as GetSelectTableName, a as GetSelectTableSelection, S as SelectMode, J as JoinNullability, B as BuildSubquerySelection, b as SelectResult, c as JoinType, M as MapColumnsToTableAlias, d as AppendToResult, e as AppendToNullabilityMap, f as SelectResultFields } from './select.types.d-1ea8ee3b.js';
5
5
 
6
- declare class CheckBuilder {
7
- name: string;
8
- value: SQL;
9
- static readonly [entityKind]: string;
10
- protected brand: 'PgConstraintBuilder';
11
- constructor(name: string, value: SQL);
12
- }
13
- declare class Check {
14
- table: AnyPgTable;
6
+ declare class RelationalQueryBuilder<TSchema extends TablesRelationalConfig, TFields extends TableRelationalConfig> {
7
+ private fullSchema;
8
+ private schema;
9
+ private tableNamesMap;
10
+ private table;
11
+ private tableConfig;
12
+ private dialect;
13
+ private session;
15
14
  static readonly [entityKind]: string;
16
- readonly name: string;
17
- readonly value: SQL;
18
- constructor(table: AnyPgTable, builder: CheckBuilder);
15
+ constructor(fullSchema: Record<string, unknown>, schema: TSchema, tableNamesMap: Record<string, string>, table: AnyPgTable, tableConfig: TableRelationalConfig, dialect: PgDialect, session: PgSession);
16
+ findMany<TConfig extends DBQueryConfig<'many', true, TSchema, TFields>>(config?: KnownKeysOnly<TConfig, DBQueryConfig<'many', true, TSchema, TFields>>): PgRelationalQuery<BuildQueryResult<TSchema, TFields, TConfig>[]>;
17
+ findFirst<TSelection extends Omit<DBQueryConfig<'many', true, TSchema, TFields>, 'limit'>>(config?: KnownKeysOnly<TSelection, Omit<DBQueryConfig<'many', true, TSchema, TFields>, 'limit'>>): PgRelationalQuery<BuildQueryResult<TSchema, TFields, TSelection> | undefined>;
19
18
  }
20
- declare function check(name: string, value: SQL): CheckBuilder;
21
-
22
- type UpdateDeleteAction = 'cascade' | 'restrict' | 'no action' | 'set null' | 'set default';
23
- type Reference = () => {
24
- readonly columns: AnyPgColumn[];
25
- readonly foreignTable: AnyPgTable;
26
- readonly foreignColumns: AnyPgColumn[];
27
- };
28
- declare class ForeignKeyBuilder {
29
- static readonly [entityKind]: string;
30
- constructor(config: () => {
31
- columns: AnyPgColumn[];
32
- foreignColumns: AnyPgColumn[];
33
- }, actions?: {
34
- onUpdate?: UpdateDeleteAction;
35
- onDelete?: UpdateDeleteAction;
36
- } | undefined);
37
- onUpdate(action: UpdateDeleteAction): this;
38
- onDelete(action: UpdateDeleteAction): this;
39
- }
40
- type AnyForeignKeyBuilder = ForeignKeyBuilder;
41
- declare class ForeignKey {
42
- readonly table: AnyPgTable;
19
+ declare class PgRelationalQuery<TResult> extends QueryPromise<TResult> {
20
+ private fullSchema;
21
+ private schema;
22
+ private tableNamesMap;
23
+ private table;
24
+ private tableConfig;
25
+ private dialect;
26
+ private session;
27
+ private config;
28
+ private mode;
43
29
  static readonly [entityKind]: string;
44
- readonly reference: Reference;
45
- readonly onUpdate: UpdateDeleteAction | undefined;
46
- readonly onDelete: UpdateDeleteAction | undefined;
47
- constructor(table: AnyPgTable, builder: ForeignKeyBuilder);
48
- getName(): string;
49
- }
50
- type ColumnsWithTable<TTableName extends string, TColumns extends AnyPgColumn[]> = {
51
- [Key in keyof TColumns]: AnyPgColumn<{
52
- tableName: TTableName;
30
+ protected $brand: 'PgRelationalQuery';
31
+ constructor(fullSchema: Record<string, unknown>, schema: TablesRelationalConfig, tableNamesMap: Record<string, string>, table: AnyPgTable, tableConfig: TableRelationalConfig, dialect: PgDialect, session: PgSession, config: DBQueryConfig<'many', true> | true, mode: 'many' | 'first');
32
+ private _prepare;
33
+ prepare(name: string): PreparedQuery<PreparedQueryConfig & {
34
+ execute: TResult;
53
35
  }>;
54
- };
55
- declare function foreignKey<TTableName extends string, TForeignTableName extends string, TColumns extends [AnyPgColumn<{
56
- tableName: TTableName;
57
- }>, ...AnyPgColumn<{
58
- tableName: TTableName;
59
- }>[]]>(config: {
60
- columns: TColumns;
61
- foreignColumns: ColumnsWithTable<TForeignTableName, TColumns>;
62
- }): ForeignKeyBuilder;
63
-
64
- interface ReferenceConfig {
65
- ref: () => AnyPgColumn;
66
- actions: {
67
- onUpdate?: UpdateDeleteAction;
68
- onDelete?: UpdateDeleteAction;
69
- };
70
- }
71
- interface PgColumnBuilderHKT extends ColumnBuilderHKTBase {
72
- _type: PgColumnBuilder<PgColumnBuilderHKT, Assume<this['config'], ColumnBuilderBaseConfig>>;
73
- _columnHKT: PgColumnHKT;
36
+ execute(): Promise<TResult>;
74
37
  }
75
- interface PgColumnHKT extends ColumnHKTBase {
76
- _type: PgColumn<PgColumnHKT, Assume<this['config'], ColumnBaseConfig>>;
38
+
39
+ interface PgRefreshMaterializedView<TQueryResult extends QueryResultHKT> extends QueryPromise<QueryResultKind<TQueryResult, never>> {
77
40
  }
78
- declare abstract class PgColumnBuilder<THKT extends ColumnBuilderHKTBase, T extends ColumnBuilderBaseConfig, TRuntimeConfig extends object = {}, TTypeConfig extends object = {}> extends ColumnBuilder<THKT, T, TRuntimeConfig, TTypeConfig & {
79
- pgBrand: 'PgColumnBuilder';
80
- }> {
81
- private foreignKeyConfigs;
41
+ declare class PgRefreshMaterializedView<TQueryResult extends QueryResultHKT> extends QueryPromise<QueryResultKind<TQueryResult, never>> {
42
+ private session;
43
+ private dialect;
82
44
  static readonly [entityKind]: string;
83
- array(size?: number): PgArrayBuilder<{
84
- name: NonNullable<T['name']>;
85
- notNull: NonNullable<T['notNull']>;
86
- hasDefault: NonNullable<T['hasDefault']>;
87
- data: T['data'][];
88
- driverParam: T['driverParam'][] | string;
45
+ private config;
46
+ constructor(view: PgMaterializedView, session: PgSession, dialect: PgDialect);
47
+ concurrently(): this;
48
+ withNoData(): this;
49
+ toSQL(): {
50
+ sql: Query['sql'];
51
+ params: Query['params'];
52
+ };
53
+ private _prepare;
54
+ prepare(name: string): PreparedQuery<PreparedQueryConfig & {
55
+ execute: QueryResultKind<TQueryResult, never>;
89
56
  }>;
90
- references(ref: ReferenceConfig['ref'], actions?: ReferenceConfig['actions']): this;
91
- unique(name?: string, config?: {
92
- nulls: 'distinct' | 'not distinct';
93
- }): this;
94
- }
95
- type AnyPgColumnBuilder<TPartial extends Partial<ColumnBuilderBaseConfig> = {}> = PgColumnBuilder<PgColumnBuilderHKT, Required<Update<ColumnBuilderBaseConfig, TPartial>>>;
96
- declare abstract class PgColumn<THKT extends ColumnHKT, T extends ColumnBaseConfig, TRuntimeConfig extends object = {}, TTypeConfig extends object = {}> extends Column<THKT, T, TRuntimeConfig, TTypeConfig & {
97
- pgBrand: 'PgColumn';
98
- }> {
99
- readonly table: AnyPgTable;
100
- static readonly [entityKind]: string;
101
- constructor(table: AnyPgTable, config: ColumnBuilderRuntimeConfig<T['data']> & TRuntimeConfig);
102
- }
103
- type AnyPgColumn<TPartial extends Partial<ColumnBaseConfig> = {}> = PgColumn<PgColumnHKT, Required<Update<ColumnBaseConfig, TPartial>>>;
104
- interface AnyPgColumnHKT extends AnyColumnHKT {
105
- type: AnyPgColumn<Assume<this['config'], Partial<ColumnBaseConfig>>>;
57
+ execute: ReturnType<this['prepare']>['execute'];
106
58
  }
107
59
 
108
- interface IndexConfig {
109
- name?: string;
110
- columns: IndexColumn[];
111
- /**
112
- * If true, the index will be created as `create unique index` instead of `create index`.
113
- */
114
- unique: boolean;
115
- /**
116
- * If true, the index will be created as `create index concurrently` instead of `create index`.
117
- */
118
- concurrently?: boolean;
119
- /**
120
- * If true, the index will be created as `create index ... on only <table>` instead of `create index ... on <table>`.
121
- */
122
- only: boolean;
123
- /**
124
- * If set, the index will be created as `create index ... using <method>`.
125
- */
126
- using?: SQL;
127
- /**
128
- * If set, the index will be created as `create index ... asc | desc`.
129
- */
130
- order?: 'asc' | 'desc';
131
- /**
132
- * If set, adds `nulls first` or `nulls last` to the index.
133
- */
134
- nulls?: 'first' | 'last';
135
- /**
136
- * Condition for partial index.
137
- */
138
- where?: SQL;
139
- }
140
- type IndexColumn = AnyPgColumn;
141
- declare class IndexBuilderOn {
142
- private unique;
143
- private name?;
144
- static readonly [entityKind]: string;
145
- constructor(unique: boolean, name?: string | undefined);
146
- on(...columns: [IndexColumn, ...IndexColumn[]]): IndexBuilder;
147
- onOnly(...columns: [IndexColumn, ...IndexColumn[]]): IndexBuilder;
148
- }
149
- interface AnyIndexBuilder {
150
- build(table: AnyPgTable): Index;
151
- }
152
- interface IndexBuilder extends AnyIndexBuilder {
153
- }
154
- declare class IndexBuilder implements AnyIndexBuilder {
155
- static readonly [entityKind]: string;
156
- constructor(columns: IndexColumn[], unique: boolean, only: boolean, name?: string);
157
- concurrently(): this;
158
- using(method: SQL): this;
159
- asc(): Omit<this, 'asc' | 'desc'>;
160
- desc(): Omit<this, 'asc' | 'desc'>;
161
- nullsFirst(): Omit<this, 'nullsFirst' | 'nullsLast'>;
162
- nullsLast(): Omit<this, 'nullsFirst' | 'nullsLast'>;
163
- where(condition: SQL): Omit<this, 'where'>;
164
- }
165
- declare class Index {
60
+ type SubqueryWithSelection<TSelection extends ColumnsSelection, TAlias extends string> = Subquery<TAlias, AddAliasToSelection<TSelection, TAlias, 'pg'>> & AddAliasToSelection<TSelection, TAlias, 'pg'>;
61
+ type WithSubqueryWithSelection<TSelection extends ColumnsSelection, TAlias extends string> = WithSubquery<TAlias, AddAliasToSelection<TSelection, TAlias, 'pg'>> & AddAliasToSelection<TSelection, TAlias, 'pg'>;
62
+
63
+ declare class PgDatabase<TQueryResult extends QueryResultHKT, TFullSchema extends Record<string, unknown> = Record<string, never>, TSchema extends TablesRelationalConfig = ExtractTablesWithRelations<TFullSchema>> {
166
64
  static readonly [entityKind]: string;
167
- readonly config: IndexConfig & {
168
- table: AnyPgTable;
65
+ readonly _: {
66
+ readonly schema: TSchema | undefined;
67
+ readonly tableNamesMap: Record<string, string>;
68
+ };
69
+ query: TFullSchema extends Record<string, never> ? DrizzleTypeError<'Seems like the schema generic is missing - did you forget to add it to your DB type?'> : {
70
+ [K in keyof TSchema]: RelationalQueryBuilder<TSchema, TSchema[K]>;
71
+ };
72
+ constructor(
73
+ /** @internal */
74
+ dialect: PgDialect,
75
+ /** @internal */
76
+ session: PgSession<any, any, any>, schema: RelationalSchemaConfig<TSchema> | undefined);
77
+ $with<TAlias extends string>(alias: TAlias): {
78
+ as<TSelection extends ColumnsSelection>(qb: TypedQueryBuilder<TSelection, unknown> | ((qb: QueryBuilder) => TypedQueryBuilder<TSelection, unknown>)): WithSubqueryWithSelection<TSelection, TAlias>;
79
+ };
80
+ with(...queries: WithSubquery[]): {
81
+ select: {
82
+ (): PgSelectBuilder<undefined>;
83
+ <TSelection extends SelectedFields>(fields: TSelection): PgSelectBuilder<TSelection, "db">;
84
+ };
169
85
  };
170
- constructor(config: IndexConfig, table: AnyPgTable);
86
+ select(): PgSelectBuilder<undefined>;
87
+ select<TSelection extends SelectedFields>(fields: TSelection): PgSelectBuilder<TSelection>;
88
+ selectDistinct(): PgSelectBuilder<undefined>;
89
+ selectDistinct<TSelection extends SelectedFields>(fields: TSelection): PgSelectBuilder<TSelection>;
90
+ selectDistinctOn(on: (AnyPgColumn | SQLWrapper)[]): PgSelectBuilder<undefined>;
91
+ selectDistinctOn<TSelection extends SelectedFields>(on: (AnyPgColumn | SQLWrapper)[], fields: TSelection): PgSelectBuilder<TSelection>;
92
+ update<TTable extends AnyPgTable>(table: TTable): PgUpdateBuilder<TTable, TQueryResult>;
93
+ insert<TTable extends AnyPgTable>(table: TTable): PgInsertBuilder<TTable, TQueryResult>;
94
+ delete<TTable extends AnyPgTable>(table: TTable): PgDelete<TTable, TQueryResult>;
95
+ refreshMaterializedView<TView extends PgMaterializedView>(view: TView): PgRefreshMaterializedView<TQueryResult>;
96
+ execute<TRow extends Record<string, unknown> = Record<string, unknown>>(query: SQLWrapper): Promise<QueryResultKind<TQueryResult, TRow>>;
97
+ transaction<T>(transaction: (tx: PgTransaction<TQueryResult, TFullSchema, TSchema>) => Promise<T>, config?: PgTransactionConfig): Promise<T>;
171
98
  }
172
- type GetColumnsTableName<TColumns> = TColumns extends AnyPgColumn ? TColumns['_']['name'] : TColumns extends AnyPgColumn[] ? TColumns[number]['_']['name'] : never;
173
- declare function index(name?: string): IndexBuilderOn;
174
- declare function uniqueIndex(name?: string): IndexBuilderOn;
175
99
 
176
- declare function primaryKey<TTableName extends string, TColumns extends AnyPgColumn<{
177
- tableName: TTableName;
178
- }>[]>(...columns: TColumns): PrimaryKeyBuilder;
179
- declare class PrimaryKeyBuilder {
180
- static readonly [entityKind]: string;
181
- constructor(columns: AnyPgColumn[]);
100
+ interface PreparedQueryConfig {
101
+ execute: unknown;
102
+ all: unknown;
103
+ values: unknown;
182
104
  }
183
- declare class PrimaryKey {
184
- readonly table: AnyPgTable;
105
+ declare abstract class PreparedQuery<T extends PreparedQueryConfig> {
185
106
  static readonly [entityKind]: string;
186
- readonly columns: AnyPgColumn<{}>[];
187
- constructor(table: AnyPgTable, columns: AnyPgColumn<{}>[]);
188
- getName(): string;
107
+ abstract execute(placeholderValues?: Record<string, unknown>): Promise<T['execute']>;
189
108
  }
190
-
191
- declare function unique(name?: string): UniqueOnConstraintBuilder;
192
- declare function uniqueKeyName(table: AnyPgTable, columns: string[]): string;
193
- declare class UniqueConstraintBuilder {
194
- private name?;
195
- static readonly [entityKind]: string;
196
- constructor(columns: AnyPgColumn[], name?: string | undefined);
197
- nullsNotDistinct(): this;
109
+ interface PgTransactionConfig {
110
+ isolationLevel?: 'read uncommitted' | 'read committed' | 'repeatable read' | 'serializable';
111
+ accessMode?: 'read only' | 'read write';
112
+ deferrable?: boolean;
198
113
  }
199
- declare class UniqueOnConstraintBuilder {
114
+ declare abstract class PgSession<TQueryResult extends QueryResultHKT = QueryResultHKT, TFullSchema extends Record<string, unknown> = Record<string, never>, TSchema extends TablesRelationalConfig = Record<string, never>> {
115
+ protected dialect: PgDialect;
200
116
  static readonly [entityKind]: string;
201
- constructor(name?: string);
202
- on(...columns: [AnyPgColumn, ...AnyPgColumn[]]): UniqueConstraintBuilder;
117
+ constructor(dialect: PgDialect);
118
+ abstract prepareQuery<T extends PreparedQueryConfig = PreparedQueryConfig>(query: Query, fields: SelectedFieldsOrdered | undefined, name: string | undefined, customResultMapper?: (rows: unknown[][], mapColumnValue?: (value: unknown) => unknown) => T['execute']): PreparedQuery<T>;
119
+ execute<T>(query: SQL): Promise<T>;
120
+ all<T = unknown>(query: SQL): Promise<T[]>;
121
+ abstract transaction<T>(transaction: (tx: PgTransaction<TQueryResult, TFullSchema, TSchema>) => Promise<T>, config?: PgTransactionConfig): Promise<T>;
203
122
  }
204
- declare class UniqueConstraint {
205
- readonly table: AnyPgTable;
123
+ declare abstract class PgTransaction<TQueryResult extends QueryResultHKT, TFullSchema extends Record<string, unknown> = Record<string, never>, TSchema extends TablesRelationalConfig = Record<string, never>> extends PgDatabase<TQueryResult, TFullSchema, TSchema> {
124
+ protected schema: {
125
+ fullSchema: Record<string, unknown>;
126
+ schema: TSchema;
127
+ tableNamesMap: Record<string, string>;
128
+ } | undefined;
129
+ protected readonly nestedIndex: number;
206
130
  static readonly [entityKind]: string;
207
- readonly columns: AnyPgColumn<{}>[];
208
- readonly name?: string;
209
- readonly nullsNotDistinct: boolean;
210
- constructor(table: AnyPgTable, columns: AnyPgColumn<{}>[], nullsNotDistinct: boolean, name?: string);
211
- getName(): string | undefined;
131
+ constructor(dialect: PgDialect, session: PgSession<any, any, any>, schema: {
132
+ fullSchema: Record<string, unknown>;
133
+ schema: TSchema;
134
+ tableNamesMap: Record<string, string>;
135
+ } | undefined, nestedIndex?: number);
136
+ rollback(): never;
137
+ setTransaction(config: PgTransactionConfig): Promise<void>;
138
+ abstract transaction<T>(transaction: (tx: PgTransaction<TQueryResult, TFullSchema, TSchema>) => Promise<T>): Promise<T>;
212
139
  }
140
+ interface QueryResultHKT {
141
+ readonly $brand: 'QueryRowHKT';
142
+ readonly row: unknown;
143
+ readonly type: unknown;
144
+ }
145
+ type QueryResultKind<TKind extends QueryResultHKT, TRow> = (TKind & {
146
+ readonly row: TRow;
147
+ })['type'];
213
148
 
214
- type PgTableExtraConfig = Record<string, AnyIndexBuilder | CheckBuilder | ForeignKeyBuilder | PrimaryKeyBuilder | UniqueConstraintBuilder>;
215
- type TableConfig = TableConfig$1<AnyPgColumn>;
216
- declare class PgTable<T extends TableConfig> extends Table<T> {
149
+ declare class PgDialect {
217
150
  static readonly [entityKind]: string;
151
+ migrate(migrations: MigrationMeta[], session: PgSession): Promise<void>;
152
+ escapeName(name: string): string;
153
+ escapeParam(num: number): string;
154
+ escapeString(str: string): string;
155
+ buildDeleteQuery({ table, where, returning }: PgDeleteConfig): SQL;
156
+ buildUpdateSet(table: AnyPgTable, set: UpdateSet): SQL;
157
+ buildUpdateQuery({ table, set, where, returning }: PgUpdateConfig): SQL;
158
+ /**
159
+ * Builds selection SQL with provided fields/expressions
160
+ *
161
+ * Examples:
162
+ *
163
+ * `select <selection> from`
164
+ *
165
+ * `insert ... returning <selection>`
166
+ *
167
+ * If `isSingleTable` is true, then columns won't be prefixed with table name
168
+ */
169
+ private buildSelection;
170
+ buildSelectQuery({ withList, fields, fieldsFlat, where, having, table, joins, orderBy, groupBy, limit, offset, lockingClauses, distinct, }: PgSelectConfig): SQL;
171
+ buildInsertQuery({ table, values, onConflict, returning }: PgInsertConfig): SQL;
172
+ buildRefreshMaterializedViewQuery({ view, concurrently, withNoData }: {
173
+ view: PgMaterializedView;
174
+ concurrently?: boolean;
175
+ withNoData?: boolean;
176
+ }): SQL;
177
+ prepareTyping(encoder: DriverValueEncoder<unknown, unknown>): QueryTypingsValue;
178
+ sqlToQuery(sql: SQL): Query;
179
+ buildRelationalQueryWithoutPK({ fullSchema, schema, tableNamesMap, table, tableConfig, queryConfig: config, tableAlias, nestedQueryRelation, joinOn, }: {
180
+ fullSchema: Record<string, unknown>;
181
+ schema: TablesRelationalConfig;
182
+ tableNamesMap: Record<string, string>;
183
+ table: AnyPgTable;
184
+ tableConfig: TableRelationalConfig;
185
+ queryConfig: true | DBQueryConfig<'many', true>;
186
+ tableAlias: string;
187
+ nestedQueryRelation?: Relation;
188
+ joinOn?: SQL;
189
+ }): BuildRelationalQueryResult<AnyPgTable, PgColumn>;
218
190
  }
219
- type AnyPgTable<TPartial extends Partial<TableConfig> = {}> = PgTable<UpdateTableConfig<TableConfig, TPartial>>;
220
- interface AnyPgTableHKT extends AnyTableHKT {
221
- type: AnyPgTable<Assume<this['config'], Partial<TableConfig>>>;
191
+
192
+ interface PgDeleteConfig {
193
+ where?: SQL | undefined;
194
+ table: AnyPgTable;
195
+ returning?: SelectedFieldsOrdered;
222
196
  }
223
- type PgTableWithColumns<T extends TableConfig> = PgTable<T> & {
224
- [Key in keyof T['columns']]: T['columns'][Key];
225
- };
226
- interface PgTableFn<TSchema extends string | undefined = undefined> {
227
- <TTableName extends string, TColumnsMap extends Record<string, AnyPgColumnBuilder>>(name: TTableName, columns: TColumnsMap, extraConfig?: (self: BuildColumns<TTableName, TColumnsMap>) => PgTableExtraConfig): PgTableWithColumns<{
228
- name: TTableName;
229
- schema: TSchema;
230
- columns: BuildColumns<TTableName, TColumnsMap>;
231
- }>;
197
+ interface PgDelete<TTable extends AnyPgTable, TQueryResult extends QueryResultHKT, TReturning extends Record<string, unknown> | undefined = undefined> extends QueryPromise<TReturning extends undefined ? QueryResultKind<TQueryResult, never> : TReturning[]> {
232
198
  }
233
- declare const pgTable: PgTableFn;
234
- declare function pgTableCreator(customizeTableName: (name: string) => string): PgTableFn;
235
-
236
- interface PgArrayBuilderHKT extends ColumnBuilderHKTBase {
237
- _type: PgArrayBuilder<Assume<this['config'], ColumnBuilderBaseConfig>>;
238
- _columnHKT: PgArrayHKT;
239
- }
240
- interface PgArrayHKT extends ColumnHKTBase {
241
- _type: PgArray<Assume<this['config'], ColumnBaseConfig>>;
242
- }
243
- declare class PgArrayBuilder<T extends ColumnBuilderBaseConfig> extends PgColumnBuilder<PgArrayBuilderHKT, T, {
244
- baseBuilder: PgColumnBuilder<PgColumnBuilderHKT, {
245
- name: T['name'];
246
- notNull: T['notNull'];
247
- hasDefault: T['hasDefault'];
248
- data: Assume<T['data'], unknown[]>[number];
249
- driverParam: string | Assume<T['driverParam'], unknown[]>[number];
250
- }>;
251
- size: number | undefined;
252
- }> {
253
- static readonly [entityKind] = "PgArrayBuilder";
254
- constructor(name: string, baseBuilder: PgArrayBuilder<T>['config']['baseBuilder'], size: number | undefined);
255
- }
256
- declare class PgArray<T extends ColumnBaseConfig> extends PgColumn<PgArrayHKT, T, {}, {
257
- baseColumn: BuildColumn<string, Assume<PgColumnBuilder<PgColumnBuilderHKT, {
258
- name: T['name'];
259
- notNull: T['notNull'];
260
- hasDefault: T['hasDefault'];
261
- data: Assume<T['data'], unknown[]>[number];
262
- driverParam: string | Assume<T['driverParam'], unknown[]>[number];
263
- }>, AnyColumnBuilder>>;
264
- }> {
265
- readonly baseColumn: AnyPgColumn;
266
- readonly range?: [number | undefined, number | undefined] | undefined;
267
- protected $pgColumnBrand: 'PgArray';
268
- readonly size: number | undefined;
199
+ declare class PgDelete<TTable extends AnyPgTable, TQueryResult extends QueryResultHKT, TReturning extends Record<string, unknown> | undefined = undefined> extends QueryPromise<TReturning extends undefined ? QueryResultKind<TQueryResult, never> : TReturning[]> implements SQLWrapper {
200
+ private session;
201
+ private dialect;
269
202
  static readonly [entityKind]: string;
270
- constructor(table: AnyPgTable<{
271
- name: T['tableName'];
272
- }>, config: PgArrayBuilder<T>['config'], baseColumn: AnyPgColumn, range?: [number | undefined, number | undefined] | undefined);
273
- getSQLType(): string;
274
- mapFromDriverValue(value: unknown[] | string): T['data'];
275
- mapToDriverValue(value: unknown[], isNestedArray?: boolean): unknown[] | string;
203
+ private config;
204
+ constructor(table: TTable, session: PgSession, dialect: PgDialect);
205
+ where(where: SQL | undefined): Omit<this, 'where'>;
206
+ returning(): PgDelete<TTable, TQueryResult, InferModel<TTable>>;
207
+ returning<TSelectedFields extends SelectedFieldsFlat>(fields: TSelectedFields): PgDelete<TTable, TQueryResult, SelectResultFields<TSelectedFields>>;
208
+ toSQL(): {
209
+ sql: Query['sql'];
210
+ params: Query['params'];
211
+ };
212
+ private _prepare;
213
+ prepare(name: string): PreparedQuery<PreparedQueryConfig & {
214
+ execute: TReturning extends undefined ? QueryResultKind<TQueryResult, never> : TReturning[];
215
+ }>;
216
+ execute: ReturnType<this['prepare']>['execute'];
276
217
  }
277
218
 
278
- interface ViewWithConfig {
279
- checkOption: 'local' | 'cascaded';
280
- securityBarrier: boolean;
281
- securityInvoker: boolean;
219
+ interface PgUpdateConfig {
220
+ where?: SQL | undefined;
221
+ set: UpdateSet;
222
+ table: AnyPgTable;
223
+ returning?: SelectedFieldsOrdered;
282
224
  }
283
- declare class DefaultViewBuilderCore<TConfig extends {
284
- name: string;
285
- columns?: unknown;
286
- }> {
287
- protected name: TConfig['name'];
288
- protected schema: string | undefined;
225
+ type PgUpdateSetSource<TTable extends AnyPgTable> = {
226
+ [Key in keyof TTable['_']['columns']]?: GetColumnData<TTable['_']['columns'][Key]> | SQL;
227
+ } & {};
228
+ declare class PgUpdateBuilder<TTable extends AnyPgTable, TQueryResult extends QueryResultHKT> {
229
+ private table;
230
+ private session;
231
+ private dialect;
289
232
  static readonly [entityKind]: string;
290
233
  readonly _: {
291
- readonly name: TConfig['name'];
292
- readonly columns: TConfig['columns'];
293
- };
294
- constructor(name: TConfig['name'], schema: string | undefined);
295
- protected config: {
296
- with?: ViewWithConfig;
234
+ readonly table: TTable;
297
235
  };
298
- with(config: ViewWithConfig): this;
236
+ constructor(table: TTable, session: PgSession, dialect: PgDialect);
237
+ set(values: PgUpdateSetSource<TTable>): PgUpdate<TTable, TQueryResult>;
299
238
  }
300
- declare class ViewBuilder<TName extends string = string> extends DefaultViewBuilderCore<{
301
- name: TName;
302
- }> {
303
- static readonly [entityKind]: string;
304
- as<TSelectedFields extends SelectedFields>(qb: TypedQueryBuilder<TSelectedFields> | ((qb: QueryBuilder) => TypedQueryBuilder<TSelectedFields>)): PgViewWithSelection<TName, false, AddAliasToSelection<TSelectedFields, TName>>;
305
- }
306
- declare class ManualViewBuilder<TName extends string = string, TColumns extends Record<string, AnyPgColumnBuilder> = Record<string, AnyPgColumnBuilder>> extends DefaultViewBuilderCore<{
307
- name: TName;
308
- columns: TColumns;
309
- }> {
310
- static readonly [entityKind]: string;
311
- private columns;
312
- constructor(name: TName, columns: TColumns, schema: string | undefined);
313
- existing(): PgViewWithSelection<TName, true, BuildColumns<TName, TColumns>>;
314
- as(query: SQL): PgViewWithSelection<TName, false, BuildColumns<TName, TColumns>>;
315
- }
316
- interface PgMaterializedViewWithConfig {
317
- [Key: string]: string | number | boolean | SQL;
239
+ interface PgUpdate<TTable extends AnyPgTable, TQueryResult extends QueryResultHKT, TReturning extends Record<string, unknown> | undefined = undefined> extends QueryPromise<TReturning extends undefined ? QueryResultKind<TQueryResult, never> : TReturning[]>, SQLWrapper {
318
240
  }
319
- declare class MaterializedViewBuilderCore<TConfig extends {
320
- name: string;
321
- columns?: unknown;
322
- }> {
323
- protected name: TConfig['name'];
324
- protected schema: string | undefined;
241
+ declare class PgUpdate<TTable extends AnyPgTable, TQueryResult extends QueryResultHKT, TReturning extends Record<string, unknown> | undefined = undefined> extends QueryPromise<TReturning extends undefined ? QueryResultKind<TQueryResult, never> : TReturning[]> implements SQLWrapper {
242
+ private session;
243
+ private dialect;
325
244
  static readonly [entityKind]: string;
326
- _: {
327
- readonly name: TConfig['name'];
328
- readonly columns: TConfig['columns'];
329
- };
330
- constructor(name: TConfig['name'], schema: string | undefined);
331
- protected config: {
332
- with?: PgMaterializedViewWithConfig;
333
- using?: string;
334
- tablespace?: string;
335
- withNoData?: boolean;
245
+ readonly _: {
246
+ readonly table: TTable;
247
+ readonly return: TReturning;
336
248
  };
337
- using(using: string): this;
338
- with(config: PgMaterializedViewWithConfig): this;
339
- tablespace(tablespace: string): this;
340
- withNoData(): this;
341
- }
342
- declare class MaterializedViewBuilder<TName extends string = string> extends MaterializedViewBuilderCore<{
343
- name: TName;
344
- }> {
345
- static readonly [entityKind]: string;
346
- as<TSelectedFields extends SelectedFields>(qb: TypedQueryBuilder<TSelectedFields> | ((qb: QueryBuilder) => TypedQueryBuilder<TSelectedFields>)): PgMaterializedViewWithSelection<TName, false, AddAliasToSelection<TSelectedFields, TName>>;
249
+ private config;
250
+ constructor(table: TTable, set: UpdateSet, session: PgSession, dialect: PgDialect);
251
+ where(where: SQL | undefined): this;
252
+ returning(): PgUpdate<TTable, TQueryResult, InferModel<TTable>>;
253
+ returning<TSelectedFields extends SelectedFields>(fields: TSelectedFields): PgUpdate<TTable, TQueryResult, SelectResultFields<TSelectedFields>>;
254
+ toSQL(): Omit<Query, 'typings'>;
255
+ private _prepare;
256
+ prepare(name: string): PreparedQuery<PreparedQueryConfig & {
257
+ execute: TReturning extends undefined ? QueryResultKind<TQueryResult, never> : TReturning[];
258
+ }>;
259
+ execute: ReturnType<this['prepare']>['execute'];
347
260
  }
348
- declare class ManualMaterializedViewBuilder<TName extends string = string, TColumns extends Record<string, AnyPgColumnBuilder> = Record<string, AnyPgColumnBuilder>> extends MaterializedViewBuilderCore<{
349
- name: TName;
350
- columns: TColumns;
351
- }> {
352
- static readonly [entityKind]: string;
353
- private columns;
354
- constructor(name: TName, columns: TColumns, schema: string | undefined);
355
- existing(): PgMaterializedViewWithSelection<TName, true, BuildColumns<TName, TColumns>>;
356
- as(query: SQL): PgMaterializedViewWithSelection<TName, false, BuildColumns<TName, TColumns>>;
261
+
262
+ interface PgInsertConfig<TTable extends AnyPgTable = AnyPgTable> {
263
+ table: TTable;
264
+ values: Record<string, Param | SQL>[];
265
+ onConflict?: SQL;
266
+ returning?: SelectedFieldsOrdered;
357
267
  }
358
- declare abstract class PgViewBase<TName extends string = string, TExisting extends boolean = boolean, TSelectedFields extends ColumnsSelection = ColumnsSelection> extends View<TName, TExisting, TSelectedFields> {
268
+ type PgInsertValue<TTable extends AnyPgTable> = {
269
+ [Key in keyof InferModel<TTable, 'insert'>]: InferModel<TTable, 'insert'>[Key] | SQL | Placeholder;
270
+ } & {};
271
+ declare class PgInsertBuilder<TTable extends AnyPgTable, TQueryResult extends QueryResultHKT> {
272
+ private table;
273
+ private session;
274
+ private dialect;
359
275
  static readonly [entityKind]: string;
360
- readonly _: View<TName, TExisting, TSelectedFields>['_'] & {
361
- readonly viewBrand: 'PgViewBase';
362
- };
276
+ constructor(table: TTable, session: PgSession, dialect: PgDialect);
277
+ values(value: PgInsertValue<TTable>): PgInsert<TTable, TQueryResult>;
278
+ values(values: PgInsertValue<TTable>[]): PgInsert<TTable, TQueryResult>;
363
279
  }
364
- declare const PgViewConfig: unique symbol;
365
- declare class PgView<TName extends string = string, TExisting extends boolean = boolean, TSelectedFields extends ColumnsSelection = ColumnsSelection> extends PgViewBase<TName, TExisting, TSelectedFields> {
366
- static readonly [entityKind]: string;
367
- [PgViewConfig]: {
368
- with?: ViewWithConfig;
369
- } | undefined;
370
- constructor({ pgConfig, config }: {
371
- pgConfig: {
372
- with?: ViewWithConfig;
373
- } | undefined;
374
- config: {
375
- name: TName;
376
- schema: string | undefined;
377
- selectedFields: SelectedFields;
378
- query: SQL | undefined;
379
- };
380
- });
280
+ interface PgInsert<TTable extends AnyPgTable, TQueryResult extends QueryResultHKT, TReturning extends Record<string, unknown> | undefined = undefined> extends QueryPromise<TReturning extends undefined ? QueryResultKind<TQueryResult, never> : TReturning[]>, SQLWrapper {
381
281
  }
382
- type PgViewWithSelection<TName extends string = string, TExisting extends boolean = boolean, TSelectedFields extends ColumnsSelection = ColumnsSelection> = PgView<TName, TExisting, TSelectedFields> & TSelectedFields;
383
- declare const PgMaterializedViewConfig: unique symbol;
384
- declare class PgMaterializedView<TName extends string = string, TExisting extends boolean = boolean, TSelectedFields extends ColumnsSelection = ColumnsSelection> extends PgViewBase<TName, TExisting, TSelectedFields> {
282
+ declare class PgInsert<TTable extends AnyPgTable, TQueryResult extends QueryResultHKT, TReturning extends Record<string, unknown> | undefined = undefined> extends QueryPromise<TReturning extends undefined ? QueryResultKind<TQueryResult, never> : TReturning[]> implements SQLWrapper {
283
+ private session;
284
+ private dialect;
385
285
  static readonly [entityKind]: string;
386
- readonly [PgMaterializedViewConfig]: {
387
- readonly with?: PgMaterializedViewWithConfig;
388
- readonly using?: string;
389
- readonly tablespace?: string;
390
- readonly withNoData?: boolean;
391
- } | undefined;
392
- constructor({ pgConfig, config }: {
393
- pgConfig: {
394
- with: PgMaterializedViewWithConfig | undefined;
395
- using: string | undefined;
396
- tablespace: string | undefined;
397
- withNoData: boolean | undefined;
398
- } | undefined;
399
- config: {
400
- name: TName;
401
- schema: string | undefined;
402
- selectedFields: SelectedFields;
403
- query: SQL | undefined;
404
- };
405
- });
286
+ _: {
287
+ table: TTable;
288
+ return: TReturning;
289
+ };
290
+ private config;
291
+ constructor(table: TTable, values: PgInsertConfig['values'], session: PgSession, dialect: PgDialect);
292
+ returning(): PgInsert<TTable, TQueryResult, InferModel<TTable>>;
293
+ returning<TSelectedFields extends SelectedFieldsFlat>(fields: TSelectedFields): PgInsert<TTable, TQueryResult, SelectResultFields<TSelectedFields>>;
294
+ onConflictDoNothing(config?: {
295
+ target?: IndexColumn | IndexColumn[];
296
+ where?: SQL;
297
+ }): this;
298
+ onConflictDoUpdate(config: {
299
+ target: IndexColumn | IndexColumn[];
300
+ where?: SQL;
301
+ set: PgUpdateSetSource<TTable>;
302
+ }): this;
303
+ toSQL(): {
304
+ sql: Query['sql'];
305
+ params: Query['params'];
306
+ };
307
+ private _prepare;
308
+ prepare(name: string): PreparedQuery<PreparedQueryConfig & {
309
+ execute: TReturning extends undefined ? QueryResultKind<TQueryResult, never> : TReturning[];
310
+ }>;
311
+ execute: ReturnType<this['prepare']>['execute'];
406
312
  }
407
- type PgMaterializedViewWithSelection<TName extends string = string, TExisting extends boolean = boolean, TSelectedFields extends ColumnsSelection = ColumnsSelection> = PgMaterializedView<TName, TExisting, TSelectedFields> & TSelectedFields;
408
- declare function pgView<TName extends string>(name: TName): ViewBuilder<TName>;
409
- declare function pgView<TName extends string, TColumns extends Record<string, AnyPgColumnBuilder>>(name: TName, columns: TColumns): ManualViewBuilder<TName, TColumns>;
410
- declare function pgMaterializedView<TName extends string>(name: TName): MaterializedViewBuilder<TName>;
411
- declare function pgMaterializedView<TName extends string, TColumns extends Record<string, AnyPgColumnBuilder>>(name: TName, columns: TColumns): ManualMaterializedViewBuilder<TName, TColumns>;
412
-
413
- type SubqueryWithSelection<TSelection extends ColumnsSelection, TAlias extends string> = Subquery<TAlias, AddAliasToSelection<TSelection, TAlias>> & AddAliasToSelection<TSelection, TAlias>;
414
- type WithSubqueryWithSelection<TSelection extends ColumnsSelection, TAlias extends string> = WithSubquery<TAlias, AddAliasToSelection<TSelection, TAlias>> & AddAliasToSelection<TSelection, TAlias>;
415
313
 
416
314
  type CreatePgSelectFromBuilderMode<TBuilderMode extends 'db' | 'qb', TTableName extends string | undefined, TSelection extends ColumnsSelection, TSelectMode extends SelectMode> = TBuilderMode extends 'db' ? PgSelect<TTableName, TSelection, TSelectMode> : PgSelectQueryBuilder<PgSelectQueryBuilderHKT, TTableName, TSelection, TSelectMode>;
417
315
  declare class PgSelectBuilder<TSelection extends SelectedFields | undefined, TBuilderMode extends 'db' | 'qb' = 'db'> {
@@ -585,7 +483,10 @@ declare abstract class PgSelectQueryBuilder<THKT extends PgSelectHKTBase, TTable
585
483
  * {@link https://www.postgresql.org/docs/current/sql-select.html#SQL-FOR-UPDATE-SHARE|Postgres locking clause documentation}
586
484
  */
587
485
  for(strength: LockStrength, config?: LockConfig): this;
588
- toSQL(): Simplify<Omit<Query, 'typings'>>;
486
+ toSQL(): {
487
+ sql: Query['sql'];
488
+ params: Query['params'];
489
+ };
589
490
  as<TAlias extends string>(alias: TAlias): SubqueryWithSelection<BuildSubquerySelection<TSelection, TNullabilityMap>, TAlias>;
590
491
  }
591
492
  interface PgSelect<TTableName extends string | undefined, TSelection extends ColumnsSelection, TSelectMode extends SelectMode, TNullabilityMap extends Record<string, JoinNullability> = TTableName extends string ? Record<TTableName, 'not-null'> : {}> extends PgSelectQueryBuilder<PgSelectHKT, TTableName, TSelection, TSelectMode, TNullabilityMap>, QueryPromise<SelectResult<TSelection, TSelectMode, TNullabilityMap>[]> {
@@ -606,398 +507,237 @@ declare class PgSelect<TTableName extends string | undefined, TSelection, TSelec
606
507
  execute: ReturnType<this['prepare']>['execute'];
607
508
  }
608
509
 
609
- interface Join {
610
- on: SQL | undefined;
611
- table: AnyPgTable | Subquery | PgViewBase | SQL;
612
- alias: string | undefined;
613
- joinType: JoinType;
614
- lateral?: boolean;
615
- }
616
- type AnyPgSelect = PgSelect<any, any, any, any>;
617
- type BuildAliasTable<TTable extends AnyTable, TAlias extends string> = PgTableWithColumns<Assume<UpdateTableConfig<TTable['_']['config'], {
618
- name: TAlias;
619
- columns: MapColumnsToTableAlias<TTable['_']['columns'], TAlias>;
620
- }>, TableConfig>>;
621
- interface PgSelectConfig {
622
- withList?: Subquery[];
623
- fields: Record<string, unknown>;
624
- fieldsFlat?: SelectedFieldsOrdered;
625
- where?: SQL;
626
- having?: SQL;
627
- table: AnyPgTable | Subquery | PgViewBase | SQL;
628
- limit?: number | Placeholder;
629
- offset?: number | Placeholder;
630
- joins?: Join[];
631
- orderBy?: (AnyPgColumn | SQL | SQL.Aliased)[];
632
- groupBy?: (AnyPgColumn | SQL | SQL.Aliased)[];
633
- lockingClauses?: {
634
- strength: LockStrength;
635
- config: LockConfig;
636
- }[];
637
- distinct?: boolean | {
638
- on: (AnyPgColumn | SQLWrapper)[];
510
+ declare class QueryBuilder {
511
+ static readonly [entityKind]: string;
512
+ private dialect;
513
+ $with<TAlias extends string>(alias: TAlias): {
514
+ as<TSelection extends ColumnsSelection>(qb: TypedQueryBuilder<TSelection, unknown> | ((qb: QueryBuilder) => TypedQueryBuilder<TSelection, unknown>)): WithSubqueryWithSelection<TSelection, TAlias>;
639
515
  };
640
- }
641
- type JoinFn<THKT extends PgSelectHKTBase, TTableName extends string | undefined, TSelectMode extends SelectMode, TJoinType extends JoinType, TSelection, TNullabilityMap extends Record<string, JoinNullability>> = <TJoinedTable extends AnyPgTable | Subquery | PgViewBase | SQL, TJoinedName extends GetSelectTableName<TJoinedTable> = GetSelectTableName<TJoinedTable>>(table: TJoinedTable, on: ((aliases: TSelection) => SQL | undefined) | SQL | undefined) => PgSelectKind<THKT, TTableName, AppendToResult<TTableName, TSelection, TJoinedName, TJoinedTable extends Table ? TJoinedTable['_']['columns'] : TJoinedTable extends Subquery ? Assume<TJoinedTable['_']['selectedFields'], SelectedFields> : never, TSelectMode>, TSelectMode extends 'partial' ? TSelectMode : 'multiple', AppendToNullabilityMap<TNullabilityMap, TJoinedName, TJoinType>>;
642
- type SelectedFieldsFlat = SelectedFieldsFlat$1<AnyPgColumn>;
643
- type SelectedFields = SelectedFields$1<AnyPgColumn, AnyPgTable>;
644
- type SelectedFieldsOrdered = SelectedFieldsOrdered$1<AnyPgColumn>;
645
- type LockStrength = 'update' | 'no key update' | 'share' | 'key share';
646
- type LockConfig = {
647
- of?: AnyPgTable;
648
- } & ({
649
- noWait: true;
650
- skipLocked?: undefined;
651
- } | {
652
- noWait?: undefined;
653
- skipLocked: true;
654
- } | {
655
- noWait?: undefined;
656
- skipLocked?: undefined;
657
- });
658
- interface PgSelectHKTBase {
659
- tableName: string | undefined;
660
- selection: unknown;
661
- selectMode: SelectMode;
662
- nullabilityMap: unknown;
663
- _type: unknown;
664
- }
665
- type PgSelectKind<T extends PgSelectHKTBase, TTableName extends string | undefined, TSelection, TSelectMode extends SelectMode, TNullabilityMap extends Record<string, JoinNullability>> = (T & {
666
- tableName: TTableName;
667
- selection: TSelection;
668
- selectMode: TSelectMode;
669
- nullabilityMap: TNullabilityMap;
670
- })['_type'];
671
- interface PgSelectQueryBuilderHKT extends PgSelectHKTBase {
672
- _type: PgSelectQueryBuilder<this, this['tableName'], Assume<this['selection'], ColumnsSelection>, this['selectMode'], Assume<this['nullabilityMap'], Record<string, JoinNullability>>>;
673
- }
674
- interface PgSelectHKT extends PgSelectHKTBase {
675
- _type: PgSelect<this['tableName'], Assume<this['selection'], ColumnsSelection>, this['selectMode'], Assume<this['nullabilityMap'], Record<string, JoinNullability>>>;
516
+ with(...queries: WithSubquery[]): {
517
+ select: {
518
+ (): PgSelectBuilder<undefined, 'qb'>;
519
+ <TSelection extends SelectedFields>(fields: TSelection): PgSelectBuilder<TSelection, "qb">;
520
+ };
521
+ selectDistinct: {
522
+ (): PgSelectBuilder<undefined, 'qb'>;
523
+ <TSelection_1 extends SelectedFields>(fields: TSelection_1): PgSelectBuilder<TSelection_1, "qb">;
524
+ };
525
+ selectDistinctOn: {
526
+ (on: (AnyPgColumn | SQLWrapper)[]): PgSelectBuilder<undefined, 'qb'>;
527
+ <TSelection_2 extends SelectedFields>(on: (AnyPgColumn | SQLWrapper)[], fields: TSelection_2): PgSelectBuilder<TSelection_2, "qb">;
528
+ };
529
+ };
530
+ select(): PgSelectBuilder<undefined, 'qb'>;
531
+ select<TSelection extends SelectedFields>(fields: TSelection): PgSelectBuilder<TSelection, 'qb'>;
532
+ selectDistinct(): PgSelectBuilder<undefined>;
533
+ selectDistinct<TSelection extends SelectedFields>(fields: TSelection): PgSelectBuilder<TSelection>;
534
+ selectDistinctOn(on: (AnyPgColumn | SQLWrapper)[]): PgSelectBuilder<undefined>;
535
+ selectDistinctOn<TSelection extends SelectedFields>(on: (AnyPgColumn | SQLWrapper)[], fields: TSelection): PgSelectBuilder<TSelection>;
536
+ private getDialect;
676
537
  }
677
538
 
678
- interface PreparedQueryConfig {
679
- execute: unknown;
680
- all: unknown;
681
- values: unknown;
539
+ interface ViewWithConfig {
540
+ checkOption: 'local' | 'cascaded';
541
+ securityBarrier: boolean;
542
+ securityInvoker: boolean;
682
543
  }
683
- declare abstract class PreparedQuery<T extends PreparedQueryConfig> {
544
+ declare class DefaultViewBuilderCore<TConfig extends {
545
+ name: string;
546
+ columns?: unknown;
547
+ }> {
548
+ protected name: TConfig['name'];
549
+ protected schema: string | undefined;
684
550
  static readonly [entityKind]: string;
685
- abstract execute(placeholderValues?: Record<string, unknown>): Promise<T['execute']>;
686
- }
687
- interface PgTransactionConfig {
688
- isolationLevel?: 'read uncommitted' | 'read committed' | 'repeatable read' | 'serializable';
689
- accessMode?: 'read only' | 'read write';
690
- deferrable?: boolean;
691
- }
692
- declare abstract class PgSession<TQueryResult extends QueryResultHKT = QueryResultHKT, TFullSchema extends Record<string, unknown> = Record<string, never>, TSchema extends TablesRelationalConfig = Record<string, never>> {
693
- protected dialect: PgDialect;
694
- static readonly [entityKind]: string;
695
- constructor(dialect: PgDialect);
696
- abstract prepareQuery<T extends PreparedQueryConfig = PreparedQueryConfig>(query: Query, fields: SelectedFieldsOrdered | undefined, name: string | undefined, customResultMapper?: (rows: unknown[][], mapColumnValue?: (value: unknown) => unknown) => T['execute']): PreparedQuery<T>;
697
- execute<T>(query: SQL): Promise<T>;
698
- all<T = unknown>(query: SQL): Promise<T[]>;
699
- abstract transaction<T>(transaction: (tx: PgTransaction<TQueryResult, TFullSchema, TSchema>) => Promise<T>, config?: PgTransactionConfig): Promise<T>;
551
+ readonly _: {
552
+ readonly name: TConfig['name'];
553
+ readonly columns: TConfig['columns'];
554
+ };
555
+ constructor(name: TConfig['name'], schema: string | undefined);
556
+ protected config: {
557
+ with?: ViewWithConfig;
558
+ };
559
+ with(config: ViewWithConfig): this;
700
560
  }
701
- declare abstract class PgTransaction<TQueryResult extends QueryResultHKT, TFullSchema extends Record<string, unknown> = Record<string, never>, TSchema extends TablesRelationalConfig = Record<string, never>> extends PgDatabase<TQueryResult, TFullSchema, TSchema> {
702
- protected schema: {
703
- fullSchema: Record<string, unknown>;
704
- schema: TSchema;
705
- tableNamesMap: Record<string, string>;
706
- } | undefined;
707
- protected readonly nestedIndex: number;
561
+ declare class ViewBuilder<TName extends string = string> extends DefaultViewBuilderCore<{
562
+ name: TName;
563
+ }> {
708
564
  static readonly [entityKind]: string;
709
- constructor(dialect: PgDialect, session: PgSession<any, any, any>, schema: {
710
- fullSchema: Record<string, unknown>;
711
- schema: TSchema;
712
- tableNamesMap: Record<string, string>;
713
- } | undefined, nestedIndex?: number);
714
- rollback(): never;
715
- setTransaction(config: PgTransactionConfig): Promise<void>;
716
- abstract transaction<T>(transaction: (tx: PgTransaction<TQueryResult, TFullSchema, TSchema>) => Promise<T>): Promise<T>;
717
- }
718
- interface QueryResultHKT {
719
- readonly $brand: 'QueryRowHKT';
720
- readonly row: unknown;
721
- readonly type: unknown;
565
+ as<TSelectedFields extends SelectedFields>(qb: TypedQueryBuilder<TSelectedFields> | ((qb: QueryBuilder) => TypedQueryBuilder<TSelectedFields>)): PgViewWithSelection<TName, false, AddAliasToSelection<TSelectedFields, TName, 'pg'>>;
722
566
  }
723
- type QueryResultKind<TKind extends QueryResultHKT, TRow> = (TKind & {
724
- readonly row: TRow;
725
- })['type'];
726
-
727
- interface PgDeleteConfig {
728
- where?: SQL | undefined;
729
- table: AnyPgTable;
730
- returning?: SelectedFieldsOrdered;
731
- }
732
- interface PgDelete<TTable extends AnyPgTable, TQueryResult extends QueryResultHKT, TReturning extends Record<string, unknown> | undefined = undefined> extends QueryPromise<TReturning extends undefined ? QueryResultKind<TQueryResult, never> : TReturning[]> {
733
- }
734
- declare class PgDelete<TTable extends AnyPgTable, TQueryResult extends QueryResultHKT, TReturning extends Record<string, unknown> | undefined = undefined> extends QueryPromise<TReturning extends undefined ? QueryResultKind<TQueryResult, never> : TReturning[]> implements SQLWrapper {
735
- private session;
736
- private dialect;
567
+ declare class ManualViewBuilder<TName extends string = string, TColumns extends Record<string, PgColumnBuilder> = Record<string, PgColumnBuilder>> extends DefaultViewBuilderCore<{
568
+ name: TName;
569
+ columns: TColumns;
570
+ }> {
737
571
  static readonly [entityKind]: string;
738
- private config;
739
- constructor(table: TTable, session: PgSession, dialect: PgDialect);
740
- where(where: SQL | undefined): Omit<this, 'where'>;
741
- returning(): PgDelete<TTable, TQueryResult, InferModel<TTable>>;
742
- returning<TSelectedFields extends SelectedFieldsFlat>(fields: TSelectedFields): PgDelete<TTable, TQueryResult, SelectResultFields<TSelectedFields>>;
743
- toSQL(): Simplify<Omit<Query, 'typings'>>;
744
- private _prepare;
745
- prepare(name: string): PreparedQuery<PreparedQueryConfig & {
746
- execute: TReturning extends undefined ? QueryResultKind<TQueryResult, never> : TReturning[];
747
- }>;
748
- execute: ReturnType<this['prepare']>['execute'];
572
+ private columns;
573
+ constructor(name: TName, columns: TColumns, schema: string | undefined);
574
+ existing(): PgViewWithSelection<TName, true, BuildColumns<TName, TColumns, 'pg'>>;
575
+ as(query: SQL): PgViewWithSelection<TName, false, BuildColumns<TName, TColumns, 'pg'>>;
749
576
  }
750
-
751
- interface PgUpdateConfig {
752
- where?: SQL | undefined;
753
- set: UpdateSet;
754
- table: AnyPgTable;
755
- returning?: SelectedFieldsOrdered;
577
+ interface PgMaterializedViewWithConfig {
578
+ [Key: string]: string | number | boolean | SQL;
756
579
  }
757
- type PgUpdateSetSource<TTable extends AnyPgTable> = Simplify<{
758
- [Key in keyof TTable['_']['columns']]?: GetColumnData<TTable['_']['columns'][Key]> | SQL;
759
- }>;
760
- declare class PgUpdateBuilder<TTable extends AnyPgTable, TQueryResult extends QueryResultHKT> {
761
- private table;
762
- private session;
763
- private dialect;
580
+ declare class MaterializedViewBuilderCore<TConfig extends {
581
+ name: string;
582
+ columns?: unknown;
583
+ }> {
584
+ protected name: TConfig['name'];
585
+ protected schema: string | undefined;
764
586
  static readonly [entityKind]: string;
765
- readonly _: {
766
- readonly table: TTable;
587
+ _: {
588
+ readonly name: TConfig['name'];
589
+ readonly columns: TConfig['columns'];
767
590
  };
768
- constructor(table: TTable, session: PgSession, dialect: PgDialect);
769
- set(values: PgUpdateSetSource<TTable>): PgUpdate<TTable, TQueryResult>;
770
- }
771
- interface PgUpdate<TTable extends AnyPgTable, TQueryResult extends QueryResultHKT, TReturning extends Record<string, unknown> | undefined = undefined> extends QueryPromise<TReturning extends undefined ? QueryResultKind<TQueryResult, never> : TReturning[]>, SQLWrapper {
772
- }
773
- declare class PgUpdate<TTable extends AnyPgTable, TQueryResult extends QueryResultHKT, TReturning extends Record<string, unknown> | undefined = undefined> extends QueryPromise<TReturning extends undefined ? QueryResultKind<TQueryResult, never> : TReturning[]> implements SQLWrapper {
774
- private session;
775
- private dialect;
776
- static readonly [entityKind]: string;
777
- readonly _: {
778
- readonly table: TTable;
779
- readonly return: TReturning;
591
+ constructor(name: TConfig['name'], schema: string | undefined);
592
+ protected config: {
593
+ with?: PgMaterializedViewWithConfig;
594
+ using?: string;
595
+ tablespace?: string;
596
+ withNoData?: boolean;
780
597
  };
781
- private config;
782
- constructor(table: TTable, set: UpdateSet, session: PgSession, dialect: PgDialect);
783
- where(where: SQL | undefined): this;
784
- returning(): PgUpdate<TTable, TQueryResult, InferModel<TTable>>;
785
- returning<TSelectedFields extends SelectedFields>(fields: TSelectedFields): PgUpdate<TTable, TQueryResult, SelectResultFields<TSelectedFields>>;
786
- toSQL(): Omit<Query, 'typings'>;
787
- private _prepare;
788
- prepare(name: string): PreparedQuery<PreparedQueryConfig & {
789
- execute: TReturning extends undefined ? QueryResultKind<TQueryResult, never> : TReturning[];
790
- }>;
791
- execute: ReturnType<this['prepare']>['execute'];
792
- }
793
-
794
- interface PgInsertConfig<TTable extends AnyPgTable = AnyPgTable> {
795
- table: TTable;
796
- values: Record<string, Param | SQL>[];
797
- onConflict?: SQL;
798
- returning?: SelectedFieldsOrdered;
598
+ using(using: string): this;
599
+ with(config: PgMaterializedViewWithConfig): this;
600
+ tablespace(tablespace: string): this;
601
+ withNoData(): this;
799
602
  }
800
- type PgInsertValue<TTable extends AnyPgTable> = Simplify<{
801
- [Key in keyof InferModel<TTable, 'insert'>]: InferModel<TTable, 'insert'>[Key] | SQL | Placeholder;
802
- }>;
803
- declare class PgInsertBuilder<TTable extends AnyPgTable, TQueryResult extends QueryResultHKT> {
804
- private table;
805
- private session;
806
- private dialect;
603
+ declare class MaterializedViewBuilder<TName extends string = string> extends MaterializedViewBuilderCore<{
604
+ name: TName;
605
+ }> {
807
606
  static readonly [entityKind]: string;
808
- constructor(table: TTable, session: PgSession, dialect: PgDialect);
809
- values(value: PgInsertValue<TTable>): PgInsert<TTable, TQueryResult>;
810
- values(values: PgInsertValue<TTable>[]): PgInsert<TTable, TQueryResult>;
607
+ as<TSelectedFields extends SelectedFields>(qb: TypedQueryBuilder<TSelectedFields> | ((qb: QueryBuilder) => TypedQueryBuilder<TSelectedFields>)): PgMaterializedViewWithSelection<TName, false, AddAliasToSelection<TSelectedFields, TName, 'pg'>>;
811
608
  }
812
- interface PgInsert<TTable extends AnyPgTable, TQueryResult extends QueryResultHKT, TReturning extends Record<string, unknown> | undefined = undefined> extends QueryPromise<TReturning extends undefined ? QueryResultKind<TQueryResult, never> : TReturning[]>, SQLWrapper {
609
+ declare class ManualMaterializedViewBuilder<TName extends string = string, TColumns extends Record<string, AnyPgColumnBuilder> = Record<string, AnyPgColumnBuilder>> extends MaterializedViewBuilderCore<{
610
+ name: TName;
611
+ columns: TColumns;
612
+ }> {
613
+ static readonly [entityKind]: string;
614
+ private columns;
615
+ constructor(name: TName, columns: TColumns, schema: string | undefined);
616
+ existing(): PgMaterializedViewWithSelection<TName, true, BuildColumns<TName, TColumns, 'pg'>>;
617
+ as(query: SQL): PgMaterializedViewWithSelection<TName, false, BuildColumns<TName, TColumns, 'pg'>>;
813
618
  }
814
- declare class PgInsert<TTable extends AnyPgTable, TQueryResult extends QueryResultHKT, TReturning extends Record<string, unknown> | undefined = undefined> extends QueryPromise<TReturning extends undefined ? QueryResultKind<TQueryResult, never> : TReturning[]> implements SQLWrapper {
815
- private session;
816
- private dialect;
619
+ declare abstract class PgViewBase<TName extends string = string, TExisting extends boolean = boolean, TSelectedFields extends ColumnsSelection = ColumnsSelection> extends View<TName, TExisting, TSelectedFields> {
817
620
  static readonly [entityKind]: string;
818
- _: {
819
- table: TTable;
820
- return: TReturning;
621
+ readonly _: View<TName, TExisting, TSelectedFields>['_'] & {
622
+ readonly viewBrand: 'PgViewBase';
821
623
  };
822
- private config;
823
- constructor(table: TTable, values: PgInsertConfig['values'], session: PgSession, dialect: PgDialect);
824
- returning(): PgInsert<TTable, TQueryResult, InferModel<TTable>>;
825
- returning<TSelectedFields extends SelectedFieldsFlat>(fields: TSelectedFields): PgInsert<TTable, TQueryResult, SelectResultFields<TSelectedFields>>;
826
- onConflictDoNothing(config?: {
827
- target?: IndexColumn | IndexColumn[];
828
- where?: SQL;
829
- }): this;
830
- onConflictDoUpdate(config: {
831
- target: IndexColumn | IndexColumn[];
832
- where?: SQL;
833
- set: PgUpdateSetSource<TTable>;
834
- }): this;
835
- toSQL(): Simplify<Omit<Query, 'typings'>>;
836
- private _prepare;
837
- prepare(name: string): PreparedQuery<PreparedQueryConfig & {
838
- execute: TReturning extends undefined ? QueryResultKind<TQueryResult, never> : TReturning[];
839
- }>;
840
- execute: ReturnType<this['prepare']>['execute'];
841
624
  }
842
-
843
- declare class QueryBuilder {
625
+ declare const PgViewConfig: unique symbol;
626
+ declare class PgView<TName extends string = string, TExisting extends boolean = boolean, TSelectedFields extends ColumnsSelection = ColumnsSelection> extends PgViewBase<TName, TExisting, TSelectedFields> {
844
627
  static readonly [entityKind]: string;
845
- private dialect;
846
- $with<TAlias extends string>(alias: TAlias): {
847
- as<TSelection extends ColumnsSelection>(qb: TypedQueryBuilder<TSelection, unknown> | ((qb: QueryBuilder) => TypedQueryBuilder<TSelection, unknown>)): WithSubqueryWithSelection<TSelection, TAlias>;
848
- };
849
- with(...queries: WithSubquery[]): {
850
- select: {
851
- (): PgSelectBuilder<undefined, 'qb'>;
852
- <TSelection extends SelectedFields>(fields: TSelection): PgSelectBuilder<TSelection, "qb">;
853
- };
854
- selectDistinct: {
855
- (): PgSelectBuilder<undefined, 'qb'>;
856
- <TSelection_1 extends SelectedFields>(fields: TSelection_1): PgSelectBuilder<TSelection_1, "qb">;
857
- };
858
- selectDistinctOn: {
859
- (on: (AnyPgColumn | SQLWrapper)[]): PgSelectBuilder<undefined, 'qb'>;
860
- <TSelection_2 extends SelectedFields>(on: (AnyPgColumn | SQLWrapper)[], fields: TSelection_2): PgSelectBuilder<TSelection_2, "qb">;
628
+ [PgViewConfig]: {
629
+ with?: ViewWithConfig;
630
+ } | undefined;
631
+ constructor({ pgConfig, config }: {
632
+ pgConfig: {
633
+ with?: ViewWithConfig;
634
+ } | undefined;
635
+ config: {
636
+ name: TName;
637
+ schema: string | undefined;
638
+ selectedFields: SelectedFields;
639
+ query: SQL | undefined;
861
640
  };
862
- };
863
- select(): PgSelectBuilder<undefined, 'qb'>;
864
- select<TSelection extends SelectedFields>(fields: TSelection): PgSelectBuilder<TSelection, 'qb'>;
865
- selectDistinct(): PgSelectBuilder<undefined>;
866
- selectDistinct<TSelection extends SelectedFields>(fields: TSelection): PgSelectBuilder<TSelection>;
867
- selectDistinctOn(on: (AnyPgColumn | SQLWrapper)[]): PgSelectBuilder<undefined>;
868
- selectDistinctOn<TSelection extends SelectedFields>(on: (AnyPgColumn | SQLWrapper)[], fields: TSelection): PgSelectBuilder<TSelection>;
869
- private getDialect;
870
- }
871
-
872
- interface PgRefreshMaterializedView<TQueryResult extends QueryResultHKT> extends QueryPromise<QueryResultKind<TQueryResult, never>> {
641
+ });
873
642
  }
874
- declare class PgRefreshMaterializedView<TQueryResult extends QueryResultHKT> extends QueryPromise<QueryResultKind<TQueryResult, never>> {
875
- private session;
876
- private dialect;
643
+ type PgViewWithSelection<TName extends string = string, TExisting extends boolean = boolean, TSelectedFields extends ColumnsSelection = ColumnsSelection> = PgView<TName, TExisting, TSelectedFields> & TSelectedFields;
644
+ declare const PgMaterializedViewConfig: unique symbol;
645
+ declare class PgMaterializedView<TName extends string = string, TExisting extends boolean = boolean, TSelectedFields extends ColumnsSelection = ColumnsSelection> extends PgViewBase<TName, TExisting, TSelectedFields> {
877
646
  static readonly [entityKind]: string;
878
- private config;
879
- constructor(view: PgMaterializedView, session: PgSession, dialect: PgDialect);
880
- concurrently(): this;
881
- withNoData(): this;
882
- toSQL(): Simplify<Omit<Query, 'typings'>>;
883
- private _prepare;
884
- prepare(name: string): PreparedQuery<PreparedQueryConfig & {
885
- execute: QueryResultKind<TQueryResult, never>;
886
- }>;
887
- execute: ReturnType<this['prepare']>['execute'];
647
+ readonly [PgMaterializedViewConfig]: {
648
+ readonly with?: PgMaterializedViewWithConfig;
649
+ readonly using?: string;
650
+ readonly tablespace?: string;
651
+ readonly withNoData?: boolean;
652
+ } | undefined;
653
+ constructor({ pgConfig, config }: {
654
+ pgConfig: {
655
+ with: PgMaterializedViewWithConfig | undefined;
656
+ using: string | undefined;
657
+ tablespace: string | undefined;
658
+ withNoData: boolean | undefined;
659
+ } | undefined;
660
+ config: {
661
+ name: TName;
662
+ schema: string | undefined;
663
+ selectedFields: SelectedFields;
664
+ query: SQL | undefined;
665
+ };
666
+ });
888
667
  }
668
+ type PgMaterializedViewWithSelection<TName extends string = string, TExisting extends boolean = boolean, TSelectedFields extends ColumnsSelection = ColumnsSelection> = PgMaterializedView<TName, TExisting, TSelectedFields> & TSelectedFields;
669
+ declare function pgView<TName extends string>(name: TName): ViewBuilder<TName>;
670
+ declare function pgView<TName extends string, TColumns extends Record<string, AnyPgColumnBuilder>>(name: TName, columns: TColumns): ManualViewBuilder<TName, TColumns>;
671
+ declare function pgMaterializedView<TName extends string>(name: TName): MaterializedViewBuilder<TName>;
672
+ declare function pgMaterializedView<TName extends string, TColumns extends Record<string, AnyPgColumnBuilder>>(name: TName, columns: TColumns): ManualMaterializedViewBuilder<TName, TColumns>;
889
673
 
890
- declare class PgDialect {
891
- static readonly [entityKind]: string;
892
- migrate(migrations: MigrationMeta[], session: PgSession): Promise<void>;
893
- escapeName(name: string): string;
894
- escapeParam(num: number): string;
895
- escapeString(str: string): string;
896
- buildDeleteQuery({ table, where, returning }: PgDeleteConfig): SQL;
897
- buildUpdateSet(table: AnyPgTable, set: UpdateSet): SQL;
898
- buildUpdateQuery({ table, set, where, returning }: PgUpdateConfig): SQL;
899
- /**
900
- * Builds selection SQL with provided fields/expressions
901
- *
902
- * Examples:
903
- *
904
- * `select <selection> from`
905
- *
906
- * `insert ... returning <selection>`
907
- *
908
- * If `isSingleTable` is true, then columns won't be prefixed with table name
909
- */
910
- private buildSelection;
911
- buildSelectQuery({ withList, fields, fieldsFlat, where, having, table, joins, orderBy, groupBy, limit, offset, lockingClauses, distinct, }: PgSelectConfig): SQL;
912
- buildInsertQuery({ table, values, onConflict, returning }: PgInsertConfig): SQL;
913
- buildRefreshMaterializedViewQuery({ view, concurrently, withNoData }: {
914
- view: PgMaterializedView;
915
- concurrently?: boolean;
916
- withNoData?: boolean;
917
- }): SQL;
918
- prepareTyping(encoder: DriverValueEncoder<unknown, unknown>): QueryTypingsValue;
919
- sqlToQuery(sql: SQL): Query;
920
- buildRelationalQueryWithoutPK({ fullSchema, schema, tableNamesMap, table, tableConfig, queryConfig: config, tableAlias, nestedQueryRelation, joinOn, }: {
921
- fullSchema: Record<string, unknown>;
922
- schema: TablesRelationalConfig;
923
- tableNamesMap: Record<string, string>;
924
- table: AnyPgTable;
925
- tableConfig: TableRelationalConfig;
926
- queryConfig: true | DBQueryConfig<'many', true>;
927
- tableAlias: string;
928
- nestedQueryRelation?: Relation;
929
- joinOn?: SQL;
930
- }): BuildRelationalQueryResult<AnyPgTable, AnyPgColumn>;
674
+ interface Join {
675
+ on: SQL | undefined;
676
+ table: PgTable | Subquery | PgViewBase | SQL;
677
+ alias: string | undefined;
678
+ joinType: JoinType;
679
+ lateral?: boolean;
931
680
  }
932
-
933
- declare class RelationalQueryBuilder<TSchema extends TablesRelationalConfig, TFields extends TableRelationalConfig> {
934
- private fullSchema;
935
- private schema;
936
- private tableNamesMap;
937
- private table;
938
- private tableConfig;
939
- private dialect;
940
- private session;
941
- static readonly [entityKind]: string;
942
- constructor(fullSchema: Record<string, unknown>, schema: TSchema, tableNamesMap: Record<string, string>, table: AnyPgTable, tableConfig: TableRelationalConfig, dialect: PgDialect, session: PgSession);
943
- findMany<TConfig extends DBQueryConfig<'many', true, TSchema, TFields>>(config?: KnownKeysOnly<TConfig, DBQueryConfig<'many', true, TSchema, TFields>>): PgRelationalQuery<BuildQueryResult<TSchema, TFields, TConfig>[]>;
944
- findFirst<TSelection extends Omit<DBQueryConfig<'many', true, TSchema, TFields>, 'limit'>>(config?: KnownKeysOnly<TSelection, Omit<DBQueryConfig<'many', true, TSchema, TFields>, 'limit'>>): PgRelationalQuery<BuildQueryResult<TSchema, TFields, TSelection> | undefined>;
681
+ type AnyPgSelect = PgSelect<any, any, any, any>;
682
+ type BuildAliasTable<TTable extends PgTable | View, TAlias extends string> = TTable extends Table ? PgTableWithColumns<UpdateTableConfig<TTable['_']['config'], {
683
+ name: TAlias;
684
+ columns: MapColumnsToTableAlias<TTable['_']['columns'], TAlias, 'pg'>;
685
+ }>> : TTable extends View ? PgViewWithSelection<TAlias, TTable['_']['existing'], MapColumnsToTableAlias<TTable['_']['selectedFields'], TAlias, 'pg'>> : never;
686
+ interface PgSelectConfig {
687
+ withList?: Subquery[];
688
+ fields: Record<string, unknown>;
689
+ fieldsFlat?: SelectedFieldsOrdered;
690
+ where?: SQL;
691
+ having?: SQL;
692
+ table: PgTable | Subquery | PgViewBase | SQL;
693
+ limit?: number | Placeholder;
694
+ offset?: number | Placeholder;
695
+ joins?: Join[];
696
+ orderBy?: (PgColumn | SQL | SQL.Aliased)[];
697
+ groupBy?: (PgColumn | SQL | SQL.Aliased)[];
698
+ lockingClauses?: {
699
+ strength: LockStrength;
700
+ config: LockConfig;
701
+ }[];
702
+ distinct?: boolean | {
703
+ on: (PgColumn | SQLWrapper)[];
704
+ };
945
705
  }
946
- declare class PgRelationalQuery<TResult> extends QueryPromise<TResult> {
947
- private fullSchema;
948
- private schema;
949
- private tableNamesMap;
950
- private table;
951
- private tableConfig;
952
- private dialect;
953
- private session;
954
- private config;
955
- private mode;
956
- static readonly [entityKind]: string;
957
- protected $brand: 'PgRelationalQuery';
958
- constructor(fullSchema: Record<string, unknown>, schema: TablesRelationalConfig, tableNamesMap: Record<string, string>, table: AnyPgTable, tableConfig: TableRelationalConfig, dialect: PgDialect, session: PgSession, config: DBQueryConfig<'many', true> | true, mode: 'many' | 'first');
959
- private _prepare;
960
- prepare(name: string): PreparedQuery<PreparedQueryConfig & {
961
- execute: TResult;
962
- }>;
963
- execute(): Promise<TResult>;
706
+ type JoinFn<THKT extends PgSelectHKTBase, TTableName extends string | undefined, TSelectMode extends SelectMode, TJoinType extends JoinType, TSelection, TNullabilityMap extends Record<string, JoinNullability>> = <TJoinedTable extends PgTable | Subquery | PgViewBase | SQL, TJoinedName extends GetSelectTableName<TJoinedTable> = GetSelectTableName<TJoinedTable>>(table: TJoinedTable, on: ((aliases: TSelection) => SQL | undefined) | SQL | undefined) => PgSelectKind<THKT, TTableName, AppendToResult<TTableName, TSelection, TJoinedName, TJoinedTable extends Table ? TJoinedTable['_']['columns'] : TJoinedTable extends Subquery ? Assume<TJoinedTable['_']['selectedFields'], SelectedFields> : never, TSelectMode>, TSelectMode extends 'partial' ? TSelectMode : 'multiple', AppendToNullabilityMap<TNullabilityMap, TJoinedName, TJoinType>>;
707
+ type SelectedFieldsFlat = SelectedFieldsFlat$1<PgColumn>;
708
+ type SelectedFields = SelectedFields$1<PgColumn, PgTable>;
709
+ type SelectedFieldsOrdered = SelectedFieldsOrdered$1<PgColumn>;
710
+ type LockStrength = 'update' | 'no key update' | 'share' | 'key share';
711
+ type LockConfig = {
712
+ of?: PgTable;
713
+ } & ({
714
+ noWait: true;
715
+ skipLocked?: undefined;
716
+ } | {
717
+ noWait?: undefined;
718
+ skipLocked: true;
719
+ } | {
720
+ noWait?: undefined;
721
+ skipLocked?: undefined;
722
+ });
723
+ interface PgSelectHKTBase {
724
+ tableName: string | undefined;
725
+ selection: unknown;
726
+ selectMode: SelectMode;
727
+ nullabilityMap: unknown;
728
+ _type: unknown;
964
729
  }
965
-
966
- declare class PgDatabase<TQueryResult extends QueryResultHKT, TFullSchema extends Record<string, unknown> = Record<string, never>, TSchema extends TablesRelationalConfig = ExtractTablesWithRelations<TFullSchema>> {
967
- static readonly [entityKind]: string;
968
- readonly _: {
969
- readonly schema: TSchema | undefined;
970
- readonly tableNamesMap: Record<string, string>;
971
- };
972
- query: TFullSchema extends Record<string, never> ? DrizzleTypeError<'Seems like the schema generic is missing - did you forget to add it to your DB type?'> : {
973
- [K in keyof TSchema]: RelationalQueryBuilder<TSchema, TSchema[K]>;
974
- };
975
- constructor(
976
- /** @internal */
977
- dialect: PgDialect,
978
- /** @internal */
979
- session: PgSession<any, any, any>, schema: RelationalSchemaConfig<TSchema> | undefined);
980
- $with<TAlias extends string>(alias: TAlias): {
981
- as<TSelection extends ColumnsSelection>(qb: TypedQueryBuilder<TSelection, unknown> | ((qb: QueryBuilder) => TypedQueryBuilder<TSelection, unknown>)): WithSubqueryWithSelection<TSelection, TAlias>;
982
- };
983
- with(...queries: WithSubquery[]): {
984
- select: {
985
- (): PgSelectBuilder<undefined>;
986
- <TSelection extends SelectedFields>(fields: TSelection): PgSelectBuilder<TSelection, "db">;
987
- };
988
- };
989
- select(): PgSelectBuilder<undefined>;
990
- select<TSelection extends SelectedFields>(fields: TSelection): PgSelectBuilder<TSelection>;
991
- selectDistinct(): PgSelectBuilder<undefined>;
992
- selectDistinct<TSelection extends SelectedFields>(fields: TSelection): PgSelectBuilder<TSelection>;
993
- selectDistinctOn(on: (AnyPgColumn | SQLWrapper)[]): PgSelectBuilder<undefined>;
994
- selectDistinctOn<TSelection extends SelectedFields>(on: (AnyPgColumn | SQLWrapper)[], fields: TSelection): PgSelectBuilder<TSelection>;
995
- update<TTable extends AnyPgTable>(table: TTable): PgUpdateBuilder<TTable, TQueryResult>;
996
- insert<TTable extends AnyPgTable>(table: TTable): PgInsertBuilder<TTable, TQueryResult>;
997
- delete<TTable extends AnyPgTable>(table: TTable): PgDelete<TTable, TQueryResult>;
998
- refreshMaterializedView<TView extends PgMaterializedView>(view: TView): PgRefreshMaterializedView<TQueryResult>;
999
- execute<TRow extends Record<string, unknown> = Record<string, unknown>>(query: SQLWrapper): Promise<QueryResultKind<TQueryResult, TRow>>;
1000
- transaction<T>(transaction: (tx: PgTransaction<TQueryResult, TFullSchema, TSchema>) => Promise<T>, config?: PgTransactionConfig): Promise<T>;
730
+ type PgSelectKind<T extends PgSelectHKTBase, TTableName extends string | undefined, TSelection, TSelectMode extends SelectMode, TNullabilityMap extends Record<string, JoinNullability>> = (T & {
731
+ tableName: TTableName;
732
+ selection: TSelection;
733
+ selectMode: TSelectMode;
734
+ nullabilityMap: TNullabilityMap;
735
+ })['_type'];
736
+ interface PgSelectQueryBuilderHKT extends PgSelectHKTBase {
737
+ _type: PgSelectQueryBuilder<this, this['tableName'], Assume<this['selection'], ColumnsSelection>, this['selectMode'], Assume<this['nullabilityMap'], Record<string, JoinNullability>>>;
738
+ }
739
+ interface PgSelectHKT extends PgSelectHKTBase {
740
+ _type: PgSelect<this['tableName'], Assume<this['selection'], ColumnsSelection>, this['selectMode'], Assume<this['nullabilityMap'], Record<string, JoinNullability>>>;
1001
741
  }
1002
742
 
1003
- export { PgInsert as $, AnyPgTable as A, BuildAliasTable as B, Check as C, IndexColumn as D, IndexBuilderOn as E, ForeignKey as F, AnyIndexBuilder as G, IndexBuilder as H, Index as I, GetColumnsTableName as J, index as K, uniqueIndex as L, primaryKey as M, PrimaryKeyBuilder as N, unique as O, PgColumnBuilder as P, uniqueKeyName as Q, ReferenceConfig as R, UniqueConstraintBuilder as S, UniqueOnConstraintBuilder as T, UniqueConstraint as U, ViewWithConfig as V, PgDeleteConfig as W, PgDelete as X, PgInsertConfig as Y, PgInsertValue as Z, PgInsertBuilder as _, PgColumn as a, QueryBuilder as a0, PgRefreshMaterializedView as a1, PgSelectBuilder as a2, PgSelectQueryBuilder as a3, PgSelect as a4, Join as a5, AnyPgSelect as a6, PgSelectConfig as a7, JoinFn as a8, SelectedFieldsFlat as a9, pgTable as aA, pgTableCreator as aB, DefaultViewBuilderCore as aC, ViewBuilder as aD, ManualViewBuilder as aE, MaterializedViewBuilderCore as aF, MaterializedViewBuilder as aG, ManualMaterializedViewBuilder as aH, PgViewBase as aI, PgViewConfig as aJ, PgViewWithSelection as aK, PgMaterializedViewConfig as aL, PgMaterializedViewWithSelection as aM, SelectedFields as aa, SelectedFieldsOrdered as ab, LockStrength as ac, LockConfig as ad, PgSelectHKTBase as ae, PgSelectKind as af, PgSelectQueryBuilderHKT as ag, PgSelectHKT as ah, PgUpdateConfig as ai, PgUpdateSetSource as aj, PgUpdateBuilder as ak, PgUpdate as al, PreparedQueryConfig as am, PreparedQuery as an, PgTransactionConfig as ao, PgSession as ap, PgTransaction as aq, QueryResultHKT as ar, QueryResultKind as as, SubqueryWithSelection as at, WithSubqueryWithSelection as au, PgTableExtraConfig as av, TableConfig as aw, PgTable as ax, AnyPgTableHKT as ay, PgTableWithColumns as az, PgTableFn as b, pgMaterializedView as c, AnyPgColumn as d, PrimaryKey as e, PgView as f, PgMaterializedView as g, PgMaterializedViewWithConfig as h, CheckBuilder as i, check as j, PgArrayBuilderHKT as k, PgArrayHKT as l, PgArrayBuilder as m, PgArray as n, PgColumnBuilderHKT as o, pgView as p, PgColumnHKT as q, AnyPgColumnBuilder as r, AnyPgColumnHKT as s, PgDatabase as t, PgDialect as u, UpdateDeleteAction as v, Reference as w, ForeignKeyBuilder as x, AnyForeignKeyBuilder as y, foreignKey as z };
743
+ export { PgViewConfig as $, AnyPgSelect as A, BuildAliasTable as B, PgUpdateConfig as C, PgUpdateSetSource as D, PgUpdateBuilder as E, PgUpdate as F, PreparedQueryConfig as G, PreparedQuery as H, PgTransactionConfig as I, Join as J, PgSession as K, LockStrength as L, PgTransaction as M, QueryResultHKT as N, QueryResultKind as O, PgViewBase as P, QueryBuilder as Q, SubqueryWithSelection as R, SelectedFieldsFlat as S, DefaultViewBuilderCore as T, ViewBuilder as U, ViewWithConfig as V, WithSubqueryWithSelection as W, ManualViewBuilder as X, MaterializedViewBuilderCore as Y, MaterializedViewBuilder as Z, ManualMaterializedViewBuilder as _, pgMaterializedView as a, PgViewWithSelection as a0, PgMaterializedViewConfig as a1, PgMaterializedViewWithSelection as a2, PgView as b, PgMaterializedView as c, PgMaterializedViewWithConfig as d, PgDatabase as e, PgDialect as f, PgDeleteConfig as g, PgDelete as h, PgInsertConfig as i, PgInsertValue as j, PgInsertBuilder as k, PgInsert as l, PgRefreshMaterializedView as m, PgSelectBuilder as n, PgSelectQueryBuilder as o, pgView as p, PgSelect as q, PgSelectConfig as r, JoinFn as s, SelectedFields as t, SelectedFieldsOrdered as u, LockConfig as v, PgSelectHKTBase as w, PgSelectKind as x, PgSelectQueryBuilderHKT as y, PgSelectHKT as z };