@hold-baby/bucket-db 0.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/README.md +39 -0
- package/package.json +45 -0
- package/src/core/bucket-db.ts +43 -0
- package/src/core/collection.ts +173 -0
- package/src/index/shard-manager.ts +142 -0
- package/src/index.ts +37 -0
- package/src/query/evaluator.ts +87 -0
- package/src/storage/ali-oss.d.ts +43 -0
- package/src/storage/fs-adapter.ts +222 -0
- package/src/storage/memory-adapter.ts +105 -0
- package/src/storage/oss-adapter.ts +122 -0
- package/src/storage/s3-adapter.ts +151 -0
- package/src/types/collection.ts +34 -0
- package/src/types/document.ts +22 -0
- package/src/types/errors.ts +49 -0
- package/src/types/index.ts +36 -0
- package/src/types/query.ts +50 -0
- package/src/types/storage.ts +46 -0
- package/src/utils/hash.ts +24 -0
- package/tests/core/bucket-db.test.d.ts +2 -0
- package/tests/core/bucket-db.test.d.ts.map +1 -0
- package/tests/core/bucket-db.test.js +34 -0
- package/tests/core/bucket-db.test.js.map +1 -0
- package/tests/core/bucket-db.test.ts +50 -0
- package/tests/core/collection.test.d.ts +2 -0
- package/tests/core/collection.test.d.ts.map +1 -0
- package/tests/core/collection.test.js +148 -0
- package/tests/core/collection.test.js.map +1 -0
- package/tests/core/collection.test.ts +195 -0
- package/tests/core/dbpath.test.ts +52 -0
- package/tests/index/shard-manager.test.d.ts +2 -0
- package/tests/index/shard-manager.test.d.ts.map +1 -0
- package/tests/index/shard-manager.test.js +106 -0
- package/tests/index/shard-manager.test.js.map +1 -0
- package/tests/index/shard-manager.test.ts +129 -0
- package/tests/query/evaluator.test.d.ts +2 -0
- package/tests/query/evaluator.test.d.ts.map +1 -0
- package/tests/query/evaluator.test.js +87 -0
- package/tests/query/evaluator.test.js.map +1 -0
- package/tests/query/evaluator.test.ts +104 -0
- package/tests/storage/fs-adapter.test.ts +111 -0
- package/tests/storage/memory-adapter.test.d.ts +2 -0
- package/tests/storage/memory-adapter.test.d.ts.map +1 -0
- package/tests/storage/memory-adapter.test.js +65 -0
- package/tests/storage/memory-adapter.test.js.map +1 -0
- package/tests/storage/memory-adapter.test.ts +84 -0
- package/tests/storage/oss-adapter.test.d.ts +2 -0
- package/tests/storage/oss-adapter.test.d.ts.map +1 -0
- package/tests/storage/oss-adapter.test.js +34 -0
- package/tests/storage/oss-adapter.test.js.map +1 -0
- package/tests/storage/oss-adapter.test.ts +40 -0
- package/tests/storage/s3-adapter.test.d.ts +2 -0
- package/tests/storage/s3-adapter.test.d.ts.map +1 -0
- package/tests/storage/s3-adapter.test.js +51 -0
- package/tests/storage/s3-adapter.test.js.map +1 -0
- package/tests/storage/s3-adapter.test.ts +62 -0
- package/tests/utils/hash.test.d.ts +2 -0
- package/tests/utils/hash.test.d.ts.map +1 -0
- package/tests/utils/hash.test.js +36 -0
- package/tests/utils/hash.test.js.map +1 -0
- package/tests/utils/hash.test.ts +42 -0
- package/tsconfig.json +12 -0
package/README.md
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# @bucket-db/core
|
|
2
|
+
|
|
3
|
+
Core database engine for BucketDB.
|
|
4
|
+
|
|
5
|
+
See the [main README](../../README.md) for usage and documentation.
|
|
6
|
+
|
|
7
|
+
## API
|
|
8
|
+
|
|
9
|
+
### BucketDB
|
|
10
|
+
|
|
11
|
+
Main database class.
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
const db = new BucketDB(adapter, dbPath, options);
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
### Collection
|
|
18
|
+
|
|
19
|
+
Type-safe collection interface.
|
|
20
|
+
|
|
21
|
+
```typescript
|
|
22
|
+
const collection = db.collection<T>(name);
|
|
23
|
+
|
|
24
|
+
await collection.insert(data);
|
|
25
|
+
await collection.findById(id);
|
|
26
|
+
await collection.find(filter, options);
|
|
27
|
+
await collection.update(id, data, options);
|
|
28
|
+
await collection.delete(id);
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
### Storage Adapters
|
|
32
|
+
|
|
33
|
+
- `MemoryStorageAdapter` - In-memory storage for testing
|
|
34
|
+
- `S3Adapter` - AWS S3 storage
|
|
35
|
+
- `OSSAdapter` - Alibaba Cloud OSS storage
|
|
36
|
+
|
|
37
|
+
## License
|
|
38
|
+
|
|
39
|
+
MIT
|
package/package.json
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@hold-baby/bucket-db",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"types": "./dist/index.d.ts",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"default": "./dist/index.js"
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
"scripts": {
|
|
14
|
+
"build": "tsc",
|
|
15
|
+
"dev": "tsc --watch",
|
|
16
|
+
"test": "bun test",
|
|
17
|
+
"test:watch": "bun test --watch"
|
|
18
|
+
},
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"@aws-sdk/client-s3": "^3.490.0",
|
|
21
|
+
"ali-oss": "^6.18.1"
|
|
22
|
+
},
|
|
23
|
+
"devDependencies": {
|
|
24
|
+
"@types/bun": "latest",
|
|
25
|
+
"typescript": "^5.3.3"
|
|
26
|
+
},
|
|
27
|
+
"publishConfig": {
|
|
28
|
+
"access": "public"
|
|
29
|
+
},
|
|
30
|
+
"keywords": [
|
|
31
|
+
"database",
|
|
32
|
+
"document-db",
|
|
33
|
+
"s3",
|
|
34
|
+
"oss",
|
|
35
|
+
"typescript",
|
|
36
|
+
"cloud-storage"
|
|
37
|
+
],
|
|
38
|
+
"author": "holdbaby5483",
|
|
39
|
+
"license": "MIT",
|
|
40
|
+
"repository": {
|
|
41
|
+
"type": "git",
|
|
42
|
+
"url": "https://github.com/holdbaby5483/bucket-db.git",
|
|
43
|
+
"directory": "packages/core"
|
|
44
|
+
}
|
|
45
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { StorageAdapter, Document } from '../types/index.js';
|
|
2
|
+
import { Collection, CollectionOptions } from './collection.js';
|
|
3
|
+
|
|
4
|
+
export interface BucketDBOptions {
|
|
5
|
+
defaultShardCount?: number;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Main BucketDB class
|
|
10
|
+
*/
|
|
11
|
+
export class BucketDB {
|
|
12
|
+
private collections = new Map<string, Collection<any>>();
|
|
13
|
+
|
|
14
|
+
constructor(
|
|
15
|
+
private adapter: StorageAdapter,
|
|
16
|
+
private dbPath: string,
|
|
17
|
+
private options: BucketDBOptions = {}
|
|
18
|
+
) {}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Get or create a collection
|
|
22
|
+
*/
|
|
23
|
+
collection<T extends Document>(
|
|
24
|
+
name: string,
|
|
25
|
+
options?: CollectionOptions
|
|
26
|
+
): Collection<T> {
|
|
27
|
+
if (this.collections.has(name)) {
|
|
28
|
+
return this.collections.get(name)!;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const collection = new Collection<T>(
|
|
32
|
+
this.adapter,
|
|
33
|
+
this.dbPath,
|
|
34
|
+
name,
|
|
35
|
+
{
|
|
36
|
+
shardCount: options?.shardCount || this.options.defaultShardCount || 16,
|
|
37
|
+
}
|
|
38
|
+
);
|
|
39
|
+
|
|
40
|
+
this.collections.set(name, collection);
|
|
41
|
+
return collection;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
ICollection,
|
|
3
|
+
Document,
|
|
4
|
+
InsertDocument,
|
|
5
|
+
UpdateDocument,
|
|
6
|
+
QueryFilter,
|
|
7
|
+
QueryOptions,
|
|
8
|
+
UpdateOptions,
|
|
9
|
+
StorageAdapter,
|
|
10
|
+
} from '../types/index.js';
|
|
11
|
+
import { DocumentNotFoundError, ConcurrentUpdateError } from '../types/index.js';
|
|
12
|
+
import { ShardManager } from '../index/shard-manager.js';
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Generate UUID v4
|
|
16
|
+
*/
|
|
17
|
+
function generateId(): string {
|
|
18
|
+
return crypto.randomUUID();
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Get current ISO timestamp
|
|
23
|
+
*/
|
|
24
|
+
function now(): string {
|
|
25
|
+
return new Date().toISOString();
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface CollectionOptions {
|
|
29
|
+
shardCount?: number;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Collection class for type-safe CRUD operations
|
|
34
|
+
*/
|
|
35
|
+
export class Collection<T extends Document> implements ICollection<T> {
|
|
36
|
+
private shardManager: ShardManager;
|
|
37
|
+
private basePath: string;
|
|
38
|
+
|
|
39
|
+
constructor(
|
|
40
|
+
private adapter: StorageAdapter,
|
|
41
|
+
private dbPath: string,
|
|
42
|
+
private collectionName: string,
|
|
43
|
+
options: CollectionOptions = {}
|
|
44
|
+
) {
|
|
45
|
+
this.basePath = `${dbPath}/${collectionName}`;
|
|
46
|
+
this.shardManager = new ShardManager(
|
|
47
|
+
adapter,
|
|
48
|
+
this.basePath,
|
|
49
|
+
options.shardCount || 16
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
private getDocPath(id: string): string {
|
|
54
|
+
return `${this.basePath}/docs/${id}.json`;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
async insert(data: InsertDocument<T>): Promise<T> {
|
|
58
|
+
const id = generateId();
|
|
59
|
+
const timestamp = now();
|
|
60
|
+
|
|
61
|
+
const document: T = {
|
|
62
|
+
...data,
|
|
63
|
+
id,
|
|
64
|
+
_createdAt: timestamp,
|
|
65
|
+
_updatedAt: timestamp,
|
|
66
|
+
} as T;
|
|
67
|
+
|
|
68
|
+
// Write document to storage
|
|
69
|
+
const { etag } = await this.adapter.put(this.getDocPath(id), document);
|
|
70
|
+
|
|
71
|
+
document._etag = etag;
|
|
72
|
+
|
|
73
|
+
// Add to index
|
|
74
|
+
await this.shardManager.addDocument(document);
|
|
75
|
+
|
|
76
|
+
return document;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
async findById(id: string): Promise<T | null> {
|
|
80
|
+
try {
|
|
81
|
+
const { data, etag } = await this.adapter.get(this.getDocPath(id));
|
|
82
|
+
const doc = data as T;
|
|
83
|
+
doc._etag = etag;
|
|
84
|
+
return doc;
|
|
85
|
+
} catch (error) {
|
|
86
|
+
return null;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
async find(filter: QueryFilter<T>, options?: QueryOptions): Promise<T[]> {
|
|
91
|
+
// Query index to get matching document IDs
|
|
92
|
+
const indexResults = await this.shardManager.query(filter);
|
|
93
|
+
|
|
94
|
+
// Get full documents
|
|
95
|
+
const ids = indexResults.map(doc => doc.id);
|
|
96
|
+
|
|
97
|
+
if (ids.length === 0) {
|
|
98
|
+
return [];
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// Batch get documents
|
|
102
|
+
const docMap = await this.adapter.batchGet(ids.map(id => this.getDocPath(id)));
|
|
103
|
+
|
|
104
|
+
let results = Array.from(docMap.values()).map(obj => {
|
|
105
|
+
const doc = obj.data as T;
|
|
106
|
+
doc._etag = obj.etag;
|
|
107
|
+
return doc;
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
// Apply pagination
|
|
111
|
+
if (options?.offset) {
|
|
112
|
+
results = results.slice(options.offset);
|
|
113
|
+
}
|
|
114
|
+
if (options?.limit) {
|
|
115
|
+
results = results.slice(0, options.limit);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
return results;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
async update(
|
|
122
|
+
id: string,
|
|
123
|
+
data: UpdateDocument<T>,
|
|
124
|
+
options?: UpdateOptions
|
|
125
|
+
): Promise<T> {
|
|
126
|
+
// Get current document
|
|
127
|
+
const current = await this.findById(id);
|
|
128
|
+
if (!current) {
|
|
129
|
+
throw new DocumentNotFoundError(id);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// Check optimistic lock if etag provided
|
|
133
|
+
if (options?.etag && current._etag !== options.etag) {
|
|
134
|
+
throw new ConcurrentUpdateError(`ETag mismatch for document ${id}`);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// Merge updates
|
|
138
|
+
const updated: T = {
|
|
139
|
+
...current,
|
|
140
|
+
...data,
|
|
141
|
+
id,
|
|
142
|
+
_updatedAt: now(),
|
|
143
|
+
} as T;
|
|
144
|
+
|
|
145
|
+
// Write document
|
|
146
|
+
const { etag } = await this.adapter.put(
|
|
147
|
+
this.getDocPath(id),
|
|
148
|
+
updated,
|
|
149
|
+
current._etag ? { ifMatch: current._etag } : undefined
|
|
150
|
+
);
|
|
151
|
+
|
|
152
|
+
updated._etag = etag;
|
|
153
|
+
|
|
154
|
+
// Update index
|
|
155
|
+
await this.shardManager.updateDocument(updated);
|
|
156
|
+
|
|
157
|
+
return updated;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
async delete(id: string): Promise<void> {
|
|
161
|
+
// Check if document exists
|
|
162
|
+
const exists = await this.findById(id);
|
|
163
|
+
if (!exists) {
|
|
164
|
+
throw new DocumentNotFoundError(id);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// Delete from storage
|
|
168
|
+
await this.adapter.delete(this.getDocPath(id));
|
|
169
|
+
|
|
170
|
+
// Remove from index
|
|
171
|
+
await this.shardManager.removeDocument(id);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import type { StorageAdapter, IndexShard, QueryFilter } from '../types/index.js';
|
|
2
|
+
import { StorageError } from '../types/index.js';
|
|
3
|
+
import { getShardId, formatShardId } from '../utils/hash.js';
|
|
4
|
+
import { evaluateFilter } from '../query/evaluator.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Manages index shards for a collection
|
|
8
|
+
*/
|
|
9
|
+
export class ShardManager {
|
|
10
|
+
constructor(
|
|
11
|
+
private adapter: StorageAdapter,
|
|
12
|
+
private collectionPath: string,
|
|
13
|
+
private shardCount: number
|
|
14
|
+
) {}
|
|
15
|
+
|
|
16
|
+
private getShardPath(shardId: number): string {
|
|
17
|
+
return `${this.collectionPath}/index/shard-${formatShardId(shardId)}.json`;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
private async readShard(shardId: number): Promise<IndexShard> {
|
|
21
|
+
const path = this.getShardPath(shardId);
|
|
22
|
+
try {
|
|
23
|
+
const { data } = await this.adapter.get(path);
|
|
24
|
+
return data as IndexShard;
|
|
25
|
+
} catch (error) {
|
|
26
|
+
// Return empty shard if not found
|
|
27
|
+
return {
|
|
28
|
+
shardId: formatShardId(shardId),
|
|
29
|
+
documents: {},
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
private async writeShard(shardId: number, shard: IndexShard, etag?: string): Promise<void> {
|
|
35
|
+
const path = this.getShardPath(shardId);
|
|
36
|
+
await this.adapter.put(path, shard, etag ? { ifMatch: etag } : undefined);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Add document to index
|
|
41
|
+
*/
|
|
42
|
+
async addDocument(doc: Record<string, any>): Promise<void> {
|
|
43
|
+
const docId = doc.id;
|
|
44
|
+
const shardId = getShardId(docId, this.shardCount);
|
|
45
|
+
|
|
46
|
+
// Retry logic for optimistic locking
|
|
47
|
+
let retries = 3;
|
|
48
|
+
while (retries > 0) {
|
|
49
|
+
try {
|
|
50
|
+
const shard = await this.readShard(shardId);
|
|
51
|
+
const currentEtag = shard.documents[docId] ?
|
|
52
|
+
(await this.adapter.get(this.getShardPath(shardId))).etag :
|
|
53
|
+
undefined;
|
|
54
|
+
|
|
55
|
+
shard.documents[docId] = { ...doc };
|
|
56
|
+
|
|
57
|
+
await this.writeShard(shardId, shard, currentEtag);
|
|
58
|
+
return;
|
|
59
|
+
} catch (error) {
|
|
60
|
+
if (error instanceof StorageError && error.message.includes('PreconditionFailed')) {
|
|
61
|
+
retries--;
|
|
62
|
+
await new Promise(resolve => setTimeout(resolve, 100 * (4 - retries)));
|
|
63
|
+
continue;
|
|
64
|
+
}
|
|
65
|
+
throw error;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
throw new StorageError('Failed to add document after retries');
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Update document in index
|
|
73
|
+
*/
|
|
74
|
+
async updateDocument(doc: Record<string, any>): Promise<void> {
|
|
75
|
+
await this.addDocument(doc); // Same as add - upsert behavior
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Remove document from index
|
|
80
|
+
*/
|
|
81
|
+
async removeDocument(docId: string): Promise<void> {
|
|
82
|
+
const shardId = getShardId(docId, this.shardCount);
|
|
83
|
+
|
|
84
|
+
let retries = 3;
|
|
85
|
+
while (retries > 0) {
|
|
86
|
+
try {
|
|
87
|
+
const { etag } = await this.adapter.get(this.getShardPath(shardId));
|
|
88
|
+
const shard = await this.readShard(shardId);
|
|
89
|
+
|
|
90
|
+
delete shard.documents[docId];
|
|
91
|
+
|
|
92
|
+
await this.writeShard(shardId, shard, etag);
|
|
93
|
+
return;
|
|
94
|
+
} catch (error) {
|
|
95
|
+
if (error instanceof StorageError && error.message.includes('PreconditionFailed')) {
|
|
96
|
+
retries--;
|
|
97
|
+
await new Promise(resolve => setTimeout(resolve, 100 * (4 - retries)));
|
|
98
|
+
continue;
|
|
99
|
+
}
|
|
100
|
+
// Ignore if shard doesn't exist
|
|
101
|
+
if (error instanceof StorageError && error.message.includes('not found')) {
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
throw error;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Find document by ID
|
|
111
|
+
*/
|
|
112
|
+
async findById(docId: string): Promise<Record<string, any> | null> {
|
|
113
|
+
const shardId = getShardId(docId, this.shardCount);
|
|
114
|
+
const shard = await this.readShard(shardId);
|
|
115
|
+
return shard.documents[docId] || null;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Query documents matching filter
|
|
120
|
+
*/
|
|
121
|
+
async query(filter: QueryFilter<any>): Promise<Array<Record<string, any>>> {
|
|
122
|
+
// Read all shards in parallel
|
|
123
|
+
const shardPromises = Array.from(
|
|
124
|
+
{ length: this.shardCount },
|
|
125
|
+
(_, i) => this.readShard(i)
|
|
126
|
+
);
|
|
127
|
+
|
|
128
|
+
const shards = await Promise.all(shardPromises);
|
|
129
|
+
|
|
130
|
+
// Filter documents across all shards
|
|
131
|
+
const results: Array<Record<string, any>> = [];
|
|
132
|
+
for (const shard of shards) {
|
|
133
|
+
for (const doc of Object.values(shard.documents)) {
|
|
134
|
+
if (evaluateFilter(doc, filter)) {
|
|
135
|
+
results.push(doc);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
return results;
|
|
141
|
+
}
|
|
142
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
// Core classes
|
|
2
|
+
export { BucketDB } from './core/bucket-db.js';
|
|
3
|
+
export { Collection } from './core/collection.js';
|
|
4
|
+
|
|
5
|
+
// Storage adapters
|
|
6
|
+
export { MemoryStorageAdapter } from './storage/memory-adapter.js';
|
|
7
|
+
export { S3Adapter } from './storage/s3-adapter.js';
|
|
8
|
+
export { OSSAdapter } from './storage/oss-adapter.js';
|
|
9
|
+
export { FileSystemAdapter } from './storage/fs-adapter.js';
|
|
10
|
+
|
|
11
|
+
// Re-export types from @bucket-db/types
|
|
12
|
+
export type {
|
|
13
|
+
Document,
|
|
14
|
+
InsertDocument,
|
|
15
|
+
UpdateDocument,
|
|
16
|
+
QueryOperator,
|
|
17
|
+
QueryValue,
|
|
18
|
+
QueryFilter,
|
|
19
|
+
QueryOptions,
|
|
20
|
+
UpdateOptions,
|
|
21
|
+
StorageAdapter,
|
|
22
|
+
StorageObject,
|
|
23
|
+
PutOptions,
|
|
24
|
+
StorageCredentials,
|
|
25
|
+
StorageAdapterConfig,
|
|
26
|
+
ICollection,
|
|
27
|
+
CollectionMeta,
|
|
28
|
+
IndexShard,
|
|
29
|
+
} from './types/index.js';
|
|
30
|
+
|
|
31
|
+
export {
|
|
32
|
+
BucketDBError,
|
|
33
|
+
DocumentNotFoundError,
|
|
34
|
+
ConcurrentUpdateError,
|
|
35
|
+
ValidationError,
|
|
36
|
+
StorageError,
|
|
37
|
+
} from './types/index.js';
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import type { QueryValue, QueryFilter } from '../types/index.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Evaluates a single operator condition
|
|
5
|
+
*/
|
|
6
|
+
export function evaluateOperator<T>(
|
|
7
|
+
value: T,
|
|
8
|
+
condition: QueryValue<T>
|
|
9
|
+
): boolean {
|
|
10
|
+
// Direct value comparison (implicit $eq)
|
|
11
|
+
if (typeof condition !== 'object' || condition === null || Array.isArray(condition)) {
|
|
12
|
+
return value === condition;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const operators = condition as Record<string, any>;
|
|
16
|
+
|
|
17
|
+
// Check all operators (AND logic)
|
|
18
|
+
for (const [op, operand] of Object.entries(operators)) {
|
|
19
|
+
switch (op) {
|
|
20
|
+
case '$eq':
|
|
21
|
+
if (value !== operand) return false;
|
|
22
|
+
break;
|
|
23
|
+
|
|
24
|
+
case '$ne':
|
|
25
|
+
if (value === operand) return false;
|
|
26
|
+
break;
|
|
27
|
+
|
|
28
|
+
case '$gt':
|
|
29
|
+
if (!(value > operand)) return false;
|
|
30
|
+
break;
|
|
31
|
+
|
|
32
|
+
case '$gte':
|
|
33
|
+
if (!(value >= operand)) return false;
|
|
34
|
+
break;
|
|
35
|
+
|
|
36
|
+
case '$lt':
|
|
37
|
+
if (!(value < operand)) return false;
|
|
38
|
+
break;
|
|
39
|
+
|
|
40
|
+
case '$lte':
|
|
41
|
+
if (!(value <= operand)) return false;
|
|
42
|
+
break;
|
|
43
|
+
|
|
44
|
+
case '$in':
|
|
45
|
+
if (!Array.isArray(operand) || !operand.includes(value)) return false;
|
|
46
|
+
break;
|
|
47
|
+
|
|
48
|
+
case '$nin':
|
|
49
|
+
if (!Array.isArray(operand) || operand.includes(value)) return false;
|
|
50
|
+
break;
|
|
51
|
+
|
|
52
|
+
default:
|
|
53
|
+
throw new Error(`Unknown operator: ${op}`);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return true;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Evaluates a complete filter against a document
|
|
62
|
+
*/
|
|
63
|
+
export function evaluateFilter<T extends Record<string, any>>(
|
|
64
|
+
document: T,
|
|
65
|
+
filter: QueryFilter<T>
|
|
66
|
+
): boolean {
|
|
67
|
+
// Empty filter matches all documents
|
|
68
|
+
if (Object.keys(filter).length === 0) {
|
|
69
|
+
return true;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// All conditions must match (AND logic)
|
|
73
|
+
for (const [field, condition] of Object.entries(filter)) {
|
|
74
|
+
const value = document[field];
|
|
75
|
+
|
|
76
|
+
// If field doesn't exist in document, fail the match
|
|
77
|
+
if (value === undefined) {
|
|
78
|
+
return false;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
if (!evaluateOperator(value, condition as QueryValue<any>)) {
|
|
82
|
+
return false;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
return true;
|
|
87
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
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
|
+
}
|