-
Notifications
You must be signed in to change notification settings - Fork 180
/
PgLiteral.ts
57 lines (51 loc) · 1.25 KB
/
PgLiteral.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
import type { PublicPart } from '../operations/generalTypes';
/**
* Represents a string that should not be escaped when used in a query.
*
* This will be used in `pgm.func` to create unescaped strings.
*/
export class PgLiteral {
/**
* Creates a new `PgLiteral` instance.
*
* @param str The string value.
* @returns The new `PgLiteral` instance.
*/
static create(str: string): PgLiteral {
return new PgLiteral(str);
}
/**
* Indicates that this object is a `PgLiteral`.
*/
public readonly literal = true;
/**
* Creates a new `PgLiteral` instance.
*
* @param value The string value.
*/
constructor(public readonly value: string) {}
/**
* Returns the string value.
*
* @returns The string value.
*/
toString(): string {
return this.value;
}
}
export type PgLiteralValue = PublicPart<PgLiteral>;
/**
* Checks if the given value is a `PgLiteral`.
*
* @param val The value to check.
* @returns `true` if the value is a `PgLiteral`, or `false` otherwise.
*/
export function isPgLiteral(val: unknown): val is PgLiteral {
return (
val instanceof PgLiteral ||
(typeof val === 'object' &&
val !== null &&
'literal' in val &&
(val as { literal: unknown }).literal === true)
);
}