/
Collection.ts
116 lines (108 loc) · 2.98 KB
/
Collection.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
import { IArcGISContext } from "../ArcGISContext";
import {
EntityType,
IHubCollection,
IHubSearchOptions,
IHubSearchResponse,
IHubSearchResult,
IQuery,
} from "./types";
import { hubSearch } from "./hubSearch";
import { cloneObject } from "../util";
/**
* Collection Class
*
* Abstracts searching a Collection
*
* For more information, check out the [Catalog & Collection Guide](/hub.js/guides/concepts/catalog-collection/)
*/
export class Collection implements IHubCollection {
private _context: IArcGISContext;
private _collection: IHubCollection;
private constructor(collection: IHubCollection, context: IArcGISContext) {
this._collection = collection;
this._context = context;
}
/**
* Create an instance of a Collection from a JSON object
* @param collection
* @param context
* @returns
*/
public static fromJson(
collection: IHubCollection,
context: IArcGISContext
): Collection {
return new Collection(collection, context);
}
/**
* Return the JSON object backing the instance
* @returns
*/
toJson(): IHubCollection {
return cloneObject(this._collection);
}
// Getters
public get label(): string {
return this._collection.label;
}
public get key(): string {
return this._collection.key;
}
public get include(): string[] {
return this._collection.include || [];
}
public get scope(): IQuery {
return this._collection.scope;
}
public get sortField(): string {
return this._collection.sortField || "title";
}
public get sortDirection(): "asc" | "desc" {
return this._collection.sortDirection || "asc";
}
public get targetEntity(): EntityType {
return this._collection.targetEntity;
}
/**
* Search the collection using a string or IQuery
* @param query
* @param options
* @returns
*/
public async search(
query: string | IQuery,
options: IHubSearchOptions = {}
): Promise<IHubSearchResponse<IHubSearchResult>> {
let qry: IQuery;
if (typeof query === "string") {
// construct a query from that...
qry = {
targetEntity: this._collection.targetEntity,
filters: [
{
predicates: [
{
term: query,
},
],
},
],
};
} else {
qry = query;
}
// TODO: What should happen when a Query is passed in that has a targetEntity that doesn't match the collection's targetEntity?
// merge the passed in query w/ the scope
qry.filters = [...qry.filters, ...this.scope.filters];
const opts = cloneObject(options);
opts.requestOptions = this._context.hubRequestOptions;
// inject default sort info if not specified
opts.sortField = options.sortField || this.sortField;
opts.sortOrder = options.sortOrder || this.sortDirection;
// inject default includes if not specified
opts.include = options.include || this.include;
// execute the search and return results
return hubSearch(qry, opts);
}
}