tsense 0.0.3 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +31 -0
- package/dist/tsense.d.ts +8 -1
- package/dist/tsense.js +17 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -38,6 +38,12 @@ export const UsersCollection = new TSense("users", {
|
|
|
38
38
|
type: "string",
|
|
39
39
|
sort: true,
|
|
40
40
|
},
|
|
41
|
+
company: {
|
|
42
|
+
type: "string",
|
|
43
|
+
override: {} as "netflix" | "google",
|
|
44
|
+
facet: true,
|
|
45
|
+
optional: true,
|
|
46
|
+
},
|
|
41
47
|
work_history: {
|
|
42
48
|
// object and object[] auto-infers enable_nested_fields
|
|
43
49
|
type: "object[]",
|
|
@@ -91,4 +97,29 @@ const results = await UsersCollection.searchDocuments({
|
|
|
91
97
|
],
|
|
92
98
|
},
|
|
93
99
|
});
|
|
100
|
+
|
|
101
|
+
/*
|
|
102
|
+
typed as
|
|
103
|
+
count: number;
|
|
104
|
+
data: {
|
|
105
|
+
id?: string | undefined;
|
|
106
|
+
phone?: string | null | undefined;
|
|
107
|
+
...
|
|
108
|
+
}[];
|
|
109
|
+
facet: {
|
|
110
|
+
netflix: number;
|
|
111
|
+
google: number;
|
|
112
|
+
// enabled by enable_facet_total
|
|
113
|
+
total: number;
|
|
114
|
+
};
|
|
115
|
+
*/
|
|
116
|
+
const faceted = await UsersCollection.searchDocuments(
|
|
117
|
+
{
|
|
118
|
+
search: "john",
|
|
119
|
+
},
|
|
120
|
+
{
|
|
121
|
+
facet_by: "company",
|
|
122
|
+
enable_facet_total: true,
|
|
123
|
+
},
|
|
124
|
+
);
|
|
94
125
|
```
|
package/dist/tsense.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { Client } from "typesense";
|
|
2
2
|
import type { CustomCollectionField, Filters, InferCollectionTypes } from "./types/core.js";
|
|
3
|
+
import type { Simplify } from "./types/helpers.js";
|
|
3
4
|
export declare class TSense<Options extends CustomCollectionField, Fields extends Record<string, Options>, Inferred = InferCollectionTypes<Fields>> {
|
|
4
5
|
private name;
|
|
5
6
|
private data;
|
|
@@ -19,9 +20,15 @@ export declare class TSense<Options extends CustomCollectionField, Fields extend
|
|
|
19
20
|
private maybeArray;
|
|
20
21
|
delete(): Promise<void>;
|
|
21
22
|
create(): Promise<this>;
|
|
22
|
-
searchDocuments(data: Filters<Inferred
|
|
23
|
+
searchDocuments<FacetBy extends keyof Inferred, EnableFacetTotal extends boolean | undefined = undefined>(data: Filters<Inferred>, facet?: {
|
|
24
|
+
facet_by?: FacetBy;
|
|
25
|
+
enable_facet_total?: EnableFacetTotal;
|
|
26
|
+
}): Promise<{
|
|
23
27
|
count: number;
|
|
24
28
|
data: Inferred[];
|
|
29
|
+
facet: FacetBy extends keyof Inferred ? Simplify<Record<NonNullable<Inferred[FacetBy]> extends string ? NonNullable<Inferred[FacetBy]> : never, number> & (EnableFacetTotal extends true ? {
|
|
30
|
+
total: number;
|
|
31
|
+
} : {})> : never;
|
|
25
32
|
}>;
|
|
26
33
|
upsertDocuments(items: Inferred | Inferred[]): Promise<any[] | undefined>;
|
|
27
34
|
}
|
package/dist/tsense.js
CHANGED
|
@@ -147,9 +147,9 @@ export class TSense {
|
|
|
147
147
|
return this;
|
|
148
148
|
});
|
|
149
149
|
}
|
|
150
|
-
searchDocuments(data) {
|
|
150
|
+
searchDocuments(data, facet) {
|
|
151
151
|
return __awaiter(this, void 0, void 0, function* () {
|
|
152
|
-
var _a, _b, _c;
|
|
152
|
+
var _a, _b, _c, _d, _e;
|
|
153
153
|
const res = yield this.data.client
|
|
154
154
|
.collections(this.name)
|
|
155
155
|
.documents()
|
|
@@ -160,9 +160,22 @@ export class TSense {
|
|
|
160
160
|
filter_by: this.buildFilter(data).join("&&"),
|
|
161
161
|
page: data.page,
|
|
162
162
|
limit: data.limit,
|
|
163
|
+
facet_by: facet === null || facet === void 0 ? void 0 : facet.facet_by,
|
|
163
164
|
});
|
|
165
|
+
const facet_result = (facet === null || facet === void 0 ? void 0 : facet.facet_by)
|
|
166
|
+
? facet.enable_facet_total
|
|
167
|
+
? { total: 0 }
|
|
168
|
+
: {}
|
|
169
|
+
: undefined;
|
|
170
|
+
if ((_d = (_c = res.facet_counts) === null || _c === void 0 ? void 0 : _c[0]) === null || _d === void 0 ? void 0 : _d.counts) {
|
|
171
|
+
for (const iter of res.facet_counts[0].counts) {
|
|
172
|
+
facet_result[iter.value] = iter.count;
|
|
173
|
+
if (facet === null || facet === void 0 ? void 0 : facet.enable_facet_total)
|
|
174
|
+
facet_result.total += iter.count;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
164
177
|
const result = [];
|
|
165
|
-
for (const hit of (
|
|
178
|
+
for (const hit of (_e = res.hits) !== null && _e !== void 0 ? _e : []) {
|
|
166
179
|
if (data.highlight) {
|
|
167
180
|
for (const [key, value] of Object.entries(hit.highlight)) {
|
|
168
181
|
if (value) {
|
|
@@ -176,6 +189,7 @@ export class TSense {
|
|
|
176
189
|
return {
|
|
177
190
|
data: result,
|
|
178
191
|
count: res.found,
|
|
192
|
+
facet: facet_result,
|
|
179
193
|
};
|
|
180
194
|
});
|
|
181
195
|
}
|