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 | 139x 209x 209x 348x 348x 139x 139x 827x 557x 352x 345x 476x 475x 207x 207x 341x 207x | import { AbstractDataType } from './abstract-data-type.ts';
/** Key/value pairs of an `HSTORE` column: values are strings, or `null` for SQL `NULL`. */
export type HstoreValue = Readonly<Record<string, string | null>>;
const PAIR = /"((?:[^"\\]|\\.)*)"\s*=>\s*(?:NULL|"((?:[^"\\]|\\.)*)")/g;
/**
* `HSTORE` (`DataTypes.HSTORE`); values are plain objects, like Sequelize v6 with `pg-hstore`.
*
* @internal
*/
export class HstoreType extends AbstractDataType {
/** Converts hstore text into an object; values are strings or `null`. */
readonly parse = (value: unknown): unknown =>
typeof value === 'string' ? HstoreType.#parse(value) : value;
/** Converts an object into hstore text; non-object values (e.g. hstore text) are kept. */
readonly serialize = (value: unknown): unknown =>
typeof value === 'object' && value !== null && !Array.isArray(value)
? HstoreType.#serialize(value)
: value;
constructor() {
super('HSTORE', 'HSTORE');
Object.freeze(this);
}
static #quote(text: string): string {
return `"${text.replaceAll('\\', '\\\\').replaceAll('"', '\\"')}"`;
}
static #unquote(text: string): string {
return text.replaceAll(/\\(.)/g, '$1');
}
static #text(value: unknown): string {
return typeof value === 'object' ? JSON.stringify(value) : String(value);
}
static #serialize(value: object): string {
return Object.entries(value)
.filter(([, item]) => item !== undefined)
.map(
([key, item]) =>
`${HstoreType.#quote(key)}=>${item === null ? 'NULL' : HstoreType.#quote(HstoreType.#text(item))}`,
)
.join(',');
}
static #parse(text: string): Record<string, string | null> {
const result: Record<string, string | null> = {};
for (const match of text.matchAll(PAIR)) {
result[HstoreType.#unquote(match[1] ?? '')] =
match[2] === undefined ? null : HstoreType.#unquote(match[2]);
}
return result;
}
}
|