@lancedb/lancedb 0.7.1 → 0.9.0

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.
Files changed (92) hide show
  1. package/dist/arrow.d.ts +2 -3
  2. package/dist/arrow.js +2 -27
  3. package/dist/connection.d.ts +9 -0
  4. package/dist/connection.js +16 -2
  5. package/dist/embedding/embedding_function.d.ts +11 -0
  6. package/dist/embedding/embedding_function.js +28 -7
  7. package/dist/embedding/index.d.ts +2 -1
  8. package/dist/embedding/index.js +7 -6
  9. package/dist/embedding/openai.d.ts +1 -1
  10. package/dist/embedding/registry.d.ts +9 -7
  11. package/dist/embedding/registry.js +24 -6
  12. package/dist/embedding/transformers.d.ts +37 -0
  13. package/dist/embedding/transformers.js +147 -0
  14. package/dist/index.d.ts +1 -1
  15. package/dist/query.js +15 -9
  16. package/dist/remote/client.d.ts +1 -1
  17. package/dist/remote/client.js +6 -8
  18. package/dist/remote/connection.d.ts +2 -3
  19. package/dist/remote/connection.js +2 -2
  20. package/dist/sanitize.js +4 -2
  21. package/dist/table.d.ts +3 -0
  22. package/dist/table.js +1 -1
  23. package/package.json +17 -13
  24. package/Cargo.toml +0 -28
  25. package/biome.json +0 -158
  26. package/build.rs +0 -5
  27. package/dist/native.d.ts +0 -208
  28. package/examples/ann_indexes.ts +0 -49
  29. package/examples/basic.ts +0 -149
  30. package/examples/embedding.ts +0 -83
  31. package/examples/filtering.ts +0 -34
  32. package/examples/jsconfig.json +0 -27
  33. package/examples/package-lock.json +0 -79
  34. package/examples/package.json +0 -18
  35. package/examples/search.ts +0 -37
  36. package/jest.config.js +0 -7
  37. package/lancedb/arrow.ts +0 -947
  38. package/lancedb/connection.ts +0 -333
  39. package/lancedb/embedding/embedding_function.ts +0 -194
  40. package/lancedb/embedding/index.ts +0 -113
  41. package/lancedb/embedding/openai.ts +0 -113
  42. package/lancedb/embedding/registry.ts +0 -188
  43. package/lancedb/index.ts +0 -142
  44. package/lancedb/indices.ts +0 -203
  45. package/lancedb/merge.ts +0 -70
  46. package/lancedb/query.ts +0 -507
  47. package/lancedb/remote/client.ts +0 -221
  48. package/lancedb/remote/connection.ts +0 -201
  49. package/lancedb/remote/index.ts +0 -3
  50. package/lancedb/remote/table.ts +0 -226
  51. package/lancedb/sanitize.ts +0 -588
  52. package/lancedb/table.ts +0 -669
  53. package/lancedb/util.ts +0 -69
  54. package/native.d.ts +0 -208
  55. package/nodejs-artifacts/arrow.d.ts +0 -250
  56. package/nodejs-artifacts/arrow.js +0 -768
  57. package/nodejs-artifacts/connection.d.ts +0 -171
  58. package/nodejs-artifacts/connection.js +0 -135
  59. package/nodejs-artifacts/embedding/embedding_function.d.ts +0 -79
  60. package/nodejs-artifacts/embedding/embedding_function.js +0 -112
  61. package/nodejs-artifacts/embedding/index.d.ts +0 -28
  62. package/nodejs-artifacts/embedding/index.js +0 -114
  63. package/nodejs-artifacts/embedding/openai.d.ts +0 -18
  64. package/nodejs-artifacts/embedding/openai.js +0 -105
  65. package/nodejs-artifacts/embedding/registry.d.ts +0 -53
  66. package/nodejs-artifacts/embedding/registry.js +0 -127
  67. package/nodejs-artifacts/index.d.ts +0 -55
  68. package/nodejs-artifacts/index.js +0 -57
  69. package/nodejs-artifacts/indices.d.ts +0 -165
  70. package/nodejs-artifacts/indices.js +0 -71
  71. package/nodejs-artifacts/merge.d.ts +0 -54
  72. package/nodejs-artifacts/merge.js +0 -64
  73. package/nodejs-artifacts/native.d.ts +0 -208
  74. package/nodejs-artifacts/native.js +0 -330
  75. package/nodejs-artifacts/query.d.ts +0 -283
  76. package/nodejs-artifacts/query.js +0 -448
  77. package/nodejs-artifacts/remote/client.d.ts +0 -28
  78. package/nodejs-artifacts/remote/client.js +0 -172
  79. package/nodejs-artifacts/remote/connection.d.ts +0 -25
  80. package/nodejs-artifacts/remote/connection.js +0 -110
  81. package/nodejs-artifacts/remote/index.d.ts +0 -3
  82. package/nodejs-artifacts/remote/index.js +0 -9
  83. package/nodejs-artifacts/remote/table.d.ts +0 -42
  84. package/nodejs-artifacts/remote/table.js +0 -179
  85. package/nodejs-artifacts/sanitize.d.ts +0 -31
  86. package/nodejs-artifacts/sanitize.js +0 -436
  87. package/nodejs-artifacts/table.d.ts +0 -395
  88. package/nodejs-artifacts/table.js +0 -230
  89. package/nodejs-artifacts/util.d.ts +0 -14
  90. package/nodejs-artifacts/util.js +0 -65
  91. package/tsconfig.json +0 -25
  92. package/typedoc.json +0 -10
