All files / third-parties/drizzle-adapter/src/ydb-core db.ts

80.32% Statements 49/61
66.66% Branches 16/24
76.47% Functions 26/34
80.32% Lines 49/61

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 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303                                                                                20x                                     62x 62x     62x                           62x 62x 19x     19x   62x 14x   14x       14x 14x 7x     7x       7x       7x                 7x   7x     62x 19x   14x       14x     3x       3x             2x   1x     43x         115x       2x       3x       38x       2x       2x         2x                                                                                                   2x                         3x       44x           1x             1x           36x       3x       2x       5x       2x       91x       2x             4x               4x      
import { entityKind } from 'drizzle-orm/entity'
import type { RelationalSchemaConfig, TablesRelationalConfig } from 'drizzle-orm/relations'
import { type SQL, type SQLWrapper } from 'drizzle-orm/sql/sql'
import type { WithSubquery } from 'drizzle-orm/subquery'
import type { DrizzleTypeError } from 'drizzle-orm/utils'
import type { YdbTransactionConfig } from '../ydb/driver.js'
import type { YdbDialect } from '../ydb/dialect.js'
import type { YdbSession } from './session.js'
import type { YdbQuerySource } from './session.js'
import type {
	YdbSchemaDefinition,
	YdbSchemaRelations,
	YdbSchemaWithoutTables,
} from './schema.types.js'
import type { YdbTable } from './table.js'
import {
	YdbBatchDeleteBuilder,
	YdbBatchUpdateBuilder,
	YdbCountBuilder,
	YdbDeleteBuilder,
	YdbInsertBuilder,
	YdbQueryBuilder,
	YdbRelationalQueryBuilder,
	YdbReplaceBuilder,
	YdbSelectBuilder,
	YdbUpdateBuilder,
	YdbUpsertBuilder,
} from './query-builders/index.js'
 
export type YdbTransactionScope<
	TSchemaDefinition extends YdbSchemaDefinition = YdbSchemaWithoutTables,
	TSchemaRelations extends TablesRelationalConfig = YdbSchemaRelations<TSchemaDefinition>,
> = Omit<YdbDatabase<TSchemaDefinition, TSchemaRelations>, 'transaction'> & {
	rollback(): never
}
 
export class YdbDatabase<
	TSchemaDefinition extends YdbSchemaDefinition = YdbSchemaWithoutTables,
	TSchemaRelations extends TablesRelationalConfig = YdbSchemaRelations<TSchemaDefinition>,
