@nimbus-cqrs/mongodb 2.0.0-beta.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 (121) hide show
  1. package/LICENSE +85 -0
  2. package/README.md +26 -0
  3. package/esm/_dnt.shims.d.ts +2 -0
  4. package/esm/_dnt.shims.d.ts.map +1 -0
  5. package/esm/_dnt.shims.js +57 -0
  6. package/esm/deps/jsr.io/@std/fmt/1.0.10/colors.d.ts +700 -0
  7. package/esm/deps/jsr.io/@std/fmt/1.0.10/colors.d.ts.map +1 -0
  8. package/esm/deps/jsr.io/@std/fmt/1.0.10/colors.js +903 -0
  9. package/esm/deps/jsr.io/@std/text/1.0.18/_util.d.ts +3 -0
  10. package/esm/deps/jsr.io/@std/text/1.0.18/_util.d.ts.map +1 -0
  11. package/esm/deps/jsr.io/@std/text/1.0.18/_util.js +13 -0
  12. package/esm/deps/jsr.io/@std/text/1.0.18/closest_string.d.ts +42 -0
  13. package/esm/deps/jsr.io/@std/text/1.0.18/closest_string.d.ts.map +1 -0
  14. package/esm/deps/jsr.io/@std/text/1.0.18/closest_string.js +46 -0
  15. package/esm/deps/jsr.io/@std/text/1.0.18/compare_similarity.d.ts +47 -0
  16. package/esm/deps/jsr.io/@std/text/1.0.18/compare_similarity.d.ts.map +1 -0
  17. package/esm/deps/jsr.io/@std/text/1.0.18/compare_similarity.js +39 -0
  18. package/esm/deps/jsr.io/@std/text/1.0.18/levenshtein_distance.d.ts +23 -0
  19. package/esm/deps/jsr.io/@std/text/1.0.18/levenshtein_distance.d.ts.map +1 -0
  20. package/esm/deps/jsr.io/@std/text/1.0.18/levenshtein_distance.js +127 -0
  21. package/esm/deps/jsr.io/@std/text/1.0.18/mod.d.ts +26 -0
  22. package/esm/deps/jsr.io/@std/text/1.0.18/mod.d.ts.map +1 -0
  23. package/esm/deps/jsr.io/@std/text/1.0.18/mod.js +27 -0
  24. package/esm/deps/jsr.io/@std/text/1.0.18/to_camel_case.d.ts +16 -0
  25. package/esm/deps/jsr.io/@std/text/1.0.18/to_camel_case.d.ts.map +1 -0
  26. package/esm/deps/jsr.io/@std/text/1.0.18/to_camel_case.js +22 -0
  27. package/esm/deps/jsr.io/@std/text/1.0.18/to_kebab_case.d.ts +16 -0
  28. package/esm/deps/jsr.io/@std/text/1.0.18/to_kebab_case.d.ts.map +1 -0
  29. package/esm/deps/jsr.io/@std/text/1.0.18/to_kebab_case.js +21 -0
  30. package/esm/deps/jsr.io/@std/text/1.0.18/to_pascal_case.d.ts +16 -0
  31. package/esm/deps/jsr.io/@std/text/1.0.18/to_pascal_case.d.ts.map +1 -0
  32. package/esm/deps/jsr.io/@std/text/1.0.18/to_pascal_case.js +21 -0
  33. package/esm/deps/jsr.io/@std/text/1.0.18/to_snake_case.d.ts +16 -0
  34. package/esm/deps/jsr.io/@std/text/1.0.18/to_snake_case.d.ts.map +1 -0
  35. package/esm/deps/jsr.io/@std/text/1.0.18/to_snake_case.js +21 -0
  36. package/esm/deps/jsr.io/@std/text/1.0.18/word_similarity_sort.d.ts +42 -0
  37. package/esm/deps/jsr.io/@std/text/1.0.18/word_similarity_sort.d.ts.map +1 -0
  38. package/esm/deps/jsr.io/@std/text/1.0.18/word_similarity_sort.js +43 -0
  39. package/esm/deps/jsr.io/@std/ulid/1.0.0/_util.d.ts +14 -0
  40. package/esm/deps/jsr.io/@std/ulid/1.0.0/_util.d.ts.map +1 -0
  41. package/esm/deps/jsr.io/@std/ulid/1.0.0/_util.js +65 -0
  42. package/esm/deps/jsr.io/@std/ulid/1.0.0/decode_time.d.ts +20 -0
  43. package/esm/deps/jsr.io/@std/ulid/1.0.0/decode_time.d.ts.map +1 -0
  44. package/esm/deps/jsr.io/@std/ulid/1.0.0/decode_time.js +43 -0
  45. package/esm/deps/jsr.io/@std/ulid/1.0.0/mod.d.ts +44 -0
  46. package/esm/deps/jsr.io/@std/ulid/1.0.0/mod.d.ts.map +1 -0
  47. package/esm/deps/jsr.io/@std/ulid/1.0.0/mod.js +47 -0
  48. package/esm/deps/jsr.io/@std/ulid/1.0.0/monotonic_ulid.d.ts +44 -0
  49. package/esm/deps/jsr.io/@std/ulid/1.0.0/monotonic_ulid.d.ts.map +1 -0
  50. package/esm/deps/jsr.io/@std/ulid/1.0.0/monotonic_ulid.js +51 -0
  51. package/esm/deps/jsr.io/@std/ulid/1.0.0/ulid.d.ts +31 -0
  52. package/esm/deps/jsr.io/@std/ulid/1.0.0/ulid.d.ts.map +1 -0
  53. package/esm/deps/jsr.io/@std/ulid/1.0.0/ulid.js +37 -0
  54. package/esm/index.d.ts +21 -0
  55. package/esm/index.d.ts.map +1 -0
  56. package/esm/index.js +20 -0
  57. package/esm/lib/crud/aggregate.d.ts +31 -0
  58. package/esm/lib/crud/aggregate.d.ts.map +1 -0
  59. package/esm/lib/crud/aggregate.js +37 -0
  60. package/esm/lib/crud/bulkWrite.d.ts +25 -0
  61. package/esm/lib/crud/bulkWrite.d.ts.map +1 -0
  62. package/esm/lib/crud/bulkWrite.js +22 -0
  63. package/esm/lib/crud/countDocuments.d.ts +25 -0
  64. package/esm/lib/crud/countDocuments.d.ts.map +1 -0
  65. package/esm/lib/crud/countDocuments.js +22 -0
  66. package/esm/lib/crud/deleteMany.d.ts +25 -0
  67. package/esm/lib/crud/deleteMany.d.ts.map +1 -0
  68. package/esm/lib/crud/deleteMany.js +22 -0
  69. package/esm/lib/crud/deleteOne.d.ts +25 -0
  70. package/esm/lib/crud/deleteOne.d.ts.map +1 -0
  71. package/esm/lib/crud/deleteOne.js +22 -0
  72. package/esm/lib/crud/find.d.ts +39 -0
  73. package/esm/lib/crud/find.d.ts.map +1 -0
  74. package/esm/lib/crud/find.js +53 -0
  75. package/esm/lib/crud/findOne.d.ts +29 -0
  76. package/esm/lib/crud/findOne.d.ts.map +1 -0
  77. package/esm/lib/crud/findOne.js +38 -0
  78. package/esm/lib/crud/findOneAndDelete.d.ts +31 -0
  79. package/esm/lib/crud/findOneAndDelete.d.ts.map +1 -0
  80. package/esm/lib/crud/findOneAndDelete.js +44 -0
  81. package/esm/lib/crud/findOneAndReplace.d.ts +33 -0
  82. package/esm/lib/crud/findOneAndReplace.d.ts.map +1 -0
  83. package/esm/lib/crud/findOneAndReplace.js +45 -0
  84. package/esm/lib/crud/findOneAndUpdate.d.ts +33 -0
  85. package/esm/lib/crud/findOneAndUpdate.d.ts.map +1 -0
  86. package/esm/lib/crud/findOneAndUpdate.js +45 -0
  87. package/esm/lib/crud/insertMany.d.ts +25 -0
  88. package/esm/lib/crud/insertMany.d.ts.map +1 -0
  89. package/esm/lib/crud/insertMany.js +22 -0
  90. package/esm/lib/crud/insertOne.d.ts +25 -0
  91. package/esm/lib/crud/insertOne.d.ts.map +1 -0
  92. package/esm/lib/crud/insertOne.js +22 -0
  93. package/esm/lib/crud/replaceOne.d.ts +27 -0
  94. package/esm/lib/crud/replaceOne.d.ts.map +1 -0
  95. package/esm/lib/crud/replaceOne.js +23 -0
  96. package/esm/lib/crud/updateMany.d.ts +27 -0
  97. package/esm/lib/crud/updateMany.d.ts.map +1 -0
  98. package/esm/lib/crud/updateMany.js +23 -0
  99. package/esm/lib/crud/updateOne.d.ts +27 -0
  100. package/esm/lib/crud/updateOne.d.ts.map +1 -0
  101. package/esm/lib/crud/updateOne.js +23 -0
  102. package/esm/lib/deployCollection.d.ts +33 -0
  103. package/esm/lib/deployCollection.d.ts.map +1 -0
  104. package/esm/lib/deployCollection.js +125 -0
  105. package/esm/lib/handleMongoError.d.ts +10 -0
  106. package/esm/lib/handleMongoError.d.ts.map +1 -0
  107. package/esm/lib/handleMongoError.js +29 -0
  108. package/esm/lib/mongoConnectionManager.d.ts +107 -0
  109. package/esm/lib/mongoConnectionManager.d.ts.map +1 -0
  110. package/esm/lib/mongoConnectionManager.js +195 -0
  111. package/esm/lib/mongoJSON.d.ts +36 -0
  112. package/esm/lib/mongoJSON.d.ts.map +1 -0
  113. package/esm/lib/mongoJSON.js +96 -0
  114. package/esm/lib/repository.d.ts +145 -0
  115. package/esm/lib/repository.d.ts.map +1 -0
  116. package/esm/lib/repository.js +235 -0
  117. package/esm/lib/tracing.d.ts +17 -0
  118. package/esm/lib/tracing.d.ts.map +1 -0
  119. package/esm/lib/tracing.js +62 -0
  120. package/esm/package.json +3 -0
  121. package/package.json +35 -0
