@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.
Files changed (56) hide show
  1. package/README.md +81 -0
  2. package/package.json +18 -0
  3. package/src/__tests__/helpers/mockBlockStore.d.ts +113 -0
  4. package/src/__tests__/helpers/mockBlockStore.js +380 -0
  5. package/src/__tests__/helpers/mockBlockStore.js.map +1 -0
  6. package/src/index.d.ts +31 -0
  7. package/src/index.js +78 -0
  8. package/src/index.js.map +1 -0
  9. package/src/lib/aggregation.d.ts +18 -0
  10. package/src/lib/aggregation.js +407 -0
  11. package/src/lib/aggregation.js.map +1 -0
  12. package/src/lib/cblIndex.d.ts +268 -0
  13. package/src/lib/cblIndex.js +856 -0
  14. package/src/lib/cblIndex.js.map +1 -0
  15. package/src/lib/collection.d.ts +305 -0
  16. package/src/lib/collection.js +991 -0
  17. package/src/lib/collection.js.map +1 -0
  18. package/src/lib/cursor.d.ts +8 -0
  19. package/src/lib/cursor.js +13 -0
  20. package/src/lib/cursor.js.map +1 -0
  21. package/src/lib/database.d.ts +158 -0
  22. package/src/lib/database.js +332 -0
  23. package/src/lib/database.js.map +1 -0
  24. package/src/lib/errors.d.ts +85 -0
  25. package/src/lib/errors.js +103 -0
  26. package/src/lib/errors.js.map +1 -0
  27. package/src/lib/expressMiddleware.d.ts +57 -0
  28. package/src/lib/expressMiddleware.js +488 -0
  29. package/src/lib/expressMiddleware.js.map +1 -0
  30. package/src/lib/headRegistry.d.ts +60 -0
  31. package/src/lib/headRegistry.js +216 -0
  32. package/src/lib/headRegistry.js.map +1 -0
  33. package/src/lib/indexing.d.ts +7 -0
  34. package/src/lib/indexing.js +14 -0
  35. package/src/lib/indexing.js.map +1 -0
  36. package/src/lib/model.d.ts +162 -0
  37. package/src/lib/model.js +260 -0
  38. package/src/lib/model.js.map +1 -0
  39. package/src/lib/pooledStoreAdapter.d.ts +44 -0
  40. package/src/lib/pooledStoreAdapter.js +109 -0
  41. package/src/lib/pooledStoreAdapter.js.map +1 -0
  42. package/src/lib/queryEngine.d.ts +48 -0
  43. package/src/lib/queryEngine.js +461 -0
  44. package/src/lib/queryEngine.js.map +1 -0
  45. package/src/lib/schemaValidation.d.ts +80 -0
  46. package/src/lib/schemaValidation.js +353 -0
  47. package/src/lib/schemaValidation.js.map +1 -0
  48. package/src/lib/transaction.d.ts +7 -0
  49. package/src/lib/transaction.js +12 -0
  50. package/src/lib/transaction.js.map +1 -0
  51. package/src/lib/types.d.ts +360 -0
  52. package/src/lib/types.js +6 -0
  53. package/src/lib/types.js.map +1 -0
  54. package/src/lib/updateEngine.d.ts +7 -0
  55. package/src/lib/updateEngine.js +13 -0
  56. package/src/lib/updateEngine.js.map +1 -0