> {
	static readonly [entityKind] = 'YdbDatabase'
 
	readonly _: {
		readonly schema: TSchemaRelations | undefined
		readonly fullSchema: TSchemaDefinition
		readonly tableNamesMap: Record<string, string>
		readonly session: YdbSession
	}
 
	query: TSchemaDefinition extends YdbSchemaWithoutTables
		? DrizzleTypeError<'Seems like the schema generic is missing - did you forget to add it to your DB type?'>
		: {
				[K in keyof TSchemaRelations]: YdbRelationalQueryBuilder<
					TSchemaRelations,
					TSchemaRelations[K]
				>
			}
 
	constructor(
		protected readonly dialect: YdbDialect,
		protected readonly session: YdbSession,
		schema?: RelationalSchemaConfig<TSchemaRelations>
	) {
		this._ = schema
			? {
					schema: schema.schema,
					fullSchema: schema.fullSchema as TSchemaDefinition,
					tableNamesMap: schema.tableNamesMap,
					session,
				}
			: {
					schema: undefined,
					fullSchema: {} as TSchemaDefinition,
					tableNamesMap: {},
					session,
				}
 
		let queryBuilders = new Map<string, YdbRelationalQueryBuilder<TSchemaRelations, any>>()
		let getRelationalTableConfig = (tableKey: string) => {
			let schemaByKey = this._.schema as
				| Record<string, TSchemaRelations[keyof TSchemaRelations]>
				| undefined
			return schemaByKey?.[tableKey]
		}
		let getRelationalQueryBuilder = (tableKey: string) => {
			let tableConfig = getRelationalTableConfig(tableKey)
 
			Iif (!tableConfig) {
				return undefined
			}
 
			let cached = queryBuilders.get(tableKey)
			if (cached) {
				return cached
			}
 
			let table = (this._.fullSchema as Record<string, unknown>)[tableConfig.tsName] as
				| YdbTable
				| undefined
 
			Iif (!table) {
				throw new Error(`Table ${tableConfig.tsName} not found in schema`)
			}
 
			let builder = new YdbRelationalQueryBuilder(
				this._.fullSchema,
				this._.schema!,
				this._.tableNamesMap,
				table,
				tableConfig,
				this.dialect,
				this.session
			)
			queryBuilders.set(tableKey, builder)
 
			return builder
		}
 
		if (this._.schema) {
			this.query = new Proxy(Object.create(null), {
				get: (_target, property) => {
					Iif (typeof property !== 'string') {
						return undefined
					}
 
					return getRelationalQueryBuilder(property)
				},
				getOwnPropertyDescriptor: (_target, property) => {
					Iif (typeof property !== 'string' || !getRelationalTableConfig(property)) {
						return undefined
					}
 
					return {
						configurable: true,
						enumerable: true,
						get: () => getRelationalQueryBuilder(property),
					}
				},
				has: (_target, property) => {
					return typeof property === 'string' && !!getRelationalTableConfig(property)
				},
				ownKeys: () => Object.keys(this._.schema!),
			}) as typeof this.query
		} else {
			this.query = Object.create(null) as typeof this.query
		}
	}
 
	execute<T = unknown>(query: YdbQuerySource): Promise<T> {
		return this.session.execute<T>(query)
	}
 
	all<T = unknown>(query: YdbQuerySource): Promise<T[]> {
		return this.session.all<T>(query)
	}
 
	get<T = unknown>(query: YdbQuerySource): Promise<T> {
		return this.session.get<T>(query)
	}
 
	values<T extends unknown[] = unknown[]>(query: YdbQuerySource): Promise<T[]> {
		return this.session.values<T>(query)
	}
 
	$with<TAlias extends string>(alias: TAlias) {
		return new YdbQueryBuilder(this.dialect).$with(alias)
	}
 
	with(...queries: WithSubquery[]) {
		let { session, dialect } = this
 
		function select<TFields extends Record<string, unknown> | undefined = undefined>(
			fields?: TFields
		) {
			return new YdbSelectBuilder(session, dialect, fields as any, {}, queries)
		}
 
		function selectDistinct<TFields extends Record<string, unknown> | undefined = undefined>(
			fields?: TFields
		) {
			return new YdbSelectBuilder(
				session,
				dialect,
				fields as any,
				{ distinct: true },
				queries
			)
		}
 
		function selectDistinctOn<TFields extends Record<string, unknown> | undefined = undefined>(
			on: SQLWrapper | SQLWrapper[],
			fields?: TFields
		) {
			return new YdbSelectBuilder(
				session,
				dialect,
				fields as any,
				{
					distinctOn: Array.isArray(on) ? on : [on],
				},
				queries
			)
		}
 
		function insert(table: YdbTable) {
			return new YdbInsertBuilder(table, session, dialect, queries)
		}
 
		function upsert(table: YdbTable) {
			return new YdbUpsertBuilder(table, session, dialect, queries)
		}
 
		function replace(table: YdbTable) {
			return new YdbReplaceBuilder(table, session, dialect, queries)
		}
 
		function update(table: YdbTable) {
			return new YdbUpdateBuilder(table, session, dialect, queries)
		}
 
		function delete_(table: YdbTable) {
			return new YdbDeleteBuilder(table, session, dialect, queries)
		}
 
		return {
			select,
			selectDistinct,
			selectDistinctOn,
			insert,
			upsert,
			replace,
			update,
			delete: delete_,
		}
	}
 
	$count(source: YdbTable | SQLWrapper, filters?: SQL) {
		return new YdbCountBuilder({ source, filters, session: this.session })
	}
 
	select<TFields extends Record<string, unknown> | undefined = undefined>(fields?: TFields) {
		return new YdbSelectBuilder(this.session, this.dialect, fields as any)
	}
 
	selectDistinct<TFields extends Record<string, unknown> | undefined = undefined>(
		fields?: TFields
	) {
		return new YdbSelectBuilder(this.session, this.dialect, fields as any, { distinct: true })
	}
 
	selectDistinctOn<TFields extends Record<string, unknown> | undefined = undefined>(
		on: SQLWrapper | SQLWrapper[],
		fields?: TFields
	) {
		return new YdbSelectBuilder(this.session, this.dialect, fields as any, {
			distinctOn: Array.isArray(on) ? on : [on],
		})
	}
 
	insert(table: YdbTable) {
		return new YdbInsertBuilder(table, this.session, this.dialect)
	}
 
	upsert(table: YdbTable) {
		return new YdbUpsertBuilder(table, this.session, this.dialect)
	}
 
	replace(table: YdbTable) {
		return new YdbReplaceBuilder(table, this.session, this.dialect)
	}
 
	update(table: YdbTable) {
		return new YdbUpdateBuilder(table, this.session, this.dialect)
	}
 
	batchUpdate(table: YdbTable) {
		return new YdbBatchUpdateBuilder(table, this.session, this.dialect)
	}
 
	delete(table: YdbTable) {
		return new YdbDeleteBuilder(table, this.session, this.dialect)
	}
 
	batchDelete(table: YdbTable) {
		return new YdbBatchDeleteBuilder(table, this.session, this.dialect)
	}
 
	transaction<T>(
		transaction: (tx: YdbTransactionScope<TSchemaDefinition, TSchemaRelations>) => Promise<T>,
		config?: YdbTransactionConfig
	) {
		let schema = this._.schema
			? ({
					fullSchema: this._.fullSchema,
					schema: this._.schema,
					tableNamesMap: this._.tableNamesMap,
				} as RelationalSchemaConfig<TSchemaRelations>)
			: undefined
 
		return this.session.transaction(transaction, config, schema)
	}
}