@tinacms/search 1.0.2 → 1.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.
@@ -1,7 +1,21 @@
1
1
  import type { SearchClient } from '../types';
2
+ import { MemoryLevel } from 'memory-level';
3
+ declare type TinaSearchIndexerClientOptions = {
4
+ stopwordLanguages?: string[];
5
+ tokenSplitRegex?: string;
6
+ };
7
+ declare type TinaCloudSearchIndexerClientOptions = {
8
+ apiUrl: string;
9
+ branch: string;
10
+ indexerToken: string;
11
+ } & TinaSearchIndexerClientOptions;
2
12
  export declare class LocalSearchIndexClient implements SearchClient {
3
- protected searchIndex: any;
4
- constructor(searchIndex: any);
13
+ searchIndex: any;
14
+ protected readonly memoryLevel: MemoryLevel;
15
+ private readonly stopwords;
16
+ private readonly tokenSplitRegex;
17
+ constructor(options: TinaSearchIndexerClientOptions);
18
+ onStartIndexing(): Promise<void>;
5
19
  put(docs: any[]): Promise<any>;
6
20
  del(ids: string[]): Promise<any>;
7
21
  query(query: string, options: {
@@ -13,34 +27,13 @@ export declare class LocalSearchIndexClient implements SearchClient {
13
27
  nextCursor: string | null;
14
28
  prevCursor: string | null;
15
29
  }>;
30
+ export(filename: string): Promise<void>;
16
31
  }
17
- declare type TinaCloudSearchIndexerClientOptions = {
18
- apiUrl: string;
19
- branch: string;
20
- indexerToken: string;
21
- stopwordLanguages?: string[];
22
- };
23
- export declare class TinaCMSSearchIndexClient implements SearchClient {
24
- private memoryLevel;
25
- private searchIndex;
32
+ export declare class TinaCMSSearchIndexClient extends LocalSearchIndexClient {
26
33
  private readonly apiUrl;
27
34
  private readonly branch;
28
35
  private readonly indexerToken;
29
- private readonly stopwordLanguages;
30
36
  constructor(options: TinaCloudSearchIndexerClientOptions);
31
- put(docs: any[]): Promise<void>;
32
- del(ids: string[]): Promise<void>;
33
- onStartIndexing(): Promise<void>;
34
37
  onFinishIndexing(): Promise<void>;
35
- getSearchIndex(): any;
36
- query(query: string, options: {
37
- cursor?: string;
38
- limit?: number;
39
- } | undefined): Promise<{
40
- results: any[];
41
- total: number;
42
- nextCursor: string | null;
43
- prevCursor: string | null;
44
- }>;
45
38
  }
46
39
  export {};
@@ -31,7 +31,10 @@ const extractText = (data, acc, indexableNodeTypes) => {
31
31
  }
32
32
  }
33
33
  }
34
- (_b = (_a = data.children) == null ? void 0 : _a.forEach) == null ? void 0 : _b.call(_a, (child) => extractText(child, acc, indexableNodeTypes));
34
+ (_b = (_a = data.children) == null ? void 0 : _a.forEach) == null ? void 0 : _b.call(
35
+ _a,
36
+ (child) => extractText(child, acc, indexableNodeTypes)
37
+ );
35
38
  }
36
39
  };
37
40
  const relativePath = (path, collection) => {
@@ -65,16 +68,35 @@ const processDocumentForIndexing = (data, path, collection, textIndexLength, fie
65
68
  if (data[f.name]) {
66
69
  if (f.type === "object") {
67
70
  if (isList) {
68
- data[f.name] = data[f.name].map((obj) => processDocumentForIndexing(obj, path, collection, textIndexLength, f));
71
+ data[f.name] = data[f.name].map(
72
+ (obj) => processDocumentForIndexing(
73
+ obj,
74
+ path,
75
+ collection,
76
+ textIndexLength,
77
+ f
78
+ )
79
+ );
69
80
  } else {
70
- data[f.name] = processDocumentForIndexing(data[f.name], path, collection, textIndexLength, f);
81
+ data[f.name] = processDocumentForIndexing(
82
+ data[f.name],
83
+ path,
84
+ collection,
85
+ textIndexLength,
86
+ f
87
+ );
71
88
  }
72
89
  } else if (f.type === "string") {
73
90
  const fieldTextIndexLength = f.maxSearchIndexFieldLength || textIndexLength;
74
91
  if (isList) {
75
- data[f.name] = data[f.name].map((value) => processTextFieldValue(value, fieldTextIndexLength));
92
+ data[f.name] = data[f.name].map(
93
+ (value) => processTextFieldValue(value, fieldTextIndexLength)
94
+ );
76
95
  } else {
77
- data[f.name] = processTextFieldValue(data[f.name], fieldTextIndexLength);
96
+ data[f.name] = processTextFieldValue(
97
+ data[f.name],
98
+ fieldTextIndexLength
99
+ );
78
100
  }
79
101
  } else if (f.type === "rich-text") {
80
102
  const fieldTextIndexLength = f.maxSearchIndexFieldLength || textIndexLength;
@@ -144,4 +166,9 @@ const parseSearchIndexResponse = (data, options) => {
144
166
  };
145
167
  }
146
168
  };
147
- export { optionsToSearchIndexOptions, parseSearchIndexResponse, processDocumentForIndexing, queryToSearchIndexQuery };
169
+ export {
170
+ optionsToSearchIndexOptions,
171
+ parseSearchIndexResponse,
172
+ processDocumentForIndexing,
173
+ queryToSearchIndexQuery
174
+ };
@@ -35,7 +35,10 @@
35
35
  }
36
36
  }
