tsense 0.0.13 → 0.0.14

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/dist/tsense.d.ts CHANGED
@@ -65,6 +65,7 @@ export declare class TSense<T extends Type> {
65
65
  private fieldTransformers;
66
66
  infer: T["infer"];
67
67
  constructor(options: TsenseOptions<T>);
68
+ private getBaseType;
68
69
  private inferType;
69
70
  private serializeDoc;
70
71
  private deserializeDoc;
@@ -84,6 +85,7 @@ export declare class TSense<T extends Type> {
84
85
  updateMany(filter: FilterFor<T["infer"]>, data: Partial<T["infer"]>): Promise<UpdateResult>;
85
86
  search(options: SearchOptions<T["infer"]>): Promise<SearchResult<T["infer"]>>;
86
87
  searchList(options: SearchListOptions<T["infer"]>): Promise<SearchListResult<T["infer"]>>;
88
+ count(filter?: FilterFor<T["infer"]>): Promise<number>;
87
89
  upsert(docs: T["infer"] | T["infer"][]): Promise<UpsertResult[]>;
88
90
  }
89
91
  export {};
package/dist/tsense.js CHANGED
@@ -35,16 +35,21 @@ export class TSense {
35
35
  });
36
36
  this.extractFields((_a = options.transformers) !== null && _a !== void 0 ? _a : defaultTransformers);
37
37
  }
38
+ getBaseType(expression, domain) {
39
+ if (domain && domain !== "undefined")
40
+ return domain;
41
+ return expression.replace(/ \| undefined$/, "");
42
+ }
38
43
  inferType(arkType) {
39
44
  const direct = arkToTsense[arkType];
40
45
  if (direct)
41
46
  return direct;
42
47
  if (arkType.includes("[]"))
43
48
  return "object[]";
49
+ if (arkType.includes("'") || arkType.includes('"'))
50
+ return "string";
44
51
  if (arkType.includes("{") || arkType.includes("|"))
45
52
  return "object";
46
- if (arkType.startsWith("'") || arkType.includes("'"))
47
- return "string";
48
53
  return "string";
49
54
  }
50
55
  serializeDoc(doc) {
@@ -95,7 +100,8 @@ export class TSense {
95
100
  const meta = prop.value.meta;
96
101
  const expression = String(prop.value.expression);
97
102
  const domain = prop.value.domain;
98
- const transformer = transformers.find((t) => t.match(expression, domain));
103
+ const baseType = this.getBaseType(expression, domain);
104
+ const transformer = transformers.find((t) => t.match(baseType, domain));
99
105
  if (transformer) {
100
106
  this.fieldTransformers.set(prop.key, transformer);
101
107
  this.fields.push({
@@ -108,7 +114,7 @@ export class TSense {
108
114
  });
109
115
  continue;
110
116
  }
111
- const type = (_a = meta === null || meta === void 0 ? void 0 : meta.type) !== null && _a !== void 0 ? _a : this.inferType(domain !== null && domain !== void 0 ? domain : expression);
117
+ const type = (_a = meta === null || meta === void 0 ? void 0 : meta.type) !== null && _a !== void 0 ? _a : this.inferType(baseType);
112
118
  this.fields.push({
113
119
  name: prop.key,
114
120
  type,
@@ -218,6 +224,7 @@ export class TSense {
218
224
  }
219
225
  drop() {
220
226
  return __awaiter(this, void 0, void 0, function* () {
227
+ console.log("dropping");
221
228
  yield this.axios({
222
229
  method: "DELETE",
223
230
  url: `/collections/${this.options.name}`,
@@ -302,7 +309,9 @@ export class TSense {
302
309
  yield this.ensureSynced();
303
310
  const params = {
304
311
  q: (_a = options.query) !== null && _a !== void 0 ? _a : "",
305
- query_by: ((_b = options.queryBy) !== null && _b !== void 0 ? _b : [this.options.defaultSearchField]).join(","),
312
+ query_by: ((_b = options.queryBy) !== null && _b !== void 0 ? _b : [
313
+ this.options.defaultSearchField,
314
+ ]).join(","),
306
315
  };
307
316
  const sortBy = this.buildSort(options);
308
317
  if (sortBy)
@@ -385,7 +394,9 @@ export class TSense {
385
394
  const page = options.cursor ? Number(options.cursor) : 1;
386
395
  const params = {
387
396
  q: (_b = options.query) !== null && _b !== void 0 ? _b : "",
388
- query_by: ((_c = options.queryBy) !== null && _c !== void 0 ? _c : [this.options.defaultSearchField]).join(","),
397
+ query_by: ((_c = options.queryBy) !== null && _c !== void 0 ? _c : [
398
+ this.options.defaultSearchField,
399
+ ]).join(","),
389
400
  per_page: limit,
390
401
  page,
391
402
  sort_by: `${field}:${options.sort.direction}`,
@@ -410,6 +421,30 @@ export class TSense {
410
421
  return { data, nextCursor, total: res.found };
411
422
  });
412
423
  }
424
+ count(filter) {
425
+ return __awaiter(this, void 0, void 0, function* () {
426
+ yield this.ensureSynced();
427
+ const filterBy = this.buildFilter(filter).join("&&");
428
+ if (!filterBy) {
429
+ const { data } = yield this.axios({
430
+ method: "GET",
431
+ url: `/collections/${this.options.name}`,
432
+ });
433
+ return data.num_documents;
434
+ }
435
+ const { data } = yield this.axios({
436
+ method: "GET",
437
+ url: `/collections/${this.options.name}/documents/search`,
438
+ params: {
439
+ q: "*",
440
+ query_by: this.options.defaultSearchField,
441
+ per_page: 0,
442
+ filter_by: filterBy,
443
+ },
444
+ });
445
+ return data.found;
446
+ });
447
+ }
413
448
  upsert(docs) {
414
449
  return __awaiter(this, void 0, void 0, function* () {
415
450
  yield this.ensureSynced();
@@ -421,7 +456,9 @@ export class TSense {
421
456
  this.options.schema.assert(item);
422
457
  }
423
458
  }
424
- const payload = items.map((item) => JSON.stringify(this.serializeDoc(item))).join("\n");
459
+ const payload = items
460
+ .map((item) => JSON.stringify(this.serializeDoc(item)))
461
+ .join("\n");
425
462
  const params = { action: "upsert" };
426
463
  if (this.options.batchSize) {
427
464
  params.batch_size = this.options.batchSize;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tsense",
3
- "version": "0.0.13",
3
+ "version": "0.0.14",
4
4
  "private": false,
5
5
  "description": "Opinionated, fully typed typesense client",
6
6
  "keywords": [
@@ -42,11 +42,11 @@
42
42
  "@changesets/cli": "^2.29.7",
43
43
  "@types/bun": "latest",
44
44
  "arktype": "^2.1.29",
45
- "oxfmt": "^0.23.0",
46
- "oxlint": "^1.38.0"
45
+ "oxfmt": "^0.27.0",
46
+ "oxlint": "^1.42.0"
47
47
  },
48
48
  "peerDependencies": {
49
49
  "arktype": "^2.1.29",
50
50
  "typescript": "^5"
51
51
  }
52
- }
52
+ }