eigen-db 1.0.0 → 1.2.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.
- package/CHANGELOG.md +11 -0
- package/README.md +241 -1
- package/package.json +6 -3
package/CHANGELOG.md
ADDED
package/README.md
CHANGED
|
@@ -1,3 +1,243 @@
|
|
|
1
1
|
# Eigen DB
|
|
2
2
|
|
|
3
|
-
High
|
|
3
|
+
High-performance vector database for the web.
|
|
4
|
+
|
|
5
|
+
`eigen-db` stores and queries embedding vectors in-browser, using:
|
|
6
|
+
|
|
7
|
+
- OPFS (Origin Private File System) for persistence
|
|
8
|
+
- WASM SIMD for fast compute when available
|
|
9
|
+
- JavaScript fallback when WASM SIMD is unavailable
|
|
10
|
+
|
|
11
|
+
## Install
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
npm install eigen-db
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Guide: Set up and query
|
|
18
|
+
|
|
19
|
+
### 1) Open a database
|
|
20
|
+
|
|
21
|
+
```ts
|
|
22
|
+
import { VectorDB } from "eigen-db";
|
|
23
|
+
|
|
24
|
+
const db = await VectorDB.open({
|
|
25
|
+
name: "my-index", // optional, defaults to "default"
|
|
26
|
+
dimensions: 1536, // required
|
|
27
|
+
normalize: true, // optional, defaults to true
|
|
28
|
+
});
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
### 2) Insert vectors
|
|
32
|
+
|
|
33
|
+
```ts
|
|
34
|
+
db.set("doc:1", embedding1);
|
|
35
|
+
db.set("doc:2", embedding2);
|
|
36
|
+
|
|
37
|
+
db.setMany([
|
|
38
|
+
["doc:3", embedding3],
|
|
39
|
+
["doc:4", embedding4],
|
|
40
|
+
]);
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
Notes:
|
|
44
|
+
|
|
45
|
+
- Each vector must be a `Float32Array` with exactly `dimensions` elements.
|
|
46
|
+
- Duplicate keys use last-write-wins semantics.
|
|
47
|
+
|
|
48
|
+
### 3) Query nearest vectors
|
|
49
|
+
|
|
50
|
+
```ts
|
|
51
|
+
const queryVector = embeddingQuery;
|
|
52
|
+
|
|
53
|
+
const results = db.query(queryVector, { topK: 10 });
|
|
54
|
+
|
|
55
|
+
for (let i = 0; i < results.length; i++) {
|
|
56
|
+
const { key, score } = results.get(i);
|
|
57
|
+
console.log(i, key, score);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// Or paginate for UI rendering:
|
|
61
|
+
const page0 = results.getPage(0, 10);
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
### 4) Persist and lifecycle
|
|
65
|
+
|
|
66
|
+
```ts
|
|
67
|
+
await db.flush(); // persist current state
|
|
68
|
+
await db.close(); // flush + mark closed
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
To delete all vectors and storage:
|
|
72
|
+
|
|
73
|
+
```ts
|
|
74
|
+
await db.clear();
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
## Full API Reference
|
|
78
|
+
|
|
79
|
+
## Exports
|
|
80
|
+
|
|
81
|
+
```ts
|
|
82
|
+
export { VectorDB };
|
|
83
|
+
export { ResultSet };
|
|
84
|
+
export type { ResultItem };
|
|
85
|
+
export { VectorCapacityExceededError };
|
|
86
|
+
export type { OpenOptions, OpenOptionsInternal, SetOptions, QueryOptions };
|
|
87
|
+
export { InMemoryStorageProvider, OPFSStorageProvider };
|
|
88
|
+
export type { StorageProvider };
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
### `VectorDB`
|
|
92
|
+
|
|
93
|
+
#### `VectorDB.open(options)`
|
|
94
|
+
|
|
95
|
+
```ts
|
|
96
|
+
static open(options: OpenOptions): Promise<VectorDB>
|
|
97
|
+
static open(options: OpenOptionsInternal): Promise<VectorDB>
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
Opens (or creates) a database instance and loads persisted data.
|
|
101
|
+
|
|
102
|
+
#### Properties
|
|
103
|
+
|
|
104
|
+
- `size: number` — current number of key-vector pairs
|
|
105
|
+
|
|
106
|
+
#### Methods
|
|
107
|
+
|
|
108
|
+
- `set(key: string, value: Float32Array, options?: SetOptions): void`
|
|
109
|
+
- Inserts or overwrites a vector.
|
|
110
|
+
- Throws on dimension mismatch.
|
|
111
|
+
- `get(key: string): Float32Array | undefined`
|
|
112
|
+
- Returns a copy of the stored vector.
|
|
113
|
+
- `setMany(entries: [string, Float32Array][]): void`
|
|
114
|
+
- Batch insert/update.
|
|
115
|
+
- `getMany(keys: string[]): (Float32Array | undefined)[]`
|
|
116
|
+
- Batch lookup.
|
|
117
|
+
- `query(value: Float32Array, options?: QueryOptions): ResultSet`
|
|
118
|
+
- Returns similarity-ranked results.
|
|
119
|
+
- Throws on dimension mismatch.
|
|
120
|
+
- `flush(): Promise<void>`
|
|
121
|
+
- Persists in-memory state to storage.
|
|
122
|
+
- `close(): Promise<void>`
|
|
123
|
+
- Flushes and closes the instance.
|
|
124
|
+
- Subsequent operations throw.
|
|
125
|
+
- `clear(): Promise<void>`
|
|
126
|
+
- Clears in-memory state and destroys storage for this DB.
|
|
127
|
+
|
|
128
|
+
### `ResultSet`
|
|
129
|
+
|
|
130
|
+
Represents a lazily resolved, score-sorted search result collection.
|
|
131
|
+
|
|
132
|
+
#### Properties
|
|
133
|
+
|
|
134
|
+
- `length: number` — number of results available (bounded by `topK`)
|
|
135
|
+
|
|
136
|
+
#### Methods
|
|
137
|
+
|
|
138
|
+
- `get(rank: number): ResultItem`
|
|
139
|
+
- Returns the item at rank (`0` is best match).
|
|
140
|
+
- Throws `RangeError` when out of bounds.
|
|
141
|
+
- `getPage(page: number, pageSize: number): ResultItem[]`
|
|
142
|
+
- Convenience pagination helper.
|
|
143
|
+
|
|
144
|
+
#### Static
|
|
145
|
+
|
|
146
|
+
- `fromScores(scores, resolveKey, topK): ResultSet`
|
|
147
|
+
- Constructs a sorted lazy result set from raw scores.
|
|
148
|
+
|
|
149
|
+
### `ResultItem`
|
|
150
|
+
|
|
151
|
+
```ts
|
|
152
|
+
interface ResultItem {
|
|
153
|
+
key: string;
|
|
154
|
+
score: number;
|
|
155
|
+
}
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
### Option types
|
|
159
|
+
|
|
160
|
+
#### `OpenOptions`
|
|
161
|
+
|
|
162
|
+
```ts
|
|
163
|
+
interface OpenOptions {
|
|
164
|
+
name?: string; // OPFS directory name, default: "default"
|
|
165
|
+
dimensions: number; // vector size
|
|
166
|
+
normalize?: boolean; // default: true
|
|
167
|
+
}
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
#### `OpenOptionsInternal`
|
|
171
|
+
|
|
172
|
+
Advanced/testing override options.
|
|
173
|
+
|
|
174
|
+
```ts
|
|
175
|
+
interface OpenOptionsInternal extends OpenOptions {
|
|
176
|
+
storage?: StorageProvider;
|
|
177
|
+
wasmBinary?: Uint8Array | null;
|
|
178
|
+
}
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
- `storage`: provide custom storage implementation (for example, tests)
|
|
182
|
+
- `wasmBinary`:
|
|
183
|
+
- `Uint8Array`: use provided precompiled WASM
|
|
184
|
+
- `null`: force JavaScript-only compute
|
|
185
|
+
- omitted: use embedded SIMD binary
|
|
186
|
+
|
|
187
|
+
#### `SetOptions`
|
|
188
|
+
|
|
189
|
+
```ts
|
|
190
|
+
interface SetOptions {
|
|
191
|
+
normalize?: boolean;
|
|
192
|
+
}
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
#### `QueryOptions`
|
|
196
|
+
|
|
197
|
+
```ts
|
|
198
|
+
interface QueryOptions {
|
|
199
|
+
topK?: number; // default: all vectors
|
|
200
|
+
normalize?: boolean;
|
|
201
|
+
}
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
### Storage
|
|
205
|
+
|
|
206
|
+
#### `StorageProvider`
|
|
207
|
+
|
|
208
|
+
```ts
|
|
209
|
+
interface StorageProvider {
|
|
210
|
+
readAll(fileName: string): Promise<Uint8Array>;
|
|
211
|
+
append(fileName: string, data: Uint8Array): Promise<void>;
|
|
212
|
+
write(fileName: string, data: Uint8Array): Promise<void>;
|
|
213
|
+
destroy(): Promise<void>;
|
|
214
|
+
}
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
#### `OPFSStorageProvider`
|
|
218
|
+
|
|
219
|
+
Browser persistence provider backed by OPFS.
|
|
220
|
+
|
|
221
|
+
```ts
|
|
222
|
+
new OPFSStorageProvider(dirName: string)
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
#### `InMemoryStorageProvider`
|
|
226
|
+
|
|
227
|
+
Non-persistent in-memory provider, useful for tests or ephemeral sessions.
|
|
228
|
+
|
|
229
|
+
```ts
|
|
230
|
+
new InMemoryStorageProvider();
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
### Errors
|
|
234
|
+
|
|
235
|
+
#### `VectorCapacityExceededError`
|
|
236
|
+
|
|
237
|
+
Thrown when memory growth would exceed WASM 32-bit memory limits for the configured dimension size.
|
|
238
|
+
|
|
239
|
+
## Practical notes
|
|
240
|
+
|
|
241
|
+
- Similarity is dot product; with normalization enabled (default), this behaves like cosine similarity.
|
|
242
|
+
- Querying an empty database returns a `ResultSet` with `length === 0`.
|
|
243
|
+
- `flush()` writes deduplicated state, and reopen preserves key-to-slot mapping.
|
package/package.json
CHANGED
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "eigen-db",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"files": [
|
|
6
6
|
"dist",
|
|
7
|
-
"src/lib"
|
|
7
|
+
"src/lib",
|
|
8
|
+
"CHANGELOG.md",
|
|
9
|
+
"README.md"
|
|
8
10
|
],
|
|
9
11
|
"main": "./dist/",
|
|
12
|
+
"types": "./src/lib/index.ts",
|
|
10
13
|
"scripts": {
|
|
11
14
|
"dev": "vite",
|
|
12
15
|
"compile-wat": "tsx scripts/compile-wat.ts",
|
|
@@ -24,4 +27,4 @@
|
|
|
24
27
|
"vitest": "^4.0.18",
|
|
25
28
|
"wabt": "^1.0.39"
|
|
26
29
|
}
|
|
27
|
-
}
|
|
30
|
+
}
|