generated from Arnesfield/template.ts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.ts
159 lines (153 loc) · 4.41 KB
/
types.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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/**
* The schema options.
* @template TFrom The property of data.
*/
export interface SchemaOptions<TFrom extends keyof any> {
/**
* The property of data to map from.
*
* Value in {@linkcode MappedRecord} defaults to `null` if not provided.
*/
from?: TFrom;
/**
* The data type.
* @default 'TEXT'
*/
type?: string;
/**
* Transforms data using the following map.
*
* If the data is an array, each item is remapped using this object.
*
* If the remapping required is complex, prefer to remap the data manually using {@linkcode Remaq.each}.
*/
remap?: { [K: keyof any]: any };
/**
* Validate the {@linkcode value} and throw an error if it is invalid.
*
* Validation only occurs after the {@linkcode value} has beem {@linkcode remap}ped.
* @param value The value to validate.
* @returns Determines if the {@linkcode value} is valid or not.
*/
validate?(value: any): boolean;
}
/**
* The schema type. Map column names to the properties of {@linkcode TData data}.
*
* Set column name to `true` to not map to any {@linkcode TData data} properties
* but retain the property in {@linkcode MappedRecord} (defaults to `null`).
* @template TData The type of data.
*/
export interface Schema<TData extends { [K: keyof any]: any }> {
[name: string]: keyof TData | SchemaOptions<keyof TData> | true;
}
/**
* The mapped record.
* @template TSchema The schema type.
*/
export type MappedRecord<TSchema extends { [K: keyof any]: any }> = {
[Key in keyof TSchema]: any;
};
/**
* Called once per each {@linkcode MappedRecord record} created.
* @template TData The type of data.
* @template TSchema The schema type.
* @param record The {@linkcode MappedRecord record} created.
* @param value The {@linkcode TData data} to remap.
* @param index The index of {@linkcode TData data}.
* @param data All {@linkcode TData data} to remap.
* @returns Return `false` to exclude the {@linkcode MappedRecord record} from {@linkcode Remaq.records records()}.
*/
export type EachRecordCallback<
TData extends { [K: keyof any]: any },
TSchema extends Schema<TData>
> = (
record: MappedRecord<TSchema>,
value: TData,
index: number,
data: TData[]
) => boolean | void;
/**
* The get query options.
*/
export interface GetQueryOptions {
/**
* The JSON string to use.
*
* Defaults to an escaped JSON.stringify({@linkcode Remaq.records records}).
*/
data?: string;
/**
* The table alias to use.
* @default 'tbl'
*/
alias?: string;
/**
* The selects to use.
* @default ['*']
*/
selects?: string[];
/**
* Set to `true` to use `jsonb_to_recordset` instead of `json_to_recordset`.
* @default false
*/
jsonb?: boolean;
}
/**
* The get query results.
*/
export interface GetQueryResults {
/**
* The raw PostgreSQL select query.
*/
raw: string;
/**
* The JSON string of {@linkcode Remaq.records records} (or
* {@linkcode GetQueryOptions.data data} if provided).
*/
json: string;
/**
* The columns including their data type.
*
* e.g. `"name" TEXT`, `"value" INT`
*/
columns: string[];
}
/**
* The Remaq object.
* @template TData The type of data.
* @template TSchema The schema type.
*/
export interface Remaq<
TData extends { [K: keyof any]: any },
TSchema extends Schema<TData>
> {
/**
* The schema object.
*/
readonly schema: TSchema;
/**
* Get remapped object records.
* @returns The mapped records.
*/
records(): MappedRecord<TSchema>[];
/**
* Set a callback function that is used when creating {@linkcode MappedRecord records}.
* @param callback A function that accepts up to four arguments and is called once per each {@linkcode MappedRecord record} created. Return `false` to exclude the {@linkcode MappedRecord record} from {@linkcode records()}.
* @returns The {@linkcode Remaq} object.
*/
each(callback: EachRecordCallback<TData, TSchema>): this;
/**
* Set the {@linkcode Schema schema} to use when formatting {@linkcode TData data}.
* @template USchema The schema type.
* @param schema The schema.
* @returns A new {@linkcode Remaq} object.
*/
using<USchema extends Schema<TData>>(schema: USchema): Remaq<TData, USchema>;
/**
* Use the {@linkcode records} to create a PostgreSQL select query using `json/b_to_recordset`.
* @param options The get query options.
* @returns The get query results.
*/
getQuery(options?: GetQueryOptions): GetQueryResults;
}