package/lancedb/table.ts DELETED
@@ -1,669 +0,0 @@
1
- // Copyright 2024 Lance Developers.
2
- //
3
- // Licensed under the Apache License, Version 2.0 (the "License");
4
- // you may not use this file except in compliance with the License.
5
- // You may obtain a copy of the License at
6
- //
7
- // http://www.apache.org/licenses/LICENSE-2.0
8
- //
9
- // Unless required by applicable law or agreed to in writing, software
10
- // distributed under the License is distributed on an "AS IS" BASIS,
11
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- // See the License for the specific language governing permissions and
13
- // limitations under the License.
14
-
15
- import {
16
- Table as ArrowTable,
17
- Data,
18
- IntoVector,
19
- Schema,
20
- TableLike,
21
- fromDataToBuffer,
22
- fromTableToBuffer,
23
- fromTableToStreamBuffer,
24
- isArrowTable,
25
- makeArrowTable,
26
- tableFromIPC,
27
- } from "./arrow";
28
- import { CreateTableOptions } from "./connection";
29
-
30
- import { EmbeddingFunctionConfig, getRegistry } from "./embedding/registry";
31
- import { IndexOptions } from "./indices";
32
- import { MergeInsertBuilder } from "./merge";
33
- import {
34
- AddColumnsSql,
35
- ColumnAlteration,
36
- IndexConfig,
37
- IndexStatistics,
38
- OptimizeStats,
39
- Table as _NativeTable,
40
- } from "./native";
41
- import { Query, VectorQuery } from "./query";
42
- import { sanitizeTable } from "./sanitize";
43
- import { IntoSql, toSQL } from "./util";
44
- export { IndexConfig } from "./native";
45
-
46
- /**
47
- * Options for adding data to a table.
48
- */
49
- export interface AddDataOptions {
50
- /**
51
- * If "append" (the default) then the new data will be added to the table
52
- *
53
- * If "overwrite" then the new data will replace the existing data in the table.
54
- */
55
- mode: "append" | "overwrite";
56
- }
57
-
58
- export interface UpdateOptions {
59
- /**
60
- * A filter that limits the scope of the update.
61
- *
62
- * This should be an SQL filter expression.
63
- *
64
- * Only rows that satisfy the expression will be updated.
65
- *
66
- * For example, this could be 'my_col == 0' to replace all instances
67
- * of 0 in a column with some other default value.
68
- */
69
- where: string;
70
- }
71
-
72
- export interface OptimizeOptions {
73
- /**
74
- * If set then all versions older than the given date
75
- * be removed. The current version will never be removed.
76
- * The default is 7 days
77
- * @example
78
- * // Delete all versions older than 1 day
79
- * const olderThan = new Date();
80
- * olderThan.setDate(olderThan.getDate() - 1));
81
- * tbl.cleanupOlderVersions(olderThan);
82
- *
83
- * // Delete all versions except the current version
84
- * tbl.cleanupOlderVersions(new Date());
85
- */
86
- cleanupOlderThan: Date;
87
- }
88
-
89
- /**
90
- * A Table is a collection of Records in a LanceDB Database.
91
- *
92
- * A Table object is expected to be long lived and reused for multiple operations.
93
- * Table objects will cache a certain amount of index data in memory. This cache
94
- * will be freed when the Table is garbage collected. To eagerly free the cache you
95
- * can call the `close` method. Once the Table is closed, it cannot be used for any
96
- * further operations.
97
- *
98
- * Closing a table is optional. It not closed, it will be closed when it is garbage
99
- * collected.
100
- */
101
- export abstract class Table {
102
- [Symbol.for("nodejs.util.inspect.custom")](): string {
103
- return this.display();
104
- }
105
- /** Returns the name of the table */
106
- abstract get name(): string;
107
-
108
- /** Return true if the table has not been closed */
109
- abstract isOpen(): boolean;
110
- /**
111
- * Close the table, releasing any underlying resources.
112
- *
113
- * It is safe to call this method multiple times.
114
- *
115
- * Any attempt to use the table after it is closed will result in an error.
116
- */
117
- abstract close(): void;
118
- /** Return a brief description of the table */
119
- abstract display(): string;
120
- /** Get the schema of the table. */
121
- abstract schema(): Promise<Schema>;
122
- /**
123
- * Insert records into this Table.
124
- * @param {Data} data Records to be inserted into the Table
125
- */
126
- abstract add(data: Data, options?: Partial<AddDataOptions>): Promise<void>;
127
- /**
128
- * Update existing records in the Table
129
- * @param opts.values The values to update. The keys are the column names and the values
130
- * are the values to set.
131
- * @example
132
- * ```ts
133
- * table.update({where:"x = 2", values:{"vector": [10, 10]}})
134
- * ```
135
- */
136
- abstract update(
137
- opts: {
138
- values: Map<string, IntoSql> | Record<string, IntoSql>;
139
- } & Partial<UpdateOptions>,
140
- ): Promise<void>;
141
- /**
142
- * Update existing records in the Table
143
- * @param opts.valuesSql The values to update. The keys are the column names and the values
144
- * are the values to set. The values are SQL expressions.
145
- * @example
146
- * ```ts
147
- * table.update({where:"x = 2", valuesSql:{"x": "x + 1"}})
148
- * ```
149
- */
150
- abstract update(
151
- opts: {
152
- valuesSql: Map<string, string> | Record<string, string>;
153
- } & Partial<UpdateOptions>,
154
- ): Promise<void>;
155
- /**
156
- * Update existing records in the Table
157
- *
158
- * An update operation can be used to adjust existing values. Use the
159
- * returned builder to specify which columns to update. The new value
160
- * can be a literal value (e.g. replacing nulls with some default value)
161
- * or an expression applied to the old value (e.g. incrementing a value)
162
- *
163
- * An optional condition can be specified (e.g. "only update if the old
164
- * value is 0")
165
- *
166
- * Note: if your condition is something like "some_id_column == 7" and
167
- * you are updating many rows (with different ids) then you will get
168
- * better performance with a single [`merge_insert`] call instead of
169
- * repeatedly calilng this method.
170
- * @param {Map<string, string> | Record<string, string>} updates - the
171
- * columns to update
172
- *
173
- * Keys in the map should specify the name of the column to update.
174
- * Values in the map provide the new value of the column. These can
175
- * be SQL literal strings (e.g. "7" or "'foo'") or they can be expressions
176
- * based on the row being updated (e.g. "my_col + 1")
177
- * @param {Partial<UpdateOptions>} options - additional options to control
178
- * the update behavior
179
- */
180
- abstract update(
181
- updates: Map<string, string> | Record<string, string>,
182
- options?: Partial<UpdateOptions>,
183
- ): Promise<void>;
184
-
185
- /** Count the total number of rows in the dataset. */
186
- abstract countRows(filter?: string): Promise<number>;
187
- /** Delete the rows that satisfy the predicate. */
188
- abstract delete(predicate: string): Promise<void>;
189
- /**
190
- * Create an index to speed up queries.
191
- *
192
- * Indices can be created on vector columns or scalar columns.
193
- * Indices on vector columns will speed up vector searches.
194
- * Indices on scalar columns will speed up filtering (in both
195
- * vector and non-vector searches)
196
- *
197
- * @note We currently don't support custom named indexes,
198
- * The index name will always be `${column}_idx`
199
- * @example
200
- * // If the column has a vector (fixed size list) data type then
201
- * // an IvfPq vector index will be created.
202
- * const table = await conn.openTable("my_table");
203
- * await table.createIndex("vector");
204
- * @example
205
- * // For advanced control over vector index creation you can specify
206
- * // the index type and options.
207
- * const table = await conn.openTable("my_table");
208
- * await table.createIndex("vector", {
209
- * config: lancedb.Index.ivfPq({
210
- * numPartitions: 128,
211
- * numSubVectors: 16,
212
- * }),
213
- * });
214
- * @example
215
- * // Or create a Scalar index
216
- * await table.createIndex("my_float_col");
217
- */
218
- abstract createIndex(
219
- column: string,
220
- options?: Partial<IndexOptions>,
221
- ): Promise<void>;
222
- /**
223
- * Create a {@link Query} Builder.
224
- *
225
- * Queries allow you to search your existing data. By default the query will
226
- * return all the data in the table in no particular order. The builder
227
- * returned by this method can be used to control the query using filtering,
228
- * vector similarity, sorting, and more.
229
- *
230
- * Note: By default, all columns are returned. For best performance, you should
231
- * only fetch the columns you need.
232
- *
233
- * When appropriate, various indices and statistics based pruning will be used to
234
- * accelerate the query.
235
- * @example
236
- * // SQL-style filtering
237
- * //
238
- * // This query will return up to 1000 rows whose value in the `id` column
239
- * // is greater than 5. LanceDb supports a broad set of filtering functions.
240
- * for await (const batch of table
241
- * .query()
242
- * .where("id > 1")
243
- * .select(["id"])
244
- * .limit(20)) {
245
- * console.log(batch);
246
- * }
247
- * @example
248
- * // Vector Similarity Search
249
- * //
250
- * // This example will find the 10 rows whose value in the "vector" column are
251
- * // closest to the query vector [1.0, 2.0, 3.0]. If an index has been created
252
- * // on the "vector" column then this will perform an ANN search.
253
- * //
254
- * // The `refineFactor` and `nprobes` methods are used to control the recall /
255
- * // latency tradeoff of the search.
256
- * for await (const batch of table
257
- * .query()
258
- * .where("id > 1")
259
- * .select(["id"])
260
- * .limit(20)) {
261
- * console.log(batch);
262
- * }
263
- * @example
264
- * // Scan the full dataset
265
- * //
266
- * // This query will return everything in the table in no particular order.
267
- * for await (const batch of table.query()) {
268
- * console.log(batch);
269
- * }
270
- * @returns {Query} A builder that can be used to parameterize the query
271
- */
272
- abstract query(): Query;
273
- /**
274
- * Create a search query to find the nearest neighbors
275
- * of the given query vector
276
- * @param {string} query - the query. This will be converted to a vector using the table's provided embedding function
277
- * @note If no embedding functions are defined in the table, this will error when collecting the results.
278
- */
279
- abstract search(query: string): VectorQuery;
280
- /**
281
- * Create a search query to find the nearest neighbors
282
- * of the given query vector
283
- * @param {IntoVector} query - the query vector
284
- */
285
- abstract search(query: IntoVector): VectorQuery;
286
- /**
287
- * Search the table with a given query vector.
288
- *
289
- * This is a convenience method for preparing a vector query and
290
- * is the same thing as calling `nearestTo` on the builder returned
291
- * by `query`. @see {@link Query#nearestTo} for more details.
292
- */
293
- abstract vectorSearch(vector: IntoVector): VectorQuery;
294
- /**
295
- * Add new columns with defined values.
296
- * @param {AddColumnsSql[]} newColumnTransforms pairs of column names and
297
- * the SQL expression to use to calculate the value of the new column. These
298
- * expressions will be evaluated for each row in the table, and can
299
- * reference existing columns in the table.
300
- */
301
- abstract addColumns(newColumnTransforms: AddColumnsSql[]): Promise<void>;
302
-
303
- /**
304
- * Alter the name or nullability of columns.
305
- * @param {ColumnAlteration[]} columnAlterations One or more alterations to
306
- * apply to columns.
307
- */
308
- abstract alterColumns(columnAlterations: ColumnAlteration[]): Promise<void>;
309
- /**
310
- * Drop one or more columns from the dataset
311
- *
312
- * This is a metadata-only operation and does not remove the data from the
313
- * underlying storage. In order to remove the data, you must subsequently
314
- * call ``compact_files`` to rewrite the data without the removed columns and
315
- * then call ``cleanup_files`` to remove the old files.
316
- * @param {string[]} columnNames The names of the columns to drop. These can
317
- * be nested column references (e.g. "a.b.c") or top-level column names
318
- * (e.g. "a").
319
- */
320
- abstract dropColumns(columnNames: string[]): Promise<void>;
321
- /** Retrieve the version of the table */
322
-
323
- abstract version(): Promise<number>;
324
- /**
325
- * Checks out a specific version of the table _This is an in-place operation._
326
- *
327
- * This allows viewing previous versions of the table. If you wish to
328
- * keep writing to the dataset starting from an old version, then use
329
- * the `restore` function.
330
- *
331
- * Calling this method will set the table into time-travel mode. If you
332
- * wish to return to standard mode, call `checkoutLatest`.
333
- * @param {number} version The version to checkout
334
- * @example
335
- * ```typescript
336
- * import * as lancedb from "@lancedb/lancedb"
337
- * const db = await lancedb.connect("./.lancedb");
338
- * const table = await db.createTable("my_table", [
339
- * { vector: [1.1, 0.9], type: "vector" },
340
- * ]);
341
- *
342
- * console.log(await table.version()); // 1
343
- * console.log(table.display());
344
- * await table.add([{ vector: [0.5, 0.2], type: "vector" }]);
345
- * await table.checkout(1);
346
- * console.log(await table.version()); // 2
347
- * ```
348
- */
349
- abstract checkout(version: number): Promise<void>;
350
- /**
351
- * Checkout the latest version of the table. _This is an in-place operation._
352
- *
353
- * The table will be set back into standard mode, and will track the latest
354
- * version of the table.
355
- */
356
- abstract checkoutLatest(): Promise<void>;
357
-
358
- /**
359
- * Restore the table to the currently checked out version
360
- *
361
- * This operation will fail if checkout has not been called previously
362
- *
363
- * This operation will overwrite the latest version of the table with a
364
- * previous version. Any changes made since the checked out version will
365
- * no longer be visible.
366
- *
367
- * Once the operation concludes the table will no longer be in a checked
368
- * out state and the read_consistency_interval, if any, will apply.
369
- */
370
- abstract restore(): Promise<void>;
371
- /**
372
- * Optimize the on-disk data and indices for better performance.
373
- *
374
- * Modeled after ``VACUUM`` in PostgreSQL.
375
- *
376
- * Optimization covers three operations:
377
- *
378
- * - Compaction: Merges small files into larger ones
379
- * - Prune: Removes old versions of the dataset
380
- * - Index: Optimizes the indices, adding new data to existing indices
381
- *
382
- *
383
- * Experimental API
384
- * ----------------
385
- *
386
- * The optimization process is undergoing active development and may change.
387
- * Our goal with these changes is to improve the performance of optimization and
388
- * reduce the complexity.
389
- *
390
- * That being said, it is essential today to run optimize if you want the best
391
- * performance. It should be stable and safe to use in production, but it our
392
- * hope that the API may be simplified (or not even need to be called) in the
393
- * future.
394
- *
395
- * The frequency an application shoudl call optimize is based on the frequency of
396
- * data modifications. If data is frequently added, deleted, or updated then
397
- * optimize should be run frequently. A good rule of thumb is to run optimize if
398
- * you have added or modified 100,000 or more records or run more than 20 data
399
- * modification operations.
400
- */
401
- abstract optimize(options?: Partial<OptimizeOptions>): Promise<OptimizeStats>;
402
- /** List all indices that have been created with {@link Table.createIndex} */
403
- abstract listIndices(): Promise<IndexConfig[]>;
404
- /** Return the table as an arrow table */
405
- abstract toArrow(): Promise<ArrowTable>;
406
-
407
- abstract mergeInsert(on: string | string[]): MergeInsertBuilder;
408
-
409
- /** List all the stats of a specified index
410
- *
411
- * @param {string} name The name of the index.
412
- * @returns {IndexStatistics | undefined} The stats of the index. If the index does not exist, it will return undefined
413
- */
414
- abstract indexStats(name: string): Promise<IndexStatistics | undefined>;
415
-
416
- static async parseTableData(
417
- data: Record<string, unknown>[] | TableLike,
418
- options?: Partial<CreateTableOptions>,
419
- streaming = false,
420
- ) {
421
- let mode: string = options?.mode ?? "create";
422
- const existOk = options?.existOk ?? false;
423
-
424
- if (mode === "create" && existOk) {
425
- mode = "exist_ok";
426
- }
427
-
428
- let table: ArrowTable;
429
- if (isArrowTable(data)) {
430
- table = sanitizeTable(data);
431
- } else {
432
- table = makeArrowTable(data as Record<string, unknown>[], options);
433
- }
434
- if (streaming) {
435
- const buf = await fromTableToStreamBuffer(
436
- table,
437
- options?.embeddingFunction,
438
- options?.schema,
439
- );
440
- return { buf, mode };
441
- } else {
442
- const buf = await fromTableToBuffer(
443
- table,
444
- options?.embeddingFunction,
445
- options?.schema,
446
- );
447
- return { buf, mode };
448
- }
449
- }
450
- }
451
-
452
- export class LocalTable extends Table {
453
- private readonly inner: _NativeTable;
454
-
455
- constructor(inner: _NativeTable) {
456
- super();
457
- this.inner = inner;
458
- }
459
- get name(): string {
460
- return this.inner.name;
461
- }
462
- isOpen(): boolean {
463
- return this.inner.isOpen();
464
- }
465
-
466
- close(): void {
467
- this.inner.close();
468
- }
469
-
470
- display(): string {
471
- return this.inner.display();
472
- }
473
-
474
- private async getEmbeddingFunctions(): Promise<
475
- Map<string, EmbeddingFunctionConfig>
476
- > {
477
- const schema = await this.schema();
478
- const registry = getRegistry();
479
- return registry.parseFunctions(schema.metadata);
480
- }
481
-
482
- /** Get the schema of the table. */
483
- async schema(): Promise<Schema> {
484
- const schemaBuf = await this.inner.schema();
485
- const tbl = tableFromIPC(schemaBuf);
486
- return tbl.schema;
487
- }
488
-
489
- async add(data: Data, options?: Partial<AddDataOptions>): Promise<void> {
490
- const mode = options?.mode ?? "append";
491
- const schema = await this.schema();
492
- const registry = getRegistry();
493
- const functions = registry.parseFunctions(schema.metadata);
494
-
495
- const buffer = await fromDataToBuffer(
496
- data,
497
- functions.values().next().value,
498
- schema,
499
- );
500
- await this.inner.add(buffer, mode);
501
- }
502
-
503
- async update(
504
- optsOrUpdates:
505
- | (Map<string, string> | Record<string, string>)
506
- | ({
507
- values: Map<string, IntoSql> | Record<string, IntoSql>;
508
- } & Partial<UpdateOptions>)
509
- | ({
510
- valuesSql: Map<string, string> | Record<string, string>;
511
- } & Partial<UpdateOptions>),
512
- options?: Partial<UpdateOptions>,
513
- ) {
514
- const isValues =
515
- "values" in optsOrUpdates && typeof optsOrUpdates.values !== "string";
516
- const isValuesSql =
517
- "valuesSql" in optsOrUpdates &&
518
- typeof optsOrUpdates.valuesSql !== "string";
519
- const isMap = (obj: unknown): obj is Map<string, string> => {
520
- return obj instanceof Map;
521
- };
522
-
523
- let predicate;
524
- let columns: [string, string][];
525
- switch (true) {
526
- case isMap(optsOrUpdates):
527
- columns = Array.from(optsOrUpdates.entries());
528
- predicate = options?.where;
529
- break;
530
- case isValues && isMap(optsOrUpdates.values):
531
- columns = Array.from(optsOrUpdates.values.entries()).map(([k, v]) => [
532
- k,
533
- toSQL(v),
534
- ]);
535
- predicate = optsOrUpdates.where;
536
- break;
537
- case isValues && !isMap(optsOrUpdates.values):
538
- columns = Object.entries(optsOrUpdates.values).map(([k, v]) => [
539
- k,
540
- toSQL(v),
541
- ]);
542
- predicate = optsOrUpdates.where;
543
- break;
544
-
545
- case isValuesSql && isMap(optsOrUpdates.valuesSql):
546
- columns = Array.from(optsOrUpdates.valuesSql.entries());
547
- predicate = optsOrUpdates.where;
548
- break;
549
- case isValuesSql && !isMap(optsOrUpdates.valuesSql):
550
- columns = Object.entries(optsOrUpdates.valuesSql).map(([k, v]) => [
551
- k,
552
- v,
553
- ]);
554
- predicate = optsOrUpdates.where;
555
- break;
556
- default:
557
- columns = Object.entries(optsOrUpdates as Record<string, string>);
558
- predicate = options?.where;
559
- }
560
- await this.inner.update(predicate, columns);
561
- }
562
-
563
- async countRows(filter?: string): Promise<number> {
564
- return await this.inner.countRows(filter);
565
- }
566
-
567
- async delete(predicate: string): Promise<void> {
568
- await this.inner.delete(predicate);
569
- }
570
-
571
- async createIndex(column: string, options?: Partial<IndexOptions>) {
572
- // Bit of a hack to get around the fact that TS has no package-scope.
573
- // biome-ignore lint/suspicious/noExplicitAny: skip
574
- const nativeIndex = (options?.config as any)?.inner;
575
- await this.inner.createIndex(nativeIndex, column, options?.replace);
576
- }
577
-
578
- query(): Query {
579
- return new Query(this.inner);
580
- }
581
- search(query: string | IntoVector): VectorQuery {
582
- if (typeof query !== "string") {
583
- return this.vectorSearch(query);
584
- } else {
585
- const queryPromise = this.getEmbeddingFunctions().then(
586
- async (functions) => {
587
- // TODO: Support multiple embedding functions
588
- const embeddingFunc: EmbeddingFunctionConfig | undefined = functions
589
- .values()
590
- .next().value;
591
- if (!embeddingFunc) {
592
- return Promise.reject(
593
- new Error("No embedding functions are defined in the table"),
594
- );
595
- }
596
- return await embeddingFunc.function.computeQueryEmbeddings(query);
597
- },
598
- );
599
-
600
- return this.query().nearestTo(queryPromise);
601
- }
602
- }
603
-
604
- vectorSearch(vector: IntoVector): VectorQuery {
605
- return this.query().nearestTo(vector);
606
- }
607
-
608
- // TODO: Support BatchUDF
609
-
610
- async addColumns(newColumnTransforms: AddColumnsSql[]): Promise<void> {
611
- await this.inner.addColumns(newColumnTransforms);
612
- }
613
-
614
- async alterColumns(columnAlterations: ColumnAlteration[]): Promise<void> {
615
- await this.inner.alterColumns(columnAlterations);
616
- }
617
-
618
- async dropColumns(columnNames: string[]): Promise<void> {
619
- await this.inner.dropColumns(columnNames);
620
- }
621
-
622
- async version(): Promise<number> {
623
- return await this.inner.version();
624
- }
625
-
626
- async checkout(version: number): Promise<void> {
627
- await this.inner.checkout(version);
628
- }
629
-
630
- async checkoutLatest(): Promise<void> {
631
- await this.inner.checkoutLatest();
632
- }
633
-
634
- async restore(): Promise<void> {
635
- await this.inner.restore();
636
- }
637
-
638
- async optimize(options?: Partial<OptimizeOptions>): Promise<OptimizeStats> {
639
- let cleanupOlderThanMs;
640
- if (
641
- options?.cleanupOlderThan !== undefined &&
642
- options?.cleanupOlderThan !== null
643
- ) {
644
- cleanupOlderThanMs =
645
- new Date().getTime() - options.cleanupOlderThan.getTime();
646
- }
647
- return await this.inner.optimize(cleanupOlderThanMs);
648
- }
649
-
650
- async listIndices(): Promise<IndexConfig[]> {
651
- return await this.inner.listIndices();
652
- }
653
-
654
- async toArrow(): Promise<ArrowTable> {
655
- return await this.query().toArrow();
656
- }
657
-
658
- async indexStats(name: string): Promise<IndexStatistics | undefined> {
659
- const stats = await this.inner.indexStats(name);
660
- if (stats === null) {
661
- return undefined;
662
- }
663
- return stats;
664
- }
665
- mergeInsert(on: string | string[]): MergeInsertBuilder {
666
- on = Array.isArray(on) ? on : [on];
667
- return new MergeInsertBuilder(this.inner.mergeInsert(on));
668
- }
669
- }