All files / third-parties/drizzle-adapter/src/ydb-core/query-builders delete.ts

95.91% Statements 47/49
69.23% Branches 18/26
94.73% Functions 18/19
95.91% Lines 47/49

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187                                                  101x                             101x 101x 101x 101x 101x       96x 1x     95x 96x       3x 1x     2x 2x         6x   6x 6x     4x 4x 4x 4x       5x 5x       5x 5x       99x 2x   2x               97x                   1x 1x       94x                 91x                       6x 6x 6x 6x       3x 3x       1x       1x       1x       3x               1x 1x       1x                 1x      
import { is } from 'drizzle-orm/entity'
import { QueryPromise } from 'drizzle-orm/query-promise'
import { SQL, type SQL as SQLType, type SQLWrapper } from 'drizzle-orm/sql/sql'
import type { Subquery } from 'drizzle-orm/subquery'
import { Table } from 'drizzle-orm/table'
import type { YdbPreparedQueryConfig, YdbSession } from '../session.js'
import type { YdbTable } from '../table.js'
import { type YdbSelectedFieldsOrdered, orderSelectedFields } from '../result-mapping.js'
import { YdbDialect } from '../../ydb/dialect.js'
import { validateSetBasedMutationSelection } from './utils.js'
import { YdbQueryBuilder } from './query-builder.js'
 
type DeleteOnQuery =
	| SQLType
	| {
			getSQL(): SQLType
			getSelectedFields(): Record<string, unknown> | undefined
	  }
 
function getAllReturningFields(table: YdbTable): Record<string, unknown> {
	return (table as any)[(Table as any).Symbol.Columns] ?? {}
}
 
export class YdbDeleteBuilder<TResult = unknown> extends QueryPromise<TResult> {
	#whereClause: SQLType | undefined
	#usingTables: SQLWrapper[] = []
	#onQuery: DeleteOnQuery | undefined
	#returningFields: YdbSelectedFieldsOrdered | undefined
 
	readonly #table: YdbTable
	readonly #session: YdbSession
	readonly #dialect: YdbDialect
	readonly #withList: Subquery[]
 
	constructor(
		table: YdbTable,
		session: YdbSession,
		dialect = new YdbDialect(),
		withList: Subquery[] = []
	) {
		super()
		this.#table = table
		this.#session = session
		this.#dialect = dialect
		this.#withList = withList
	}
 
	where(where: SQLType | undefined): this {
		if (this.#onQuery) {
			throw new Error('YDB delete().on() does not support where()')
		}
 
		this.#whereClause = where ?? undefined
		return this
	}
 
	using(...tables: SQLWrapper[]): this {
		if (this.#onQuery) {
			throw new Error('YDB delete().on() does not support using()')
		}
 
		this.#usingTables = [...tables]
		return this
	}
 
	on(query: DeleteOnQuery | ((qb: YdbQueryBuilder) => DeleteOnQuery)): this {
		let resolved =
			typeof query === 'function' ? query(new YdbQueryBuilder(this.#dialect)) : query
 
		Eif (!is(resolved, SQL)) {
			validateSetBasedMutationSelection(this.#table, resolved.getSelectedFields(), 'delete')
		}
 
		this.#onQuery = resolved
		this.#whereClause = undefined
		this.#usingTables = []
		return this
	}
 
	returning(fields: Record<string, unknown> = getAllReturningFields(this.#table)): this {
		let orderedFields = orderSelectedFields(fields)
		Iif (orderedFields.length === 0) {
			throw new Error('YDB returning() requires at least one field')
		}
 
		this.#returningFields = orderedFields
		return this
	}
 
	getSQL(): SQLType {
		if (this.#onQuery) {
			let onSql = is(this.#onQuery, SQL) ? this.#onQuery : this.#onQuery.getSQL()
 
			return this.#dialect.buildDeleteQuery({
				table: this.#table,
				on: onSql,
				withList: this.#withList,
				returning: this.#returningFields,
			})
		}
 
		return this.#dialect.buildDeleteQuery({
			table: this.#table,
			where: this.#whereClause,
			using: this.#usingTables.length > 0 ? [...this.#usingTables] : undefined,
			withList: this.#withList,
			returning: this.#returningFields,
		})
	}
 
	toSQL() {
		let { typings: _typings, ...query } = this.#dialect.sqlToQuery(this.getSQL())
		return query
	}
 
	prepare(name?: string) {
		return this.#session.prepareQuery<YdbPreparedQueryConfig & { execute: TResult }>(
			this.getSQL(),
			this.#returningFields,
			name,
			this.#returningFields !== undefined
		)
	}
 
	override execute(): Promise<TResult> {
		return this.prepare().execute() as Promise<TResult>
	}
}
 
export class YdbBatchDeleteBuilder<TResult = unknown> extends QueryPromise<TResult> {
	#whereClause: SQLType | undefined
 
	readonly #table: YdbTable
	readonly #session: YdbSession
	readonly #dialect: YdbDialect
 
	constructor(table: YdbTable, session: YdbSession, dialect = new YdbDialect()) {
		super()
		this.#table = table
		this.#session = session
		this.#dialect = dialect
	}
 
	where(where: SQLType | undefined): this {
		this.#whereClause = where ?? undefined
		return this
	}
 
	using(): never {
		throw new Error('YDB batchDelete().using() is not supported')
	}
 
	on(): never {
		throw new Error('YDB batchDelete().on() is not supported')
	}
 
	returning(): never {
		throw new Error('YDB batchDelete().returning() is not supported')
	}
 
	getSQL(): SQLType {
		return this.#dialect.buildDeleteQuery({
			table: this.#table,
			where: this.#whereClause,
			batch: true,
		})
	}
 
	toSQL() {
		let { typings: _typings, ...query } = this.#dialect.sqlToQuery(this.getSQL())
		return query
	}
 
	prepare(name?: string) {
		return this.#session.prepareQuery<YdbPreparedQueryConfig & { execute: TResult }>(
			this.getSQL(),
			undefined,
			name,
			false
		)
	}
 
	override execute(): Promise<TResult> {
		return this.prepare().execute() as Promise<TResult>
	}
}