|
| 1 | +import type { ColumnBuilderBaseConfig, ColumnBuilderRuntimeConfig, MakeColumnConfig } from '~/column-builder.ts'; |
| 2 | +import type { ColumnBaseConfig } from '~/column.ts'; |
| 3 | +import { entityKind } from '~/entity.ts'; |
| 4 | +import type { AnySingleStoreTable } from '~/singlestore-core/table.ts'; |
| 5 | +import { type Equal, getColumnNameAndConfig } from '~/utils.ts'; |
| 6 | +import { SingleStoreColumn, SingleStoreColumnBuilder } from './common.ts'; |
| 7 | + |
| 8 | +type BlobMode = 'buffer' | 'json' | 'bigint'; |
| 9 | + |
| 10 | +export type SingleStoreBigIntBuilderInitial<TName extends string> = SingleStoreBigIntBuilder<{ |
| 11 | + name: TName; |
| 12 | + dataType: 'bigint'; |
| 13 | + columnType: 'SingleStoreBigInt'; |
| 14 | + data: bigint; |
| 15 | + driverParam: Buffer; |
| 16 | + enumValues: undefined; |
| 17 | +}>; |
| 18 | + |
| 19 | +export class SingleStoreBigIntBuilder<T extends ColumnBuilderBaseConfig<'bigint', 'SingleStoreBigInt'>> |
| 20 | + extends SingleStoreColumnBuilder<T> |
| 21 | +{ |
| 22 | + static override readonly [entityKind]: string = 'SingleStoreBigIntBuilder'; |
| 23 | + |
| 24 | + constructor(name: T['name']) { |
| 25 | + super(name, 'bigint', 'SingleStoreBigInt'); |
| 26 | + } |
| 27 | + |
| 28 | + /** @internal */ |
| 29 | + override build<TTableName extends string>( |
| 30 | + table: AnySingleStoreTable<{ name: TTableName }>, |
| 31 | + ): SingleStoreBigInt<MakeColumnConfig<T, TTableName>> { |
| 32 | + return new SingleStoreBigInt<MakeColumnConfig<T, TTableName>>(table, this.config as ColumnBuilderRuntimeConfig<any>); |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +export class SingleStoreBigInt<T extends ColumnBaseConfig<'bigint', 'SingleStoreBigInt'>> extends SingleStoreColumn<T> { |
| 37 | + static override readonly [entityKind]: string = 'SingleStoreBigInt'; |
| 38 | + |
| 39 | + getSQLType(): string { |
| 40 | + return 'blob'; |
| 41 | + } |
| 42 | + |
| 43 | + override mapFromDriverValue(value: Buffer | Uint8Array): bigint { |
| 44 | + if (Buffer.isBuffer(value)) { |
| 45 | + return BigInt(value.toString()); |
| 46 | + } |
| 47 | + |
| 48 | + return BigInt(String.fromCodePoint(...value)); |
| 49 | + } |
| 50 | + |
| 51 | + override mapToDriverValue(value: bigint): Buffer { |
| 52 | + return Buffer.from(value.toString()); |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +export type SingleStoreBlobJsonBuilderInitial<TName extends string> = SingleStoreBlobJsonBuilder<{ |
| 57 | + name: TName; |
| 58 | + dataType: 'json'; |
| 59 | + columnType: 'SingleStoreBlobJson'; |
| 60 | + data: unknown; |
| 61 | + driverParam: Buffer; |
| 62 | + enumValues: undefined; |
| 63 | +}>; |
| 64 | + |
| 65 | +export class SingleStoreBlobJsonBuilder<T extends ColumnBuilderBaseConfig<'json', 'SingleStoreBlobJson'>> |
| 66 | + extends SingleStoreColumnBuilder<T> |
| 67 | +{ |
| 68 | + static override readonly [entityKind]: string = 'SingleStoreBlobJsonBuilder'; |
| 69 | + |
| 70 | + constructor(name: T['name']) { |
| 71 | + super(name, 'json', 'SingleStoreBlobJson'); |
| 72 | + } |
| 73 | + |
| 74 | + /** @internal */ |
| 75 | + override build<TTableName extends string>( |
| 76 | + table: AnySingleStoreTable<{ name: TTableName }>, |
| 77 | + ): SingleStoreBlobJson<MakeColumnConfig<T, TTableName>> { |
| 78 | + return new SingleStoreBlobJson<MakeColumnConfig<T, TTableName>>( |
| 79 | + table, |
| 80 | + this.config as ColumnBuilderRuntimeConfig<any>, |
| 81 | + ); |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +export class SingleStoreBlobJson<T extends ColumnBaseConfig<'json', 'SingleStoreBlobJson'>> extends SingleStoreColumn<T> { |
| 86 | + static override readonly [entityKind]: string = 'SingleStoreBlobJson'; |
| 87 | + |
| 88 | + getSQLType(): string { |
| 89 | + return 'blob'; |
| 90 | + } |
| 91 | + |
| 92 | + override mapFromDriverValue(value: Buffer | Uint8Array | ArrayBuffer): T['data'] { |
| 93 | + if (Buffer.isBuffer(value)) { |
| 94 | + return JSON.parse(value.toString()); |
| 95 | + } |
| 96 | + |
| 97 | + // for sqlite durable objects |
| 98 | + // eslint-disable-next-line no-instanceof/no-instanceof |
| 99 | + if (value instanceof ArrayBuffer) { |
| 100 | + const decoder = new TextDecoder(); |
| 101 | + return JSON.parse(decoder.decode(value)); |
| 102 | + } |
| 103 | + |
| 104 | + return JSON.parse(String.fromCodePoint(...value)); |
| 105 | + } |
| 106 | + |
| 107 | + override mapToDriverValue(value: T['data']): Buffer { |
| 108 | + return Buffer.from(JSON.stringify(value)); |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +export type SingleStoreBlobBufferBuilderInitial<TName extends string> = SingleStoreBlobBufferBuilder<{ |
| 113 | + name: TName; |
| 114 | + dataType: 'buffer'; |
| 115 | + columnType: 'SingleStoreBlobBuffer'; |
| 116 | + data: Buffer; |
| 117 | + driverParam: Buffer; |
| 118 | + enumValues: undefined; |
| 119 | +}>; |
| 120 | + |
| 121 | +export class SingleStoreBlobBufferBuilder<T extends ColumnBuilderBaseConfig<'buffer', 'SingleStoreBlobBuffer'>> |
| 122 | + extends SingleStoreColumnBuilder<T> |
| 123 | +{ |
| 124 | + static override readonly [entityKind]: string = 'SingleStoreBlobBufferBuilder'; |
| 125 | + |
| 126 | + constructor(name: T['name']) { |
| 127 | + super(name, 'buffer', 'SingleStoreBlobBuffer'); |
| 128 | + } |
| 129 | + |
| 130 | + /** @internal */ |
| 131 | + override build<TTableName extends string>( |
| 132 | + table: AnySingleStoreTable<{ name: TTableName }>, |
| 133 | + ): SingleStoreBlobBuffer<MakeColumnConfig<T, TTableName>> { |
| 134 | + return new SingleStoreBlobBuffer<MakeColumnConfig<T, TTableName>>(table, this.config as ColumnBuilderRuntimeConfig<any>); |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +export class SingleStoreBlobBuffer<T extends ColumnBaseConfig<'buffer', 'SingleStoreBlobBuffer'>> extends SingleStoreColumn<T> { |
| 139 | + static override readonly [entityKind]: string = 'SingleStoreBlobBuffer'; |
| 140 | + |
| 141 | + getSQLType(): string { |
| 142 | + return 'blob'; |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +export interface BlobConfig<TMode extends BlobMode = BlobMode> { |
| 147 | + mode: TMode; |
| 148 | +} |
| 149 | + |
| 150 | +/** |
| 151 | + * It's recommended to use `text('...', { mode: 'json' })` instead of `blob` in JSON mode, because it supports JSON functions: |
| 152 | + * >All JSON functions currently throw an error if any of their arguments are BLOBs because BLOBs are reserved for a future enhancement in which BLOBs will store the binary encoding for JSON. |
| 153 | + * |
| 154 | + * https://www.sqlite.org/json1.html |
| 155 | + */ |
| 156 | +export function blob(): SingleStoreBlobJsonBuilderInitial<''>; |
| 157 | +export function blob<TMode extends BlobMode = BlobMode>( |
| 158 | + config?: BlobConfig<TMode>, |
| 159 | +): Equal<TMode, 'bigint'> extends true ? SingleStoreBigIntBuilderInitial<''> |
| 160 | + : Equal<TMode, 'buffer'> extends true ? SingleStoreBlobBufferBuilderInitial<''> |
| 161 | + : SingleStoreBlobJsonBuilderInitial<''>; |
| 162 | +export function blob<TName extends string, TMode extends BlobMode = BlobMode>( |
| 163 | + name: TName, |
| 164 | + config?: BlobConfig<TMode>, |
| 165 | +): Equal<TMode, 'bigint'> extends true ? SingleStoreBigIntBuilderInitial<TName> |
| 166 | + : Equal<TMode, 'buffer'> extends true ? SingleStoreBlobBufferBuilderInitial<TName> |
| 167 | + : SingleStoreBlobJsonBuilderInitial<TName>; |
| 168 | +export function blob(a?: string | BlobConfig, b?: BlobConfig) { |
| 169 | + const { name, config } = getColumnNameAndConfig<BlobConfig | undefined>(a, b); |
| 170 | + if (config?.mode === 'json') { |
| 171 | + return new SingleStoreBlobJsonBuilder(name); |
| 172 | + } |
| 173 | + if (config?.mode === 'bigint') { |
| 174 | + return new SingleStoreBigIntBuilder(name); |
| 175 | + } |
| 176 | + return new SingleStoreBlobBufferBuilder(name); |
| 177 | +} |
0 commit comments