tsense 0.0.1 → 0.0.3

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 CHANGED
@@ -4,7 +4,7 @@ Opinionated, fully-typed typesense client
4
4
 
5
5
  # TO-DO
6
6
  - [ ] Remove base typesense dependency
7
- - [ ] Better filter support
7
+ - [ ] Better filter support (includes, exact, etc...)
8
8
  - [ ] Documentation
9
9
  - [ ] Improve tests
10
10
  - [ ] Facet
@@ -72,7 +72,9 @@ const results = await UsersCollection.searchDocuments({
72
72
  search: "john",
73
73
  search_keys: ["name"],
74
74
  // can sort multiple fields
75
- order_by: ["age desc", "id asc"],
75
+ order_by: ["age desc", "name asc"],
76
+ // compiles into
77
+ // age:>=20&&((email:=@google.com)||(email:=@netflix.com))
76
78
  filter: {
77
79
  // min and max range on numbers
78
80
  age: {
@@ -115,7 +115,12 @@ export class TSense {
115
115
  }
116
116
  create() {
117
117
  return __awaiter(this, void 0, void 0, function* () {
118
- const fields = [];
118
+ const fields = [
119
+ {
120
+ name: "id",
121
+ type: "int32",
122
+ },
123
+ ];
119
124
  for (const [name, field] of Object.entries(this.data.fields)) {
120
125
  const isSimpleField = typeof field === "string";
121
126
  if (isSimpleField) {
package/dist/index.d.ts CHANGED
@@ -1 +1 @@
1
- export { TSense } from "./collection.js";
1
+ export { TSense } from "./tsense.js";
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- export { TSense } from "./collection.js";
1
+ export { TSense } from "./tsense.js";
@@ -0,0 +1,27 @@
1
+ import type { Client } from "typesense";
2
+ import type { CustomCollectionField, Filters, InferCollectionTypes } from "./types/core.js";
3
+ export declare class TSense<Options extends CustomCollectionField, Fields extends Record<string, Options>, Inferred = InferCollectionTypes<Fields>> {
4
+ private name;
5
+ private data;
6
+ infer: Inferred;
7
+ constructor(name: string, data: {
8
+ fields: Fields;
9
+ client: Client;
10
+ default_search_field?: NoInfer<keyof Inferred>;
11
+ default_sorting_field?: NoInfer<keyof Inferred>;
12
+ batch_size?: number;
13
+ enable_nested_fields?: boolean;
14
+ });
15
+ private checkNested;
16
+ private buildObjectFilter;
17
+ private buildSort;
18
+ private buildFilter;
19
+ private maybeArray;
20
+ delete(): Promise<void>;
21
+ create(): Promise<this>;
22
+ searchDocuments(data: Filters<Inferred>): Promise<{
23
+ count: number;
24
+ data: Inferred[];
25
+ }>;
26
+ upsertDocuments(items: Inferred | Inferred[]): Promise<any[] | undefined>;
27
+ }
package/dist/tsense.js ADDED
@@ -0,0 +1,201 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ const requiresNested = ["object", "object[]"];
11
+ export class TSense {
12
+ constructor(name, data) {
13
+ this.name = name;
14
+ this.data = data;
15
+ this.infer = undefined;
16
+ }
17
+ checkNested(fields) {
18
+ if (this.data.enable_nested_fields) {
19
+ return;
20
+ }
21
+ for (const field of fields) {
22
+ requiresNested.includes(field.type);
23
+ this.data.enable_nested_fields = true;
24
+ return;
25
+ }
26
+ }
27
+ buildObjectFilter(key, value) {
28
+ if (Array.isArray(value)) {
29
+ return `(${key}:[${value.join(",")}])`;
30
+ }
31
+ if ("not" in value) {
32
+ return `${key}:!=${value.not}`;
33
+ }
34
+ const min = value.min != null ? value.min : undefined;
35
+ const max = value.max != null ? value.max : undefined;
36
+ if (min != null && max != null) {
37
+ return `${key}:[${min}..${max}]`;
38
+ }
39
+ if (max != null) {
40
+ return `${key}:<=${max}`;
41
+ }
42
+ if (min != null) {
43
+ return `${key}:>=${min}`;
44
+ }
45
+ }
46
+ buildSort(data) {
47
+ var _a;
48
+ if (!data.order_by) {
49
+ return;
50
+ }
51
+ const order = [];
52
+ for (const key of this.maybeArray(data.order_by)) {
53
+ const splitted = key.split(" ");
54
+ let direction = splitted.at(-1);
55
+ if (direction !== "asc" && direction !== "desc") {
56
+ direction = (_a = data.direction) !== null && _a !== void 0 ? _a : "desc";
57
+ }
58
+ // safeguard
59
+ if (splitted[0] === "undefined") {
60
+ continue;
61
+ }
62
+ if (splitted[0] === "score") {
63
+ splitted[0] = "_text_match";
64
+ }
65
+ order.push(`${splitted[0]}:${direction}`);
66
+ }
67
+ return order.join(",");
68
+ }
69
+ buildFilter(data) {
70
+ var _a;
71
+ const filter = [];
72
+ for (const entry of Object.entries((_a = data.filter) !== null && _a !== void 0 ? _a : {})) {
73
+ const [key, value] = entry;
74
+ if (value == null)
75
+ continue;
76
+ if (key === "OR") {
77
+ const orFilter = [];
78
+ for (const condition of value) {
79
+ const filter = this.buildFilter({ filter: condition });
80
+ orFilter.push(`(${filter.join("||")})`);
81
+ }
82
+ filter.push(`(${orFilter.join("||")})`);
83
+ continue;
84
+ }
85
+ switch (typeof value) {
86
+ case "string":
87
+ case "number":
88
+ case "boolean":
89
+ filter.push(`${key}:=${value}`);
90
+ break;
91
+ case "object": {
92
+ const built = this.buildObjectFilter(key, value);
93
+ if (built) {
94
+ filter.push(built);
95
+ }
96
+ break;
97
+ }
98
+ default: {
99
+ break;
100
+ }
101
+ }
102
+ }
103
+ return filter;
104
+ }
105
+ maybeArray(d) {
106
+ if (Array.isArray(d)) {
107
+ return d;
108
+ }
109
+ return [d];
110
+ }
111
+ delete() {
112
+ return __awaiter(this, void 0, void 0, function* () {
113
+ yield this.data.client.collections(this.name).delete();
114
+ });
115
+ }
116
+ create() {
117
+ return __awaiter(this, void 0, void 0, function* () {
118
+ const fields = [];
119
+ for (const [name, field] of Object.entries(this.data.fields)) {
120
+ const isSimpleField = typeof field === "string";
121
+ if (isSimpleField) {
122
+ const isOptional = field[field.length - 1] === "?";
123
+ const realType = isOptional ? field.slice(0, -1) : field;
124
+ fields.push({
125
+ name,
126
+ type: realType,
127
+ optional: isOptional,
128
+ });
129
+ continue;
130
+ }
131
+ fields.push({
132
+ name,
133
+ type: field.type,
134
+ optional: field.optional,
135
+ facet: field.facet,
136
+ index: field.index,
137
+ sort: field.sort,
138
+ });
139
+ }
140
+ this.checkNested(fields);
141
+ yield this.data.client.collections().create({
142
+ name: this.name,
143
+ fields,
144
+ default_sorting_field: this.data.default_sorting_field,
145
+ enable_nested_fields: this.data.enable_nested_fields,
146
+ });
147
+ return this;
148
+ });
149
+ }
150
+ searchDocuments(data) {
151
+ return __awaiter(this, void 0, void 0, function* () {
152
+ var _a, _b, _c;
153
+ const res = yield this.data.client
154
+ .collections(this.name)
155
+ .documents()
156
+ .search({
157
+ q: (_a = data.search) !== null && _a !== void 0 ? _a : "",
158
+ query_by: (_b = data.search_keys) !== null && _b !== void 0 ? _b : [this.data.default_search_field],
159
+ sort_by: this.buildSort(data),
160
+ filter_by: this.buildFilter(data).join("&&"),
161
+ page: data.page,
162
+ limit: data.limit,
163
+ });
164
+ const result = [];
165
+ for (const hit of (_c = res.hits) !== null && _c !== void 0 ? _c : []) {
166
+ if (data.highlight) {
167
+ for (const [key, value] of Object.entries(hit.highlight)) {
168
+ if (value) {
169
+ // @ts-expect-error
170
+ hit.document[key] = value.snippet;
171
+ }
172
+ }
173
+ }
174
+ result.push(hit.document);
175
+ }
176
+ return {
177
+ data: result,
178
+ count: res.found,
179
+ };
180
+ });
181
+ }
182
+ upsertDocuments(items) {
183
+ return __awaiter(this, void 0, void 0, function* () {
184
+ const parsed = [];
185
+ for (const item of this.maybeArray(items)) {
186
+ parsed.push(JSON.stringify(item));
187
+ }
188
+ if (!parsed.length) {
189
+ return;
190
+ }
191
+ const res = yield this.data.client
192
+ .collections(this.name)
193
+ .documents()
194
+ .import(parsed.join("\n"), {
195
+ action: "upsert",
196
+ batch_size: this.data.batch_size,
197
+ });
198
+ return res.split("\n").map((v) => JSON.parse(v));
199
+ });
200
+ }
201
+ }
package/package.json CHANGED
@@ -1,11 +1,19 @@
1
1
  {
2
2
  "name": "tsense",
3
- "version": "0.0.1",
3
+ "version": "0.0.3",
4
4
  "main": "dist/index.js",
5
+ "types": "dist/index.d.ts",
6
+ "exports": {
7
+ ".": {
8
+ "types": "./dist/index.d.ts",
9
+ "import": "./dist/index.js"
10
+ }
11
+ },
5
12
  "files": [
6
13
  "dist"
7
14
  ],
8
- "description": "Opinionated, fully typed typesense client, inspired by prisma syntax",
15
+ "sideEffects": false,
16
+ "description": "Opinionated, fully typed typesense client",
9
17
  "license": "MIT",
10
18
  "keywords": [
11
19
  "typesense"