@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.
Files changed (138) hide show
  1. package/README.md +234 -19
  2. package/dist/cache/lru-cache.d.ts +47 -0
  3. package/dist/cache/lru-cache.d.ts.map +1 -0
  4. package/dist/cache/lru-cache.js +110 -0
  5. package/dist/cache/lru-cache.js.map +1 -0
  6. package/dist/core/bucket-db.d.ts +20 -0
  7. package/dist/core/bucket-db.d.ts.map +1 -0
  8. package/dist/core/bucket-db.js +32 -0
  9. package/dist/core/bucket-db.js.map +1 -0
  10. package/dist/core/collection.d.ts +26 -0
  11. package/dist/core/collection.d.ts.map +1 -0
  12. package/dist/core/collection.js +213 -0
  13. package/dist/core/collection.js.map +1 -0
  14. package/dist/core/wal.d.ts +45 -0
  15. package/dist/core/wal.d.ts.map +1 -0
  16. package/dist/core/wal.js +80 -0
  17. package/dist/core/wal.js.map +1 -0
  18. package/dist/index/shard-manager.d.ts +36 -0
  19. package/dist/index/shard-manager.d.ts.map +1 -0
  20. package/dist/index/shard-manager.js +177 -0
  21. package/dist/index/shard-manager.js.map +1 -0
  22. package/dist/index.d.ts +9 -0
  23. package/dist/index.d.ts.map +1 -0
  24. package/dist/index.js +10 -0
  25. package/dist/index.js.map +1 -0
  26. package/dist/query/evaluator.d.ts +10 -0
  27. package/dist/query/evaluator.d.ts.map +1 -0
  28. package/dist/query/evaluator.js +127 -0
  29. package/dist/query/evaluator.js.map +1 -0
  30. package/dist/storage/fs-adapter.d.ts +24 -0
  31. package/dist/storage/fs-adapter.d.ts.map +1 -0
  32. package/dist/storage/fs-adapter.js +208 -0
  33. package/dist/storage/fs-adapter.js.map +1 -0
  34. package/dist/storage/memory-adapter.d.ts +22 -0
  35. package/dist/storage/memory-adapter.d.ts.map +1 -0
  36. package/dist/storage/memory-adapter.js +78 -0
  37. package/dist/storage/memory-adapter.js.map +1 -0
  38. package/dist/storage/oss-adapter.d.ts +26 -0
  39. package/dist/storage/oss-adapter.d.ts.map +1 -0
  40. package/dist/storage/oss-adapter.js +145 -0
  41. package/dist/storage/oss-adapter.js.map +1 -0
  42. package/dist/storage/s3-adapter.d.ts +26 -0
  43. package/dist/storage/s3-adapter.d.ts.map +1 -0
  44. package/dist/storage/s3-adapter.js +170 -0
  45. package/dist/storage/s3-adapter.js.map +1 -0
  46. package/dist/types/collection.d.ts +40 -0
  47. package/dist/types/collection.d.ts.map +1 -0
  48. package/dist/types/collection.js +2 -0
  49. package/dist/types/collection.js.map +1 -0
  50. package/{src/types/document.ts → dist/types/document.d.ts} +6 -10
  51. package/dist/types/document.d.ts.map +1 -0
  52. package/dist/types/document.js +2 -0
  53. package/dist/types/document.js.map +1 -0
  54. package/dist/types/errors.d.ts +32 -0
  55. package/dist/types/errors.d.ts.map +1 -0
  56. package/dist/types/errors.js +48 -0
  57. package/dist/types/errors.js.map +1 -0
  58. package/dist/types/index.d.ts +6 -0
  59. package/dist/types/index.d.ts.map +1 -0
  60. package/dist/types/index.js +3 -0
  61. package/dist/types/index.js.map +1 -0
  62. package/dist/types/query.d.ts +37 -0
  63. package/dist/types/query.d.ts.map +1 -0
  64. package/dist/types/query.js +2 -0
  65. package/dist/types/query.js.map +1 -0
  66. package/dist/types/storage.d.ts +45 -0
  67. package/dist/types/storage.d.ts.map +1 -0
  68. package/dist/types/storage.js +2 -0
  69. package/dist/types/storage.js.map +1 -0
  70. package/dist/utils/hash.d.ts +13 -0
  71. package/dist/utils/hash.d.ts.map +1 -0
  72. package/dist/utils/hash.js +23 -0
  73. package/dist/utils/hash.js.map +1 -0
  74. package/dist/utils/path-validator.d.ts +46 -0
  75. package/dist/utils/path-validator.d.ts.map +1 -0
  76. package/dist/utils/path-validator.js +99 -0
  77. package/dist/utils/path-validator.js.map +1 -0
  78. package/llms.txt +46 -0
  79. package/package.json +14 -3
  80. package/src/core/bucket-db.ts +0 -43
  81. package/src/core/collection.ts +0 -173
  82. package/src/index/shard-manager.ts +0 -142
  83. package/src/index.ts +0 -37
  84. package/src/query/evaluator.ts +0 -87
  85. package/src/storage/ali-oss.d.ts +0 -43
  86. package/src/storage/fs-adapter.ts +0 -222
  87. package/src/storage/memory-adapter.ts +0 -105
  88. package/src/storage/oss-adapter.ts +0 -122
  89. package/src/storage/s3-adapter.ts +0 -151
  90. package/src/types/collection.ts +0 -34
  91. package/src/types/errors.ts +0 -49
  92. package/src/types/index.ts +0 -36
  93. package/src/types/query.ts +0 -50
  94. package/src/types/storage.ts +0 -46
  95. package/src/utils/hash.ts +0 -24
  96. package/tests/core/bucket-db.test.d.ts +0 -2
  97. package/tests/core/bucket-db.test.d.ts.map +0 -1
  98. package/tests/core/bucket-db.test.js +0 -34
  99. package/tests/core/bucket-db.test.js.map +0 -1
  100. package/tests/core/bucket-db.test.ts +0 -50
  101. package/tests/core/collection.test.d.ts +0 -2
  102. package/tests/core/collection.test.d.ts.map +0 -1
  103. package/tests/core/collection.test.js +0 -148
  104. package/tests/core/collection.test.js.map +0 -1
  105. package/tests/core/collection.test.ts +0 -195
  106. package/tests/core/dbpath.test.ts +0 -52
  107. package/tests/index/shard-manager.test.d.ts +0 -2
  108. package/tests/index/shard-manager.test.d.ts.map +0 -1
  109. package/tests/index/shard-manager.test.js +0 -106
  110. package/tests/index/shard-manager.test.js.map +0 -1
  111. package/tests/index/shard-manager.test.ts +0 -129
  112. package/tests/query/evaluator.test.d.ts +0 -2
  113. package/tests/query/evaluator.test.d.ts.map +0 -1
  114. package/tests/query/evaluator.test.js +0 -87
  115. package/tests/query/evaluator.test.js.map +0 -1
  116. package/tests/query/evaluator.test.ts +0 -104
  117. package/tests/storage/fs-adapter.test.ts +0 -111
  118. package/tests/storage/memory-adapter.test.d.ts +0 -2
  119. package/tests/storage/memory-adapter.test.d.ts.map +0 -1
  120. package/tests/storage/memory-adapter.test.js +0 -65
  121. package/tests/storage/memory-adapter.test.js.map +0 -1
  122. package/tests/storage/memory-adapter.test.ts +0 -84
  123. package/tests/storage/oss-adapter.test.d.ts +0 -2
  124. package/tests/storage/oss-adapter.test.d.ts.map +0 -1
  125. package/tests/storage/oss-adapter.test.js +0 -34
  126. package/tests/storage/oss-adapter.test.js.map +0 -1
  127. package/tests/storage/oss-adapter.test.ts +0 -40
  128. package/tests/storage/s3-adapter.test.d.ts +0 -2
  129. package/tests/storage/s3-adapter.test.d.ts.map +0 -1
  130. package/tests/storage/s3-adapter.test.js +0 -51
  131. package/tests/storage/s3-adapter.test.js.map +0 -1
  132. package/tests/storage/s3-adapter.test.ts +0 -62
  133. package/tests/utils/hash.test.d.ts +0 -2
  134. package/tests/utils/hash.test.d.ts.map +0 -1
  135. package/tests/utils/hash.test.js +0 -36
  136. package/tests/utils/hash.test.js.map +0 -1
  137. package/tests/utils/hash.test.ts +0 -42
  138. package/tsconfig.json +0 -12
