/
searchBuilderOptions.ts
346 lines (309 loc) · 8.25 KB
/
searchBuilderOptions.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
import * as knex from 'knex';
import {Knex} from 'knex';
import Field from './field';
import Editor from './editor';
function isNumeric(n) {
return !isNaN(parseFloat(n)) && isFinite(n);
}
export interface IOption {
label: string;
value: string | number;
}
/**
* Left join object structure
* @interface ILeftJoin
* @private
*/
interface ILeftJoin {
table: string;
fn?: Function;
field1?: string;
field2?: string;
operator?: string;
}
export type IRenderer = (str: string) => string;
export type CustomOptions = (db: Knex) => Promise<IOption[]>;
/**
* The Options class provides a convenient method of specifying where Editor
* should get the list of options for a `select`, `radio` or `checkbox` field.
* This is normally from a table that is _left joined_ to the main table being
* edited, and a list of the values available from the joined table is shown to
* the end user to let them select from.
*
* `Options` instances are used with the {@link Field.options} method.
*
* @export
* @class Options
*/
export default class SearchBuilderOptions {
private _table: string;
private _value: string;
private _label: string[];
private _leftJoin: ILeftJoin[];
private _renderer: IRenderer;
private _where: any;
private _order: string;
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Public methods
*/
/**
* Get the column(s) to be used for the label
*
* @returns {string[]} Label columns
*/
public label(): string[];
/**
* Set the column(s) to be used for the label
*
* @param {string[]} label Database column names
* @returns {Options} Self for chaining
*/
public label(label: string[]): SearchBuilderOptions;
public label(label?: string[]): any {
if (label === undefined) {
return this._label;
}
this._label = Array.isArray(label) ?
label :
[label];
return this;
}
/**
* Get the ORDER BY clause for the SQL.
*
* @returns {string} ORDER BY clause
*/
public order(): string;
/**
* Set the ORDER BY clause to use in the SQL. If this option is not
* provided the ordering will be based on the rendered output, either
* numerically or alphabetically based on the data returned by the renderer.
*
* @param {string} order ORDER BY statement
* @returns {Options} Self for chaining
*/
public order(order: string): SearchBuilderOptions;
public order(order?: string): any {
if (order === undefined) {
return this._order;
}
this._order = order;
return this;
}
/**
* Get the configured label renderer
*
* @returns {IRenderer} Self for chaining
*/
public render(): IRenderer;
/**
* Set the label renderer. The renderer can be used to combine
* multiple database columns into a single string that is shown as the label
* to the end user in the list of options.
*
* @param {IRenderer} fn Renderering function
* @returns {Options} Self for chaining
*/
public render(fn: IRenderer): SearchBuilderOptions;
public render(fn?: IRenderer): any {
if (fn === undefined) {
return this._renderer;
}
this._renderer = fn;
return this;
}
/**
* Get the table that the options will be gathered from.
*
* @returns {string} Table name
*/
public table(): string;
/**
* Set the database table from which to gather the options for the list.
*
* @param {string} table Table name
* @returns {Options} Self for chaining
*/
public table(table: string): SearchBuilderOptions;
public table(table?: string): any {
if (table === undefined) {
return this._table;
}
this._table = table;
return this;
}
/**
* Get the column name to use for the value in the options list.
*
* @returns {string} Column name
*/
public value(): string;
/**
* Set the column name to use for the value in the options list. This would
* normally be the primary key for the table.
*
* @param {string} value Column name
* @returns {Options} Self for chaining
*/
public value(value: string): SearchBuilderOptions;
public value(value?: string): any {
if (value === undefined) {
return this._value;
}
this._value = value;
return this;
}
/**
* Get the WHERE condition for this option set.
*
* @returns {*} Knex WHERE condition
*/
public where(): any;
/**
* Set the method to use for a WHERE condition if one is to be applied to
* the query to get the options.
*
* @param {*} where Knex WHERE condition
* @returns {Options} Self for chaining
*/
public where(where: any): SearchBuilderOptions;
public where(where?: any): any {
if (where === undefined) {
return this._where;
}
this._where = where;
return this;
}
/**
* Set the method to use for a leftJoin condition if one is to be applied
* to the query to retrieve data from two tables
*
* @param table the table for the join
* @param field1 the first field
* @param operator operator for the join
* @param field2 the second field
*/
public leftJoin(table: string, field1: string | Function, operator: string, field2: string): this {
if(this._leftJoin === undefined || this._leftJoin === null) {
this._leftJoin = [];
}
if (typeof field1 === 'function') {
this._leftJoin.push({
field1: '',
field2: '',
fn: field1,
operator: '',
table,
});
}
else {
this._leftJoin.push({
field1,
field2,
operator,
table,
});
}
return this;
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Internal methods
*/
/**
* Execution function for getting the SearchBuilder options
* @param field The field to retrieve data from
* @param editor The editor instance
* @param http The http sent to the server
* @param fieldsIn All of the fields
* @param leftJoinIn Info for a leftJoin if required
*/
public async exec(
field: Field, editor: Editor, http: any, fieldsIn: any, leftJoinIn: any
): Promise<IOption[]> {
let label;
let value;
let table;
let formatter = this._renderer;
let join = this._leftJoin;
let fields = fieldsIn;
// First get a value for `value`. This can be retrieved from either the
// SearchBuilderOptions or the fieldName if it has not been declared
if (this._value === undefined) {
let sbopts = field.searchBuilderOptions();
value = sbopts.label() !== undefined ?
sbopts.label()[0] :
value = field.name();
}
// Otherwise we can just get it from the value that has been defined
else {
value = this._value;
}
// If label is undefined then just assume the same value as `value`
if (this._label === undefined) {
label = value;
}
// Otherwise work it out from what has been defined
else {
label = this._label;
}
// If the table has not been defined then get it from the editor instance
table = this._table !== undefined ?
this._table :
editor.readTable().length > 0 ?
editor.readTable()[0] :
editor.table()[0];
if (leftJoinIn !== undefined && leftJoinIn !== null && this._leftJoin === undefined) {
join = leftJoinIn;
}
let db = editor.db();
// Create a list of the fields that we need to get from the db
// let fields = [ value ].concat(label);
// We need a default formatter if one isn't provided
if (! formatter) {
formatter = function(str) {
return str;
};
}
// This query will get the count's according to any selections made in the SearchBuilders
let query = db
.select(label + ' as label', value + ' as value')
.from(table)
.distinct()
.groupBy(value);
// If a left join needs to be done for the above queries we can just do it in the same place
if (join !== null && join !== undefined) {
for (let joiner of join) {
if(joiner.fn) {
query.leftJoin(joiner.table, joiner.fn as any);
}
else {
query.leftJoin(joiner.table, function() {
this.on(joiner.field1, joiner.operator, joiner.field2);
});
}
}
}
let res = await query;
let out = [];
// Create the output array and add the values of count, label, total and value for each unique entry
for (let recordCou of res) {
out.push({
value: recordCou.value,
label: recordCou.label,
})
}
// Only sort if there was no SQL order field
if (! this._order) {
out.sort(function(a, b) {
if (isNumeric(a) && isNumeric(b)) {
return (a.label * 1) - (b.label * 1);
}
return a.label < b.label ?
-1 : a.label > b.label ?
1 :
0;
});
}
return out;
}
}