-
Notifications
You must be signed in to change notification settings - Fork 35
/
update.ts
78 lines (68 loc) · 2.25 KB
/
update.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import { buildConfigValidator } from '../validator';
import { Type } from '@sinclair/typebox';
import { DataOperationsProvider } from './dataOperationsProvider';
import {
RecordIdSchema,
RecordValuesSchema,
RecordSparseValuesSchema,
} from './types';
import type {
RecordId,
RecordValues,
RecordSparseValues,
RecordMetadata,
} from './types';
const UpdateRecordOptionsSchema = Type.Object(
{
id: RecordIdSchema,
values: Type.Optional(RecordValuesSchema),
sparseValues: Type.Optional(RecordSparseValuesSchema),
metadata: Type.Optional(Type.Object({}, { additionalProperties: true })),
},
{ additionalProperties: false }
);
/**
* This type is very similar to { @link PineconeRecord }, but differs because the
* values field is optional here. This is to allow for situations where perhaps
* the caller only wants to update metadata for a given record while leaving
* stored vector values as they are.
*/
export type UpdateOptions<T extends RecordMetadata = RecordMetadata> = {
/** The id of the record you would like to update */
id: RecordId;
/** The vector values you would like to store with this record */
values?: RecordValues;
/** The sparse values you would like to store with this record.
*
* @see [Understanding hybrid search](https://docs.pinecone.io/docs/hybrid-search)
*/
sparseValues?: RecordSparseValues;
/**
* The metadata you would like to store with this record.
*/
metadata?: Partial<T>;
};
export class UpdateCommand<T extends RecordMetadata = RecordMetadata> {
apiProvider: DataOperationsProvider;
namespace: string;
validator: ReturnType<typeof buildConfigValidator>;
constructor(apiProvider, namespace) {
this.apiProvider = apiProvider;
this.namespace = namespace;
this.validator = buildConfigValidator(UpdateRecordOptionsSchema, 'update');
}
async run(options: UpdateOptions<T>): Promise<void> {
this.validator(options);
const requestOptions = {
id: options['id'],
values: options['values'],
sparseValues: options['sparseValues'],
setMetadata: options['metadata'],
};
const api = await this.apiProvider.provide();
await api.update({
updateRequest: { ...requestOptions, namespace: this.namespace },
});
return;
}
}