@upstash/vector 1.0.1 → 1.0.2

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Upstash
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,273 +1,131 @@
1
- # Upstash Vector Node.js Client
1
+ # Upstash Vector Node.js Client · ![license](https://img.shields.io/npm/l/%40upstash%2Fvector) [![Tests](https://github.com/upstash/vector-js/actions/workflows/tests.yaml/badge.svg)](https://github.com/upstash/vector-js/actions/workflows/tests.yaml) ![npm (scoped)](https://img.shields.io/npm/v/@upstash/vector) ![npm bundle size](https://img.shields.io/bundlephobia/minzip/@upstash/vector) ![npm weekly download](https://img.shields.io/npm/dw/%40upstash%2Fvector)
2
2
 
3
- This is the official Node.js client for [Upstash](https://upstash.com/), written in TypeScript.
3
+ `@upstash/vector` is an HTTP/REST based client for Typescript, built on top of [Upstash REST API](https://upstash.com/docs/vector/api/endpoints/).
4
4
 
5
- ## Documentation
5
+ It is the only connectionless (HTTP based) Vector client and designed for:
6
6
 
7
- - [**Reference Documentation**](https://upstash.com/docs/vector/overall/getstarted)
7
+ - Serverless functions (AWS Lambda ...)
8
+ - Cloudflare Workers
9
+ - Next.js, Jamstack ...
10
+ - Client side web/mobile applications
11
+ - WebAssembly
12
+ - and other environments where HTTP is preferred over TCP.
8
13
 
9
- ## Installation
14
+ See [the list of APIs](https://upstash.com/docs/vector/api/endpoints) supported.
10
15
 
11
- ```
12
- npm install @upstash/vector
13
- pnpm add @upstash/vector
14
- ```
15
-
16
- ## Usage
17
-
18
- ### Initializing the client
16
+ ## Quick Start
19
17
 
20
- There are two pieces of configuration required to use the Upstash vector client: an REST token and REST URL. These values can be passed using environment variables or in code through a configuration object. Find your configuration values in the console dashboard at [https://console.upstash.com/](https://console.upstash.com/).
18
+ ### Install
21
19
 
22
- #### Using environment variables
23
-
24
- The environment variables used to configure the client are the following:
20
+ #### Node.js
25
21
 
26
22
  ```bash
27
- UPSTASH_VECTOR_REST_URL="your_rest_url"
28
- UPSTASH_VECTOR_REST_TOKEN="your_rest_token"
23
+ npm install @upstash/vector
29
24
  ```
30
25
 
31
- When these environment variables are set, the client constructor does not require any additional arguments.
26
+ ### Create Index
32
27
 
33
- ```typescript
34
- import { Index } from "@upstash/vector";
35
-
36
- const index = new Index();
37
- ```
28
+ Create a new index on [Upstash](https://console.upstash.com/vector)
38
29
 
39
- #### Using a configuration object
30
+ ## Basic Usage:
40
31
 
41
- If you prefer to pass configuration in code, the constructor accepts a config object containing the `url` and `token` values. This
42
- could be useful if your application needs to interact with multiple projects, each with a different configuration.
43
-
44
- ```typescript
32
+ ```ts
45
33
  import { Index } from "@upstash/vector";
46
34
 
47
- const index = new Index({
48
- url: "<UPSTASH_VECTOR_REST_URL>",
49
- token: "<UPSTASH_VECTOR_REST_TOKEN>",
50
- });
51
- ```
52
-
53
- ## Index operations
54
-
55
- Upstash vector indexes support operations for working with vector data using operations such as upsert, query, fetch, and delete.
56
-
57
- ### Accessing an index
58
-
59
- To perform data operations on an index, access it using the `index` method.
60
-
61
- ```typescript
62
- // Now perform index operations
63
- await index.fetch([1, 2, 3], { includeMetadata: true, includeVectors: true });
64
- ```
65
-
66
- ### Accesing an index, with metadata typing
67
-
68
- If you are storing metadata alongside your vector values, you can pass a type parameter to `index()` in order to get proper TypeScript typechecking.
69
-
70
- ```typescript
71
35
  type Metadata = {
72
36
  title: string,
73
37
  genre: 'sci-fi' | 'fantasy' | 'horror' | 'action'
38
+ category: "classic" | "modern"
74
39
  }
75
40
 
41
+ const index = new Index<Metadata>({
42
+ url: "<UPSTASH_VECTOR_REST_URL>",
43
+ token: "<UPSTASH_VECTOR_REST_TOKEN>",
44
+ });
45
+
46
+ //Upsert Data
76
47
  await index.upsert([{
77
- id: '1234',
48
+ id: 'upstash-rocks',
78
49
  vector: [
79
50
  .... // embedding values
80
51
  ],
81
52
  metadata: {
82
53
  title: 'Lord of The Rings',
83
- genre: 'drama',
54
+ genre: 'fantasy',
84
55
  category: 'classic'
85
56
  }
86
57
  }])
87
58
 
59
+ //Query Data
88
60
  const results = await index.query<Metadata>({
89
61
  vector: [
90
62
  ... // query embedding
91
63
  ],
92
64
  includeVectors: true,
65
+ includeMetadata: true
93
66
  topK: 1,
94
67
  })
95
68
 
96
- if (results[0].metadata) {
97
- // Since we passed the Metadata type parameter above,
98
- // we can interact with metadata fields without having to
99
- // do any typecasting.
100
- const { title, genre, category } = results[0].metadata;
101
- console.log(`The best match in fantasy was ${title}`)
102
- }
103
- ```
104
-
105
- ### Upsert records
106
-
107
- Upstash vector expects records inserted into indexes to have the following form:
108
-
109
- ```typescript
110
- type UpstashRecord = {
111
- id: number | string;
112
- vector: number[];
113
- metadata?: Record<string, unknown>;
114
- };
115
- ```
116
-
117
- #### Upsert many
118
-
119
- To upsert some records, you can use the client like so:
120
-
121
- ```typescript
122
- // Prepare your data. The length of each array
123
- // of vector values must match the dimension of
124
- // the index where you plan to store them.
125
- const records = [
126
- {
127
- id: "1",
128
- vector: [0.236, 0.971, 0.559],
129
- },
130
- {
131
- id: "2",
132
- vector: [0.685, 0.111, 0.857],
133
- },
134
- ];
135
-
136
- // Upsert the data into your index
137
- await index.upsert(records);
138
- ```
139
-
140
- #### Upsert one
141
-
142
- ```typescript
143
- // Prepare your data. The length of each array
144
- // of vector values must match the dimension of
145
- // the index where you plan to store them.
146
- const record = {
147
- id: "1",
148
- vector: [0.236, 0.971, 0.559],
149
- };
150
- // Upsert the data into your index
151
- await index.upsert(record);
152
- ```
153
-
154
- ### Querying
155
-
156
- #### Querying with vector values
157
-
158
- The query method accepts a large number of options. The dimension of the query vector must match the dimension of your index.
159
-
160
- ```typescript
161
- type QueryOptions = {
162
- vector: number[];
163
- topK: number;
164
- includeVectors?: boolean;
165
- includeMetadata?: boolean;
166
- };
167
- ```
168
-
169
- For example, to query by vector values you would pass the `vector` param in the options configuration. For brevity sake this example query vector is tiny (dimension 2), but in a more realistic use case this query vector would be an embedding outputted by a model. Look at the [Example code](#example-code) to see more realistic examples of how to use `query`.
170
-
171
- ```typescript
172
- > await index.query({ topK: 3, vector: [ 0.22, 0.66 ]})
173
- {
174
- matches: [
175
- {
176
- id: '6345',
177
- score: 1.00000012,
178
- vector: [],
179
- metadata: undefined
180
- },
181
- {
182
- id: '1233',
183
- score: 1.00000012,
184
- vector: [],
185
- metadata: undefined
186
- },
187
- {
188
- id: '4142',
189
- score: 1.00000012,
190
- vector: [],
191
- metadata: undefined
192
- }
193
- ],
194
- namespace: ''
195
- }
196
- ```
197
-
198
- You include options to `includeMetadata: true` or `includeVectors: true` if you need this information. By default these are not returned to keep the response payload small.
199
-
200
- ### Update a record
201
-
202
- You may want to update vector `vector` or `metadata`. Specify the id and the attribute value you want to update.
203
-
204
- ```typescript
69
+ //Update Data
205
70
  await index.upsert({
206
- id: "18593",
207
- metadata: { genre: "romance" },
71
+ id: "upstash-rocks",
72
+ metadata: {
73
+ title: 'Star Wars',
74
+ genre: 'sci-fi',
75
+ category: 'classic'
76
+ }
208
77
  });
209
- ```
210
78
 
211
- ### Fetch records by their IDs
79
+ //Delete record
80
+ await index.delete("upstash-rocks");
212
81
 
213
- ```typescript
214
- const fetchResult = await index.fetch(["id-1", "id-2"]);
215
- ```
216
-
217
- ### Delete records
82
+ //Delete many by id
83
+ await index.delete(["id-1", "id-2", "id-3"]);
218
84
 
219
- For convenience there are several delete-related options. You can verify the results of a delete operation by trying to `fetch()` a record.
85
+ //Fetch records by their IDs
86
+ await index.fetch(["id-1", "id-2"]);
220
87
 
221
- #### Delete one
88
+ //Fetch records with range
89
+ await index.range({
90
+ cursor: 0,
91
+ limit: 5,
92
+ includeVectors: true,
93
+ });
222
94
 
223
- ```typescript
224
- await index.delete("id-to-delete");
225
- ```
95
+ //Reset index
96
+ await index.reset();
226
97
 
227
- #### Delete many by id
98
+ //Info about index
99
+ await index.info();
228
100
 
229
- ```typescript
230
- await index.delete(["id-1", "id-2", "id-3"]);
101
+ //Random vector based on stored vectors
102
+ await index.random();
231
103
  ```
232
104
 
233
- ### Info
234
-
235
- To get statistics of your index, you can use the client like so:
236
-
237
- ```typescript
238
- await index.info(["id-1", "id-2", "id-3"]);
239
- ```
105
+ ## Troubleshooting
240
106
 
241
- ### Reset
107
+ We have a [Discord](upstash.com/discord) for common problems. If you can't find a solution, please [open an issue](https://github.com/upstash/vector-js/issues/new).
242
108
 
243
- To delete everything related with that index:
109
+ ## Docs
244
110
 
245
- ```typescript
246
- await index.reset();
247
- ```
111
+ See [the documentation](https://upstash.com/docs/oss/sdks/ts/vector/overview) for details.
248
112
 
249
113
  ## Contributing
250
114
 
251
- ## Preparing the environment
115
+ ### [Install Bun](https://bun.sh/docs/installation)
252
116
 
253
- This project uses [Bun](https://bun.sh/) for packaging and dependency management. Make sure you have the relevant dependencies.
117
+ ### Vector Database
254
118
 
255
- You will also need a vector database on [Upstash](https://console.upstash.com/).
119
+ Create a new index on [Upstash](https://console.upstash.com/vector) and copy the url and token.
256
120
 
257
- ```commandline
258
- curl -fsSL https://bun.sh/install | bash
259
- ```
121
+ ### Running tests
260
122
 
261
- ## Code Formatting
262
-
263
- ```bash
264
- bun run fmt
123
+ ```sh
124
+ bun run test
265
125
  ```
266
126
 
267
- ## Running tests
268
-
269
- To run all the tests, make sure you have the relevant environment variables.
127
+ ### Building
270
128
 
271
- ```bash
272
- bun run test
129
+ ```sh
130
+ bun run build
273
131
  ```
package/dist/index.d.mts CHANGED
@@ -87,15 +87,6 @@ declare class QueryCommand<TMetadata> extends Command<QueryResult<TMetadata>[]>
87
87
  constructor(payload: QueryCommandPayload);
88
88
  }
89
89
 
90
- type UpsertCommandPayload = {
91
- id: number | string;
92
- vector: number[];
93
- metadata?: Record<string, unknown>;
94
- };
95
- declare class UpsertCommand extends Command<string> {
96
- constructor(payload: UpsertCommandPayload | UpsertCommandPayload[]);
97
- }
98
-
99
90
  type RangeCommandPayload = {
100
91
  cursor: number | string;
101
92
  limit: number;
@@ -122,7 +113,7 @@ type CommandArgs<TCommand extends new (_args: any) => any> = ConstructorParamete
122
113
  /**
123
114
  * Serverless vector client for upstash vector db.
124
115
  */
125
- declare class Index$1 {
116
+ declare class Index$1<TIndexMetadata extends Record<string, unknown> = Record<string, unknown>> {
126
117
  protected client: Requester;
127
118
  /**
128
119
  * Create a new vector db client
@@ -168,7 +159,7 @@ declare class Index$1 {
168
159
  *
169
160
  * @returns A promise that resolves with an array of query result objects when the request to query the index is completed.
170
161
  */
171
- query: <TMetadata>(args: CommandArgs<typeof QueryCommand>) => Promise<QueryResult<TMetadata>[]>;
162
+ query: <TMetadata extends Record<string, unknown> = TIndexMetadata>(args: CommandArgs<typeof QueryCommand>) => Promise<QueryResult<TMetadata>[]>;
172
163
  /**
173
164
  * Upserts (Updates and Inserts) specific items into the index.
174
165
  * It's used for adding new items to the index or updating existing ones.
@@ -191,7 +182,15 @@ declare class Index$1 {
191
182
  *
192
183
  * @returns {string} A promise that resolves with the result of the upsert operation after the command is executed.
193
184
  */
194
- upsert: (args: CommandArgs<typeof UpsertCommand>) => Promise<string>;
185
+ upsert: <TMetadata extends Record<string, unknown> = TIndexMetadata>(args: {
186
+ id: string | number;
187
+ vector: number[];
188
+ metadata?: (TMetadata extends infer U ? U : never) | undefined;
189
+ } | {
190
+ id: string | number;
191
+ vector: number[];
192
+ metadata?: (TMetadata extends infer U ? U : never) | undefined;
193
+ }[]) => Promise<string>;
195
194
  /**
196
195
  * It's used for retrieving specific items from the index, optionally including
197
196
  * their metadata and feature vectors.
@@ -212,10 +211,10 @@ declare class Index$1 {
212
211
  *
213
212
  * @returns {Promise<FetchReturnResponse<TMetadata>[]>} A promise that resolves with an array of fetched items or null if not found, after the command is executed.
214
213
  */
215
- fetch: <TMetadata>(ids: string[] | number[], opts: {
214
+ fetch: <TMetadata extends Record<string, unknown> = TIndexMetadata>(ids: number[] | string[], opts?: {
216
215
  includeMetadata?: boolean | undefined;
217
216
  includeVectors?: boolean | undefined;
218
- }) => Promise<FetchResult<TMetadata>[]>;
217
+ } | undefined) => Promise<FetchResult<TMetadata>[]>;
219
218
  /**
220
219
  * It's used for wiping an entire index.
221
220
  *
@@ -251,7 +250,7 @@ declare class Index$1 {
251
250
  *
252
251
  * @returns {Promise<RangeReturnResponse<TMetadata>>} A promise that resolves with the response containing the next cursor and an array of vectors, after the command is executed.
253
252
  */
254
- range: <TMetadata>(args: CommandArgs<typeof RangeCommand>) => Promise<RangeResult<TMetadata>>;
253
+ range: <TMetadata extends Record<string, unknown> = TIndexMetadata>(args: CommandArgs<typeof RangeCommand>) => Promise<RangeResult<TMetadata>>;
255
254
  /**
256
255
  * Retrieves info from the index.
257
256
  *
@@ -288,7 +287,7 @@ type IndexConfig = {
288
287
  /**
289
288
  * Serverless vector client for upstash.
290
289
  */
291
- declare class Index extends Index$1 {
290
+ declare class Index<TIndexMetadata extends Record<string, unknown> = Record<string, unknown>> extends Index$1<TIndexMetadata> {
292
291
  /**
293
292
  * Create a new vector client by providing the url and token
294
293
  *
package/dist/index.d.ts CHANGED
@@ -87,15 +87,6 @@ declare class QueryCommand<TMetadata> extends Command<QueryResult<TMetadata>[]>
87
87
  constructor(payload: QueryCommandPayload);
88
88
  }
89
89
 
90
- type UpsertCommandPayload = {
91
- id: number | string;
92
- vector: number[];
93
- metadata?: Record<string, unknown>;
94
- };
95
- declare class UpsertCommand extends Command<string> {
96
- constructor(payload: UpsertCommandPayload | UpsertCommandPayload[]);
97
- }
98
-
99
90
  type RangeCommandPayload = {
100
91
  cursor: number | string;
101
92
  limit: number;
@@ -122,7 +113,7 @@ type CommandArgs<TCommand extends new (_args: any) => any> = ConstructorParamete
122
113
  /**
123
114
  * Serverless vector client for upstash vector db.
124
115
  */
125
- declare class Index$1 {
116
+ declare class Index$1<TIndexMetadata extends Record<string, unknown> = Record<string, unknown>> {
126
117
  protected client: Requester;
127
118
  /**
128
119
  * Create a new vector db client
@@ -168,7 +159,7 @@ declare class Index$1 {
168
159
  *
169
160
  * @returns A promise that resolves with an array of query result objects when the request to query the index is completed.
170
161
  */
171
- query: <TMetadata>(args: CommandArgs<typeof QueryCommand>) => Promise<QueryResult<TMetadata>[]>;
162
+ query: <TMetadata extends Record<string, unknown> = TIndexMetadata>(args: CommandArgs<typeof QueryCommand>) => Promise<QueryResult<TMetadata>[]>;
172
163
  /**
173
164
  * Upserts (Updates and Inserts) specific items into the index.
174
165
  * It's used for adding new items to the index or updating existing ones.
@@ -191,7 +182,15 @@ declare class Index$1 {
191
182
  *
192
183
  * @returns {string} A promise that resolves with the result of the upsert operation after the command is executed.
193
184
  */
194
- upsert: (args: CommandArgs<typeof UpsertCommand>) => Promise<string>;
185
+ upsert: <TMetadata extends Record<string, unknown> = TIndexMetadata>(args: {
186
+ id: string | number;
187
+ vector: number[];
188
+ metadata?: (TMetadata extends infer U ? U : never) | undefined;
189
+ } | {
190
+ id: string | number;
191
+ vector: number[];
192
+ metadata?: (TMetadata extends infer U ? U : never) | undefined;
193
+ }[]) => Promise<string>;
195
194
  /**
196
195
  * It's used for retrieving specific items from the index, optionally including
197
196
  * their metadata and feature vectors.
@@ -212,10 +211,10 @@ declare class Index$1 {
212
211
  *
213
212
  * @returns {Promise<FetchReturnResponse<TMetadata>[]>} A promise that resolves with an array of fetched items or null if not found, after the command is executed.
214
213
  */
215
- fetch: <TMetadata>(ids: string[] | number[], opts: {
214
+ fetch: <TMetadata extends Record<string, unknown> = TIndexMetadata>(ids: number[] | string[], opts?: {
216
215
  includeMetadata?: boolean | undefined;
217
216
  includeVectors?: boolean | undefined;
218
- }) => Promise<FetchResult<TMetadata>[]>;
217
+ } | undefined) => Promise<FetchResult<TMetadata>[]>;
219
218
  /**
220
219
  * It's used for wiping an entire index.
221
220
  *
@@ -251,7 +250,7 @@ declare class Index$1 {
251
250
  *
252
251
  * @returns {Promise<RangeReturnResponse<TMetadata>>} A promise that resolves with the response containing the next cursor and an array of vectors, after the command is executed.
253
252
  */
254
- range: <TMetadata>(args: CommandArgs<typeof RangeCommand>) => Promise<RangeResult<TMetadata>>;
253
+ range: <TMetadata extends Record<string, unknown> = TIndexMetadata>(args: CommandArgs<typeof RangeCommand>) => Promise<RangeResult<TMetadata>>;
255
254
  /**
256
255
  * Retrieves info from the index.
257
256
  *
@@ -288,7 +287,7 @@ type IndexConfig = {
288
287
  /**
289
288
  * Serverless vector client for upstash.
290
289
  */
291
- declare class Index extends Index$1 {
290
+ declare class Index<TIndexMetadata extends Record<string, unknown> = Record<string, unknown>> extends Index$1<TIndexMetadata> {
292
291
  /**
293
292
  * Create a new vector client by providing the url and token
294
293
  *
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- "use strict";var b=Object.defineProperty;var w=Object.getOwnPropertyDescriptor;var E=Object.getOwnPropertyNames;var S=Object.prototype.hasOwnProperty;var U=(r,e)=>{for(var t in e)b(r,t,{get:e[t],enumerable:!0})},_=(r,e,t,o)=>{if(e&&typeof e=="object"||typeof e=="function")for(let s of E(e))!S.call(r,s)&&s!==t&&b(r,s,{get:()=>e[s],enumerable:!(o=w(e,s))||o.enumerable});return r};var P=r=>_(b({},"__esModule",{value:!0}),r);var A={};U(A,{Index:()=>C});module.exports=P(A);var a=class extends Error{constructor(e){super(e),this.name="UpstashError"}};var i=class{baseUrl;headers;options;retry;constructor(e){this.options={cache:e.cache,signal:e.signal},this.baseUrl=e.baseUrl.replace(/\/$/,""),this.headers={"Content-Type":"application/json",...e.headers},typeof e?.retry=="boolean"&&e?.retry===!1?this.retry={attempts:1,backoff:()=>0}:this.retry={attempts:e?.retry?.retries??5,backoff:e?.retry?.backoff??(t=>Math.exp(t)*50)}}async request(e){let t={cache:this.options.cache,method:"POST",headers:this.headers,body:JSON.stringify(e.body),keepalive:!0,signal:this.options.signal},o=null,s=null;for(let R=0;R<=this.retry.attempts;R++)try{o=await fetch([this.baseUrl,...e.path??[]].join("/"),t);break}catch(x){if(this.options.signal?.aborted){let T=new Blob([JSON.stringify({result:this.options.signal.reason??"Aborted"})]),g={status:200,statusText:this.options.signal.reason??"Aborted"};o=new Response(T,g);break}s=x,await new Promise(T=>setTimeout(T,this.retry.backoff(R)))}if(!o)throw s??new Error("Exhausted all retries");let y=await o.json();if(!o.ok)throw new a(`${y.error}, command was: ${JSON.stringify(e.body)}`);return{result:y.result,error:y.error}}};var n=class{payload;endpoint;constructor(e,t){this.payload=e,this.endpoint=t}async exec(e){let{result:t,error:o}=await e.request({body:this.payload,path:[this.endpoint]});if(o)throw new a(o);if(typeof t>"u")throw new Error("Request did not return a result");return t}};var p=class extends n{constructor(e){let t=[];Array.isArray(e)?t.push(...e):t.push(e),super(t,"delete")}};var c=class extends n{constructor(e){super(e,"query")}};var u=class extends n{constructor(e){super(e,"upsert")}};var m=class extends n{constructor([e,t]){super({ids:e,...t},"fetch")}};var l=class extends n{constructor(e){super(e,"range")}};var d=class extends n{constructor(){super([],"reset")}};var h=class extends n{constructor(){super([],"info")}};var f=class{client;constructor(e){this.client=e}delete=e=>new p(e).exec(this.client);query=e=>new c(e).exec(this.client);upsert=e=>new u(e).exec(this.client);fetch=(...e)=>new m(e).exec(this.client);reset=()=>new d().exec(this.client);range=e=>new l(e).exec(this.client);info=()=>new h().exec(this.client)};var C=class r extends f{constructor(e){if(typeof e<"u"&&"request"in e){super(e);return}let t=e?.token??process.env.NEXT_PUBLIC_UPSTASH_VECTOR_REST_TOKEN??process.env.UPSTASH_VECTOR_REST_TOKEN,o=e?.url??process.env.NEXT_PUBLIC_UPSTASH_VECTOR_REST_URL??process.env.UPSTASH_VECTOR_REST_URL;if(!t)throw new Error("UPSTASH_VECTOR_REST_TOKEN is missing!");if(!o)throw new Error("UPSTASH_VECTOR_REST_URL is missing!");(o.startsWith(" ")||o.endsWith(" ")||/\r|\n/.test(o))&&console.warn("The vector url contains whitespace or newline, which can cause errors!"),(t.startsWith(" ")||t.endsWith(" ")||/\r|\n/.test(t))&&console.warn("The vector token contains whitespace or newline, which can cause errors!");let s=new i({baseUrl:o,retry:e?.retry,headers:{authorization:`Bearer ${t}`},cache:e?.cache||"no-store",signal:e?.signal});super(s)}static fromEnv(e){let t=process?.env.UPSTASH_VECTOR_REST_URL;if(!t)throw new Error("Unable to find environment variable: `UPSTASH_VECTOR_REST_URL`");let o=process?.env.UPSTASH_VECTOR_REST_TOKEN;if(!o)throw new Error("Unable to find environment variable: `UPSTASH_VECTOR_REST_TOKEN`");return new r({...e,url:t,token:o})}};0&&(module.exports={Index});
1
+ "use strict";var x=Object.defineProperty;var w=Object.getOwnPropertyDescriptor;var E=Object.getOwnPropertyNames;var U=Object.prototype.hasOwnProperty;var S=(r,e)=>{for(var t in e)x(r,t,{get:e[t],enumerable:!0})},M=(r,e,t,n)=>{if(e&&typeof e=="object"||typeof e=="function")for(let s of E(e))!U.call(r,s)&&s!==t&&x(r,s,{get:()=>e[s],enumerable:!(n=w(e,s))||n.enumerable});return r};var _=r=>M(x({},"__esModule",{value:!0}),r);var P={};S(P,{Index:()=>b});module.exports=_(P);var a=class extends Error{constructor(e){super(e),this.name="UpstashError"}};var i=class{baseUrl;headers;options;retry;constructor(e){this.options={cache:e.cache,signal:e.signal},this.baseUrl=e.baseUrl.replace(/\/$/,""),this.headers={"Content-Type":"application/json",...e.headers},typeof e?.retry=="boolean"&&e?.retry===!1?this.retry={attempts:1,backoff:()=>0}:this.retry={attempts:e?.retry?.retries??5,backoff:e?.retry?.backoff??(t=>Math.exp(t)*50)}}async request(e){let t={cache:this.options.cache,method:"POST",headers:this.headers,body:JSON.stringify(e.body),keepalive:!0,signal:this.options.signal},n=null,s=null;for(let y=0;y<=this.retry.attempts;y++)try{n=await fetch([this.baseUrl,...e.path??[]].join("/"),t);break}catch(g){if(this.options.signal?.aborted){let R=new Blob([JSON.stringify({result:this.options.signal.reason??"Aborted"})]),C={status:200,statusText:this.options.signal.reason??"Aborted"};n=new Response(R,C);break}s=g,await new Promise(R=>setTimeout(R,this.retry.backoff(y)))}if(!n)throw s??new Error("Exhausted all retries");let T=await n.json();if(!n.ok)throw new a(`${T.error}, command was: ${JSON.stringify(e.body)}`);return{result:T.result,error:T.error}}};var o=class{payload;endpoint;constructor(e,t){this.payload=e,this.endpoint=t}async exec(e){let{result:t,error:n}=await e.request({body:this.payload,path:[this.endpoint]});if(n)throw new a(n);if(typeof t>"u")throw new Error("Request did not return a result");return t}};var p=class extends o{constructor(e){let t=[];Array.isArray(e)?t.push(...e):t.push(e),super(t,"delete")}};var c=class extends o{constructor(e){super(e,"query")}};var d=class extends o{constructor(e){super(e,"upsert")}};var u=class extends o{constructor([e,t]){super({ids:e,...t},"fetch")}};var m=class extends o{constructor(e){super(e,"range")}};var l=class extends o{constructor(){super([],"reset")}};var h=class extends o{constructor(){super([],"info")}};var f=class{client;constructor(e){this.client=e}delete=e=>new p(e).exec(this.client);query=e=>new c(e).exec(this.client);upsert=e=>new d(e).exec(this.client);fetch=(...e)=>new u(e).exec(this.client);reset=()=>new l().exec(this.client);range=e=>new m(e).exec(this.client);info=()=>new h().exec(this.client)};var b=class r extends f{constructor(e){if(typeof e<"u"&&"request"in e){super(e);return}let t=e?.token??process.env.NEXT_PUBLIC_UPSTASH_VECTOR_REST_TOKEN??process.env.UPSTASH_VECTOR_REST_TOKEN,n=e?.url??process.env.NEXT_PUBLIC_UPSTASH_VECTOR_REST_URL??process.env.UPSTASH_VECTOR_REST_URL;if(!t)throw new Error("UPSTASH_VECTOR_REST_TOKEN is missing!");if(!n)throw new Error("UPSTASH_VECTOR_REST_URL is missing!");(n.startsWith(" ")||n.endsWith(" ")||/\r|\n/.test(n))&&console.warn("The vector url contains whitespace or newline, which can cause errors!"),(t.startsWith(" ")||t.endsWith(" ")||/\r|\n/.test(t))&&console.warn("The vector token contains whitespace or newline, which can cause errors!");let s=new i({baseUrl:n,retry:e?.retry,headers:{authorization:`Bearer ${t}`},cache:e?.cache||"no-store",signal:e?.signal});super(s)}static fromEnv(e){let t=process?.env.UPSTASH_VECTOR_REST_URL;if(!t)throw new Error("Unable to find environment variable: `UPSTASH_VECTOR_REST_URL`");let n=process?.env.UPSTASH_VECTOR_REST_TOKEN;if(!n)throw new Error("Unable to find environment variable: `UPSTASH_VECTOR_REST_TOKEN`");return new r({...e,url:t,token:n})}};0&&(module.exports={Index});
package/dist/index.mjs CHANGED
@@ -1 +1 @@
1
- var s=class extends Error{constructor(e){super(e),this.name="UpstashError"}};var i=class{baseUrl;headers;options;retry;constructor(e){this.options={cache:e.cache,signal:e.signal},this.baseUrl=e.baseUrl.replace(/\/$/,""),this.headers={"Content-Type":"application/json",...e.headers},typeof e?.retry=="boolean"&&e?.retry===!1?this.retry={attempts:1,backoff:()=>0}:this.retry={attempts:e?.retry?.retries??5,backoff:e?.retry?.backoff??(t=>Math.exp(t)*50)}}async request(e){let t={cache:this.options.cache,method:"POST",headers:this.headers,body:JSON.stringify(e.body),keepalive:!0,signal:this.options.signal},r=null,a=null;for(let R=0;R<=this.retry.attempts;R++)try{r=await fetch([this.baseUrl,...e.path??[]].join("/"),t);break}catch(C){if(this.options.signal?.aborted){let T=new Blob([JSON.stringify({result:this.options.signal.reason??"Aborted"})]),x={status:200,statusText:this.options.signal.reason??"Aborted"};r=new Response(T,x);break}a=C,await new Promise(T=>setTimeout(T,this.retry.backoff(R)))}if(!r)throw a??new Error("Exhausted all retries");let y=await r.json();if(!r.ok)throw new s(`${y.error}, command was: ${JSON.stringify(e.body)}`);return{result:y.result,error:y.error}}};var o=class{payload;endpoint;constructor(e,t){this.payload=e,this.endpoint=t}async exec(e){let{result:t,error:r}=await e.request({body:this.payload,path:[this.endpoint]});if(r)throw new s(r);if(typeof t>"u")throw new Error("Request did not return a result");return t}};var p=class extends o{constructor(e){let t=[];Array.isArray(e)?t.push(...e):t.push(e),super(t,"delete")}};var c=class extends o{constructor(e){super(e,"query")}};var u=class extends o{constructor(e){super(e,"upsert")}};var m=class extends o{constructor([e,t]){super({ids:e,...t},"fetch")}};var l=class extends o{constructor(e){super(e,"range")}};var d=class extends o{constructor(){super([],"reset")}};var h=class extends o{constructor(){super([],"info")}};var f=class{client;constructor(e){this.client=e}delete=e=>new p(e).exec(this.client);query=e=>new c(e).exec(this.client);upsert=e=>new u(e).exec(this.client);fetch=(...e)=>new m(e).exec(this.client);reset=()=>new d().exec(this.client);range=e=>new l(e).exec(this.client);info=()=>new h().exec(this.client)};var b=class n extends f{constructor(e){if(typeof e<"u"&&"request"in e){super(e);return}let t=e?.token??process.env.NEXT_PUBLIC_UPSTASH_VECTOR_REST_TOKEN??process.env.UPSTASH_VECTOR_REST_TOKEN,r=e?.url??process.env.NEXT_PUBLIC_UPSTASH_VECTOR_REST_URL??process.env.UPSTASH_VECTOR_REST_URL;if(!t)throw new Error("UPSTASH_VECTOR_REST_TOKEN is missing!");if(!r)throw new Error("UPSTASH_VECTOR_REST_URL is missing!");(r.startsWith(" ")||r.endsWith(" ")||/\r|\n/.test(r))&&console.warn("The vector url contains whitespace or newline, which can cause errors!"),(t.startsWith(" ")||t.endsWith(" ")||/\r|\n/.test(t))&&console.warn("The vector token contains whitespace or newline, which can cause errors!");let a=new i({baseUrl:r,retry:e?.retry,headers:{authorization:`Bearer ${t}`},cache:e?.cache||"no-store",signal:e?.signal});super(a)}static fromEnv(e){let t=process?.env.UPSTASH_VECTOR_REST_URL;if(!t)throw new Error("Unable to find environment variable: `UPSTASH_VECTOR_REST_URL`");let r=process?.env.UPSTASH_VECTOR_REST_TOKEN;if(!r)throw new Error("Unable to find environment variable: `UPSTASH_VECTOR_REST_TOKEN`");return new n({...e,url:t,token:r})}};export{b as Index};
1
+ var s=class extends Error{constructor(e){super(e),this.name="UpstashError"}};var i=class{baseUrl;headers;options;retry;constructor(e){this.options={cache:e.cache,signal:e.signal},this.baseUrl=e.baseUrl.replace(/\/$/,""),this.headers={"Content-Type":"application/json",...e.headers},typeof e?.retry=="boolean"&&e?.retry===!1?this.retry={attempts:1,backoff:()=>0}:this.retry={attempts:e?.retry?.retries??5,backoff:e?.retry?.backoff??(t=>Math.exp(t)*50)}}async request(e){let t={cache:this.options.cache,method:"POST",headers:this.headers,body:JSON.stringify(e.body),keepalive:!0,signal:this.options.signal},r=null,a=null;for(let y=0;y<=this.retry.attempts;y++)try{r=await fetch([this.baseUrl,...e.path??[]].join("/"),t);break}catch(b){if(this.options.signal?.aborted){let R=new Blob([JSON.stringify({result:this.options.signal.reason??"Aborted"})]),g={status:200,statusText:this.options.signal.reason??"Aborted"};r=new Response(R,g);break}a=b,await new Promise(R=>setTimeout(R,this.retry.backoff(y)))}if(!r)throw a??new Error("Exhausted all retries");let T=await r.json();if(!r.ok)throw new s(`${T.error}, command was: ${JSON.stringify(e.body)}`);return{result:T.result,error:T.error}}};var n=class{payload;endpoint;constructor(e,t){this.payload=e,this.endpoint=t}async exec(e){let{result:t,error:r}=await e.request({body:this.payload,path:[this.endpoint]});if(r)throw new s(r);if(typeof t>"u")throw new Error("Request did not return a result");return t}};var p=class extends n{constructor(e){let t=[];Array.isArray(e)?t.push(...e):t.push(e),super(t,"delete")}};var c=class extends n{constructor(e){super(e,"query")}};var d=class extends n{constructor(e){super(e,"upsert")}};var u=class extends n{constructor([e,t]){super({ids:e,...t},"fetch")}};var m=class extends n{constructor(e){super(e,"range")}};var l=class extends n{constructor(){super([],"reset")}};var h=class extends n{constructor(){super([],"info")}};var f=class{client;constructor(e){this.client=e}delete=e=>new p(e).exec(this.client);query=e=>new c(e).exec(this.client);upsert=e=>new d(e).exec(this.client);fetch=(...e)=>new u(e).exec(this.client);reset=()=>new l().exec(this.client);range=e=>new m(e).exec(this.client);info=()=>new h().exec(this.client)};var x=class o extends f{constructor(e){if(typeof e<"u"&&"request"in e){super(e);return}let t=e?.token??process.env.NEXT_PUBLIC_UPSTASH_VECTOR_REST_TOKEN??process.env.UPSTASH_VECTOR_REST_TOKEN,r=e?.url??process.env.NEXT_PUBLIC_UPSTASH_VECTOR_REST_URL??process.env.UPSTASH_VECTOR_REST_URL;if(!t)throw new Error("UPSTASH_VECTOR_REST_TOKEN is missing!");if(!r)throw new Error("UPSTASH_VECTOR_REST_URL is missing!");(r.startsWith(" ")||r.endsWith(" ")||/\r|\n/.test(r))&&console.warn("The vector url contains whitespace or newline, which can cause errors!"),(t.startsWith(" ")||t.endsWith(" ")||/\r|\n/.test(t))&&console.warn("The vector token contains whitespace or newline, which can cause errors!");let a=new i({baseUrl:r,retry:e?.retry,headers:{authorization:`Bearer ${t}`},cache:e?.cache||"no-store",signal:e?.signal});super(a)}static fromEnv(e){let t=process?.env.UPSTASH_VECTOR_REST_URL;if(!t)throw new Error("Unable to find environment variable: `UPSTASH_VECTOR_REST_URL`");let r=process?.env.UPSTASH_VECTOR_REST_TOKEN;if(!r)throw new Error("Unable to find environment variable: `UPSTASH_VECTOR_REST_TOKEN`");return new o({...e,url:t,token:r})}};export{x as Index};
package/package.json CHANGED
@@ -1 +1 @@
1
- { "name": "@upstash/vector", "description": "An HTTP/REST based Vector DB client built on top of Upstash REST API.", "module": "./dist/index.mjs", "main": "./dist/index.js", "types": "./dist/index.d.ts", "version": "v1.0.1", "keywords": [ "vector", "upstash", "db" ], "author": "Oguzhan Olguncu <oguzhan@upstash.com>", "license": "MIT", "files": [ "dist" ], "bugs": { "url": "https://github.com/upstash/vector/issues" }, "scripts": { "test": "bun test src --coverage --bail --coverageSkipTestFiles=[test-utils.ts]", "fmt": "bunx biome check --apply ./src", "build": "tsup", "prepare": "husky install" }, "devDependencies": { "typescript": "^5.0.0", "@biomejs/biome": "^1.4.1", "bun-types": "latest", "husky": "^8.0.3", "tsup": "latest" } }
1
+ { "name": "@upstash/vector", "description": "An HTTP/REST based Vector DB client built on top of Upstash REST API.", "module": "./dist/index.mjs", "main": "./dist/index.js", "types": "./dist/index.d.ts", "version": "v1.0.2", "keywords": [ "vector", "upstash", "db" ], "author": "Oguzhan Olguncu <oguzhan@upstash.com>", "license": "MIT", "files": [ "dist" ], "bugs": { "url": "https://github.com/upstash/vector/issues" }, "scripts": { "test": "bun test src --coverage --bail --coverageSkipTestFiles=[test-utils.ts]", "fmt": "bunx biome check --apply ./src", "build": "tsup", "prepare": "husky install" }, "devDependencies": { "typescript": "^5.0.0", "@biomejs/biome": "^1.4.1", "@commitlint/cli": "^18.6.0", "@commitlint/config-conventional": "^18.6.0", "bun-types": "latest", "husky": "^8.0.3", "tsup": "latest" }, "repository": { "type": "git", "url": "https://github.com/upstash/vector-js" }, "homepage": "https://upstash.com/vector" }