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 | 412x 241x 241x 627x 4x 15x 15x 15x 30x 14x 15x 1x 1x 15x 30x 15x 11x 11x 11x 11x 11x 16x 2x 9x 9x 9x 9x 2x 72x 285x 243x 35x 17x 17x 18x 1x 17x 16x 16x 1x 208x 117x 48x 69x 18x 18x 51x | import { is } from 'drizzle-orm/entity'
import { Param, SQL, sql as yql } from 'drizzle-orm/sql/sql'
import { Table } from 'drizzle-orm/table'
import type { YdbColumn } from '../columns/common.js'
import type { YdbTable } from '../table.js'
import { getTableConfig } from '../table.utils.js'
type TableColumns = Record<string, YdbColumn>
export function getTableColumns(table: YdbTable): TableColumns {
return ((table as any)[(Table as any).Symbol.Columns] ?? {}) as TableColumns
}
export function validateTableColumnKeys(
table: YdbTable,
input: Record<string, unknown>,
operation: 'insert' | 'update' | 'upsert' | 'replace'
): void {
let columns = getTableColumns(table)
for (let key of Object.keys(input)) {
if (!(key in columns)) {
throw new Error(`Unknown column "${key}" in ${operation}()`)
}
}
}
export function getPrimaryColumnKeys(table: YdbTable): string[] {
let columns = getTableColumns(table)
let primaryColumns = new Set<YdbColumn>()
for (let column of Object.values(columns)) {
if (column.primary) {
primaryColumns.add(column)
}
}
for (let primaryKey of getTableConfig(table as any).primaryKeys) {
for (let column of primaryKey.config.columns) {
primaryColumns.add(column)
}
}
return Object.entries(columns)
.filter(([, column]) => primaryColumns.has(column))
.map(([key]) => key)
}
export function validateSetBasedMutationSelection(
table: YdbTable,
fields: Record<string, unknown> | undefined,
operation: 'update' | 'delete'
): void {
let label = operation === 'update' ? 'Update on' : 'Delete on'
Iif (!fields || Object.keys(fields).length === 0) {
throw new Error(`${label} error: selected fields must include table columns`)
}
let columns = getTableColumns(table)
let selectedKeys = Object.keys(fields)
for (let key of selectedKeys) {
if (!(key in columns)) {
throw new Error(
`${label} error: selected field "${key}" is not a column of the target table`
)
}
}
let primaryKeys = getPrimaryColumnKeys(table)
Iif (primaryKeys.length === 0) {
throw new Error(`YDB ${operation}().on() requires at least one primary key column`)
}
for (let key of primaryKeys) {
if (!selectedKeys.includes(key)) {
throw new Error(
`YDB ${operation}().on() requires primary key column "${key}" in selected fields`
)
}
}
}
export function getInsertColumnEntries(table: YdbTable): Array<[string, YdbColumn]> {
return Object.entries(getTableColumns(table)).filter(
([, column]) => !(column as any).shouldDisableInsert?.()
)
}
export function resolveInsertValue(column: YdbColumn, value: unknown): unknown {
if (value === undefined || (is(value, Param) && value.value === undefined)) {
if (column.defaultFn !== undefined) {
let defaultValue = column.defaultFn()
return is(defaultValue, SQL) ? defaultValue : yql.param(defaultValue, column)
}
if (column.default !== undefined) {
return is(column.default, SQL) ? column.default : yql.param(column.default, column)
}
if (column.onUpdateFn !== undefined) {
let onUpdateValue = column.onUpdateFn()
return is(onUpdateValue, SQL) ? onUpdateValue : yql.param(onUpdateValue, column)
}
return yql`default`
}
return is(value, SQL) || is(value, Param) ? value : yql.param(value, column)
}
export function resolveUpdateValue(column: YdbColumn, value: unknown): unknown {
if (value !== undefined) {
return is(value, SQL) || is(value, Param) ? value : yql.param(value, column)
}
if (column.onUpdateFn !== undefined) {
let onUpdateValue = column.onUpdateFn()
return is(onUpdateValue, SQL) ? onUpdateValue : yql.param(onUpdateValue, column)
}
return undefined
}
|