@@ -0,0 +1,25 @@
1
+ import type { Collection, Document, InsertOneOptions, InsertOneResult, OptionalUnlessRequiredId } from 'mongodb';
2
+ /**
3
+ * Type to define the input for the insertOne function.
4
+ */
5
+ export type InsertOneInput = {
6
+ collection: Collection<Document>;
7
+ document: OptionalUnlessRequiredId<Document>;
8
+ options?: InsertOneOptions;
9
+ };
10
+ /**
11
+ * Type to define the insertOne function.
12
+ */
13
+ export type InsertOne = (input: InsertOneInput) => Promise<InsertOneResult<Document>>;
14
+ /**
15
+ * Inserts a single document into a MongoDB collection.
16
+ *
17
+ * @param {InsertOneInput} input - The input object.
18
+ * @param input.collection - The collection to insert into.
19
+ * @param input.document - The document to insert.
20
+ * @param [input.options] - The insert options.
21
+ *
22
+ * @returns {Promise<InsertOneResult<Document>} The result of the insert operation.
23
+ */
24
+ export declare const insertOne: InsertOne;
25
+ //# sourceMappingURL=insertOne.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"insertOne.d.ts","sourceRoot":"","sources":["../../../src/lib/crud/insertOne.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACR,UAAU,EACV,QAAQ,EACR,gBAAgB,EAChB,eAAe,EACf,wBAAwB,EAC3B,MAAM,SAAS,CAAC;AAIjB;;GAEG;AACH,MAAM,MAAM,cAAc,GAAG;IACzB,UAAU,EAAE,UAAU,CAAC,QAAQ,CAAC,CAAC;IACjC,QAAQ,EAAE,wBAAwB,CAAC,QAAQ,CAAC,CAAC;IAC7C,OAAO,CAAC,EAAE,gBAAgB,CAAC;CAC9B,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,SAAS,GAAG,CACpB,KAAK,EAAE,cAAc,KACpB,OAAO,CAAC,eAAe,CAAC,QAAQ,CAAC,CAAC,CAAC;AAExC;;;;;;;;;GASG;AACH,eAAO,MAAM,SAAS,EAAE,SAYvB,CAAC"}
@@ -0,0 +1,22 @@
1
+ import { handleMongoError } from '../handleMongoError.js';
2
+ import { withSpan } from '../tracing.js';
3
+ /**
4
+ * Inserts a single document into a MongoDB collection.
5
+ *
6
+ * @param {InsertOneInput} input - The input object.
7
+ * @param input.collection - The collection to insert into.
8
+ * @param input.document - The document to insert.
9
+ * @param [input.options] - The insert options.
10
+ *
11
+ * @returns {Promise<InsertOneResult<Document>} The result of the insert operation.
12
+ */
13
+ export const insertOne = ({ collection, document, options, }) => {
14
+ return withSpan('insertOne', collection, async () => {
15
+ try {
16
+ return await collection.insertOne(document, options);
17
+ }
18
+ catch (error) {
19
+ throw handleMongoError(error);
20
+ }
21
+ });
22
+ };
@@ -0,0 +1,27 @@
1
+ import type { Collection, Document, Filter, ReplaceOptions, UpdateResult, WithoutId } from 'mongodb';
2
+ /**
3
+ * Type to define the input for the replaceOne function.
4
+ */
5
+ export type ReplaceOneInput = {
6
+ collection: Collection<Document>;
7
+ filter: Filter<Document>;
8
+ replacement: WithoutId<Document>;
9
+ options?: ReplaceOptions;
10
+ };
11
+ /**
12
+ * Type to define the replaceOne function.
13
+ */
14
+ export type ReplaceOne = (input: ReplaceOneInput) => Promise<Document | UpdateResult<Document>>;
15
+ /**
16
+ * Replaces a single document in a MongoDB collection.
17
+ *
18
+ * @param {ReplaceOneInput} input - The input object.
19
+ * @param input.collection - The collection to replace in.
20
+ * @param input.filter - The filter for the replace operation.
21
+ * @param input.replacement - The replacement document.
22
+ * @param [input.options] - The replace options.
23
+ *
24
+ * @returns {Promise<Document | UpdateResult<Document>} The result of the replace operation.
25
+ */
26
+ export declare const replaceOne: ReplaceOne;
27
+ //# sourceMappingURL=replaceOne.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"replaceOne.d.ts","sourceRoot":"","sources":["../../../src/lib/crud/replaceOne.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACR,UAAU,EACV,QAAQ,EACR,MAAM,EACN,cAAc,EACd,YAAY,EACZ,SAAS,EACZ,MAAM,SAAS,CAAC;AAIjB;;GAEG;AACH,MAAM,MAAM,eAAe,GAAG;IAC1B,UAAU,EAAE,UAAU,CAAC,QAAQ,CAAC,CAAC;IACjC,MAAM,EAAE,MAAM,CAAC,QAAQ,CAAC,CAAC;IACzB,WAAW,EAAE,SAAS,CAAC,QAAQ,CAAC,CAAC;IACjC,OAAO,CAAC,EAAE,cAAc,CAAC;CAC5B,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,UAAU,GAAG,CACrB,KAAK,EAAE,eAAe,KACrB,OAAO,CAAC,QAAQ,GAAG,YAAY,CAAC,QAAQ,CAAC,CAAC,CAAC;AAEhD;;;;;;;;;;GAUG;AACH,eAAO,MAAM,UAAU,EAAE,UAaxB,CAAC"}
@@ -0,0 +1,23 @@
1
+ import { handleMongoError } from '../handleMongoError.js';
2
+ import { withSpan } from '../tracing.js';
3
+ /**
4
+ * Replaces a single document in a MongoDB collection.
5
+ *
6
+ * @param {ReplaceOneInput} input - The input object.
7
+ * @param input.collection - The collection to replace in.
8
+ * @param input.filter - The filter for the replace operation.
9
+ * @param input.replacement - The replacement document.
10
+ * @param [input.options] - The replace options.
11
+ *
12
+ * @returns {Promise<Document | UpdateResult<Document>} The result of the replace operation.
13
+ */
14
+ export const replaceOne = ({ collection, filter, replacement, options, }) => {
15
+ return withSpan('replaceOne', collection, async () => {
16
+ try {
17
+ return await collection.replaceOne(filter, replacement, options);
18
+ }
19
+ catch (error) {
20
+ throw handleMongoError(error);
21
+ }
22
+ });
23
+ };
@@ -0,0 +1,27 @@
1
+ import type { Collection, Document, Filter, UpdateFilter, UpdateOptions, UpdateResult } from 'mongodb';
2
+ /**
3
+ * Type to define the input for the updateMany function.
4
+ */
5
+ export type UpdateManyInput = {
6
+ collection: Collection<Document>;
7
+ filter: Filter<Document>;
8
+ update: UpdateFilter<Document>;
9
+ options?: UpdateOptions;
10
+ };
11
+ /**
12
+ * Type to define the updateMany function.
13
+ */
14
+ export type UpdateMany = (input: UpdateManyInput) => Promise<UpdateResult<Document>>;
15
+ /**
16
+ * Updates multiple documents in a MongoDB collection.
17
+ *
18
+ * @param {UpdateManyInput} input - The input object.
19
+ * @param input.collection - The collection to update in.
20
+ * @param input.filter - The filter for the update operation.
21
+ * @param input.update - The update object.
22
+ * @param [input.options] - The update options.
23
+ *
24
+ * @returns {Promise<UpdateResult<Document>} The result of the update operation.
25
+ */
26
+ export declare const updateMany: UpdateMany;
27
+ //# sourceMappingURL=updateMany.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"updateMany.d.ts","sourceRoot":"","sources":["../../../src/lib/crud/updateMany.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACR,UAAU,EACV,QAAQ,EACR,MAAM,EACN,YAAY,EACZ,aAAa,EACb,YAAY,EACf,MAAM,SAAS,CAAC;AAIjB;;GAEG;AACH,MAAM,MAAM,eAAe,GAAG;IAC1B,UAAU,EAAE,UAAU,CAAC,QAAQ,CAAC,CAAC;IACjC,MAAM,EAAE,MAAM,CAAC,QAAQ,CAAC,CAAC;IACzB,MAAM,EAAE,YAAY,CAAC,QAAQ,CAAC,CAAC;IAC/B,OAAO,CAAC,EAAE,aAAa,CAAC;CAC3B,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,UAAU,GAAG,CACrB,KAAK,EAAE,eAAe,KACrB,OAAO,CAAC,YAAY,CAAC,QAAQ,CAAC,CAAC,CAAC;AAErC;;;;;;;;;;GAUG;AACH,eAAO,MAAM,UAAU,EAAE,UAaxB,CAAC"}
@@ -0,0 +1,23 @@
1
+ import { handleMongoError } from '../handleMongoError.js';
2
+ import { withSpan } from '../tracing.js';
3
+ /**
4
+ * Updates multiple documents in a MongoDB collection.
5
+ *
6
+ * @param {UpdateManyInput} input - The input object.
7
+ * @param input.collection - The collection to update in.
8
+ * @param input.filter - The filter for the update operation.
9
+ * @param input.update - The update object.
10
+ * @param [input.options] - The update options.
11
+ *
12
+ * @returns {Promise<UpdateResult<Document>} The result of the update operation.
13
+ */
14
+ export const updateMany = ({ collection, filter, update, options, }) => {
15
+ return withSpan('updateMany', collection, async () => {
16
+ try {
17
+ return await collection.updateMany(filter, update, options);
18
+ }
19
+ catch (error) {
20
+ throw handleMongoError(error);
21
+ }
22
+ });
23
+ };
@@ -0,0 +1,27 @@
1
+ import type { Collection, Document, Filter, UpdateFilter, UpdateOptions, UpdateResult } from 'mongodb';
2
+ /**
3
+ * Type to define the input for the updateOne function.
4
+ */
5
+ export type UpdateOneInput = {
6
+ collection: Collection<Document>;
7
+ filter: Filter<Document>;
8
+ update: UpdateFilter<Document> | Document[];
9
+ options?: UpdateOptions;
10
+ };
11
+ /**
12
+ * Type to define the updateOne function.
13
+ */
14
+ export type UpdateOne = (input: UpdateOneInput) => Promise<UpdateResult<Document>>;
15
+ /**
16
+ * Updates a single document in a MongoDB collection.
17
+ *
18
+ * @param {UpdateOneInput} input - The input object.
19
+ * @param input.collection - The collection to update in.
20
+ * @param input.filter - The filter for the update operation.
21
+ * @param input.update - The update object.
22
+ * @param [input.options] - The update options.
23
+ *
24
+ * @returns {Promise<UpdateResult<Document>} The result of the update operation.
25
+ */
26
+ export declare const updateOne: UpdateOne;
27
+ //# sourceMappingURL=updateOne.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"updateOne.d.ts","sourceRoot":"","sources":["../../../src/lib/crud/updateOne.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACR,UAAU,EACV,QAAQ,EACR,MAAM,EACN,YAAY,EACZ,aAAa,EACb,YAAY,EACf,MAAM,SAAS,CAAC;AAIjB;;GAEG;AACH,MAAM,MAAM,cAAc,GAAG;IACzB,UAAU,EAAE,UAAU,CAAC,QAAQ,CAAC,CAAC;IACjC,MAAM,EAAE,MAAM,CAAC,QAAQ,CAAC,CAAC;IACzB,MAAM,EAAE,YAAY,CAAC,QAAQ,CAAC,GAAG,QAAQ,EAAE,CAAC;IAC5C,OAAO,CAAC,EAAE,aAAa,CAAC;CAC3B,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,SAAS,GAAG,CACpB,KAAK,EAAE,cAAc,KACpB,OAAO,CAAC,YAAY,CAAC,QAAQ,CAAC,CAAC,CAAC;AAErC;;;;;;;;;;GAUG;AACH,eAAO,MAAM,SAAS,EAAE,SAavB,CAAC"}
@@ -0,0 +1,23 @@
1
+ import { handleMongoError } from '../handleMongoError.js';
2
+ import { withSpan } from '../tracing.js';
3
+ /**
4
+ * Updates a single document in a MongoDB collection.
5
+ *
6
+ * @param {UpdateOneInput} input - The input object.
7
+ * @param input.collection - The collection to update in.
8
+ * @param input.filter - The filter for the update operation.
9
+ * @param input.update - The update object.
10
+ * @param [input.options] - The update options.
11
+ *
12
+ * @returns {Promise<UpdateResult<Document>} The result of the update operation.
13
+ */
14
+ export const updateOne = ({ collection, filter, update, options, }) => {
15
+ return withSpan('updateOne', collection, async () => {
16
+ try {
17
+ return await collection.updateOne(filter, update, options);
18
+ }
19
+ catch (error) {
20
+ throw handleMongoError(error);
21
+ }
22
+ });
23
+ };
@@ -0,0 +1,33 @@
1
+ import type { CreateCollectionOptions, IndexDescription, MongoClient } from 'mongodb';
2
+ /**
3
+ * Type to define a mongo collection.
4
+ */
5
+ export type MongoCollectionDefinition = {
6
+ name: string;
7
+ options?: CreateCollectionOptions;
8
+ indexes?: IndexDescription[];
9
+ };
10
+ /**
11
+ * Type to define the input for the deployMongoCollection function.
12
+ */
13
+ export type DeployMongoCollectionInput = {
14
+ mongoClient: MongoClient;
15
+ dbName: string;
16
+ collectionDefinition: MongoCollectionDefinition;
17
+ allowUpdateIndexes: boolean;
18
+ };
19
+ /**
20
+ * Deploys a collection to a MongoDB database.
21
+ *
22
+ * Either creates a new collection, or updates an existing collection (collMod).
23
+ * Indexes are created or dropped to match the input set.
24
+ * Indexes on existing collections are only updated if `allowUpdateIndexes` is set to `true`.
25
+ *
26
+ * @param {DeployMongoCollectionInput} input
27
+ * @param {MongoClient} input.mongoClient - A MongoDB client instance
28
+ * @param {string} input.dbName - The name of the database
29
+ * @param {MongoCollectionDefinition} input.collectionDefinition - The collection definition
30
+ * @param {boolean} input.allowUpdateIndexes - Whether to update indexes on existing collections
31
+ */
32
+ export declare const deployMongoCollection: ({ mongoClient, dbName, collectionDefinition, allowUpdateIndexes, }: DeployMongoCollectionInput) => Promise<string>;
33
+ //# sourceMappingURL=deployCollection.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"deployCollection.d.ts","sourceRoot":"","sources":["../../src/lib/deployCollection.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACR,uBAAuB,EAEvB,gBAAgB,EAChB,WAAW,EACd,MAAM,SAAS,CAAC;AAEjB;;GAEG;AACH,MAAM,MAAM,yBAAyB,GAAG;IACpC,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,CAAC,EAAE,uBAAuB,CAAC;IAClC,OAAO,CAAC,EAAE,gBAAgB,EAAE,CAAC;CAChC,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,0BAA0B,GAAG;IACrC,WAAW,EAAE,WAAW,CAAC;IACzB,MAAM,EAAE,MAAM,CAAC;IACf,oBAAoB,EAAE,yBAAyB,CAAC;IAChD,kBAAkB,EAAE,OAAO,CAAC;CAC/B,CAAC;AAEF;;;;;;;;;;;;GAYG;AACH,eAAO,MAAM,qBAAqB,GAAU,oEAKzC,0BAA0B,KAAG,OAAO,CAAC,MAAM,CA2B7C,CAAC"}
@@ -0,0 +1,125 @@
1
+ import { getLogger } from '../deps/jsr.io/@nimbus/core/2.0.0/src/index.js';
2
+ /**
3
+ * Deploys a collection to a MongoDB database.
4
+ *
5
+ * Either creates a new collection, or updates an existing collection (collMod).
6
+ * Indexes are created or dropped to match the input set.
7
+ * Indexes on existing collections are only updated if `allowUpdateIndexes` is set to `true`.
8
+ *
9
+ * @param {DeployMongoCollectionInput} input
10
+ * @param {MongoClient} input.mongoClient - A MongoDB client instance
11
+ * @param {string} input.dbName - The name of the database
12
+ * @param {MongoCollectionDefinition} input.collectionDefinition - The collection definition
13
+ * @param {boolean} input.allowUpdateIndexes - Whether to update indexes on existing collections
14
+ */
15
+ export const deployMongoCollection = async ({ mongoClient, dbName, collectionDefinition, allowUpdateIndexes, }) => {
16
+ const db = mongoClient.db(dbName);
17
+ const collectionName = collectionDefinition.name;
18
+ getLogger().info({
19
+ category: 'Nimbus',
20
+ message: `Deploying collection "${collectionName}" on database "${dbName}" ...`,
21
+ });
22
+ if (await collectionExists(db, collectionDefinition)) {
23
+ getLogger().info({
24
+ category: 'Nimbus',
25
+ message: `Collection "${collectionName}" exists. Updating collection ...`,
26
+ });
27
+ await updateCollection(db, allowUpdateIndexes, collectionDefinition);
28
+ }
29
+ else {
30
+ getLogger().info({
31
+ category: 'Nimbus',
32
+ message: `Collection "${collectionName}" does not exist. Creating collection ...`,
33
+ });
34
+ await createCollection(db, collectionDefinition);
35
+ }
36
+ return 'OK';
37
+ };
38
+ /**
39
+ * Checks if a collection exists in a database.
40
+ *
41
+ * @param {Db} db - The database to check.
42
+ * @param {MongoCollectionDefinition} collectionDefinition - The collection definition
43
+ * @returns {Promise<boolean>} Whether the collection exists.
44
+ */
45
+ const collectionExists = async (db, { name }) => {
46
+ const collections = await db
47
+ .listCollections({ name: name }, { nameOnly: true })
48
+ .toArray();
49
+ return collections.some((item) => item.name === name);
50
+ };
51
+ /**
52
+ * Creates a collection in a database.
53
+ *
54
+ * @param {Db} db - The database to create the collection in.
55
+ * @param {MongoCollectionDefinition} collectionDefinition - The collection definition
56
+ */
57
+ const createCollection = async (db, { name, options, indexes }) => {
58
+ await db.createCollection(name, options);
59
+ getLogger().info({
60
+ category: 'Nimbus',
61
+ message: `Collection "${name}" created.`,
62
+ });
63
+ if (indexes?.length) {
64
+ await db.collection(name).createIndexes(indexes);
65
+ getLogger().info({
66
+ category: 'Nimbus',
67
+ message: `Added ${indexes.length} indexes for collection "${name}".`,
68
+ });
69
+ }
70
+ };
71
+ /**
72
+ * Updates a collection in a database.
73
+ *
74
+ * @param {Db} db - The database to update the collection in.
75
+ * @param {boolean} allowUpdateIndexes - Whether to update indexes on existing collections
76
+ * @param {MongoCollectionDefinition} collectionDefinition - The collection definition
77
+ */
78
+ const updateCollection = async (db, allowUpdateIndexes, { name, options, indexes }) => {
79
+ await db.command({ collMod: name, ...options });
80
+ getLogger().info({
81
+ category: 'Nimbus',
82
+ message: `Collection "${name}" updated.`,
83
+ });
84
+ if (allowUpdateIndexes) {
85
+ const indexesWithNames = (indexes ?? []).map((index) => {
86
+ if (index.name) {
87
+ return index;
88
+ }
89
+ return {
90
+ ...index,
91
+ name: Object.entries(index.key)
92
+ .reduce((acc, [k, v]) => {
93
+ return [...acc, `${k}_${String(v)}`];
94
+ }, [])
95
+ .join('_'),
96
+ };
97
+ });
98
+ const existingIndexes = await (await db.collection(name).listIndexes().toArray())
99
+ .map((obj) => obj.name)
100
+ .filter((i) => i !== '_id_');
101
+ const indexesToAdd = indexesWithNames.filter((index) => {
102
+ return !existingIndexes.includes(index.name);
103
+ });
104
+ const indexesToDelete = existingIndexes.filter((existingIndex) => {
105
+ return !indexesWithNames.some((i) => i.name === existingIndex);
106
+ });
107
+ if (indexesToAdd.length) {
108
+ await db.collection(name).createIndexes(indexesToAdd);
109
+ getLogger().info({
110
+ category: 'Nimbus',
111
+ message: `Added ${indexesToAdd.length} indexes for collection "${name}".`,
112
+ });
113
+ }
114
+ if (indexesToDelete.length) {
115
+ await Promise.all(indexesToDelete?.map((index) => {
116
+ console.log(`Dropping ${index}`);
117
+ return db.collection(name).dropIndex(index);
118
+ }));
119
+ getLogger().info({
120
+ category: 'Nimbus',
121
+ message: `Deleted ${indexesToDelete.length} indexes for collection "${name}".`,
122
+ });
123
+ }
124
+ }
125
+ };
@@ -0,0 +1,10 @@
1
+ import { type Exception } from '../deps/jsr.io/@nimbus/core/2.0.0/src/index.js';
2
+ /**
3
+ * Handles MongoDB errors and converts them
4
+ * to Nimbus exceptions based on the error code.
5
+ *
6
+ * @param {any} error - The error to handle.
7
+ * @returns {Exception} The Nimbus exception.
8
+ */
9
+ export declare const handleMongoError: (error: any) => Exception;
10
+ //# sourceMappingURL=handleMongoError.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"handleMongoError.d.ts","sourceRoot":"","sources":["../../src/lib/handleMongoError.ts"],"names":[],"mappings":"AAAA,OAAO,EACH,KAAK,SAAS,EAGjB,MAAM,gDAAgD,CAAC;AAExD;;;;;;GAMG;AACH,eAAO,MAAM,gBAAgB,GAAI,OAAO,GAAG,KAAG,SAuB7C,CAAC"}
@@ -0,0 +1,29 @@
1
+ import { GenericException, InvalidInputException, } from '../deps/jsr.io/@nimbus/core/2.0.0/src/index.js';
2
+ /**
3
+ * Handles MongoDB errors and converts them
4
+ * to Nimbus exceptions based on the error code.
5
+ *
6
+ * @param {any} error - The error to handle.
7
+ * @returns {Exception} The Nimbus exception.
8
+ */
9
+ export const handleMongoError = (error) => {
10
+ if (error.code && error.code === 121) {
11
+ return new InvalidInputException(error.message, {
12
+ code: error.code,
13
+ ...(error.errInfo?.['details'] && {
14
+ details: error.errInfo?.['details'],
15
+ }),
16
+ ...(error.writeErrors && { writeErrors: error.writeErrors }),
17
+ ...(error.result && { result: error.result }),
18
+ }).fromError(error);
19
+ }
20
+ if (error.code && error.code === 2) {
21
+ return new InvalidInputException().fromError(error);
22
+ }
23
+ if (error.code && error.code === 11000) {
24
+ return new InvalidInputException(error.message, {
25
+ keyValue: error['keyValue'],
26
+ }).fromError(error);
27
+ }
28
+ return new GenericException().fromError(error);
29
+ };
@@ -0,0 +1,107 @@
1
+ import { type Collection, type Db, MongoClient, type MongoClientOptions } from 'mongodb';
2
+ /**
3
+ * Singleton wrapper around a single long-lived `MongoClient`.
4
+ *
5
+ * The MongoDB Node driver already handles connection pooling, server
6
+ * monitoring (SDAM), automatic reconnection, retryable reads/writes and
7
+ * per-socket idle eviction via `maxIdleTimeMS`. This class therefore does
8
+ * the minimum on top of the driver: hold one `MongoClient` for the lifetime
9
+ * of the application and provide typed `getDatabase` / `getCollection`
10
+ * convenience methods.
11
+ *
12
+ * Call {@link MongoConnectionManager#close} on graceful shutdown
13
+ * (e.g. on `SIGTERM` / `SIGINT`) to drain the pool.
14
+ *
15
+ * @example
16
+ * ```ts
17
+ * import { MongoConnectionManager } from '@nimbus/mongodb';
18
+ * import { ServerApiVersion } from 'mongodb';
19
+ *
20
+ * export const mongoManager = MongoConnectionManager.getInstance(
21
+ * process.env['MONGO_URL'] ?? '',
22
+ * {
23
+ * appName: 'my-app',
24
+ * serverApi: {
25
+ * version: ServerApiVersion.v1,
26
+ * strict: false,
27
+ * deprecationErrors: true,
28
+ * },
29
+ * },
30
+ * );
31
+ *
32
+ * process.on('SIGTERM', () => {
33
+ * mongoManager.close().catch(console.error);
34
+ * });
35
+ * ```
36
+ */
37
+ export declare class MongoConnectionManager {
38
+ private static _instance;
39
+ private _client;
40
+ private _connecting;
41
+ private readonly _uri;
42
+ private readonly _options?;
43
+ /**
44
+ * Initialize the MongoConnectionManager.
45
+ *
46
+ * @param {string} uri - The MongoDB connection URI
47
+ * @param {MongoClientOptions} [options] - Options forwarded to the underlying `MongoClient`
48
+ */
49
+ private constructor();
50
+ /**
51
+ * Get the singleton instance of the MongoConnectionManager.
52
+ *
53
+ * Subsequent calls return the existing instance and ignore the arguments.
54
+ *
55
+ * @param {string} uri - The MongoDB connection URI
56
+ * @param {MongoClientOptions} [options] - Options forwarded to the underlying `MongoClient`
57
+ *
58
+ * @returns {MongoConnectionManager} The singleton instance of the MongoConnectionManager
59
+ */
60
+ static getInstance(uri: string, options?: MongoClientOptions): MongoConnectionManager;
61
+ /**
62
+ * Get the connected MongoDB client.
63
+ *
64
+ * Lazily creates and connects a single `MongoClient` on the first call.
65
+ * Concurrent first-callers share the same in-flight `connect()` promise
66
+ * so only one connection is established.
67
+ *
68
+ * @returns {Promise<MongoClient>} The MongoDB client instance
69
+ */
70
+ getClient(): Promise<MongoClient>;
71
+ /**
72
+ * Get a database instance from the connected MongoDB client.
73
+ *
74
+ * @param {string} dbName - The name of the database
75
+ * @returns {Promise<Db>} The database instance
76
+ */
77
+ getDatabase(dbName: string): Promise<Db>;
78
+ /**
79
+ * Get a collection instance from the connected MongoDB client.
80
+ *
81
+ * @param {string} dbName - The name of the database
82
+ * @param {string} collectionName - The name of the collection
83
+ * @returns {Promise<Collection>} The collection instance
84
+ */
85
+ getCollection(dbName: string, collectionName: string): Promise<Collection>;
86
+ /**
87
+ * Check the health of the MongoDB connection by issuing a `ping` against
88
+ * the `admin` database.
89
+ *
90
+ * @returns {Promise<{ status: 'healthy' | 'error'; details?: string }>} The health status and optional error details
91
+ */
92
+ healthCheck(): Promise<{
93
+ status: 'healthy' | 'error';
94
+ details?: string;
95
+ }>;
96
+ /**
97
+ * Close the underlying MongoDB client and drain its connection pool.
98
+ *
99
+ * Intended for graceful shutdown handlers. After `close()` resolves, the
100
+ * next call to {@link MongoConnectionManager#getClient} establishes a
101
+ * fresh connection.
102
+ *
103
+ * @returns {Promise<void>}
104
+ */
105
+ close(): Promise<void>;
106
+ }
107
+ //# sourceMappingURL=mongoConnectionManager.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mongoConnectionManager.d.ts","sourceRoot":"","sources":["../../src/lib/mongoConnectionManager.ts"],"names":[],"mappings":"AACA,OAAO,EACH,KAAK,UAAU,EACf,KAAK,EAAE,EACP,WAAW,EACX,KAAK,kBAAkB,EAC1B,MAAM,SAAS,CAAC;AAEjB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,qBAAa,sBAAsB;IAC/B,OAAO,CAAC,MAAM,CAAC,SAAS,CAAuC;IAC/D,OAAO,CAAC,OAAO,CAA4B;IAC3C,OAAO,CAAC,WAAW,CAAqC;IACxD,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAS;IAC9B,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAqB;IAE/C;;;;;OAKG;IACH,OAAO;IAKP;;;;;;;;;OASG;WACW,WAAW,CACrB,GAAG,EAAE,MAAM,EACX,OAAO,CAAC,EAAE,kBAAkB,GAC7B,sBAAsB;IAWzB;;;;;;;;OAQG;IACI,SAAS,IAAI,OAAO,CAAC,WAAW,CAAC;IAqCxC;;;;;OAKG;IACU,WAAW,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,EAAE,CAAC;IAKrD;;;;;;OAMG;IACU,aAAa,CACtB,MAAM,EAAE,MAAM,EACd,cAAc,EAAE,MAAM,GACvB,OAAO,CAAC,UAAU,CAAC;IAKtB;;;;;OAKG;IACU,WAAW,IAAI,OAAO,CAC/B;QAAE,MAAM,EAAE,SAAS,GAAG,OAAO,CAAC;QAAC,OAAO,CAAC,EAAE,MAAM,CAAA;KAAE,CACpD;IAeD;;;;;;;;OAQG;IACU,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;CAqCtC"}