-
-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathCollection.ts
More file actions
180 lines (163 loc) · 4.57 KB
/
Collection.ts
File metadata and controls
180 lines (163 loc) · 4.57 KB
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
import ApiCall from "./ApiCall";
import Collections from "./Collections";
import type {
BaseCollectionCreateSchema,
CollectionCreateSchema,
} from "./Collections";
import Documents, { DocumentSchema } from "./Documents";
import { ObjectNotFound } from "./Errors";
import Overrides from "./Overrides";
import Override from "./Override";
import Synonyms from "./Synonyms";
import Synonym from "./Synonym";
import { Document } from "./Document";
export type FieldType =
| "string"
| "int32"
| "int64"
| "float"
| "bool"
| "geopoint"
| "geopolygon"
| "geopoint[]"
| "string[]"
| "int32[]"
| "int64[]"
| "float[]"
| "bool[]"
| "object"
| "object[]"
| "auto"
| "string*"
| "image";
export interface CollectionFieldSchema
extends Partial<
Pick<BaseCollectionCreateSchema, "token_separators" | "symbols_to_index">
> {
name: string;
type: FieldType;
optional?: boolean;
facet?: boolean;
index?: boolean;
sort?: boolean;
locale?: string;
infix?: boolean;
stem?: boolean;
num_dim?: number;
store?: boolean;
range_index?: boolean;
[t: string]: unknown;
}
export interface CollectionSchema extends Required<CollectionCreateSchema> {
created_at: number;
num_documents: number;
num_memory_shards: number;
}
export interface CollectionDropFieldSchema {
name: string;
drop: true;
}
export interface CollectionUpdateSchema
extends Partial<Omit<CollectionCreateSchema, "name" | "fields">> {
fields?: (CollectionFieldSchema | CollectionDropFieldSchema)[];
synonym_sets?: string[];
curation_sets?: string[];
}
export interface CollectionDeleteOptions {
compact_store?: boolean;
}
export default class Collection<T extends DocumentSchema = object> {
private readonly _documents: Documents<T>;
private individualDocuments: Record<string, Document<T>> = {};
private readonly _overrides: Overrides;
private individualOverrides: Record<string, Override> = {};
private readonly _synonyms: Synonyms;
private individualSynonyms: Record<string, Synonym> = {};
constructor(
private readonly name: string,
private readonly apiCall: ApiCall,
private readonly configuration: any,
) {
this.name = name;
this.apiCall = apiCall;
this.configuration = configuration;
this._documents = new Documents(
this.name,
this.apiCall,
this.configuration,
);
this._overrides = new Overrides(this.name, this.apiCall);
this._synonyms = new Synonyms(this.name, this.apiCall);
}
async retrieve(): Promise<CollectionSchema> {
return this.apiCall.get<CollectionSchema>(this.endpointPath());
}
async update(schema: CollectionUpdateSchema): Promise<CollectionSchema> {
return this.apiCall.patch<CollectionSchema>(this.endpointPath(), schema);
}
async delete(
options: CollectionDeleteOptions = {},
): Promise<CollectionSchema> {
return this.apiCall.delete<CollectionSchema>(this.endpointPath(), options);
}
async exists(): Promise<boolean> {
try {
await this.retrieve();
return true;
} catch (e) {
if (e instanceof ObjectNotFound) return false;
throw e;
}
}
documents(): Documents<T>;
documents(documentId: string): Document<T>;
documents(documentId?: string): Document<T> | Documents<T> {
if (!documentId) {
return this._documents;
} else {
if (this.individualDocuments[documentId] === undefined) {
this.individualDocuments[documentId] = new Document(
this.name,
documentId,
this.apiCall,
);
}
return this.individualDocuments[documentId];
}
}
overrides(): Overrides;
overrides(overrideId: string): Override;
overrides(overrideId?: string): Overrides | Override {
if (overrideId === undefined) {
return this._overrides;
} else {
if (this.individualOverrides[overrideId] === undefined) {
this.individualOverrides[overrideId] = new Override(
this.name,
overrideId,
this.apiCall,
);
}
return this.individualOverrides[overrideId];
}
}
synonyms(): Synonyms;
synonyms(synonymId: string): Synonym;
synonyms(synonymId?: string): Synonyms | Synonym {
if (synonymId === undefined) {
return this._synonyms;
} else {
if (this.individualSynonyms[synonymId] === undefined) {
this.individualSynonyms[synonymId] = new Synonym(
this.name,
synonymId,
this.apiCall,
);
}
return this.individualSynonyms[synonymId];
}
}
private endpointPath(): string {
return `${Collections.RESOURCEPATH}/${encodeURIComponent(this.name)}`;
}
}