deno.land / x / pg_mem@2.8.1 / datatypes / t-custom-enum.ts

t-custom-enum.ts
نووسراو ببینە
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
import { Evaluator } from '../evaluator.ts';import { TypeBase } from './datatype-base.ts';import { DataType, nil, QueryError } from '../interfaces.ts';import {_IRelation, _ISchema, _IType, _Transaction} from '../interfaces-private.ts';
export function asEnum(o: _IRelation | null): CustomEnumType { if (o && o.type === 'type' && o instanceof CustomEnumType) { return o; } throw new QueryError(`"${o?.name}" is not a enum`);}export class CustomEnumType extends TypeBase<string> {
get primary(): DataType { return this.name as any; }
get name(): string { return this._name; }
constructor(readonly schema: _ISchema , private readonly _name: string , readonly values: string[]) { super(); }
install() { this.schema._registerType(this); }
doCanCast(to: _IType) { return to.primary === DataType.text; }
doCast(value: Evaluator<string>, to: _IType<string>): Evaluator<any> | nil { return value; }
prefer(type: _IType<any>): _IType | nil { return this; }
doCanBuildFrom(from: _IType): boolean | nil { return from.primary === DataType.text; }
doBuildFrom(value: Evaluator<string>, from: _IType<string>): Evaluator<string> | nil { return value .setConversion((raw: string) => { if (!this.values.includes(raw)) { throw new QueryError(`invalid input value for enum ${this.name}: "${raw}"`); } return raw; } , conv => ({ conv, toCenum: this.name })) }

drop(t: _Transaction): void { this.schema._unregisterType(this); }}
pg_mem

Version Info

Tagged at
4 months ago