@brightchain/db 0.20.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 +81 -0
- package/package.json +18 -0
- package/src/__tests__/helpers/mockBlockStore.d.ts +113 -0
- package/src/__tests__/helpers/mockBlockStore.js +380 -0
- package/src/__tests__/helpers/mockBlockStore.js.map +1 -0
- package/src/index.d.ts +31 -0
- package/src/index.js +78 -0
- package/src/index.js.map +1 -0
- package/src/lib/aggregation.d.ts +18 -0
- package/src/lib/aggregation.js +407 -0
- package/src/lib/aggregation.js.map +1 -0
- package/src/lib/cblIndex.d.ts +268 -0
- package/src/lib/cblIndex.js +856 -0
- package/src/lib/cblIndex.js.map +1 -0
- package/src/lib/collection.d.ts +305 -0
- package/src/lib/collection.js +991 -0
- package/src/lib/collection.js.map +1 -0
- package/src/lib/cursor.d.ts +8 -0
- package/src/lib/cursor.js +13 -0
- package/src/lib/cursor.js.map +1 -0
- package/src/lib/database.d.ts +158 -0
- package/src/lib/database.js +332 -0
- package/src/lib/database.js.map +1 -0
- package/src/lib/errors.d.ts +85 -0
- package/src/lib/errors.js +103 -0
- package/src/lib/errors.js.map +1 -0
- package/src/lib/expressMiddleware.d.ts +57 -0
- package/src/lib/expressMiddleware.js +488 -0
- package/src/lib/expressMiddleware.js.map +1 -0
- package/src/lib/headRegistry.d.ts +60 -0
- package/src/lib/headRegistry.js +216 -0
- package/src/lib/headRegistry.js.map +1 -0
- package/src/lib/indexing.d.ts +7 -0
- package/src/lib/indexing.js +14 -0
- package/src/lib/indexing.js.map +1 -0
- package/src/lib/model.d.ts +162 -0
- package/src/lib/model.js +260 -0
- package/src/lib/model.js.map +1 -0
- package/src/lib/pooledStoreAdapter.d.ts +44 -0
- package/src/lib/pooledStoreAdapter.js +109 -0
- package/src/lib/pooledStoreAdapter.js.map +1 -0
- package/src/lib/queryEngine.d.ts +48 -0
- package/src/lib/queryEngine.js +461 -0
- package/src/lib/queryEngine.js.map +1 -0
- package/src/lib/schemaValidation.d.ts +80 -0
- package/src/lib/schemaValidation.js +353 -0
- package/src/lib/schemaValidation.js.map +1 -0
- package/src/lib/transaction.d.ts +7 -0
- package/src/lib/transaction.js +12 -0
- package/src/lib/transaction.js.map +1 -0
- package/src/lib/types.d.ts +360 -0
- package/src/lib/types.js +6 -0
- package/src/lib/types.js.map +1 -0
- package/src/lib/updateEngine.d.ts +7 -0
- package/src/lib/updateEngine.js +13 -0
- package/src/lib/updateEngine.js.map +1 -0
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Typed error classes for brightchain-db.
|
|
3
|
+
*
|
|
4
|
+
* Provides MongoDB-compatible error names and structured properties
|
|
5
|
+
* so consumers can catch and classify errors programmatically.
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Base class for all brightchain-db errors.
|
|
9
|
+
*/
|
|
10
|
+
export declare class BrightChainDbError extends Error {
|
|
11
|
+
/** Numeric error code (MongoDB-style) */
|
|
12
|
+
readonly code: number;
|
|
13
|
+
constructor(message: string, code: number);
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Thrown when a document is not found by _id.
|
|
17
|
+
*/
|
|
18
|
+
export declare class DocumentNotFoundError extends BrightChainDbError {
|
|
19
|
+
readonly documentId: string;
|
|
20
|
+
readonly collection: string;
|
|
21
|
+
constructor(collection: string, documentId: string);
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Thrown when a unique index constraint is violated.
|
|
25
|
+
* (Re-exported from indexing.ts — use `DuplicateKeyError` from there for the canonical class.)
|
|
26
|
+
*/
|
|
27
|
+
/**
|
|
28
|
+
* Thrown when a document fails schema validation.
|
|
29
|
+
*/
|
|
30
|
+
export declare class ValidationError extends BrightChainDbError {
|
|
31
|
+
/** Field-level validation failures */
|
|
32
|
+
readonly validationErrors: ValidationFieldError[];
|
|
33
|
+
readonly collection: string;
|
|
34
|
+
constructor(collection: string, errors: ValidationFieldError[]);
|
|
35
|
+
}
|
|
36
|
+
export interface ValidationFieldError {
|
|
37
|
+
field: string;
|
|
38
|
+
message: string;
|
|
39
|
+
value?: unknown;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Thrown when a transaction operation is invalid (e.g. commit without active txn).
|
|
43
|
+
*/
|
|
44
|
+
export declare class TransactionError extends BrightChainDbError {
|
|
45
|
+
readonly sessionId?: string;
|
|
46
|
+
constructor(message: string, sessionId?: string);
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Thrown when an index operation fails.
|
|
50
|
+
*/
|
|
51
|
+
export declare class IndexError extends BrightChainDbError {
|
|
52
|
+
readonly indexName?: string;
|
|
53
|
+
constructor(message: string, indexName?: string);
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Thrown when a write concern cannot be satisfied.
|
|
57
|
+
*/
|
|
58
|
+
export declare class WriteConcernError extends BrightChainDbError {
|
|
59
|
+
readonly writeConcern: WriteConcernSpec;
|
|
60
|
+
constructor(message: string, writeConcern: WriteConcernSpec);
|
|
61
|
+
}
|
|
62
|
+
/** Write concern specification */
|
|
63
|
+
export interface WriteConcernSpec {
|
|
64
|
+
/** Number of acknowledgments required (1 = primary only, 'majority' = majority) */
|
|
65
|
+
w?: number | 'majority';
|
|
66
|
+
/** Timeout in ms for write concern acknowledgment */
|
|
67
|
+
wtimeoutMS?: number;
|
|
68
|
+
/** Whether to wait for journal sync */
|
|
69
|
+
journal?: boolean;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Thrown when one or more operations in a bulk write fail.
|
|
73
|
+
*/
|
|
74
|
+
export declare class BulkWriteError extends BrightChainDbError {
|
|
75
|
+
/** The individual write errors indexed by operation position */
|
|
76
|
+
readonly writeErrors: BulkWriteOperationError[];
|
|
77
|
+
/** Count of operations that succeeded */
|
|
78
|
+
readonly successCount: number;
|
|
79
|
+
constructor(writeErrors: BulkWriteOperationError[], successCount: number);
|
|
80
|
+
}
|
|
81
|
+
export interface BulkWriteOperationError {
|
|
82
|
+
index: number;
|
|
83
|
+
code: number;
|
|
84
|
+
message: string;
|
|
85
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Typed error classes for brightchain-db.
|
|
4
|
+
*
|
|
5
|
+
* Provides MongoDB-compatible error names and structured properties
|
|
6
|
+
* so consumers can catch and classify errors programmatically.
|
|
7
|
+
*/
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.BulkWriteError = exports.WriteConcernError = exports.IndexError = exports.TransactionError = exports.ValidationError = exports.DocumentNotFoundError = exports.BrightChainDbError = void 0;
|
|
10
|
+
// ── Base ──
|
|
11
|
+
/**
|
|
12
|
+
* Base class for all brightchain-db errors.
|
|
13
|
+
*/
|
|
14
|
+
class BrightChainDbError extends Error {
|
|
15
|
+
constructor(message, code) {
|
|
16
|
+
super(message);
|
|
17
|
+
this.name = 'BrightChainDbError';
|
|
18
|
+
this.code = code;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
exports.BrightChainDbError = BrightChainDbError;
|
|
22
|
+
// ── Document errors ──
|
|
23
|
+
/**
|
|
24
|
+
* Thrown when a document is not found by _id.
|
|
25
|
+
*/
|
|
26
|
+
class DocumentNotFoundError extends BrightChainDbError {
|
|
27
|
+
constructor(collection, documentId) {
|
|
28
|
+
super(`Document not found: collection "${collection}", _id "${documentId}"`, 404);
|
|
29
|
+
this.name = 'DocumentNotFoundError';
|
|
30
|
+
this.collection = collection;
|
|
31
|
+
this.documentId = documentId;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
exports.DocumentNotFoundError = DocumentNotFoundError;
|
|
35
|
+
// ── Duplicate key (re-export from indexing for convenience, but also provide the typed base) ──
|
|
36
|
+
/**
|
|
37
|
+
* Thrown when a unique index constraint is violated.
|
|
38
|
+
* (Re-exported from indexing.ts — use `DuplicateKeyError` from there for the canonical class.)
|
|
39
|
+
*/
|
|
40
|
+
// ── Validation errors ──
|
|
41
|
+
/**
|
|
42
|
+
* Thrown when a document fails schema validation.
|
|
43
|
+
*/
|
|
44
|
+
class ValidationError extends BrightChainDbError {
|
|
45
|
+
constructor(collection, errors) {
|
|
46
|
+
const summary = errors.map((e) => `${e.field}: ${e.message}`).join('; ');
|
|
47
|
+
super(`Document failed validation for collection "${collection}": ${summary}`, 121);
|
|
48
|
+
this.name = 'ValidationError';
|
|
49
|
+
this.collection = collection;
|
|
50
|
+
this.validationErrors = errors;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
exports.ValidationError = ValidationError;
|
|
54
|
+
// ── Transaction errors ──
|
|
55
|
+
/**
|
|
56
|
+
* Thrown when a transaction operation is invalid (e.g. commit without active txn).
|
|
57
|
+
*/
|
|
58
|
+
class TransactionError extends BrightChainDbError {
|
|
59
|
+
constructor(message, sessionId) {
|
|
60
|
+
super(message, 251); // MongoDB error code for NoSuchTransaction
|
|
61
|
+
this.name = 'TransactionError';
|
|
62
|
+
this.sessionId = sessionId;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
exports.TransactionError = TransactionError;
|
|
66
|
+
// ── Index errors ──
|
|
67
|
+
/**
|
|
68
|
+
* Thrown when an index operation fails.
|
|
69
|
+
*/
|
|
70
|
+
class IndexError extends BrightChainDbError {
|
|
71
|
+
constructor(message, indexName) {
|
|
72
|
+
super(message, 86); // MongoDB error code for IndexKeySpecsConflict
|
|
73
|
+
this.name = 'IndexError';
|
|
74
|
+
this.indexName = indexName;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
exports.IndexError = IndexError;
|
|
78
|
+
// ── Write concern errors ──
|
|
79
|
+
/**
|
|
80
|
+
* Thrown when a write concern cannot be satisfied.
|
|
81
|
+
*/
|
|
82
|
+
class WriteConcernError extends BrightChainDbError {
|
|
83
|
+
constructor(message, writeConcern) {
|
|
84
|
+
super(message, 64); // MongoDB error code for WriteConcernFailed
|
|
85
|
+
this.name = 'WriteConcernError';
|
|
86
|
+
this.writeConcern = writeConcern;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
exports.WriteConcernError = WriteConcernError;
|
|
90
|
+
// ── Bulk write errors ──
|
|
91
|
+
/**
|
|
92
|
+
* Thrown when one or more operations in a bulk write fail.
|
|
93
|
+
*/
|
|
94
|
+
class BulkWriteError extends BrightChainDbError {
|
|
95
|
+
constructor(writeErrors, successCount) {
|
|
96
|
+
super(`Bulk write operation failed: ${writeErrors.length} error(s), ${successCount} succeeded`, 65);
|
|
97
|
+
this.name = 'BulkWriteError';
|
|
98
|
+
this.writeErrors = writeErrors;
|
|
99
|
+
this.successCount = successCount;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
exports.BulkWriteError = BulkWriteError;
|
|
103
|
+
//# sourceMappingURL=errors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../../../../brightchain-db/src/lib/errors.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;AAEH,aAAa;AAEb;;GAEG;AACH,MAAa,kBAAmB,SAAQ,KAAK;IAI3C,YAAY,OAAe,EAAE,IAAY;QACvC,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,oBAAoB,CAAC;QACjC,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;IACnB,CAAC;CACF;AATD,gDASC;AAED,wBAAwB;AAExB;;GAEG;AACH,MAAa,qBAAsB,SAAQ,kBAAkB;IAI3D,YAAY,UAAkB,EAAE,UAAkB;QAChD,KAAK,CACH,mCAAmC,UAAU,WAAW,UAAU,GAAG,EACrE,GAAG,CACJ,CAAC;QACF,IAAI,CAAC,IAAI,GAAG,uBAAuB,CAAC;QACpC,IAAI,CAAC,UAAU,GAAG,UAAU,CAAC;QAC7B,IAAI,CAAC,UAAU,GAAG,UAAU,CAAC;IAC/B,CAAC;CACF;AAbD,sDAaC;AAED,iGAAiG;AA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IAAI,CAAC,IAAI,GAAG,mBAAmB,CAAC;QAChC,IAAI,CAAC,YAAY,GAAG,YAAY,CAAC;IACnC,CAAC;CACF;AARD,8CAQC;AAYD,0BAA0B;AAE1B;;GAEG;AACH,MAAa,cAAe,SAAQ,kBAAkB;IAMpD,YAAY,WAAsC,EAAE,YAAoB;QACtE,KAAK,CACH,gCAAgC,WAAW,CAAC,MAAM,cAAc,YAAY,YAAY,EACxF,EAAE,CACH,CAAC;QACF,IAAI,CAAC,IAAI,GAAG,gBAAgB,CAAC;QAC7B,IAAI,CAAC,WAAW,GAAG,WAAW,CAAC;QAC/B,IAAI,CAAC,YAAY,GAAG,YAAY,CAAC;IACnC,CAAC;CACF;AAfD,wCAeC"}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Express middleware/router for BrightChainDb.
|
|
3
|
+
*
|
|
4
|
+
* Provides a REST API for interacting with the document database:
|
|
5
|
+
*
|
|
6
|
+
* GET /:collection - Find documents (query params as filter)
|
|
7
|
+
* GET /:collection/:id - Find document by ID
|
|
8
|
+
* POST /:collection - Insert one document (body = document)
|
|
9
|
+
* POST /:collection/find - Find with rich filter (body = { filter, sort, limit, skip, projection })
|
|
10
|
+
* POST /:collection/aggregate - Aggregation pipeline (body = { pipeline })
|
|
11
|
+
* PUT /:collection/:id - Replace document
|
|
12
|
+
* PATCH /:collection/:id - Update document (body = update operators)
|
|
13
|
+
* DELETE /:collection/:id - Delete document by ID
|
|
14
|
+
* POST /:collection/insertMany - Bulk insert (body = { documents })
|
|
15
|
+
* POST /:collection/updateMany - Bulk update (body = { filter, update })
|
|
16
|
+
* POST /:collection/deleteMany - Bulk delete (body = { filter })
|
|
17
|
+
* POST /:collection/count - Count documents (body = { filter })
|
|
18
|
+
* POST /:collection/distinct - Distinct values (body = { field, filter })
|
|
19
|
+
* POST /:collection/indexes - Create index (body = { spec, options })
|
|
20
|
+
* DELETE /:collection/indexes/:name - Drop index
|
|
21
|
+
* GET /:collection/indexes - List indexes
|
|
22
|
+
* POST /:collection/bulkWrite - Bulk write operations
|
|
23
|
+
* POST /:collection/cursor - Create a server-side cursor
|
|
24
|
+
* GET /cursors/:cursorId - Fetch next batch from cursor
|
|
25
|
+
* DELETE /cursors/:cursorId - Close a cursor
|
|
26
|
+
*/
|
|
27
|
+
import type { Router } from 'express';
|
|
28
|
+
import { BrightChainDb } from './database';
|
|
29
|
+
/**
|
|
30
|
+
* Options for the DB router.
|
|
31
|
+
*/
|
|
32
|
+
export interface DbRouterOptions {
|
|
33
|
+
/** Restrict access to specific collection names (default: allow all) */
|
|
34
|
+
allowedCollections?: string[];
|
|
35
|
+
/** Maximum response size for find queries (default: 1000) */
|
|
36
|
+
maxResults?: number;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Create an Express router that provides REST access to a BrightChainDb instance.
|
|
40
|
+
*
|
|
41
|
+
* @param db - The database instance
|
|
42
|
+
* @param options - Router options
|
|
43
|
+
* @returns Express router
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* ```typescript
|
|
47
|
+
* import express from 'express';
|
|
48
|
+
* import { BrightChainDb, createDbRouter } from '@brightchain/db';
|
|
49
|
+
*
|
|
50
|
+
* const app = express();
|
|
51
|
+
* app.use(express.json());
|
|
52
|
+
*
|
|
53
|
+
* const db = new BrightChainDb(blockStore);
|
|
54
|
+
* app.use('/api/db', createDbRouter(db));
|
|
55
|
+
* ```
|
|
56
|
+
*/
|
|
57
|
+
export declare function createDbRouter(db: BrightChainDb, options?: DbRouterOptions): Router;
|