|
| 1 | +import { entityKind } from '~/entity.ts'; |
| 2 | +import type { PgQueryResultHKT, PgQueryResultKind, PreparedQueryConfig } from '~/pg-core/session.ts'; |
| 3 | +import type { PgTable } from '~/pg-core/table.ts'; |
| 4 | +import type { JoinNullability } from '~/query-builders/select.types.ts'; |
| 5 | +import { QueryPromise } from '~/query-promise.ts'; |
| 6 | +import type { RunnableQuery } from '~/runnable-query.ts'; |
| 7 | +import type { ColumnsSelection, SQL } from '~/sql/sql.ts'; |
| 8 | +import type { Subquery } from '~/subquery.ts'; |
| 9 | +import { applyMixins, type Assume } from '~/utils.ts'; |
| 10 | +import type { PgAsyncPreparedQuery, PgAsyncSession } from '../async/session.ts'; |
| 11 | +import { type Join, PgUpdateBase } from '../query-builders/update.ts'; |
| 12 | +import { extractUsedTable } from '../utils.ts'; |
| 13 | +import type { PgViewBase } from '../view-base.ts'; |
| 14 | + |
| 15 | +export type PgAsyncUpdatePrepare<T extends AnyPgAsyncUpdate> = PgAsyncPreparedQuery< |
| 16 | + PreparedQueryConfig & { |
| 17 | + execute: T['_']['returning'] extends undefined ? PgQueryResultKind<T['_']['queryResult'], never> |
| 18 | + : T['_']['returning'][]; |
| 19 | + } |
| 20 | +>; |
| 21 | + |
| 22 | +export type PgAsyncUpdate< |
| 23 | + TTable extends PgTable = PgTable, |
| 24 | + TQueryResult extends PgQueryResultHKT = PgQueryResultHKT, |
| 25 | + TFrom extends PgTable | Subquery | PgViewBase | SQL | undefined = undefined, |
| 26 | + TSelectedFields extends ColumnsSelection | undefined = undefined, |
| 27 | + TReturning extends Record<string, unknown> | undefined = Record<string, unknown> | undefined, |
| 28 | + TNullabilityMap extends Record<string, JoinNullability> = Record<TTable['_']['name'], 'not-null'>, |
| 29 | + TJoins extends Join[] = [], |
| 30 | +> = PgAsyncUpdateBase< |
| 31 | + TTable, |
| 32 | + TQueryResult, |
| 33 | + TFrom, |
| 34 | + TSelectedFields, |
| 35 | + TReturning, |
| 36 | + TNullabilityMap, |
| 37 | + TJoins, |
| 38 | + true, |
| 39 | + never |
| 40 | +>; |
| 41 | + |
| 42 | +export interface PgUpdateHKTBase { |
| 43 | + table: unknown; |
| 44 | + joins: unknown; |
| 45 | + nullabilityMap: unknown; |
| 46 | + queryResult: unknown; |
| 47 | + from: unknown; |
| 48 | + selectedFields: unknown; |
| 49 | + returning: unknown; |
| 50 | + dynamic: boolean; |
| 51 | + excludedMethods: string; |
| 52 | + _type: unknown; |
| 53 | +} |
| 54 | + |
| 55 | +export interface PgAsyncUpdateHKT extends PgUpdateHKTBase { |
| 56 | + _type: PgAsyncUpdateBase< |
| 57 | + Assume<this['table'], PgTable>, |
| 58 | + Assume<this['queryResult'], PgQueryResultHKT>, |
| 59 | + Assume<this['from'], PgTable | Subquery | PgViewBase | SQL | undefined>, |
| 60 | + Assume<this['selectedFields'], ColumnsSelection | undefined>, |
| 61 | + Assume<this['returning'], Record<string, unknown> | undefined>, |
| 62 | + Assume<this['nullabilityMap'], Record<string, JoinNullability>>, |
| 63 | + Assume<this['joins'], Join[]>, |
| 64 | + this['dynamic'], |
| 65 | + this['excludedMethods'] |
| 66 | + >; |
| 67 | +} |
| 68 | + |
| 69 | +export type AnyPgAsyncUpdate = PgAsyncUpdateBase<any, any, any, any, any, any, any, any, any>; |
| 70 | + |
| 71 | +export interface PgAsyncUpdateBase< |
| 72 | + TTable extends PgTable, |
| 73 | + TQueryResult extends PgQueryResultHKT, |
| 74 | + // eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 75 | + TFrom extends PgTable | Subquery | PgViewBase | SQL | undefined = undefined, |
| 76 | + // eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 77 | + TSelectedFields extends ColumnsSelection | undefined = undefined, |
| 78 | + TReturning extends Record<string, unknown> | undefined = undefined, |
| 79 | + // eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 80 | + TNullabilityMap extends Record<string, JoinNullability> = Record<TTable['_']['name'], 'not-null'>, |
| 81 | + // eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 82 | + TJoins extends Join[] = [], |
| 83 | + // eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 84 | + TDynamic extends boolean = false, |
| 85 | + // eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 86 | + TExcludedMethods extends string = never, |
| 87 | +> extends QueryPromise<TReturning extends undefined ? PgQueryResultKind<TQueryResult, never> : TReturning[]> {} |
| 88 | + |
| 89 | +export class PgAsyncUpdateBase< |
| 90 | + TTable extends PgTable, |
| 91 | + TQueryResult extends PgQueryResultHKT, |
| 92 | + TFrom extends PgTable | Subquery | PgViewBase | SQL | undefined = undefined, |
| 93 | + TSelectedFields extends ColumnsSelection | undefined = undefined, |
| 94 | + TReturning extends Record<string, unknown> | undefined = undefined, |
| 95 | + TNullabilityMap extends Record<string, JoinNullability> = Record<TTable['_']['name'], 'not-null'>, |
| 96 | + TJoins extends Join[] = [], |
| 97 | + TDynamic extends boolean = false, |
| 98 | + TExcludedMethods extends string = never, |
| 99 | +> extends PgUpdateBase< |
| 100 | + PgAsyncUpdateHKT, |
| 101 | + TTable, |
| 102 | + TQueryResult, |
| 103 | + TFrom, |
| 104 | + TSelectedFields, |
| 105 | + TReturning, |
| 106 | + TNullabilityMap, |
| 107 | + TJoins, |
| 108 | + TDynamic, |
| 109 | + TExcludedMethods |
| 110 | +> implements RunnableQuery<TReturning extends undefined ? PgQueryResultKind<TQueryResult, never> : TReturning[], 'pg'> { |
| 111 | + static override readonly [entityKind]: string = 'PgAsyncUpdate'; |
| 112 | + |
| 113 | + declare protected session: PgAsyncSession; |
| 114 | + |
| 115 | + /** @internal */ |
| 116 | + _prepare(name?: string): PgAsyncUpdatePrepare<this> { |
| 117 | + const query = this.session.prepareQuery< |
| 118 | + PreparedQueryConfig & { execute: TReturning[] } |
| 119 | + >(this.dialect.sqlToQuery(this.getSQL()), this.config.returning, name, true, undefined, { |
| 120 | + type: 'insert', |
| 121 | + tables: extractUsedTable(this.config.table), |
| 122 | + }, this.cacheConfig); |
| 123 | + query.joinsNotNullableMap = this.joinsNotNullableMap; |
| 124 | + return query; |
| 125 | + } |
| 126 | + |
| 127 | + prepare(name: string): PgAsyncUpdatePrepare<this> { |
| 128 | + return this._prepare(name); |
| 129 | + } |
| 130 | + |
| 131 | + execute: ReturnType<this['prepare']>['execute'] = (placeholderValues: Record<string, unknown> = {}) => { |
| 132 | + return this._prepare().execute(placeholderValues); |
| 133 | + }; |
| 134 | +} |
| 135 | + |
| 136 | +applyMixins(PgAsyncUpdateBase, [QueryPromise]); |
0 commit comments