@@ -1,49 +0,0 @@
1
- /**
2
- * Base error class for BucketDB
3
- */
4
- export class BucketDBError extends Error {
5
- constructor(message: string) {
6
- super(message);
7
- this.name = 'BucketDBError';
8
- }
9
- }
10
-
11
- /**
12
- * Document not found error
13
- */
14
- export class DocumentNotFoundError extends BucketDBError {
15
- constructor(id: string) {
16
- super(`Document not found: ${id}`);
17
- this.name = 'DocumentNotFoundError';
18
- }
19
- }
20
-
21
- /**
22
- * Concurrent update conflict error
23
- */
24
- export class ConcurrentUpdateError extends BucketDBError {
25
- constructor(message: string = 'Concurrent update conflict, please retry') {
26
- super(message);
27
- this.name = 'ConcurrentUpdateError';
28
- }
29
- }
30
-
31
- /**
32
- * Validation error
33
- */
34
- export class ValidationError extends BucketDBError {
35
- constructor(message: string) {
36
- super(message);
37
- this.name = 'ValidationError';
38
- }
39
- }
40
-
41
- /**
42
- * Storage error
43
- */
44
- export class StorageError extends BucketDBError {
45
- constructor(message: string, public readonly cause?: Error) {
46
- super(message);
47
- this.name = 'StorageError';
48
- }
49
- }
@@ -1,36 +0,0 @@
1
- // Document types
2
- export type { Document, InsertDocument, UpdateDocument } from './document.js';
3
-
4
- // Query types
5
- export type {
6
- QueryOperator,
7
- QueryValue,
8
- QueryFilter,
9
- QueryOptions,
10
- UpdateOptions,
11
- } from './query.js';
12
-
13
- // Storage types
14
- export type {
15
- StorageObject,
16
- PutOptions,
17
- StorageAdapter,
18
- StorageCredentials,
19
- StorageAdapterConfig,
20
- } from './storage.js';
21
-
22
- // Collection types
23
- export type {
24
- ICollection,
25
- CollectionMeta,
26
- IndexShard,
27
- } from './collection.js';
28
-
29
- // Error classes
30
- export {
31
- BucketDBError,
32
- DocumentNotFoundError,
33
- ConcurrentUpdateError,
34
- ValidationError,
35
- StorageError,
36
- } from './errors.js';
@@ -1,50 +0,0 @@
1
- /**
2
- * Supported query operators
3
- */
4
- export type QueryOperator =
5
- | '$eq'
6
- | '$ne'
7
- | '$gt'
8
- | '$gte'
9
- | '$lt'
10
- | '$lte'
11
- | '$in'
12
- | '$nin';
13
-
14
- /**
15
- * Query value can be direct value or operator object
16
- */
17
- export type QueryValue<T> =
18
- | T
19
- | {
20
- $eq?: T;
21
- $ne?: T;
22
- $gt?: T;
23
- $gte?: T;
24
- $lt?: T;
25
- $lte?: T;
26
- $in?: T[];
27
- $nin?: T[];
28
- };
29
-
30
- /**
31
- * Query filter for type-safe queries
32
- */
33
- export type QueryFilter<T> = {
34
- [K in keyof T]?: QueryValue<T[K]>;
35
- };
36
-
37
- /**
38
- * Query options for pagination and sorting
39
- */
40
- export interface QueryOptions {
41
- limit?: number;
42
- offset?: number;
43
- }
44
-
45
- /**
46
- * Update options with optimistic locking
47
- */
48
- export interface UpdateOptions {
49
- etag?: string;
50
- }
@@ -1,46 +0,0 @@
1
- /**
2
- * Storage object returned by adapter
3
- */
4
- export interface StorageObject {
5
- data: any;
6
- etag: string;
7
- lastModified: Date;
8
- }
9
-
10
- /**
11
- * Put options for conditional writes
12
- */
13
- export interface PutOptions {
14
- ifMatch?: string;
15
- ifNoneMatch?: string;
16
- }
17
-
18
- /**
19
- * Storage adapter interface (abstraction for S3/OSS)
20
- */
21
- export interface StorageAdapter {
22
- get(key: string): Promise<StorageObject>;
23
- put(key: string, data: any, options?: PutOptions): Promise<{ etag: string }>;
24
- delete(key: string): Promise<void>;
25
- exists(key: string): Promise<boolean>;
26
- listKeys(prefix: string): Promise<string[]>;
27
- batchGet(keys: string[]): Promise<Map<string, StorageObject>>;
28
- }
29
-
30
- /**
31
- * Storage credentials
32
- */
33
- export interface StorageCredentials {
34
- accessKeyId: string;
35
- secretAccessKey: string;
36
- }
37
-
38
- /**
39
- * Storage adapter configuration
40
- */
41
- export interface StorageAdapterConfig {
42
- bucket: string;
43
- region: string;
44
- credentials: StorageCredentials;
45
- endpoint?: string;
46
- }
package/src/utils/hash.ts DELETED
@@ -1,24 +0,0 @@
1
- /**
2
- * Simple string hash function (DJB2 algorithm)
3
- */
4
- export function hashString(str: string): number {
5
- let hash = 5381;
6
- for (let i = 0; i < str.length; i++) {
7
- hash = (hash * 33) ^ str.charCodeAt(i);
8
- }
9
- return hash >>> 0; // Convert to unsigned 32-bit integer
10
- }
11
-
12
- /**
13
- * Get shard ID for a document ID
14
- */
15
- export function getShardId(docId: string, shardCount: number): number {
16
- return hashString(docId) % shardCount;
17
- }
18
-
19
- /**
20
- * Format shard ID as zero-padded string (e.g., "00", "01", "15")
21
- */
22
- export function formatShardId(shardId: number): string {
23
- return shardId.toString().padStart(2, '0');
24
- }
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=bucket-db.test.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"bucket-db.test.d.ts","sourceRoot":"","sources":["bucket-db.test.ts"],"names":[],"mappings":""}
@@ -1,34 +0,0 @@
1
- import { describe, test, expect } from 'bun:test';
2
- import { BucketDB } from '../../src/core/bucket-db';
3
- import { MemoryStorageAdapter } from '../../src/storage/memory-adapter';
4
- describe('BucketDB', () => {
5
- test('creates database instance with memory adapter', () => {
6
- const adapter = new MemoryStorageAdapter();
7
- const db = new BucketDB(adapter, 'test-db');
8
- expect(db).toBeDefined();
9
- });
10
- test('creates collection', () => {
11
- const adapter = new MemoryStorageAdapter();
12
- const db = new BucketDB(adapter, 'test-db');
13
- const users = db.collection('users');
14
- expect(users).toBeDefined();
15
- });
16
- test('same collection name returns same instance', () => {
17
- const adapter = new MemoryStorageAdapter();
18
- const db = new BucketDB(adapter, 'test-db');
19
- const users1 = db.collection('users');
20
- const users2 = db.collection('users');
21
- expect(users1).toBe(users2);
22
- });
23
- test('end-to-end: insert and query across collections', async () => {
24
- const adapter = new MemoryStorageAdapter();
25
- const db = new BucketDB(adapter, 'test-db');
26
- const users = db.collection('users');
27
- await users.insert({ name: 'Alice', email: 'alice@example.com' });
28
- await users.insert({ name: 'Bob', email: 'bob@example.com' });
29
- const results = await users.find({ name: 'Alice' });
30
- expect(results).toHaveLength(1);
31
- expect(results[0].email).toBe('alice@example.com');
32
- });
33
- });
34
- //# sourceMappingURL=bucket-db.test.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"bucket-db.test.js","sourceRoot":"","sources":["bucket-db.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAClD,OAAO,EAAE,QAAQ,EAAE,MAAM,0BAA0B,CAAC;AACpD,OAAO,EAAE,oBAAoB,EAAE,MAAM,kCAAkC,CAAC;AAQxE,QAAQ,CAAC,UAAU,EAAE,GAAG,EAAE;IACxB,IAAI,CAAC,+CAA+C,EAAE,GAAG,EAAE;QACzD,MAAM,OAAO,GAAG,IAAI,oBAAoB,EAAE,CAAC;QAC3C,MAAM,EAAE,GAAG,IAAI,QAAQ,CAAC,OAAO,EAAE,SAAS,CAAC,CAAC;QAE5C,MAAM,CAAC,EAAE,CAAC,CAAC,WAAW,EAAE,CAAC;IAC3B,CAAC,CAAC,CAAC;IAEH,IAAI,CAAC,oBAAoB,EAAE,GAAG,EAAE;QAC9B,MAAM,OAAO,GAAG,IAAI,oBAAoB,EAAE,CAAC;QAC3C,MAAM,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AM,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,IAAI,CAAC,mBAAmB,CAAC,CAAC;IACrD,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
@@ -1,50 +0,0 @@
1
- import { describe, test, expect } from 'bun:test';
2
- import { BucketDB } from '../../src/core/bucket-db';
3
- import { MemoryStorageAdapter } from '../../src/storage/memory-adapter';
4
- import type { Document } from '../../src/index.js';
5
-
6
- interface User extends Document {
7
- name: string;
8
- email: string;
9
- }
10
-
11
- describe('BucketDB', () => {
12
- test('creates database instance with memory adapter', () => {
13
- const adapter = new MemoryStorageAdapter();
14
- const db = new BucketDB(adapter, 'test-db');
15
-
16
- expect(db).toBeDefined();
17
- });
18
-
19
- test('creates collection', () => {
20
- const adapter = new MemoryStorageAdapter();
21
- const db = new BucketDB(adapter, 'test-db');
22
-
23
- const users = db.collection<User>('users');
24
- expect(users).toBeDefined();
25
- });
26
-
27
- test('same collection name returns same instance', () => {
28
- const adapter = new MemoryStorageAdapter();
29
- const db = new BucketDB(adapter, 'test-db');
30
-
31
- const users1 = db.collection<User>('users');
32
- const users2 = db.collection<User>('users');
33
-
34
- expect(users1).toBe(users2);
35
- });
36
-
37
- test('end-to-end: insert and query across collections', async () => {
38
- const adapter = new MemoryStorageAdapter();
39
- const db = new BucketDB(adapter, 'test-db');
40
-
41
- const users = db.collection<User>('users');
42
-
43
- await users.insert({ name: 'Alice', email: 'alice@example.com' });
44
- await users.insert({ name: 'Bob', email: 'bob@example.com' });
45
-
46
- const results = await users.find({ name: 'Alice' });
47
- expect(results).toHaveLength(1);
48
- expect(results[0].email).toBe('alice@example.com');
49
- });
50
- });
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=collection.test.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"collection.test.d.ts","sourceRoot":"","sources":["collection.test.ts"],"names":[],"mappings":""}
@@ -1,148 +0,0 @@
1
- import { describe, test, expect, beforeEach } from 'bun:test';
2
- import { Collection } from '../../src/core/collection';
3
- import { MemoryStorageAdapter } from '../../src/storage/memory-adapter';
4
- import { DocumentNotFoundError, ConcurrentUpdateError } from '../../src/index.js';
5
- describe('Collection', () => {
6
- let adapter;
7
- let collection;
8
- beforeEach(() => {
9
- adapter = new MemoryStorageAdapter();
10
- collection = new Collection(adapter, 'test-db', 'users', { shardCount: 16 });
11
- });
12
- describe('insert', () => {
13
- test('inserts document with generated ID', async () => {
14
- const user = await collection.insert({
15
- name: 'Alice',
16
- age: 25,
17
- email: 'alice@example.com',
18
- status: 'active',
19
- });
20
- expect(user.id).toBeDefined();
21
- expect(user.name).toBe('Alice');
22
- expect(user.age).toBe(25);
23
- expect(user._etag).toBeDefined();
24
- expect(user._createdAt).toBeDefined();
25
- expect(user._updatedAt).toBeDefined();
26
- });
27
- test('inserts multiple documents', async () => {
28
- const user1 = await collection.insert({ name: 'Alice', age: 25, email: 'alice@example.com', status: 'active' });
29
- const user2 = await collection.insert({ name: 'Bob', age: 30, email: 'bob@example.com', status: 'active' });
30
- expect(user1.id).not.toBe(user2.id);
31
- });
32
- });
33
- describe('findById', () => {
34
- test('finds existing document', async () => {
35
- const inserted = await collection.insert({
36
- name: 'Alice',
37
- age: 25,
38
- email: 'alice@example.com',
39
- status: 'active',
40
- });
41
- const found = await collection.findById(inserted.id);
42
- expect(found).toEqual(inserted);
43
- });
44
- test('returns null for non-existent document', async () => {
45
- const found = await collection.findById('non-existent');
46
- expect(found).toBeNull();
47
- });
48
- });
49
- describe('find', () => {
50
- beforeEach(async () => {
51
- await collection.insert({ name: 'Alice', age: 25, email: 'alice@example.com', status: 'active' });
52
- await collection.insert({ name: 'Bob', age: 30, email: 'bob@example.com', status: 'active' });
53
- await collection.insert({ name: 'Charlie', age: 35, email: 'charlie@example.com', status: 'inactive' });
54
- });
55
- test('finds documents by equality filter', async () => {
56
- const results = await collection.find({ status: 'active' });
57
- expect(results).toHaveLength(2);
58
- expect(results.every(r => r.status === 'active')).toBe(true);
59
- });
60
- test('finds documents with operator filter', async () => {
61
- const results = await collection.find({ age: { $gte: 30 } });
62
- expect(results).toHaveLength(2);
63
- expect(results.every(r => r.age >= 30)).toBe(true);
64
- });
65
- test('finds documents with multiple conditions', async () => {
66
- const results = await collection.find({
67
- status: 'active',
68
- age: { $gte: 25, $lt: 35 },
69
- });
70
- expect(results).toHaveLength(2);
71
- });
72
- test('returns empty array when no matches', async () => {
73
- const results = await collection.find({ name: 'David' });
74
- expect(results).toHaveLength(0);
75
- });
76
- test('applies limit option', async () => {
77
- const results = await collection.find({ status: 'active' }, { limit: 1 });
78
- expect(results).toHaveLength(1);
79
- });
80
- test('applies offset option', async () => {
81
- const all = await collection.find({ status: 'active' });
82
- const withOffset = await collection.find({ status: 'active' }, { offset: 1 });
83
- expect(withOffset).toHaveLength(1);
84
- expect(withOffset[0].id).toBe(all[1].id);
85
- });
86
- test('applies limit and offset together', async () => {
87
- const results = await collection.find({}, { limit: 1, offset: 1 });
88
- expect(results).toHaveLength(1);
89
- });
90
- });
91
- describe('update', () => {
92
- test('updates existing document', async () => {
93
- const user = await collection.insert({
94
- name: 'Alice',
95
- age: 25,
96
- email: 'alice@example.com',
97
- status: 'active',
98
- });
99
- // Wait 1ms to ensure different timestamp
100
- await new Promise(resolve => setTimeout(resolve, 1));
101
- const updated = await collection.update(user.id, { age: 26 });
102
- expect(updated.id).toBe(user.id);
103
- expect(updated.age).toBe(26);
104
- expect(updated.name).toBe('Alice');
105
- expect(updated._etag).not.toBe(user._etag);
106
- expect(updated._updatedAt).not.toBe(user._updatedAt);
107
- });
108
- test('throws error when document not found', async () => {
109
- await expect(collection.update('non-existent', { age: 30 })).rejects.toThrow(DocumentNotFoundError);
110
- });
111
- test('optimistic locking with correct etag', async () => {
112
- const user = await collection.insert({
113
- name: 'Alice',
114
- age: 25,
115
- email: 'alice@example.com',
116
- status: 'active',
117
- });
118
- const updated = await collection.update(user.id, { age: 26 }, { etag: user._etag });
119
- expect(updated.age).toBe(26);
120
- });
121
- test('optimistic locking fails with wrong etag', async () => {
122
- const user = await collection.insert({
123
- name: 'Alice',
124
- age: 25,
125
- email: 'alice@example.com',
126
- status: 'active',
127
- });
128
- await expect(collection.update(user.id, { age: 26 }, { etag: 'wrong-etag' })).rejects.toThrow(ConcurrentUpdateError);
129
- });
130
- });
131
- describe('delete', () => {
132
- test('deletes existing document', async () => {
133
- const user = await collection.insert({
134
- name: 'Alice',
135
- age: 25,
136
- email: 'alice@example.com',
137
- status: 'active',
138
- });
139
- await collection.delete(user.id);
140
- const found = await collection.findById(user.id);
141
- expect(found).toBeNull();
142
- });
143
- test('throws error when document not found', async () => {
144
- await expect(collection.delete('non-existent')).rejects.toThrow(DocumentNotFoundError);
145
- });
146
- });
147
- });
148
- //# sourceMappingURL=collection.test.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"collection.test.js","sourceRoot":"","sources":["collection.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,IAAI,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,UAAU,CAAC;AAC9D,OAAO,EAAE,UAAU,EAAE,MAAM,2BAA2B,CAAC;AACvD,OAAO,EAAE,oBAAoB,EAAE,MAAM,kCAAkC,CAAC;AAExE,OAAO,EAAE,qBAAqB,EAAE,qBAAqB,EAAE,MAAM,kBAAkB,CAAC;AAShF,QAAQ,CAAC,YAAY,EAAE,GAAG,EAAE;IAC1B,IAAI,OAA6B,CAAC;IAClC,IAAI,UAA4B,CAAC;IAEjC,UAAU,CAAC,GAAG,EAAE;QACd,OAAO,GAAG,IAAI,oBAAoB,EAAE,CAAC;QACrC,UAAU,GAAG,IAAI,UAAU,CAAO,OAAO,EAAE,SAAS,EAAE,OAAO,EAAE,EAAE,UAAU,EAAE,EAAE,EAAE,CAAC,CAAC;IACrF,CAAC,CAAC,CAAC;IAEH,QAAQ,CAAC,QAAQ,EAAE,GAAG,EAAE;QACtB,IAAI,CAAC,oCAAoC,EAAE,KAAK,IAAI,EAAE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CAAC,QAAQ,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;YACjD,MAAM,CAAC,KAAK,CAAC,CAAC,QAAQ,EAAE,CAAC;QAC3B,CAAC,CAAC,CAAC;QAEH,IAAI,CAAC,sCAAsC,EAAE,KAAK,IAAI,EAAE;YACtD,MAAM,MAAM,CAAC,UAAU,CAAC,MAAM,CAAC,cAAc,CAAC,CAAC,CAAC,OAAO,CAAC,OAAO,CAAC,qBAAqB,CAAC,CAAC;QACzF,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
@@ -1,195 +0,0 @@
1
- import { describe, test, expect, beforeEach } from 'bun:test';
2
- import { Collection } from '../../src/core/collection';
3
- import { MemoryStorageAdapter } from '../../src/storage/memory-adapter';
4
- import type { Document } from '../../src/index.js';
5
- import { DocumentNotFoundError, ConcurrentUpdateError } from '../../src/index.js';
6
-
7
- interface User extends Document {
8
- name: string;
9
- age: number;
10
- email: string;
11
- status: 'active' | 'inactive';
12
- }
13
-
14
- describe('Collection', () => {
15
- let adapter: MemoryStorageAdapter;
16
- let collection: Collection<User>;
17
-
18
- beforeEach(() => {
19
- adapter = new MemoryStorageAdapter();
20
- collection = new Collection<User>(adapter, 'test-db', 'users', { shardCount: 16 });
21
- });
22
-
23
- describe('insert', () => {
24
- test('inserts document with generated ID', async () => {
25
- const user = await collection.insert({
26
- name: 'Alice',
27
- age: 25,
28
- email: 'alice@example.com',
29
- status: 'active',
30
- });
31
-
32
- expect(user.id).toBeDefined();
33
- expect(user.name).toBe('Alice');
34
- expect(user.age).toBe(25);
35
- expect(user._etag).toBeDefined();
36
- expect(user._createdAt).toBeDefined();
37
- expect(user._updatedAt).toBeDefined();
38
- });
39
-
40
- test('inserts multiple documents', async () => {
41
- const user1 = await collection.insert({ name: 'Alice', age: 25, email: 'alice@example.com', status: 'active' });
42
- const user2 = await collection.insert({ name: 'Bob', age: 30, email: 'bob@example.com', status: 'active' });
43
-
44
- expect(user1.id).not.toBe(user2.id);
45
- });
46
- });
47
-
48
- describe('findById', () => {
49
- test('finds existing document', async () => {
50
- const inserted = await collection.insert({
51
- name: 'Alice',
52
- age: 25,
53
- email: 'alice@example.com',
54
- status: 'active',
55
- });
56
-
57
- const found = await collection.findById(inserted.id);
58
- expect(found).toEqual(inserted);
59
- });
60
-
61
- test('returns null for non-existent document', async () => {
62
- const found = await collection.findById('non-existent');
63
- expect(found).toBeNull();
64
- });
65
- });
66
-
67
- describe('find', () => {
68
- beforeEach(async () => {
69
- await collection.insert({ name: 'Alice', age: 25, email: 'alice@example.com', status: 'active' });
70
- await collection.insert({ name: 'Bob', age: 30, email: 'bob@example.com', status: 'active' });
71
- await collection.insert({ name: 'Charlie', age: 35, email: 'charlie@example.com', status: 'inactive' });
72
- });
73
-
74
- test('finds documents by equality filter', async () => {
75
- const results = await collection.find({ status: 'active' });
76
- expect(results).toHaveLength(2);
77
- expect(results.every(r => r.status === 'active')).toBe(true);
78
- });
79
-
80
- test('finds documents with operator filter', async () => {
81
- const results = await collection.find({ age: { $gte: 30 } });
82
- expect(results).toHaveLength(2);
83
- expect(results.every(r => r.age >= 30)).toBe(true);
84
- });
85
-
86
- test('finds documents with multiple conditions', async () => {
87
- const results = await collection.find({
88
- status: 'active',
89
- age: { $gte: 25, $lt: 35 },
90
- });
91
- expect(results).toHaveLength(2);
92
- });
93
-
94
- test('returns empty array when no matches', async () => {
95
- const results = await collection.find({ name: 'David' });
96
- expect(results).toHaveLength(0);
97
- });
98
-
99
- test('applies limit option', async () => {
100
- const results = await collection.find({ status: 'active' }, { limit: 1 });
101
- expect(results).toHaveLength(1);
102
- });
103
-
104
- test('applies offset option', async () => {
105
- const all = await collection.find({ status: 'active' });
106
- const withOffset = await collection.find({ status: 'active' }, { offset: 1 });
107
-
108
- expect(withOffset).toHaveLength(1);
109
- expect(withOffset[0].id).toBe(all[1].id);
110
- });
111
-
112
- test('applies limit and offset together', async () => {
113
- const results = await collection.find({}, { limit: 1, offset: 1 });
114
- expect(results).toHaveLength(1);
115
- });
116
- });
117
-
118
- describe('update', () => {
119
- test('updates existing document', async () => {
120
- const user = await collection.insert({
121
- name: 'Alice',
122
- age: 25,
123
- email: 'alice@example.com',
124
- status: 'active',
125
- });
126
-
127
- // Wait 1ms to ensure different timestamp
128
- await new Promise(resolve => setTimeout(resolve, 1));
129
-
130
- const updated = await collection.update(user.id, { age: 26 });
131
-
132
- expect(updated.id).toBe(user.id);
133
- expect(updated.age).toBe(26);
134
- expect(updated.name).toBe('Alice');
135
- expect(updated._etag).not.toBe(user._etag);
136
- expect(updated._updatedAt).not.toBe(user._updatedAt);
137
- });
138
-
139
- test('throws error when document not found', async () => {
140
- await expect(
141
- collection.update('non-existent', { age: 30 })
142
- ).rejects.toThrow(DocumentNotFoundError);
143
- });
144
-
145
- test('optimistic locking with correct etag', async () => {
146
- const user = await collection.insert({
147
- name: 'Alice',
148
- age: 25,
149
- email: 'alice@example.com',
150
- status: 'active',
151
- });
152
-
153
- const updated = await collection.update(
154
- user.id,
155
- { age: 26 },
156
- { etag: user._etag }
157
- );
158
-
159
- expect(updated.age).toBe(26);
160
- });
161
-
162
- test('optimistic locking fails with wrong etag', async () => {
163
- const user = await collection.insert({
164
- name: 'Alice',
165
- age: 25,
166
- email: 'alice@example.com',
167
- status: 'active',
168
- });
169
-
170
- await expect(
171
- collection.update(user.id, { age: 26 }, { etag: 'wrong-etag' })
172
- ).rejects.toThrow(ConcurrentUpdateError);
173
- });
174
- });
175
-
176
- describe('delete', () => {
177
- test('deletes existing document', async () => {
178
- const user = await collection.insert({
179
- name: 'Alice',
180
- age: 25,
181
- email: 'alice@example.com',
182
- status: 'active',
183
- });
184
-
185
- await collection.delete(user.id);
186
-
187
- const found = await collection.findById(user.id);
188
- expect(found).toBeNull();
189
- });
190
-
191
- test('throws error when document not found', async () => {
192
- await expect(collection.delete('non-existent')).rejects.toThrow(DocumentNotFoundError);
193
- });
194
- });
195
- });