37
37
  }
38
- (_b = (_a = data.children) == null ? void 0 : _a.forEach) == null ? void 0 : _b.call(_a, (child) => extractText(child, acc, indexableNodeTypes));
38
+ (_b = (_a = data.children) == null ? void 0 : _a.forEach) == null ? void 0 : _b.call(
39
+ _a,
40
+ (child) => extractText(child, acc, indexableNodeTypes)
41
+ );
39
42
  }
40
43
  };
41
44
  const relativePath = (path, collection) => {
@@ -69,16 +72,35 @@
69
72
  if (data[f.name]) {
70
73
  if (f.type === "object") {
71
74
  if (isList) {
72
- data[f.name] = data[f.name].map((obj) => processDocumentForIndexing(obj, path, collection, textIndexLength, f));
75
+ data[f.name] = data[f.name].map(
76
+ (obj) => processDocumentForIndexing(
77
+ obj,
78
+ path,
79
+ collection,
80
+ textIndexLength,
81
+ f
82
+ )
83
+ );
73
84
  } else {
74
- data[f.name] = processDocumentForIndexing(data[f.name], path, collection, textIndexLength, f);
85
+ data[f.name] = processDocumentForIndexing(
86
+ data[f.name],
87
+ path,
88
+ collection,
89
+ textIndexLength,
90
+ f
91
+ );
75
92
  }
76
93
  } else if (f.type === "string") {
77
94
  const fieldTextIndexLength = f.maxSearchIndexFieldLength || textIndexLength;
78
95
  if (isList) {
79
- data[f.name] = data[f.name].map((value) => processTextFieldValue(value, fieldTextIndexLength));
96
+ data[f.name] = data[f.name].map(
97
+ (value) => processTextFieldValue(value, fieldTextIndexLength)
98
+ );
80
99
  } else {
81
- data[f.name] = processTextFieldValue(data[f.name], fieldTextIndexLength);
100
+ data[f.name] = processTextFieldValue(
101
+ data[f.name],
102
+ fieldTextIndexLength
103
+ );
82
104
  }
83
105
  } else if (f.type === "rich-text") {
84
106
  const fieldTextIndexLength = f.maxSearchIndexFieldLength || textIndexLength;
@@ -152,5 +174,5 @@
152
174
  exports2.parseSearchIndexResponse = parseSearchIndexResponse;
153
175
  exports2.processDocumentForIndexing = processDocumentForIndexing;
154
176
  exports2.queryToSearchIndexQuery = queryToSearchIndexQuery;
155
- Object.defineProperties(exports2, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
177
+ Object.defineProperty(exports2, Symbol.toStringTag, { value: "Module" });
156
178
  });
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import si from '@tinacms/search-index';
1
+ import si from 'search-index';
2
2
  export { SearchIndexer } from './indexer';
3
3
  export { LocalSearchIndexClient, TinaCMSSearchIndexClient } from './client';
4
4
  export type { SearchClient } from './types';
package/dist/index.js CHANGED
@@ -34,7 +34,7 @@ __export(src_exports, {
34
34
  });
35
35
  module.exports = __toCommonJS(src_exports);
36
36
  var sw = __toESM(require("stopword"));
37
- var import_search_index2 = __toESM(require("@tinacms/search-index"));
37
+ var import_search_index2 = __toESM(require("search-index"));
38
38
 
39
39
  // src/indexer/index.ts
40
40
  var import_graphql = require("@tinacms/graphql");
@@ -253,17 +253,33 @@ var SearchIndexer = class {
253
253
  // src/client/index.ts
254
254
  var import_sqlite_level = require("sqlite-level");
255
255
  var zlib = __toESM(require("zlib"));
256
- var import_search_index = __toESM(require("@tinacms/search-index"));
256
+ var import_search_index = __toESM(require("search-index"));
257
257
  var import_memory_level = require("memory-level");
258
258
  var import_node_fetch = __toESM(require("node-fetch"));
259
+ var DEFAULT_TOKEN_SPLIT_REGEX = /[\p{L}\d_]+/gu;
259
260
  var LocalSearchIndexClient = class {
260
- constructor(searchIndex) {
261
- this.searchIndex = searchIndex;
261
+ constructor(options) {
262
+ this.memoryLevel = new import_memory_level.MemoryLevel();
263
+ this.stopwords = lookupStopwords(options.stopwordLanguages);
264
+ this.tokenSplitRegex = options.tokenSplitRegex ? new RegExp(options.tokenSplitRegex, "gu") : DEFAULT_TOKEN_SPLIT_REGEX;
265
+ }
266
+ async onStartIndexing() {
267
+ this.searchIndex = await (0, import_search_index.default)({
268
+ db: this.memoryLevel,
269
+ stopwords: this.stopwords,
270
+ tokenSplitRegex: this.tokenSplitRegex
271
+ });
262
272
  }
263
273
  async put(docs) {
274
+ if (!this.searchIndex) {
275
+ throw new Error("onStartIndexing must be called first");
276
+ }
264
277
  return this.searchIndex.PUT(docs);
265
278
  }
266
279
  async del(ids) {
280
+ if (!this.searchIndex) {
281
+ throw new Error("onStartIndexing must be called first");
282
+ }
267
283
  return this.searchIndex.DELETE(ids);
268
284
  }
269
285
  query(query, options) {
@@ -274,30 +290,21 @@ var LocalSearchIndexClient = class {
274
290
  total: 0
275
291
  });
276
292
  }
293
+ async export(filename) {
294
+ const sqliteLevel = new import_sqlite_level.SqliteLevel({ filename });
295
+ const iterator = this.memoryLevel.iterator();
296
+ for await (const [key, value] of iterator) {
297
+ await sqliteLevel.put(key, value);
298
+ }
299
+ await sqliteLevel.close();
300
+ }
277
301
  };
278
- var TinaCMSSearchIndexClient = class {
302
+ var TinaCMSSearchIndexClient = class extends LocalSearchIndexClient {
279
303
  constructor(options) {
304
+ super(options);
280
305
  this.apiUrl = options.apiUrl;
281
306
  this.branch = options.branch;
282
307
  this.indexerToken = options.indexerToken;
283
- this.stopwordLanguages = options.stopwordLanguages;
284
- }
285
- async put(docs) {
286
- if (this.searchIndex) {
287
- await this.searchIndex.PUT(docs);
288
- }
289
- }
290
- async del(ids) {
291
- if (this.searchIndex) {
292
- await this.searchIndex.DELETE(ids);
293
- }
294
- }
295
- async onStartIndexing() {
296
- this.memoryLevel = new import_memory_level.MemoryLevel();
297
- this.searchIndex = await (0, import_search_index.default)({
298
- db: this.memoryLevel,
299
- stopwords: lookupStopwords(this.stopwordLanguages)
300
- });
301
308
  }
302
309
  async onFinishIndexing() {
303
310
  const headers = new import_node_fetch.Headers();
@@ -337,17 +344,6 @@ ${await res.text()}`
337
344
  );
338
345
  }
339
346
  }
340
- getSearchIndex() {
341
- return this.searchIndex;
342
- }
343
- query(query, options) {
344
- return Promise.resolve({
345
- nextCursor: void 0,
346
- prevCursor: void 0,
347
- results: [],
348
- total: 0
349
- });
350
- }
351
347
  };
352
348
 
353
349
  // src/index.ts
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tinacms/search",
3
- "version": "1.0.2",
3
+ "version": "1.0.4",
4
4
  "main": "dist/index.js",
5
5
  "module": "dist/index-client.es.js",
6
6
  "typings": "dist/index.d.ts",
@@ -29,13 +29,13 @@
29
29
  ]
30
30
  },
31
31
  "dependencies": {
32
- "@tinacms/graphql": "1.4.17",
33
- "@tinacms/schema-tools": "1.4.6",
32
+ "@tinacms/graphql": "1.4.19",
33
+ "@tinacms/schema-tools": "1.4.7",
34
34
  "abstract-level": "^1.0.3",
35
35
  "memory-level": "^1.0.0",
36
36
  "module-error": "^1.0.2",
37
37
  "node-fetch": "2",
38
- "@tinacms/search-index": "^3.5.2",
38
+ "search-index": "4.0.0",
39
39
  "stopword": "^2.0.8",
40
40
  "sqlite-level": "^1.0.1"
41
41
  },
@@ -51,10 +51,10 @@
51
51
  "@types/jest": "^26.0.4",
52
52
  "@types/micromatch": "^4.0.2",
53
53
  "@types/search-index": "^3.2.0",
54
- "jest": "27.0.6",
55
- "jest-diff": "27.0.6",
54
+ "jest": "^29.5.0",
55
+ "jest-diff": "^29.5.0",
56
56
  "jest-file-snapshot": "^0.5.0",
57
- "jest-matcher-utils": "27.0.6",
57
+ "jest-matcher-utils": "^29.5.0",
58
58
  "typescript": "4.3.5"
59
59
  },
60
60
  "scripts": {