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

95% Statements 38/40
62.5% Branches 5/8
100% Functions 19/19
95% Lines 38/40

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                                                                                                                          13x 13x             5x       34x         5x       5x               34x   5x       34x         2x       2x 2x                 34x   2x       34x             4x 4x 4x       4x 4x       4x                   34x   4x       34x   7x           7x 7x       7x 7x       7x 7x                   34x   7x           5x       2x                                     4x       4x             7x    
import { entityKind } from 'drizzle-orm/entity'
import type { YdbColumn } from './columns/common.js'
import type { YdbTable } from './table.js'
 
export type YdbTableOptionValue = string | number | boolean | YdbRawTableOptionValue
 
export interface YdbRawTableOptionValue {
	readonly kind: 'raw'
	readonly value: string
}
 
export interface YdbTableOptionsConfig {
	readonly table: YdbTable
	readonly options: Readonly<Record<string, YdbTableOptionValue>>
}
 
export type YdbPartitioningConfig = {
	readonly table: YdbTable
	readonly type: 'hash'
	readonly columns: readonly YdbColumn[]
}
 
export type YdbTtlUnit = 'SECONDS' | 'MILLISECONDS' | 'MICROSECONDS' | 'NANOSECONDS'
export type YdbTtlAction =
	| {
			readonly interval: string
			readonly delete?: true
	  }
	| {
			readonly interval: string
			readonly externalDataSource: string
	  }
 
export interface YdbTtlConfig {
	readonly table: YdbTable
	readonly column: YdbColumn
	readonly actions: readonly YdbTtlAction[]
	readonly unit?: YdbTtlUnit | undefined
}
 
export type YdbColumnFamilyCompression = 'off' | 'lz4' | 'zstd' | (string & {})
export type YdbColumnFamilyData = 'ssd' | 'rot' | (string & {})
 
export interface YdbColumnFamilyOptions {
	readonly data?: YdbColumnFamilyData | undefined
	readonly compression?: YdbColumnFamilyCompression | undefined
	readonly compressionLevel?: number | undefined
}
 
export interface YdbColumnFamilyConfig {
	readonly table: YdbTable
	readonly name: string
	readonly options: YdbColumnFamilyOptions
	readonly columns: readonly YdbColumn[]
}
 
function assertColumnsBelongToTable(
	table: YdbTable,
	columns: readonly YdbColumn[],
	kind: string
): void {
	for (let column of columns) {
		Iif (column.table !== table) {
			throw new Error(`${kind} column "${column.name}" does not belong to table`)
		}
	}
}
 
export function rawTableOption(value: string): YdbRawTableOptionValue {
	return { kind: 'raw', value }
}
 
export class YdbTableOptionsBuilder {
	static readonly [entityKind] = 'YdbTableOptionsBuilder'
 
	readonly #options: Readonly<Record<string, YdbTableOptionValue>>
 
	constructor(options: Readonly<Record<string, YdbTableOptionValue>>) {
		this.#options = options
	}
 
	build(table: YdbTable): YdbTableOptions {
		return new YdbTableOptions({
			table,
			options: { ...this.#options },
		})
	}
}
 
export class YdbTableOptions {
	static readonly [entityKind] = 'YdbTableOptions'
 
	constructor(readonly config: YdbTableOptionsConfig) {}
}
 
export class YdbPartitioningBuilder {
	static readonly [entityKind] = 'YdbPartitioningBuilder'
 
	readonly #columns: [YdbColumn, ...YdbColumn[]]
 
	constructor(columns: [YdbColumn, ...YdbColumn[]]) {
		this.#columns = columns
	}
 
	build(table: YdbTable): YdbPartitioning {
		assertColumnsBelongToTable(table, this.#columns, 'Partitioning')
		return new YdbPartitioning({
			table,
			type: 'hash',
			columns: [...this.#columns],
		})
	}
}
 
export class YdbPartitioning {
	static readonly [entityKind] = 'YdbPartitioning'
 
	constructor(readonly config: YdbPartitioningConfig) {}
}
 
export class YdbTtlBuilder {
	static readonly [entityKind] = 'YdbTtlBuilder'
 
	readonly #column: YdbColumn
	readonly #actions: readonly YdbTtlAction[]
	readonly #unit: YdbTtlUnit | undefined
 
	constructor(column: YdbColumn, actions: readonly YdbTtlAction[], unit?: YdbTtlUnit) {
		this.#column = column
		this.#actions = actions
		this.#unit = unit
	}
 
	build(table: YdbTable): YdbTtl {
		assertColumnsBelongToTable(table, [this.#column], 'TTL')
		Iif (this.#actions.length === 0) {
			throw new Error('YDB TTL requires at least one action')
		}
 
		return new YdbTtl({
			table,
			column: this.#column,
			actions: [...this.#actions],
			unit: this.#unit,
		})
	}
}
 
export class YdbTtl {
	static readonly [entityKind] = 'YdbTtl'
 
	constructor(readonly config: YdbTtlConfig) {}
}
 
export class YdbColumnFamilyBuilder {
	static readonly [entityKind] = 'YdbColumnFamilyBuilder'
 
	#familyColumns: YdbColumn[] = []
 
	readonly #name: string
	readonly #options: YdbColumnFamilyOptions
 
	constructor(name: string, options: YdbColumnFamilyOptions = {}) {
		this.#name = name
		this.#options = options
	}
 
	columns(...columns: YdbColumn[]): this {
		this.#familyColumns = [...columns]
		return this
	}
 
	build(table: YdbTable): YdbColumnFamily {
		assertColumnsBelongToTable(table, this.#familyColumns, 'Column family')
		return new YdbColumnFamily({
			table,
			name: this.#name,
			options: { ...this.#options },
			columns: [...this.#familyColumns],
		})
	}
}
 
export class YdbColumnFamily {
	static readonly [entityKind] = 'YdbColumnFamily'
 
	constructor(readonly config: YdbColumnFamilyConfig) {}
}
 
export function tableOptions(
	options: Readonly<Record<string, YdbTableOptionValue>>
): YdbTableOptionsBuilder {
	return new YdbTableOptionsBuilder(options)
}
 
export function partitionByHash(...columns: [YdbColumn, ...YdbColumn[]]): YdbPartitioningBuilder {
	return new YdbPartitioningBuilder(columns)
}
 
export function ttl(
	column: YdbColumn,
	interval: string,
	options?: { unit?: YdbTtlUnit }
): YdbTtlBuilder
export function ttl(
	column: YdbColumn,
	actions: [YdbTtlAction, ...YdbTtlAction[]],
	options?: { unit?: YdbTtlUnit }
): YdbTtlBuilder
export function ttl(
	column: YdbColumn,
	intervalOrActions: string | [YdbTtlAction, ...YdbTtlAction[]],
	options: { unit?: YdbTtlUnit } = {}
): YdbTtlBuilder {
	let actions =
		typeof intervalOrActions === 'string'
			? [{ interval: intervalOrActions }]
			: intervalOrActions
 
	return new YdbTtlBuilder(column, actions, options.unit)
}
 
export function columnFamily(
	name: string,
	options?: YdbColumnFamilyOptions
): YdbColumnFamilyBuilder {
	return new YdbColumnFamilyBuilder(name, options)
}