@@ -0,0 +1,360 @@
1
+ /**
2
+ * Common types used throughout brightchain-db
3
+ */
4
+ /** A document ID – opaque string */
5
+ export type DocumentId = string;
6
+ /** The shape of a stored document – any record with an optional _id */
7
+ export type BsonDocument = Record<string, unknown> & {
8
+ _id?: DocumentId;
9
+ };
10
+ /**
11
+ * MongoDB-style filter with query operators.
12
+ *
13
+ * Supports:
14
+ * - Exact match: `{ field: value }`
15
+ * - Comparison: `{ field: { $eq, $ne, $gt, $gte, $lt, $lte } }`
16
+ * - Set: `{ field: { $in: [...], $nin: [...] } }`
17
+ * - Pattern: `{ field: { $regex: /.../ } }`
18
+ * - Existence: `{ field: { $exists: true } }`
19
+ * - Logical: `{ $and: [...], $or: [...], $not: {...}, $nor: [...] }`
20
+ * - Array: `{ field: { $elemMatch: {...} } }`
21
+ */
22
+ export type FilterQuery<T> = {
23
+ [P in keyof T]?: T[P] | FilterOperator<T[P]>;
24
+ } & LogicalOperators<T>;
25
+ export interface FilterOperator<V> {
26
+ $eq?: V;
27
+ $ne?: V;
28
+ $gt?: V;
29
+ $gte?: V;
30
+ $lt?: V;
31
+ $lte?: V;
32
+ $in?: V[];
33
+ $nin?: V[];
34
+ $regex?: RegExp | string;
35
+ $exists?: boolean;
36
+ $not?: FilterOperator<V>;
37
+ $elemMatch?: Record<string, unknown>;
38
+ $size?: number;
39
+ $type?: string;
40
+ $all?: V[];
41
+ }
42
+ export interface LogicalOperators<T> {
43
+ $and?: FilterQuery<T>[];
44
+ $or?: FilterQuery<T>[];
45
+ $nor?: FilterQuery<T>[];
46
+ $not?: FilterQuery<T>;
47
+ }
48
+ /**
49
+ * MongoDB-style update operators
50
+ */
51
+ export interface UpdateOperators<T> {
52
+ $set?: Partial<T>;
53
+ $unset?: {
54
+ [P in keyof T]?: 1 | '' | true;
55
+ };
56
+ $inc?: {
57
+ [P in keyof T]?: number;
58
+ };
59
+ $push?: {
60
+ [P in keyof T]?: unknown;
61
+ };
62
+ $pull?: {
63
+ [P in keyof T]?: unknown;
64
+ };
65
+ $addToSet?: {
66
+ [P in keyof T]?: unknown;
67
+ };
68
+ $min?: Partial<T>;
69
+ $max?: Partial<T>;
70
+ $rename?: {
71
+ [key: string]: string;
72
+ };
73
+ $currentDate?: {
74
+ [P in keyof T]?: true | {
75
+ $type: 'date' | 'timestamp';
76
+ };
77
+ };
78
+ $mul?: {
79
+ [P in keyof T]?: number;
80
+ };
81
+ $pop?: {
82
+ [P in keyof T]?: 1 | -1;
83
+ };
84
+ }
85
+ /** The update document can be either operators or a full replacement */
86
+ export type UpdateQuery<T> = UpdateOperators<T> | Partial<T>;
87
+ /**
88
+ * Sort specification: 1 = ascending, -1 = descending
89
+ */
90
+ export type SortSpec<T = BsonDocument> = {
91
+ [P in keyof T]?: 1 | -1;
92
+ } & Record<string, 1 | -1>;
93
+ /**
94
+ * Projection specification: 1 = include, 0 = exclude
95
+ */
96
+ export type ProjectionSpec<T = BsonDocument> = {
97
+ [P in keyof T]?: 1 | 0;
98
+ } & Record<string, 1 | 0>;
99
+ /**
100
+ * Index specification: 1 = ascending, -1 = descending
101
+ */
102
+ export type IndexSpec = Record<string, 1 | -1>;
103
+ /**
104
+ * Options for index creation
105
+ */
106
+ export interface IndexOptions {
107
+ unique?: boolean;
108
+ name?: string;
109
+ sparse?: boolean;
110
+ background?: boolean;
111
+ /** TTL: automatically expire documents after this many seconds */
112
+ expireAfterSeconds?: number;
113
+ }
114
+ /**
115
+ * Options for find operations
116
+ */
117
+ export interface FindOptions<T = BsonDocument> {
118
+ projection?: ProjectionSpec<T>;
119
+ sort?: SortSpec<T>;
120
+ limit?: number;
121
+ skip?: number;
122
+ session?: ClientSession;
123
+ }
124
+ /**
125
+ * Options for write operations
126
+ */
127
+ export interface WriteOptions {
128
+ session?: ClientSession;
129
+ /** Write concern for this operation */
130
+ writeConcern?: WriteConcern;
131
+ }
132
+ /**
133
+ * Options for update operations
134
+ */
135
+ export interface UpdateOptions extends WriteOptions {
136
+ upsert?: boolean;
137
+ }
138
+ /**
139
+ * Result of an insert operation
140
+ */
141
+ export interface InsertOneResult {
142
+ acknowledged: boolean;
143
+ insertedId: DocumentId;
144
+ }
145
+ export interface InsertManyResult {
146
+ acknowledged: boolean;
147
+ insertedCount: number;
148
+ insertedIds: Record<number, DocumentId>;
149
+ }
150
+ /**
151
+ * Result of an update operation
152
+ */
153
+ export interface UpdateResult {
154
+ acknowledged: boolean;
155
+ matchedCount: number;
156
+ modifiedCount: number;
157
+ upsertedCount: number;
158
+ upsertedId?: DocumentId;
159
+ }
160
+ /**
161
+ * Result of a delete operation
162
+ */
163
+ export interface DeleteResult {
164
+ acknowledged: boolean;
165
+ deletedCount: number;
166
+ }
167
+ /**
168
+ * Result of a replace operation
169
+ */
170
+ export interface ReplaceResult {
171
+ acknowledged: boolean;
172
+ matchedCount: number;
173
+ modifiedCount: number;
174
+ upsertedCount: number;
175
+ upsertedId?: DocumentId;
176
+ }
177
+ /** Change stream event types */
178
+ export type ChangeEventType = 'insert' | 'update' | 'replace' | 'delete';
179
+ export interface ChangeEvent<T = BsonDocument> {
180
+ operationType: ChangeEventType;
181
+ documentKey: {
182
+ _id: DocumentId;
183
+ };
184
+ fullDocument?: T;
185
+ updateDescription?: {
186
+ updatedFields?: Partial<T>;
187
+ removedFields?: string[];
188
+ };
189
+ ns: {
190
+ db: string;
191
+ coll: string;
192
+ };
193
+ timestamp: Date;
194
+ }
195
+ /** Listener for change events */
196
+ export type ChangeListener<T = BsonDocument> = (event: ChangeEvent<T>) => void;
197
+ /**
198
+ * Aggregation pipeline stage types
199
+ */
200
+ export type AggregationStage = {
201
+ $match: Record<string, unknown>;
202
+ } | {
203
+ $group: Record<string, unknown>;
204
+ } | {
205
+ $sort: Record<string, 1 | -1>;
206
+ } | {
207
+ $limit: number;
208
+ } | {
209
+ $skip: number;
210
+ } | {
211
+ $project: Record<string, unknown>;
212
+ } | {
213
+ $unwind: string | {
214
+ path: string;
215
+ preserveNullAndEmptyArrays?: boolean;
216
+ };
217
+ } | {
218
+ $count: string;
219
+ } | {
220
+ $addFields: Record<string, unknown>;
221
+ } | {
222
+ $lookup: {
223
+ from: string;
224
+ localField: string;
225
+ foreignField: string;
226
+ as: string;
227
+ };
228
+ } | {
229
+ $replaceRoot: {
230
+ newRoot: string | Record<string, unknown>;
231
+ };
232
+ } | {
233
+ $out: string;
234
+ } | {
235
+ $sample: {
236
+ size: number;
237
+ };
238
+ } | {
239
+ $facet: Record<string, AggregationStage[]>;
240
+ };
241
+ /**
242
+ * Client session interface for transaction support
243
+ */
244
+ export interface ClientSession {
245
+ /** Unique session ID */
246
+ readonly id: string;
247
+ /** Whether a transaction is currently active */
248
+ readonly inTransaction: boolean;
249
+ /** Start a new transaction */
250
+ startTransaction(): void;
251
+ /** Commit the current transaction */
252
+ commitTransaction(): Promise<void>;
253
+ /** Abort the current transaction */
254
+ abortTransaction(): Promise<void>;
255
+ /** End the session */
256
+ endSession(): void;
257
+ }
258
+ /**
259
+ * Write concern – controls acknowledgment of write operations.
260
+ */
261
+ export interface WriteConcern {
262
+ /** Number of acknowledgments: 0 = fire-and-forget, 1 = primary (default), 'majority' */
263
+ w?: number | 'majority';
264
+ /** Timeout in ms for write concern acknowledgment */
265
+ wtimeoutMS?: number;
266
+ /** Whether to wait for journal commit before acknowledging */
267
+ journal?: boolean;
268
+ }
269
+ /**
270
+ * Read preference – controls which store replicas are read from.
271
+ */
272
+ export type ReadPreference = 'primary' | 'secondary' | 'nearest';
273
+ /**
274
+ * Options for creating a collection with validation and other settings.
275
+ */
276
+ export interface CollectionOptions {
277
+ /** Write concern for all operations on this collection */
278
+ writeConcern?: WriteConcern;
279
+ /** Read preference for all read operations on this collection */
280
+ readPreference?: ReadPreference;
281
+ }
282
+ export type BulkWriteOperation<T extends BsonDocument = BsonDocument> = {
283
+ insertOne: {
284
+ document: T;
285
+ };
286
+ } | {
287
+ updateOne: {
288
+ filter: FilterQuery<T>;
289
+ update: UpdateQuery<T>;
290
+ upsert?: boolean;
291
+ };
292
+ } | {
293
+ updateMany: {
294
+ filter: FilterQuery<T>;
295
+ update: UpdateQuery<T>;
296
+ };
297
+ } | {
298
+ deleteOne: {
299
+ filter: FilterQuery<T>;
300
+ };
301
+ } | {
302
+ deleteMany: {
303
+ filter: FilterQuery<T>;
304
+ };
305
+ } | {
306
+ replaceOne: {
307
+ filter: FilterQuery<T>;
308
+ replacement: T;
309
+ upsert?: boolean;
310
+ };
311
+ };
312
+ export interface BulkWriteOptions extends WriteOptions {
313
+ /** If true, stop on the first error. If false, continue and report all errors. (default: true) */
314
+ ordered?: boolean;
315
+ }
316
+ export interface BulkWriteResult {
317
+ acknowledged: boolean;
318
+ insertedCount: number;
319
+ matchedCount: number;
320
+ modifiedCount: number;
321
+ deletedCount: number;
322
+ upsertedCount: number;
323
+ insertedIds: Record<number, DocumentId>;
324
+ upsertedIds: Record<number, DocumentId>;
325
+ }
326
+ /**
327
+ * Text index options
328
+ */
329
+ export interface TextIndexOptions {
330
+ /** Fields to index for text search, with optional weights */
331
+ fields: Record<string, number>;
332
+ /** Default language for stemming (currently unused – basic tokenisation only) */
333
+ defaultLanguage?: string;
334
+ /** Name of the text index */
335
+ name?: string;
336
+ }
337
+ /**
338
+ * A server-side cursor session for paginated REST access.
339
+ */
340
+ export interface CursorSession {
341
+ /** Unique cursor ID */
342
+ id: string;
343
+ /** Collection name */
344
+ collection: string;
345
+ /** Pre-fetched document IDs (for position tracking) */
346
+ documentIds: DocumentId[];
347
+ /** Current offset position */
348
+ position: number;
349
+ /** Batch size */
350
+ batchSize: number;
351
+ /** Timestamp of last access */
352
+ lastAccessed: number;
353
+ /** Filter used to create this cursor */
354
+ filter: Record<string, unknown>;
355
+ /** Sort used */
356
+ sort?: Record<string, 1 | -1>;
357
+ /** Projection used */
358
+ projection?: Record<string, 0 | 1>;
359
+ }
360
+ export type { CollectionSchema, FieldSchema, SchemaType, } from './schemaValidation';
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ /**
3
+ * Common types used throughout brightchain-db
4
+ */
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../../../../brightchain-db/src/lib/types.ts"],"names":[],"mappings":";AAAA;;GAEG"}
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Update engine – re-exported from @brightchain/brightchain-lib.
3
+ *
4
+ * This module now lives in brightchain-lib/src/lib/db/updateEngine.ts.
5
+ * This file re-exports everything for backward compatibility.
6
+ */
7
+ export { applyUpdate, isOperatorUpdate, } from '@brightchain/brightchain-lib/lib/db/updateEngine';
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isOperatorUpdate = exports.applyUpdate = void 0;
4
+ /**
5
+ * Update engine – re-exported from @brightchain/brightchain-lib.
6
+ *
7
+ * This module now lives in brightchain-lib/src/lib/db/updateEngine.ts.
8
+ * This file re-exports everything for backward compatibility.
9
+ */
10
+ var updateEngine_1 = require("@brightchain/brightchain-lib/lib/db/updateEngine");
11
+ Object.defineProperty(exports, "applyUpdate", { enumerable: true, get: function () { return updateEngine_1.applyUpdate; } });
12
+ Object.defineProperty(exports, "isOperatorUpdate", { enumerable: true, get: function () { return updateEngine_1.isOperatorUpdate; } });
13
+ //# sourceMappingURL=updateEngine.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"updateEngine.js","sourceRoot":"","sources":["../../../../brightchain-db/src/lib/updateEngine.ts"],"names":[],"mappings":";;;AAAA;;;;;GAKG;AACH,iFAG0D;AAFxD,2GAAA,WAAW,OAAA;AACX,gHAAA,gBAAgB,OAAA"}