@hold-baby/bucket-db 0.2.0 → 0.3.1
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/README.md +234 -19
- package/dist/cache/lru-cache.d.ts +47 -0
- package/dist/cache/lru-cache.d.ts.map +1 -0
- package/dist/cache/lru-cache.js +110 -0
- package/dist/cache/lru-cache.js.map +1 -0
- package/dist/core/bucket-db.d.ts +20 -0
- package/dist/core/bucket-db.d.ts.map +1 -0
- package/dist/core/bucket-db.js +32 -0
- package/dist/core/bucket-db.js.map +1 -0
- package/dist/core/collection.d.ts +26 -0
- package/dist/core/collection.d.ts.map +1 -0
- package/dist/core/collection.js +213 -0
- package/dist/core/collection.js.map +1 -0
- package/dist/core/wal.d.ts +45 -0
- package/dist/core/wal.d.ts.map +1 -0
- package/dist/core/wal.js +80 -0
- package/dist/core/wal.js.map +1 -0
- package/dist/index/shard-manager.d.ts +36 -0
- package/dist/index/shard-manager.d.ts.map +1 -0
- package/dist/index/shard-manager.js +177 -0
- package/dist/index/shard-manager.js.map +1 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +10 -0
- package/dist/index.js.map +1 -0
- package/dist/query/evaluator.d.ts +10 -0
- package/dist/query/evaluator.d.ts.map +1 -0
- package/dist/query/evaluator.js +127 -0
- package/dist/query/evaluator.js.map +1 -0
- package/dist/storage/fs-adapter.d.ts +24 -0
- package/dist/storage/fs-adapter.d.ts.map +1 -0
- package/dist/storage/fs-adapter.js +208 -0
- package/dist/storage/fs-adapter.js.map +1 -0
- package/dist/storage/memory-adapter.d.ts +22 -0
- package/dist/storage/memory-adapter.d.ts.map +1 -0
- package/dist/storage/memory-adapter.js +78 -0
- package/dist/storage/memory-adapter.js.map +1 -0
- package/dist/storage/oss-adapter.d.ts +26 -0
- package/dist/storage/oss-adapter.d.ts.map +1 -0
- package/dist/storage/oss-adapter.js +145 -0
- package/dist/storage/oss-adapter.js.map +1 -0
- package/dist/storage/s3-adapter.d.ts +26 -0
- package/dist/storage/s3-adapter.d.ts.map +1 -0
- package/dist/storage/s3-adapter.js +170 -0
- package/dist/storage/s3-adapter.js.map +1 -0
- package/dist/types/collection.d.ts +40 -0
- package/dist/types/collection.d.ts.map +1 -0
- package/dist/types/collection.js +2 -0
- package/dist/types/collection.js.map +1 -0
- package/{src/types/document.ts → dist/types/document.d.ts} +6 -10
- package/dist/types/document.d.ts.map +1 -0
- package/dist/types/document.js +2 -0
- package/dist/types/document.js.map +1 -0
- package/dist/types/errors.d.ts +32 -0
- package/dist/types/errors.d.ts.map +1 -0
- package/dist/types/errors.js +48 -0
- package/dist/types/errors.js.map +1 -0
- package/dist/types/index.d.ts +6 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +3 -0
- package/dist/types/index.js.map +1 -0
- package/dist/types/query.d.ts +37 -0
- package/dist/types/query.d.ts.map +1 -0
- package/dist/types/query.js +2 -0
- package/dist/types/query.js.map +1 -0
- package/dist/types/storage.d.ts +45 -0
- package/dist/types/storage.d.ts.map +1 -0
- package/dist/types/storage.js +2 -0
- package/dist/types/storage.js.map +1 -0
- package/dist/utils/hash.d.ts +13 -0
- package/dist/utils/hash.d.ts.map +1 -0
- package/dist/utils/hash.js +23 -0
- package/dist/utils/hash.js.map +1 -0
- package/dist/utils/path-validator.d.ts +46 -0
- package/dist/utils/path-validator.d.ts.map +1 -0
- package/dist/utils/path-validator.js +99 -0
- package/dist/utils/path-validator.js.map +1 -0
- package/llms.txt +46 -0
- package/package.json +14 -3
- package/src/core/bucket-db.ts +0 -43
- package/src/core/collection.ts +0 -173
- package/src/index/shard-manager.ts +0 -142
- package/src/index.ts +0 -37
- package/src/query/evaluator.ts +0 -87
- package/src/storage/ali-oss.d.ts +0 -43
- package/src/storage/fs-adapter.ts +0 -222
- package/src/storage/memory-adapter.ts +0 -105
- package/src/storage/oss-adapter.ts +0 -122
- package/src/storage/s3-adapter.ts +0 -151
- package/src/types/collection.ts +0 -34
- package/src/types/errors.ts +0 -49
- package/src/types/index.ts +0 -36
- package/src/types/query.ts +0 -50
- package/src/types/storage.ts +0 -46
- package/src/utils/hash.ts +0 -24
- package/tests/core/bucket-db.test.d.ts +0 -2
- package/tests/core/bucket-db.test.d.ts.map +0 -1
- package/tests/core/bucket-db.test.js +0 -34
- package/tests/core/bucket-db.test.js.map +0 -1
- package/tests/core/bucket-db.test.ts +0 -50
- package/tests/core/collection.test.d.ts +0 -2
- package/tests/core/collection.test.d.ts.map +0 -1
- package/tests/core/collection.test.js +0 -148
- package/tests/core/collection.test.js.map +0 -1
- package/tests/core/collection.test.ts +0 -195
- package/tests/core/dbpath.test.ts +0 -52
- package/tests/index/shard-manager.test.d.ts +0 -2
- package/tests/index/shard-manager.test.d.ts.map +0 -1
- package/tests/index/shard-manager.test.js +0 -106
- package/tests/index/shard-manager.test.js.map +0 -1
- package/tests/index/shard-manager.test.ts +0 -129
- package/tests/query/evaluator.test.d.ts +0 -2
- package/tests/query/evaluator.test.d.ts.map +0 -1
- package/tests/query/evaluator.test.js +0 -87
- package/tests/query/evaluator.test.js.map +0 -1
- package/tests/query/evaluator.test.ts +0 -104
- package/tests/storage/fs-adapter.test.ts +0 -111
- package/tests/storage/memory-adapter.test.d.ts +0 -2
- package/tests/storage/memory-adapter.test.d.ts.map +0 -1
- package/tests/storage/memory-adapter.test.js +0 -65
- package/tests/storage/memory-adapter.test.js.map +0 -1
- package/tests/storage/memory-adapter.test.ts +0 -84
- package/tests/storage/oss-adapter.test.d.ts +0 -2
- package/tests/storage/oss-adapter.test.d.ts.map +0 -1
- package/tests/storage/oss-adapter.test.js +0 -34
- package/tests/storage/oss-adapter.test.js.map +0 -1
- package/tests/storage/oss-adapter.test.ts +0 -40
- package/tests/storage/s3-adapter.test.d.ts +0 -2
- package/tests/storage/s3-adapter.test.d.ts.map +0 -1
- package/tests/storage/s3-adapter.test.js +0 -51
- package/tests/storage/s3-adapter.test.js.map +0 -1
- package/tests/storage/s3-adapter.test.ts +0 -62
- package/tests/utils/hash.test.d.ts +0 -2
- package/tests/utils/hash.test.d.ts.map +0 -1
- package/tests/utils/hash.test.js +0 -36
- package/tests/utils/hash.test.js.map +0 -1
- package/tests/utils/hash.test.ts +0 -42
- package/tsconfig.json +0 -12
package/src/storage/ali-oss.d.ts
DELETED
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
declare module 'ali-oss' {
|
|
2
|
-
export default class OSS {
|
|
3
|
-
constructor(options: {
|
|
4
|
-
region: string;
|
|
5
|
-
accessKeyId: string;
|
|
6
|
-
accessKeySecret: string;
|
|
7
|
-
bucket: string;
|
|
8
|
-
endpoint?: string;
|
|
9
|
-
});
|
|
10
|
-
|
|
11
|
-
get(key: string): Promise<{
|
|
12
|
-
content: Buffer;
|
|
13
|
-
res: {
|
|
14
|
-
headers: {
|
|
15
|
-
etag?: string;
|
|
16
|
-
'last-modified': string;
|
|
17
|
-
};
|
|
18
|
-
};
|
|
19
|
-
}>;
|
|
20
|
-
|
|
21
|
-
put(
|
|
22
|
-
key: string,
|
|
23
|
-
data: Buffer,
|
|
24
|
-
options?: {
|
|
25
|
-
headers?: Record<string, string>;
|
|
26
|
-
}
|
|
27
|
-
): Promise<{
|
|
28
|
-
res: {
|
|
29
|
-
headers: {
|
|
30
|
-
etag?: string;
|
|
31
|
-
};
|
|
32
|
-
};
|
|
33
|
-
}>;
|
|
34
|
-
|
|
35
|
-
delete(key: string): Promise<void>;
|
|
36
|
-
|
|
37
|
-
head(key: string): Promise<void>;
|
|
38
|
-
|
|
39
|
-
list(options?: { prefix?: string }): Promise<{
|
|
40
|
-
objects?: Array<{ name: string }>;
|
|
41
|
-
}>;
|
|
42
|
-
}
|
|
43
|
-
}
|
|
@@ -1,222 +0,0 @@
|
|
|
1
|
-
import { readFileSync, writeFileSync, unlinkSync, existsSync, mkdirSync, statSync, readdirSync } from 'fs';
|
|
2
|
-
import { join, dirname, relative } from 'path';
|
|
3
|
-
import { createHash } from 'crypto';
|
|
4
|
-
import type {
|
|
5
|
-
StorageAdapter,
|
|
6
|
-
StorageObject,
|
|
7
|
-
PutOptions,
|
|
8
|
-
} from '../types/index.js';
|
|
9
|
-
import { StorageError } from '../types/index.js';
|
|
10
|
-
|
|
11
|
-
export interface FileSystemAdapterConfig {
|
|
12
|
-
basePath: string;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
interface FileMetadata {
|
|
16
|
-
etag: string;
|
|
17
|
-
data: any;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* Local file system storage adapter
|
|
22
|
-
*/
|
|
23
|
-
export class FileSystemAdapter implements StorageAdapter {
|
|
24
|
-
private basePath: string;
|
|
25
|
-
|
|
26
|
-
constructor(config: FileSystemAdapterConfig) {
|
|
27
|
-
this.basePath = config.basePath;
|
|
28
|
-
|
|
29
|
-
// Ensure base directory exists
|
|
30
|
-
if (!existsSync(this.basePath)) {
|
|
31
|
-
mkdirSync(this.basePath, { recursive: true });
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
private getFilePath(key: string): string {
|
|
36
|
-
return join(this.basePath, key);
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
private getMetadataPath(key: string): string {
|
|
40
|
-
return join(this.basePath, key + '.meta');
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
private generateETag(data: any): string {
|
|
44
|
-
const content = JSON.stringify(data);
|
|
45
|
-
return createHash('md5').update(content).digest('hex');
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
private ensureDirectoryExists(filePath: string): void {
|
|
49
|
-
const dir = dirname(filePath);
|
|
50
|
-
if (!existsSync(dir)) {
|
|
51
|
-
mkdirSync(dir, { recursive: true });
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
async get(key: string): Promise<StorageObject> {
|
|
56
|
-
const filePath = this.getFilePath(key);
|
|
57
|
-
const metaPath = this.getMetadataPath(key);
|
|
58
|
-
|
|
59
|
-
try {
|
|
60
|
-
if (!existsSync(filePath)) {
|
|
61
|
-
throw new StorageError(`Object not found: ${key}`);
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
const content = readFileSync(filePath, 'utf-8');
|
|
65
|
-
const data = JSON.parse(content);
|
|
66
|
-
|
|
67
|
-
let etag: string;
|
|
68
|
-
if (existsSync(metaPath)) {
|
|
69
|
-
const metadata: FileMetadata = JSON.parse(readFileSync(metaPath, 'utf-8'));
|
|
70
|
-
etag = metadata.etag;
|
|
71
|
-
} else {
|
|
72
|
-
etag = this.generateETag(data);
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
const stats = statSync(filePath);
|
|
76
|
-
|
|
77
|
-
return {
|
|
78
|
-
data,
|
|
79
|
-
etag,
|
|
80
|
-
lastModified: stats.mtime,
|
|
81
|
-
};
|
|
82
|
-
} catch (error: any) {
|
|
83
|
-
if (error instanceof StorageError) {
|
|
84
|
-
throw error;
|
|
85
|
-
}
|
|
86
|
-
throw new StorageError(`Failed to get object: ${error.message}`, error);
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
async put(key: string, data: any, options?: PutOptions): Promise<{ etag: string }> {
|
|
91
|
-
const filePath = this.getFilePath(key);
|
|
92
|
-
const metaPath = this.getMetadataPath(key);
|
|
93
|
-
|
|
94
|
-
try {
|
|
95
|
-
// Check conditional writes
|
|
96
|
-
if (options?.ifMatch || options?.ifNoneMatch) {
|
|
97
|
-
if (existsSync(metaPath)) {
|
|
98
|
-
const currentMeta: FileMetadata = JSON.parse(readFileSync(metaPath, 'utf-8'));
|
|
99
|
-
|
|
100
|
-
if (options.ifMatch && currentMeta.etag !== options.ifMatch) {
|
|
101
|
-
throw new StorageError('PreconditionFailed: ETag mismatch');
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
if (options.ifNoneMatch && existsSync(filePath)) {
|
|
105
|
-
throw new StorageError('PreconditionFailed: Object already exists');
|
|
106
|
-
}
|
|
107
|
-
} else if (options.ifMatch) {
|
|
108
|
-
// ifMatch provided but no existing metadata
|
|
109
|
-
throw new StorageError('PreconditionFailed: ETag mismatch');
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
// Ensure directory exists
|
|
114
|
-
this.ensureDirectoryExists(filePath);
|
|
115
|
-
|
|
116
|
-
// Generate ETag
|
|
117
|
-
const etag = this.generateETag(data);
|
|
118
|
-
|
|
119
|
-
// Write data file
|
|
120
|
-
writeFileSync(filePath, JSON.stringify(data, null, 2), 'utf-8');
|
|
121
|
-
|
|
122
|
-
// Write metadata file
|
|
123
|
-
const metadata: FileMetadata = { etag, data };
|
|
124
|
-
writeFileSync(metaPath, JSON.stringify(metadata), 'utf-8');
|
|
125
|
-
|
|
126
|
-
return { etag };
|
|
127
|
-
} catch (error: any) {
|
|
128
|
-
if (error instanceof StorageError) {
|
|
129
|
-
throw error;
|
|
130
|
-
}
|
|
131
|
-
throw new StorageError(`Failed to put object: ${error.message}`, error);
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
async delete(key: string): Promise<void> {
|
|
136
|
-
const filePath = this.getFilePath(key);
|
|
137
|
-
const metaPath = this.getMetadataPath(key);
|
|
138
|
-
|
|
139
|
-
try {
|
|
140
|
-
if (existsSync(filePath)) {
|
|
141
|
-
unlinkSync(filePath);
|
|
142
|
-
}
|
|
143
|
-
if (existsSync(metaPath)) {
|
|
144
|
-
unlinkSync(metaPath);
|
|
145
|
-
}
|
|
146
|
-
} catch (error: any) {
|
|
147
|
-
throw new StorageError(`Failed to delete object: ${error.message}`, error);
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
async exists(key: string): Promise<boolean> {
|
|
152
|
-
const filePath = this.getFilePath(key);
|
|
153
|
-
return existsSync(filePath);
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
async listKeys(prefix: string): Promise<string[]> {
|
|
157
|
-
const keys: string[] = [];
|
|
158
|
-
const prefixPath = join(this.basePath, prefix);
|
|
159
|
-
|
|
160
|
-
try {
|
|
161
|
-
const walk = (dir: string): void => {
|
|
162
|
-
if (!existsSync(dir)) {
|
|
163
|
-
return;
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
const files = readdirSync(dir, { withFileTypes: true });
|
|
167
|
-
|
|
168
|
-
for (const file of files) {
|
|
169
|
-
const fullPath = join(dir, file.name);
|
|
170
|
-
|
|
171
|
-
if (file.isDirectory()) {
|
|
172
|
-
walk(fullPath);
|
|
173
|
-
} else if (file.isFile() && !file.name.endsWith('.meta')) {
|
|
174
|
-
// Get relative path from base
|
|
175
|
-
const relativePath = relative(this.basePath, fullPath);
|
|
176
|
-
|
|
177
|
-
// Check if it starts with the prefix
|
|
178
|
-
if (relativePath.startsWith(prefix)) {
|
|
179
|
-
keys.push(relativePath);
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
};
|
|
184
|
-
|
|
185
|
-
// If prefix path exists as a directory, walk it
|
|
186
|
-
if (existsSync(prefixPath) && statSync(prefixPath).isDirectory()) {
|
|
187
|
-
walk(prefixPath);
|
|
188
|
-
} else {
|
|
189
|
-
// Walk from base and filter by prefix
|
|
190
|
-
walk(this.basePath);
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
return keys.sort();
|
|
194
|
-
} catch (error: any) {
|
|
195
|
-
throw new StorageError(`Failed to list keys: ${error.message}`, error);
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
async batchGet(keys: string[]): Promise<Map<string, StorageObject>> {
|
|
200
|
-
const results = new Map<string, StorageObject>();
|
|
201
|
-
|
|
202
|
-
// Fetch in parallel
|
|
203
|
-
const promises = keys.map(async (key) => {
|
|
204
|
-
try {
|
|
205
|
-
const obj = await this.get(key);
|
|
206
|
-
return { key, obj };
|
|
207
|
-
} catch (error) {
|
|
208
|
-
return null;
|
|
209
|
-
}
|
|
210
|
-
});
|
|
211
|
-
|
|
212
|
-
const settled = await Promise.all(promises);
|
|
213
|
-
|
|
214
|
-
for (const result of settled) {
|
|
215
|
-
if (result) {
|
|
216
|
-
results.set(result.key, result.obj);
|
|
217
|
-
}
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
return results;
|
|
221
|
-
}
|
|
222
|
-
}
|
|
@@ -1,105 +0,0 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
StorageAdapter,
|
|
3
|
-
StorageObject,
|
|
4
|
-
PutOptions,
|
|
5
|
-
} from '../types/index.js';
|
|
6
|
-
import { StorageError } from '../types/index.js';
|
|
7
|
-
|
|
8
|
-
interface StorageEntry {
|
|
9
|
-
data: any;
|
|
10
|
-
etag: string;
|
|
11
|
-
lastModified: Date;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* In-memory storage adapter for testing
|
|
16
|
-
*/
|
|
17
|
-
export class MemoryStorageAdapter implements StorageAdapter {
|
|
18
|
-
private storage = new Map<string, StorageEntry>();
|
|
19
|
-
private etagCounter = 0;
|
|
20
|
-
|
|
21
|
-
private generateETag(): string {
|
|
22
|
-
return `etag-${++this.etagCounter}-${Date.now()}`;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
async get(key: string): Promise<StorageObject> {
|
|
26
|
-
const entry = this.storage.get(key);
|
|
27
|
-
if (!entry) {
|
|
28
|
-
throw new StorageError(`Object not found: ${key}`);
|
|
29
|
-
}
|
|
30
|
-
return {
|
|
31
|
-
data: JSON.parse(JSON.stringify(entry.data)), // Deep clone
|
|
32
|
-
etag: entry.etag,
|
|
33
|
-
lastModified: entry.lastModified,
|
|
34
|
-
};
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
async put(
|
|
38
|
-
key: string,
|
|
39
|
-
data: any,
|
|
40
|
-
options?: PutOptions
|
|
41
|
-
): Promise<{ etag: string }> {
|
|
42
|
-
const existing = this.storage.get(key);
|
|
43
|
-
|
|
44
|
-
// Check ifMatch condition
|
|
45
|
-
if (options?.ifMatch) {
|
|
46
|
-
if (!existing || existing.etag !== options.ifMatch) {
|
|
47
|
-
throw new StorageError('PreconditionFailed: ETag mismatch');
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
// Check ifNoneMatch condition
|
|
52
|
-
if (options?.ifNoneMatch && existing) {
|
|
53
|
-
throw new StorageError('PreconditionFailed: Object already exists');
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
const etag = this.generateETag();
|
|
57
|
-
this.storage.set(key, {
|
|
58
|
-
data: JSON.parse(JSON.stringify(data)), // Deep clone
|
|
59
|
-
etag,
|
|
60
|
-
lastModified: new Date(),
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
return { etag };
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
async delete(key: string): Promise<void> {
|
|
67
|
-
this.storage.delete(key);
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
async exists(key: string): Promise<boolean> {
|
|
71
|
-
return this.storage.has(key);
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
async listKeys(prefix: string): Promise<string[]> {
|
|
75
|
-
const keys: string[] = [];
|
|
76
|
-
for (const key of this.storage.keys()) {
|
|
77
|
-
if (key.startsWith(prefix)) {
|
|
78
|
-
keys.push(key);
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
return keys.sort();
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
async batchGet(keys: string[]): Promise<Map<string, StorageObject>> {
|
|
85
|
-
const results = new Map<string, StorageObject>();
|
|
86
|
-
for (const key of keys) {
|
|
87
|
-
const entry = this.storage.get(key);
|
|
88
|
-
if (entry) {
|
|
89
|
-
results.set(key, {
|
|
90
|
-
data: JSON.parse(JSON.stringify(entry.data)),
|
|
91
|
-
etag: entry.etag,
|
|
92
|
-
lastModified: entry.lastModified,
|
|
93
|
-
});
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
return results;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
/**
|
|
100
|
-
* Clear all data (for testing)
|
|
101
|
-
*/
|
|
102
|
-
clear(): void {
|
|
103
|
-
this.storage.clear();
|
|
104
|
-
}
|
|
105
|
-
}
|
|
@@ -1,122 +0,0 @@
|
|
|
1
|
-
import OSS from 'ali-oss';
|
|
2
|
-
import type {
|
|
3
|
-
StorageAdapter,
|
|
4
|
-
StorageObject,
|
|
5
|
-
PutOptions,
|
|
6
|
-
StorageAdapterConfig,
|
|
7
|
-
} from '../types/index.js';
|
|
8
|
-
import { StorageError } from '../types/index.js';
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* Alibaba Cloud OSS storage adapter
|
|
12
|
-
*/
|
|
13
|
-
export class OSSAdapter implements StorageAdapter {
|
|
14
|
-
private client: OSS;
|
|
15
|
-
private bucket: string;
|
|
16
|
-
|
|
17
|
-
constructor(config: StorageAdapterConfig) {
|
|
18
|
-
this.bucket = config.bucket;
|
|
19
|
-
this.client = new OSS({
|
|
20
|
-
region: config.region,
|
|
21
|
-
accessKeyId: config.credentials.accessKeyId,
|
|
22
|
-
accessKeySecret: config.credentials.secretAccessKey,
|
|
23
|
-
bucket: config.bucket,
|
|
24
|
-
endpoint: config.endpoint,
|
|
25
|
-
});
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
async get(key: string): Promise<StorageObject> {
|
|
29
|
-
try {
|
|
30
|
-
const result = await this.client.get(key);
|
|
31
|
-
|
|
32
|
-
return {
|
|
33
|
-
data: JSON.parse(result.content.toString()),
|
|
34
|
-
etag: result.res.headers.etag?.replace(/"/g, '') || '',
|
|
35
|
-
lastModified: new Date(result.res.headers['last-modified']),
|
|
36
|
-
};
|
|
37
|
-
} catch (error: any) {
|
|
38
|
-
if (error.code === 'NoSuchKey') {
|
|
39
|
-
throw new StorageError(`Object not found: ${key}`);
|
|
40
|
-
}
|
|
41
|
-
throw new StorageError(`Failed to get object: ${error.message}`, error);
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
async put(key: string, data: any, options?: PutOptions): Promise<{ etag: string }> {
|
|
46
|
-
try {
|
|
47
|
-
const headers: Record<string, string> = {};
|
|
48
|
-
|
|
49
|
-
if (options?.ifMatch) {
|
|
50
|
-
headers['If-Match'] = options.ifMatch;
|
|
51
|
-
}
|
|
52
|
-
if (options?.ifNoneMatch) {
|
|
53
|
-
headers['If-None-Match'] = options.ifNoneMatch;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
const result = await this.client.put(key, Buffer.from(JSON.stringify(data)), {
|
|
57
|
-
headers,
|
|
58
|
-
});
|
|
59
|
-
|
|
60
|
-
return {
|
|
61
|
-
etag: result.res.headers.etag?.replace(/"/g, '') || '',
|
|
62
|
-
};
|
|
63
|
-
} catch (error: any) {
|
|
64
|
-
if (error.code === 'PreconditionFailed') {
|
|
65
|
-
throw new StorageError('PreconditionFailed: ETag mismatch');
|
|
66
|
-
}
|
|
67
|
-
throw new StorageError(`Failed to put object: ${error.message}`, error);
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
async delete(key: string): Promise<void> {
|
|
72
|
-
try {
|
|
73
|
-
await this.client.delete(key);
|
|
74
|
-
} catch (error: any) {
|
|
75
|
-
throw new StorageError(`Failed to delete object: ${error.message}`, error);
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
async exists(key: string): Promise<boolean> {
|
|
80
|
-
try {
|
|
81
|
-
await this.client.head(key);
|
|
82
|
-
return true;
|
|
83
|
-
} catch (error: any) {
|
|
84
|
-
if (error.code === 'NoSuchKey') {
|
|
85
|
-
return false;
|
|
86
|
-
}
|
|
87
|
-
throw new StorageError(`Failed to check existence: ${error.message}`, error);
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
async listKeys(prefix: string): Promise<string[]> {
|
|
92
|
-
try {
|
|
93
|
-
const result = await this.client.list({ prefix });
|
|
94
|
-
return (result.objects || []).map(obj => obj.name);
|
|
95
|
-
} catch (error: any) {
|
|
96
|
-
throw new StorageError(`Failed to list keys: ${error.message}`, error);
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
async batchGet(keys: string[]): Promise<Map<string, StorageObject>> {
|
|
101
|
-
const results = new Map<string, StorageObject>();
|
|
102
|
-
|
|
103
|
-
const promises = keys.map(async (key) => {
|
|
104
|
-
try {
|
|
105
|
-
const obj = await this.get(key);
|
|
106
|
-
return { key, obj };
|
|
107
|
-
} catch (error) {
|
|
108
|
-
return null;
|
|
109
|
-
}
|
|
110
|
-
});
|
|
111
|
-
|
|
112
|
-
const settled = await Promise.all(promises);
|
|
113
|
-
|
|
114
|
-
for (const result of settled) {
|
|
115
|
-
if (result) {
|
|
116
|
-
results.set(result.key, result.obj);
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
return results;
|
|
121
|
-
}
|
|
122
|
-
}
|
|
@@ -1,151 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
S3Client,
|
|
3
|
-
GetObjectCommand,
|
|
4
|
-
PutObjectCommand,
|
|
5
|
-
DeleteObjectCommand,
|
|
6
|
-
HeadObjectCommand,
|
|
7
|
-
ListObjectsV2Command,
|
|
8
|
-
} from '@aws-sdk/client-s3';
|
|
9
|
-
import type {
|
|
10
|
-
StorageAdapter,
|
|
11
|
-
StorageObject,
|
|
12
|
-
PutOptions,
|
|
13
|
-
StorageAdapterConfig,
|
|
14
|
-
} from '../types/index.js';
|
|
15
|
-
import { StorageError } from '../types/index.js';
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* S3 storage adapter
|
|
19
|
-
*/
|
|
20
|
-
export class S3Adapter implements StorageAdapter {
|
|
21
|
-
private client: S3Client;
|
|
22
|
-
private bucket: string;
|
|
23
|
-
|
|
24
|
-
constructor(config: StorageAdapterConfig) {
|
|
25
|
-
this.bucket = config.bucket;
|
|
26
|
-
this.client = new S3Client({
|
|
27
|
-
region: config.region,
|
|
28
|
-
credentials: config.credentials,
|
|
29
|
-
endpoint: config.endpoint,
|
|
30
|
-
});
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
async get(key: string): Promise<StorageObject> {
|
|
34
|
-
try {
|
|
35
|
-
const command = new GetObjectCommand({
|
|
36
|
-
Bucket: this.bucket,
|
|
37
|
-
Key: key,
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
const response = await this.client.send(command);
|
|
41
|
-
const body = await response.Body?.transformToString();
|
|
42
|
-
|
|
43
|
-
if (!body) {
|
|
44
|
-
throw new StorageError(`Empty response for key: ${key}`);
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
return {
|
|
48
|
-
data: JSON.parse(body),
|
|
49
|
-
etag: response.ETag?.replace(/"/g, '') || '',
|
|
50
|
-
lastModified: response.LastModified || new Date(),
|
|
51
|
-
};
|
|
52
|
-
} catch (error: any) {
|
|
53
|
-
if (error.name === 'NoSuchKey') {
|
|
54
|
-
throw new StorageError(`Object not found: ${key}`);
|
|
55
|
-
}
|
|
56
|
-
throw new StorageError(`Failed to get object: ${error.message}`, error);
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
async put(key: string, data: any, options?: PutOptions): Promise<{ etag: string }> {
|
|
61
|
-
try {
|
|
62
|
-
const command = new PutObjectCommand({
|
|
63
|
-
Bucket: this.bucket,
|
|
64
|
-
Key: key,
|
|
65
|
-
Body: JSON.stringify(data),
|
|
66
|
-
ContentType: 'application/json',
|
|
67
|
-
IfMatch: options?.ifMatch,
|
|
68
|
-
IfNoneMatch: options?.ifNoneMatch,
|
|
69
|
-
});
|
|
70
|
-
|
|
71
|
-
const response = await this.client.send(command);
|
|
72
|
-
|
|
73
|
-
return {
|
|
74
|
-
etag: response.ETag?.replace(/"/g, '') || '',
|
|
75
|
-
};
|
|
76
|
-
} catch (error: any) {
|
|
77
|
-
if (error.name === 'PreconditionFailed') {
|
|
78
|
-
throw new StorageError('PreconditionFailed: ETag mismatch');
|
|
79
|
-
}
|
|
80
|
-
throw new StorageError(`Failed to put object: ${error.message}`, error);
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
async delete(key: string): Promise<void> {
|
|
85
|
-
try {
|
|
86
|
-
const command = new DeleteObjectCommand({
|
|
87
|
-
Bucket: this.bucket,
|
|
88
|
-
Key: key,
|
|
89
|
-
});
|
|
90
|
-
|
|
91
|
-
await this.client.send(command);
|
|
92
|
-
} catch (error: any) {
|
|
93
|
-
throw new StorageError(`Failed to delete object: ${error.message}`, error);
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
async exists(key: string): Promise<boolean> {
|
|
98
|
-
try {
|
|
99
|
-
const command = new HeadObjectCommand({
|
|
100
|
-
Bucket: this.bucket,
|
|
101
|
-
Key: key,
|
|
102
|
-
});
|
|
103
|
-
|
|
104
|
-
await this.client.send(command);
|
|
105
|
-
return true;
|
|
106
|
-
} catch (error: any) {
|
|
107
|
-
if (error.name === 'NotFound' || error.name === 'NoSuchKey') {
|
|
108
|
-
return false;
|
|
109
|
-
}
|
|
110
|
-
throw new StorageError(`Failed to check existence: ${error.message}`, error);
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
async listKeys(prefix: string): Promise<string[]> {
|
|
115
|
-
try {
|
|
116
|
-
const command = new ListObjectsV2Command({
|
|
117
|
-
Bucket: this.bucket,
|
|
118
|
-
Prefix: prefix,
|
|
119
|
-
});
|
|
120
|
-
|
|
121
|
-
const response = await this.client.send(command);
|
|
122
|
-
return (response.Contents || []).map(obj => obj.Key!).filter(Boolean);
|
|
123
|
-
} catch (error: any) {
|
|
124
|
-
throw new StorageError(`Failed to list keys: ${error.message}`, error);
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
async batchGet(keys: string[]): Promise<Map<string, StorageObject>> {
|
|
129
|
-
const results = new Map<string, StorageObject>();
|
|
130
|
-
|
|
131
|
-
// Fetch in parallel
|
|
132
|
-
const promises = keys.map(async (key) => {
|
|
133
|
-
try {
|
|
134
|
-
const obj = await this.get(key);
|
|
135
|
-
return { key, obj };
|
|
136
|
-
} catch (error) {
|
|
137
|
-
return null;
|
|
138
|
-
}
|
|
139
|
-
});
|
|
140
|
-
|
|
141
|
-
const settled = await Promise.all(promises);
|
|
142
|
-
|
|
143
|
-
for (const result of settled) {
|
|
144
|
-
if (result) {
|
|
145
|
-
results.set(result.key, result.obj);
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
return results;
|
|
150
|
-
}
|
|
151
|
-
}
|
package/src/types/collection.ts
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import type { Document, InsertDocument, UpdateDocument } from './document.js';
|
|
2
|
-
import type { QueryFilter, QueryOptions, UpdateOptions } from './query.js';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Collection interface for type-safe CRUD operations
|
|
6
|
-
*/
|
|
7
|
-
export interface ICollection<T extends Document> {
|
|
8
|
-
insert(data: InsertDocument<T>): Promise<T>;
|
|
9
|
-
findById(id: string): Promise<T | null>;
|
|
10
|
-
find(filter: QueryFilter<T>, options?: QueryOptions): Promise<T[]>;
|
|
11
|
-
update(id: string, data: UpdateDocument<T>, options?: UpdateOptions): Promise<T>;
|
|
12
|
-
delete(id: string): Promise<void>;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* Collection metadata
|
|
17
|
-
*/
|
|
18
|
-
export interface CollectionMeta {
|
|
19
|
-
name: string;
|
|
20
|
-
version: number;
|
|
21
|
-
indexShardCount: number;
|
|
22
|
-
indexedFields: string[];
|
|
23
|
-
createdAt: string;
|
|
24
|
-
documentCount: number;
|
|
25
|
-
lastUpdated: string;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* Index shard structure
|
|
30
|
-
*/
|
|
31
|
-
export interface IndexShard {
|
|
32
|
-
shardId: string;
|
|
33
|
-
documents: Record<string, any>;
|
|
34
|
-
}
|