@xylabs/mongo 5.0.90 → 5.0.91

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.
@@ -1,19 +1,75 @@
1
1
  import type { BulkWriteOptions, Collection, DeleteResult, Document, Filter, FindCursor, InsertOneOptions, MongoClient, OptionalUnlessRequiredId, ReplaceOptions, UpdateFilter, WithId } from 'mongodb';
2
2
  import type { BaseMongoSdkConfig } from './Config.js';
3
+ /** Provides a typed wrapper around common MongoDB collection operations. */
3
4
  export declare class BaseMongoSdk<T extends Document> {
5
+ /** The MongoDB SDK configuration for this instance. */
4
6
  config: BaseMongoSdkConfig;
5
7
  constructor(config: BaseMongoSdkConfig);
8
+ /** Returns the MongoDB connection URI, either from the config or constructed from individual credential fields. */
6
9
  get uri(): string;
10
+ /**
11
+ * Deletes all documents matching the filter.
12
+ * @param filter - The query filter to match documents for deletion
13
+ */
7
14
  deleteMany(filter: Filter<T>): Promise<DeleteResult>;
15
+ /**
16
+ * Deletes the first document matching the filter.
17
+ * @param filter - The query filter to match a document for deletion
18
+ */
8
19
  deleteOne(filter: Filter<T>): Promise<DeleteResult>;
20
+ /**
21
+ * Finds all documents matching the filter and returns a cursor.
22
+ * @param filter - The query filter
23
+ */
9
24
  find(filter: Filter<T>): Promise<FindCursor<WithId<T>>>;
25
+ /**
26
+ * Finds a single document matching the filter.
27
+ * @param filter - The query filter
28
+ * @returns The matched document, or `null` if not found
29
+ */
10
30
  findOne(filter: Filter<T>): Promise<WithId<T> | null>;
31
+ /**
32
+ * Inserts multiple documents into the collection.
33
+ * @param items - The documents to insert
34
+ * @param options - Optional bulk write options
35
+ */
11
36
  insertMany(items: OptionalUnlessRequiredId<T>[], options?: BulkWriteOptions): Promise<import("mongodb").InsertManyResult<T>>;
37
+ /**
38
+ * Inserts a single document into the collection.
39
+ * @param item - The document to insert
40
+ * @param options - Optional insert options
41
+ */
12
42
  insertOne(item: OptionalUnlessRequiredId<T>, options?: InsertOneOptions): Promise<import("mongodb").InsertOneResult<T>>;
43
+ /**
44
+ * Replaces a single document matching the filter.
45
+ * @param filter - The query filter to match the document
46
+ * @param item - The replacement document
47
+ * @param options - Optional replace options
48
+ */
13
49
  replaceOne(filter: Filter<T>, item: OptionalUnlessRequiredId<T>, options?: ReplaceOptions): Promise<import("mongodb").UpdateResult<T>>;
50
+ /**
51
+ * Updates a single document matching the filter without upserting.
52
+ * @param filter - The query filter to match the document
53
+ * @param fields - The update operations to apply
54
+ */
14
55
  updateOne(filter: Filter<T>, fields: UpdateFilter<T>): Promise<import("mongodb").UpdateResult<T>>;
56
+ /**
57
+ * Updates a single document matching the filter, inserting it if it does not exist.
58
+ * @param filter - The query filter to match the document
59
+ * @param fields - The update operations to apply
60
+ */
15
61
  upsertOne(filter: Filter<T>, fields: UpdateFilter<T>): Promise<import("mongodb").UpdateResult<T>>;
62
+ /**
63
+ * Executes a callback with access to the configured MongoDB collection.
64
+ * @param func - A callback receiving the typed collection
65
+ * @returns The result of the callback
66
+ */
16
67
  useCollection<R>(func: (collection: Collection<T>) => Promise<R> | R): Promise<R>;
68
+ /**
69
+ * Executes a callback with a connected MongoClient, handling connection and disconnection.
70
+ * @param func - A callback receiving the connected MongoClient
71
+ * @returns The result of the callback
72
+ */
17
73
  useMongo<R>(func: (client: MongoClient) => Promise<R> | R): Promise<R>;
18
74
  }
19
75
  //# sourceMappingURL=Base.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"Base.d.ts","sourceRoot":"","sources":["../../src/Base.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,gBAAgB,EAChB,UAAU,EACV,YAAY,EACZ,QAAQ,EACR,MAAM,EACN,UAAU,EACV,gBAAgB,EAChB,WAAW,EACX,wBAAwB,EACxB,cAAc,EACd,YAAY,EACZ,MAAM,EACP,MAAM,SAAS,CAAA;AAEhB,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAA;AAGrD,qBAAa,YAAY,CAAC,CAAC,SAA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IAAI,EAAE,CAAC,UAAU,EAAE,UAAU,CAAC,CAAC,CAAC,KAAK,OAAO,CAAC,CAAC,CAAC,GAAG,CAAC;IAM1E;;;;OAIG;IACG,QAAQ,CAAC,CAAC,EAAE,IAAI,EAAE,CAAC,MAAM,EAAE,WAAW,KAAK,OAAO,CAAC,CAAC,CAAC,GAAG,CAAC;CAUhE"}
@@ -1,14 +1,25 @@
1
+ /** Public configuration options for the Mongo SDK, safe to expose to clients. */
1
2
  export interface BaseMongoSdkPublicConfig {
3
+ /** Delay in milliseconds before closing idle connections. */
2
4
  closeDelay?: number;
5
+ /** The MongoDB collection name to operate on. */
3
6
  collection: string;
7
+ /** Maximum number of connections in the connection pool. */
4
8
  maxPoolSize?: number;
5
9
  }
10
+ /** Private configuration options for the Mongo SDK containing connection credentials. */
6
11
  export interface BaseMongoSdkPrivateConfig {
12
+ /** A full MongoDB connection string, used instead of individual credential fields. */
7
13
  dbConnectionString?: string;
14
+ /** The MongoDB Atlas cluster domain. */
8
15
  dbDomain?: string;
16
+ /** The database name to connect to. */
9
17
  dbName?: string;
18
+ /** The password for MongoDB authentication. */
10
19
  dbPassword?: string;
20
+ /** The username for MongoDB authentication. */
11
21
  dbUserName?: string;
12
22
  }
23
+ /** Combined public and private MongoDB SDK configuration. */
13
24
  export type BaseMongoSdkConfig = BaseMongoSdkPublicConfig & BaseMongoSdkPrivateConfig;
14
25
  //# sourceMappingURL=Config.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"Config.d.ts","sourceRoot":"","sources":["../../src/Config.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,wBAAwB;IACvC,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB,UAAU,EAAE,MAAM,CAAA;IAClB,WAAW,CAAC,EAAE,MAAM,CAAA;CACrB;AAED,MAAM,WAAW,yBAAyB;IACxC,kBAAkB,CAAC,EAAE,MAAM,CAAA;IAC3B,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB,UAAU,CAAC,EAAE,MAAM,CAAA;CACpB;AAED,MAAM,MAAM,kBAAkB,GAAG,wBAAwB,GAAG,yBAAyB,CAAA"}
1
+ {"version":3,"file":"Config.d.ts","sourceRoot":"","sources":["../../src/Config.ts"],"names":[],"mappings":"AAAA,iFAAiF;AACjF,MAAM,WAAW,wBAAwB;IACvC,6DAA6D;IAC7D,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB,iDAAiD;IACjD,UAAU,EAAE,MAAM,CAAA;IAClB,4DAA4D;IAC5D,WAAW,CAAC,EAAE,MAAM,CAAA;CACrB;AAED,yFAAyF;AACzF,MAAM,WAAW,yBAAyB;IACxC,sFAAsF;IACtF,kBAAkB,CAAC,EAAE,MAAM,CAAA;IAC3B,wCAAwC;IACxC,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,uCAAuC;IACvC,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,+CAA+C;IAC/C,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB,+CAA+C;IAC/C,UAAU,CAAC,EAAE,MAAM,CAAA;CACpB;AAED,6DAA6D;AAC7D,MAAM,MAAM,kBAAkB,GAAG,wBAAwB,GAAG,yBAAyB,CAAA"}
@@ -1,11 +1,23 @@
1
1
  import { MongoClient } from 'mongodb';
2
+ /** Manages a shared pool of MongoClient instances, reusing connections by URI. */
2
3
  export declare class MongoClientWrapper {
4
+ /** Global cache of wrapper instances keyed by connection URI. */
3
5
  static readonly clients: Map<string, MongoClientWrapper>;
4
6
  private client;
5
7
  constructor(uri: string, maxPoolSize?: number, closeDelay?: number);
8
+ /**
9
+ * Gets or creates a cached MongoClientWrapper for the given URI.
10
+ * @param uri - The MongoDB connection URI
11
+ * @param poolSize - Maximum connection pool size
12
+ * @param closeDelay - Delay in milliseconds before closing idle connections
13
+ * @returns A cached or newly created wrapper instance
14
+ */
6
15
  static get(uri: string, poolSize?: number, closeDelay?: number): MongoClientWrapper;
16
+ /** Connects to MongoDB and returns the underlying MongoClient. */
7
17
  connect(): Promise<MongoClient>;
18
+ /** Disconnects from MongoDB. */
8
19
  disconnect(): Promise<number>;
20
+ /** Initiates a graceful close of the connection. */
9
21
  initiateClose(): Promise<void>;
10
22
  }
11
23
  //# sourceMappingURL=Wrapper.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"Wrapper.d.ts","sourceRoot":"","sources":["../../src/Wrapper.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAE,MAAM,SAAS,CAAA;AAErC,qBAAa,kBAAkB;IAC7B,MAAM,CAAC,QAAQ,CAAC,OAAO,kCAAwC;IAE/D,OAAO,CAAC,MAAM,CAAa;gBAEf,GAAG,EAAE,MAAM,EAAE,WAAW,CAAC,EAAE,MAAM,EAAE,UAAU,CAAC,EAAE,MAAM;IAKlE,MAAM,CAAC,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,MAAM,EAAE,UAAU,CAAC,EAAE,MAAM;IASxD,OAAO;IAIP,UAAU;IAIV,aAAa;CAGpB"}
1
+ {"version":3,"file":"Wrapper.d.ts","sourceRoot":"","sources":["../../src/Wrapper.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAE,MAAM,SAAS,CAAA;AAErC,kFAAkF;AAClF,qBAAa,kBAAkB;IAC7B,iEAAiE;IACjE,MAAM,CAAC,QAAQ,CAAC,OAAO,kCAAwC;IAE/D,OAAO,CAAC,MAAM,CAAa;gBAEf,GAAG,EAAE,MAAM,EAAE,WAAW,CAAC,EAAE,MAAM,EAAE,UAAU,CAAC,EAAE,MAAM;IAKlE;;;;;;OAMG;IACH,MAAM,CAAC,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,MAAM,EAAE,UAAU,CAAC,EAAE,MAAM;IAS9D,kEAAkE;IAC5D,OAAO;IAIb,gCAAgC;IAC1B,UAAU;IAIhB,oDAAoD;IAC9C,aAAa;CAGpB"}
@@ -4,12 +4,20 @@ import { assertEx } from "@xylabs/assert";
4
4
  // src/Wrapper.ts
5
5
  import { MongoClient } from "mongodb";
6
6
  var MongoClientWrapper = class _MongoClientWrapper {
7
+ /** Global cache of wrapper instances keyed by connection URI. */
7
8
  static clients = /* @__PURE__ */ new Map();
8
9
  client;
9
10
  constructor(uri, maxPoolSize, closeDelay) {
10
11
  const options = { maxPoolSize, maxIdleTimeMS: closeDelay };
11
12
  this.client = new MongoClient(uri, options);
12
13
  }
14
+ /**
15
+ * Gets or creates a cached MongoClientWrapper for the given URI.
16
+ * @param uri - The MongoDB connection URI
17
+ * @param poolSize - Maximum connection pool size
18
+ * @param closeDelay - Delay in milliseconds before closing idle connections
19
+ * @returns A cached or newly created wrapper instance
20
+ */
13
21
  static get(uri, poolSize, closeDelay) {
14
22
  let client = this.clients.get(uri);
15
23
  if (!client) {
@@ -18,12 +26,15 @@ var MongoClientWrapper = class _MongoClientWrapper {
18
26
  }
19
27
  return client;
20
28
  }
29
+ /** Connects to MongoDB and returns the underlying MongoClient. */
21
30
  async connect() {
22
31
  return await Promise.resolve(this.client);
23
32
  }
33
+ /** Disconnects from MongoDB. */
24
34
  async disconnect() {
25
35
  return await Promise.resolve(0);
26
36
  }
37
+ /** Initiates a graceful close of the connection. */
27
38
  async initiateClose() {
28
39
  await Promise.resolve();
29
40
  }
@@ -31,63 +42,118 @@ var MongoClientWrapper = class _MongoClientWrapper {
31
42
 
32
43
  // src/Base.ts
33
44
  var BaseMongoSdk = class {
45
+ /** The MongoDB SDK configuration for this instance. */
34
46
  config;
35
47
  constructor(config) {
36
48
  this.config = config;
37
49
  }
50
+ /** Returns the MongoDB connection URI, either from the config or constructed from individual credential fields. */
38
51
  get uri() {
39
52
  return this.config.dbConnectionString ?? `mongodb+srv://${this.config.dbUserName}:${this.config.dbPassword}@${this.config.dbDomain}.mongodb.net/${this.config.dbName}?retryWrites=true&w=majority`;
40
53
  }
54
+ /**
55
+ * Deletes all documents matching the filter.
56
+ * @param filter - The query filter to match documents for deletion
57
+ */
41
58
  async deleteMany(filter) {
42
59
  return await this.useCollection(async (collection) => {
43
60
  return await collection.deleteMany(filter);
44
61
  });
45
62
  }
63
+ /**
64
+ * Deletes the first document matching the filter.
65
+ * @param filter - The query filter to match a document for deletion
66
+ */
46
67
  async deleteOne(filter) {
47
68
  return await this.useCollection(async (collection) => {
48
69
  return await collection.deleteOne(filter);
49
70
  });
50
71
  }
72
+ /**
73
+ * Finds all documents matching the filter and returns a cursor.
74
+ * @param filter - The query filter
75
+ */
51
76
  async find(filter) {
52
77
  return await this.useCollection((collection) => {
53
78
  return collection.find(filter);
54
79
  });
55
80
  }
81
+ /**
82
+ * Finds a single document matching the filter.
83
+ * @param filter - The query filter
84
+ * @returns The matched document, or `null` if not found
85
+ */
56
86
  async findOne(filter) {
57
87
  return await this.useCollection(async (collection) => {
58
88
  return await collection.findOne(filter);
59
89
  });
60
90
  }
91
+ /**
92
+ * Inserts multiple documents into the collection.
93
+ * @param items - The documents to insert
94
+ * @param options - Optional bulk write options
95
+ */
61
96
  async insertMany(items, options) {
62
97
  return await this.useCollection(async (collection) => {
63
98
  return await collection.insertMany(items, options);
64
99
  });
65
100
  }
101
+ /**
102
+ * Inserts a single document into the collection.
103
+ * @param item - The document to insert
104
+ * @param options - Optional insert options
105
+ */
66
106
  async insertOne(item, options) {
67
107
  return await this.useCollection(async (collection) => {
68
108
  return await collection.insertOne(item, options);
69
109
  });
70
110
  }
111
+ /**
112
+ * Replaces a single document matching the filter.
113
+ * @param filter - The query filter to match the document
114
+ * @param item - The replacement document
115
+ * @param options - Optional replace options
116
+ */
71
117
  async replaceOne(filter, item, options) {
72
118
  return await this.useCollection(async (collection) => {
73
119
  return await collection.replaceOne(filter, item, options);
74
120
  });
75
121
  }
122
+ /**
123
+ * Updates a single document matching the filter without upserting.
124
+ * @param filter - The query filter to match the document
125
+ * @param fields - The update operations to apply
126
+ */
76
127
  async updateOne(filter, fields) {
77
128
  return await this.useCollection(async (collection) => {
78
129
  return await collection.updateOne(filter, fields, { upsert: false });
79
130
  });
80
131
  }
132
+ /**
133
+ * Updates a single document matching the filter, inserting it if it does not exist.
134
+ * @param filter - The query filter to match the document
135
+ * @param fields - The update operations to apply
136
+ */
81
137
  async upsertOne(filter, fields) {
82
138
  return await this.useCollection(async (collection) => {
83
139
  return await collection.updateOne(filter, fields, { upsert: true });
84
140
  });
85
141
  }
142
+ /**
143
+ * Executes a callback with access to the configured MongoDB collection.
144
+ * @param func - A callback receiving the typed collection
145
+ * @returns The result of the callback
146
+ */
86
147
  async useCollection(func) {
87
148
  return await this.useMongo(async (client) => {
88
149
  return await func(client.db(this.config.dbName).collection(this.config.collection));
89
150
  });
90
151
  }
152
+ /**
153
+ * Executes a callback with a connected MongoClient, handling connection and disconnection.
154
+ * @param func - A callback receiving the connected MongoClient
155
+ * @returns The result of the callback
156
+ */
91
157
  async useMongo(func) {
92
158
  const wrapper = MongoClientWrapper.get(this.uri, this.config.maxPoolSize, this.config.closeDelay);
93
159
  const connection = await wrapper.connect();
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/Base.ts","../../src/Wrapper.ts"],"sourcesContent":["import { assertEx } from '@xylabs/assert'\nimport type {\n BulkWriteOptions,\n Collection,\n DeleteResult,\n Document,\n Filter,\n FindCursor,\n InsertOneOptions,\n MongoClient,\n OptionalUnlessRequiredId,\n ReplaceOptions,\n UpdateFilter,\n WithId,\n} from 'mongodb'\n\nimport type { BaseMongoSdkConfig } from './Config.js'\nimport { MongoClientWrapper } from './Wrapper.js'\n\nexport class BaseMongoSdk<T extends Document> {\n config: BaseMongoSdkConfig\n\n constructor(config: BaseMongoSdkConfig) {\n this.config = config\n }\n\n get uri() {\n return (\n this.config.dbConnectionString\n // eslint-disable-next-line @stylistic/max-len\n ?? `mongodb+srv://${this.config.dbUserName}:${this.config.dbPassword}@${this.config.dbDomain}.mongodb.net/${this.config.dbName}?retryWrites=true&w=majority`\n )\n }\n\n async deleteMany(filter: Filter<T>) {\n return await this.useCollection<DeleteResult>(async (collection: Collection<T>) => {\n return await collection.deleteMany(filter)\n })\n }\n\n async deleteOne(filter: Filter<T>) {\n return await this.useCollection<DeleteResult>(async (collection: Collection<T>) => {\n return await collection.deleteOne(filter)\n })\n }\n\n async find(filter: Filter<T>) {\n return await this.useCollection<FindCursor<WithId<T>>>((collection: Collection<T>) => {\n return collection.find(filter)\n })\n }\n\n async findOne(filter: Filter<T>) {\n return await this.useCollection<WithId<T> | null>(async (collection: Collection<T>) => {\n return await collection.findOne(filter)\n })\n }\n\n async insertMany(items: OptionalUnlessRequiredId<T>[], options?: BulkWriteOptions) {\n return await this.useCollection(async (collection: Collection<T>) => {\n return await collection.insertMany(items, options)\n })\n }\n\n async insertOne(item: OptionalUnlessRequiredId<T>, options?: InsertOneOptions) {\n return await this.useCollection(async (collection: Collection<T>) => {\n return await collection.insertOne(item, options)\n })\n }\n\n async replaceOne(filter: Filter<T>, item: OptionalUnlessRequiredId<T>, options?: ReplaceOptions) {\n return await this.useCollection(async (collection: Collection<T>) => {\n return await collection.replaceOne(filter, item, options)\n })\n }\n\n async updateOne(filter: Filter<T>, fields: UpdateFilter<T>) {\n return await this.useCollection(async (collection: Collection<T>) => {\n return await collection.updateOne(filter, fields, { upsert: false })\n })\n }\n\n async upsertOne(filter: Filter<T>, fields: UpdateFilter<T>) {\n return await this.useCollection(async (collection: Collection<T>) => {\n return await collection.updateOne(filter, fields, { upsert: true })\n })\n }\n\n async useCollection<R>(func: (collection: Collection<T>) => Promise<R> | R) {\n return await this.useMongo<R>(async (client: MongoClient) => {\n return await func(client.db(this.config.dbName).collection<T>(this.config.collection))\n })\n }\n\n async useMongo<R>(func: (client: MongoClient) => Promise<R> | R) {\n const wrapper = MongoClientWrapper.get(this.uri, this.config.maxPoolSize, this.config.closeDelay)\n const connection = await wrapper.connect()\n assertEx(connection, () => 'Connection failed')\n try {\n return await func(connection)\n } finally {\n await wrapper.disconnect()\n }\n }\n}\n","import type { MongoClientOptions } from 'mongodb'\nimport { MongoClient } from 'mongodb'\n\nexport class MongoClientWrapper {\n static readonly clients = new Map<string, MongoClientWrapper>()\n\n private client: MongoClient\n\n constructor(uri: string, maxPoolSize?: number, closeDelay?: number) {\n const options: MongoClientOptions = { maxPoolSize, maxIdleTimeMS: closeDelay }\n this.client = new MongoClient(uri, options)\n }\n\n static get(uri: string, poolSize?: number, closeDelay?: number) {\n let client = this.clients.get(uri)\n if (!client) {\n client = new MongoClientWrapper(uri, poolSize, closeDelay)\n this.clients.set(uri, client)\n }\n return client\n }\n\n async connect() {\n return await Promise.resolve(this.client)\n }\n\n async disconnect() {\n return await Promise.resolve(0)\n }\n\n async initiateClose() {\n await Promise.resolve()\n }\n}\n"],"mappings":";AAAA,SAAS,gBAAgB;;;ACCzB,SAAS,mBAAmB;AAErB,IAAM,qBAAN,MAAM,oBAAmB;AAAA,EAC9B,OAAgB,UAAU,oBAAI,IAAgC;AAAA,EAEtD;AAAA,EAER,YAAY,KAAa,aAAsB,YAAqB;AAClE,UAAM,UAA8B,EAAE,aAAa,eAAe,WAAW;AAC7E,SAAK,SAAS,IAAI,YAAY,KAAK,OAAO;AAAA,EAC5C;AAAA,EAEA,OAAO,IAAI,KAAa,UAAmB,YAAqB;AAC9D,QAAI,SAAS,KAAK,QAAQ,IAAI,GAAG;AACjC,QAAI,CAAC,QAAQ;AACX,eAAS,IAAI,oBAAmB,KAAK,UAAU,UAAU;AACzD,WAAK,QAAQ,IAAI,KAAK,MAAM;AAAA,IAC9B;AACA,WAAO;AAAA,EACT;AAAA,EAEA,MAAM,UAAU;AACd,WAAO,MAAM,QAAQ,QAAQ,KAAK,MAAM;AAAA,EAC1C;AAAA,EAEA,MAAM,aAAa;AACjB,WAAO,MAAM,QAAQ,QAAQ,CAAC;AAAA,EAChC;AAAA,EAEA,MAAM,gBAAgB;AACpB,UAAM,QAAQ,QAAQ;AAAA,EACxB;AACF;;;ADdO,IAAM,eAAN,MAAuC;AAAA,EAC5C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O;AAAA,IAC1D,CAAC;AAAA,EACH;AAAA,EAEA,MAAM,UAAU,QAAmB,QAAyB;AAC1D,WAAO,MAAM,KAAK,cAAc,OAAO,eAA8B;AACnE,aAAO,MAAM,WAAW,UAAU,QAAQ,QAAQ,EAAE,QAAQ,MAAM,CAAC;AAAA,IACrE,CAAC;AAAA,EACH;AAAA,EAEA,MAAM,UAAU,QAAmB,QAAyB;AAC1D,WAAO,MAAM,KAAK,cAAc,OAAO,eAA8B;AACnE,aAAO,MAAM,WAAW,UAAU,QAAQ,QAAQ,EAAE,QAAQ,KAAK,CAAC;AAAA,IACpE,CAAC;AAAA,EACH;AAAA,EAEA,MAAM,cAAiB,MAAqD;AAC1E,WAAO,MAAM,KAAK,SAAY,OAAO,WAAwB;AAC3D,aAAO,MAAM,KAAK,OAAO,GAAG,KAAK,OAAO,MAAM,EAAE,WAAc,KAAK,OAAO,UAAU,CAAC;AAAA,IACvF,CAAC;AAAA,EACH;AAAA,EAEA,MAAM,SAAY,MAA+C;AAC/D,UAAM,UAAU,mBAAmB,IAAI,KAAK,KAAK,KAAK,OAAO,aAAa,KAAK,OAAO,UAAU;AAChG,UAAM,aAAa,MAAM,QAAQ,QAAQ;AACzC,aAAS,YAAY,MAAM,mBAAmB;AAC9C,QAAI;AACF,aAAO,MAAM,KAAK,UAAU;AAAA,IAC9B,UAAE;AACA,YAAM,QAAQ,WAAW;AAAA,IAC3B;AAAA,EACF;AACF;","names":[]}
1
+ {"version":3,"sources":["../../src/Base.ts","../../src/Wrapper.ts"],"sourcesContent":["import { assertEx } from '@xylabs/assert'\nimport type {\n BulkWriteOptions,\n Collection,\n DeleteResult,\n Document,\n Filter,\n FindCursor,\n InsertOneOptions,\n MongoClient,\n OptionalUnlessRequiredId,\n ReplaceOptions,\n UpdateFilter,\n WithId,\n} from 'mongodb'\n\nimport type { BaseMongoSdkConfig } from './Config.js'\nimport { MongoClientWrapper } from './Wrapper.js'\n\n/** Provides a typed wrapper around common MongoDB collection operations. */\nexport class BaseMongoSdk<T extends Document> {\n /** The MongoDB SDK configuration for this instance. */\n config: BaseMongoSdkConfig\n\n constructor(config: BaseMongoSdkConfig) {\n this.config = config\n }\n\n /** Returns the MongoDB connection URI, either from the config or constructed from individual credential fields. */\n get uri() {\n return (\n this.config.dbConnectionString\n ?? `mongodb+srv://${this.config.dbUserName}:${this.config.dbPassword}@${this.config.dbDomain}.mongodb.net/${this.config.dbName}?retryWrites=true&w=majority`\n )\n }\n\n /**\n * Deletes all documents matching the filter.\n * @param filter - The query filter to match documents for deletion\n */\n async deleteMany(filter: Filter<T>) {\n return await this.useCollection<DeleteResult>(async (collection: Collection<T>) => {\n return await collection.deleteMany(filter)\n })\n }\n\n /**\n * Deletes the first document matching the filter.\n * @param filter - The query filter to match a document for deletion\n */\n async deleteOne(filter: Filter<T>) {\n return await this.useCollection<DeleteResult>(async (collection: Collection<T>) => {\n return await collection.deleteOne(filter)\n })\n }\n\n /**\n * Finds all documents matching the filter and returns a cursor.\n * @param filter - The query filter\n */\n async find(filter: Filter<T>) {\n return await this.useCollection<FindCursor<WithId<T>>>((collection: Collection<T>) => {\n return collection.find(filter)\n })\n }\n\n /**\n * Finds a single document matching the filter.\n * @param filter - The query filter\n * @returns The matched document, or `null` if not found\n */\n async findOne(filter: Filter<T>) {\n return await this.useCollection<WithId<T> | null>(async (collection: Collection<T>) => {\n return await collection.findOne(filter)\n })\n }\n\n /**\n * Inserts multiple documents into the collection.\n * @param items - The documents to insert\n * @param options - Optional bulk write options\n */\n async insertMany(items: OptionalUnlessRequiredId<T>[], options?: BulkWriteOptions) {\n return await this.useCollection(async (collection: Collection<T>) => {\n return await collection.insertMany(items, options)\n })\n }\n\n /**\n * Inserts a single document into the collection.\n * @param item - The document to insert\n * @param options - Optional insert options\n */\n async insertOne(item: OptionalUnlessRequiredId<T>, options?: InsertOneOptions) {\n return await this.useCollection(async (collection: Collection<T>) => {\n return await collection.insertOne(item, options)\n })\n }\n\n /**\n * Replaces a single document matching the filter.\n * @param filter - The query filter to match the document\n * @param item - The replacement document\n * @param options - Optional replace options\n */\n async replaceOne(filter: Filter<T>, item: OptionalUnlessRequiredId<T>, options?: ReplaceOptions) {\n return await this.useCollection(async (collection: Collection<T>) => {\n return await collection.replaceOne(filter, item, options)\n })\n }\n\n /**\n * Updates a single document matching the filter without upserting.\n * @param filter - The query filter to match the document\n * @param fields - The update operations to apply\n */\n async updateOne(filter: Filter<T>, fields: UpdateFilter<T>) {\n return await this.useCollection(async (collection: Collection<T>) => {\n return await collection.updateOne(filter, fields, { upsert: false })\n })\n }\n\n /**\n * Updates a single document matching the filter, inserting it if it does not exist.\n * @param filter - The query filter to match the document\n * @param fields - The update operations to apply\n */\n async upsertOne(filter: Filter<T>, fields: UpdateFilter<T>) {\n return await this.useCollection(async (collection: Collection<T>) => {\n return await collection.updateOne(filter, fields, { upsert: true })\n })\n }\n\n /**\n * Executes a callback with access to the configured MongoDB collection.\n * @param func - A callback receiving the typed collection\n * @returns The result of the callback\n */\n async useCollection<R>(func: (collection: Collection<T>) => Promise<R> | R) {\n return await this.useMongo<R>(async (client: MongoClient) => {\n return await func(client.db(this.config.dbName).collection<T>(this.config.collection))\n })\n }\n\n /**\n * Executes a callback with a connected MongoClient, handling connection and disconnection.\n * @param func - A callback receiving the connected MongoClient\n * @returns The result of the callback\n */\n async useMongo<R>(func: (client: MongoClient) => Promise<R> | R) {\n const wrapper = MongoClientWrapper.get(this.uri, this.config.maxPoolSize, this.config.closeDelay)\n const connection = await wrapper.connect()\n assertEx(connection, () => 'Connection failed')\n try {\n return await func(connection)\n } finally {\n await wrapper.disconnect()\n }\n }\n}\n","import type { MongoClientOptions } from 'mongodb'\nimport { MongoClient } from 'mongodb'\n\n/** Manages a shared pool of MongoClient instances, reusing connections by URI. */\nexport class MongoClientWrapper {\n /** Global cache of wrapper instances keyed by connection URI. */\n static readonly clients = new Map<string, MongoClientWrapper>()\n\n private client: MongoClient\n\n constructor(uri: string, maxPoolSize?: number, closeDelay?: number) {\n const options: MongoClientOptions = { maxPoolSize, maxIdleTimeMS: closeDelay }\n this.client = new MongoClient(uri, options)\n }\n\n /**\n * Gets or creates a cached MongoClientWrapper for the given URI.\n * @param uri - The MongoDB connection URI\n * @param poolSize - Maximum connection pool size\n * @param closeDelay - Delay in milliseconds before closing idle connections\n * @returns A cached or newly created wrapper instance\n */\n static get(uri: string, poolSize?: number, closeDelay?: number) {\n let client = this.clients.get(uri)\n if (!client) {\n client = new MongoClientWrapper(uri, poolSize, closeDelay)\n this.clients.set(uri, client)\n }\n return client\n }\n\n /** Connects to MongoDB and returns the underlying MongoClient. */\n async connect() {\n return await Promise.resolve(this.client)\n }\n\n /** Disconnects from MongoDB. */\n async disconnect() {\n return await Promise.resolve(0)\n }\n\n /** Initiates a graceful close of the connection. */\n async initiateClose() {\n await Promise.resolve()\n }\n}\n"],"mappings":";AAAA,SAAS,gBAAgB;;;ACCzB,SAAS,mBAAmB;AAGrB,IAAM,qBAAN,MAAM,oBAAmB;AAAA;AAAA,EAE9B,OAAgB,UAAU,oBAAI,IAAgC;AAAA,EAEtD;AAAA,EAER,YAAY,KAAa,aAAsB,YAAqB;AAClE,UAAM,UAA8B,EAAE,aAAa,eAAe,WAAW;AAC7E,SAAK,SAAS,IAAI,YAAY,KAAK,OAAO;AAAA,EAC5C;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASA,OAAO,IAAI,KAAa,UAAmB,YAAqB;AAC9D,QAAI,SAAS,KAAK,QAAQ,IAAI,GAAG;AACjC,QAAI,CAAC,QAAQ;AACX,eAAS,IAAI,oBAAmB,KAAK,UAAU,UAAU;AACzD,WAAK,QAAQ,IAAI,KAAK,MAAM;AAAA,IAC9B;AACA,WAAO;AAAA,EACT;AAAA;AAAA,EAGA,MAAM,UAAU;AACd,WAAO,MAAM,QAAQ,QAAQ,KAAK,MAAM;AAAA,EAC1C;AAAA;AAAA,EAGA,MAAM,aAAa;AACjB,WAAO,MAAM,QAAQ,QAAQ,CAAC;AA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B;AAC9C,QAAI;AACF,aAAO,MAAM,KAAK,UAAU;AAAA,IAC9B,UAAE;AACA,YAAM,QAAQ,WAAW;AAAA,IAC3B;AAAA,EACF;AACF;","names":[]}
@@ -1,19 +1,75 @@
1
1
  import type { BulkWriteOptions, Collection, DeleteResult, Document, Filter, FindCursor, InsertOneOptions, MongoClient, OptionalUnlessRequiredId, ReplaceOptions, UpdateFilter, WithId } from 'mongodb';
2
2
  import type { BaseMongoSdkConfig } from './Config.js';
3
+ /** Provides a typed wrapper around common MongoDB collection operations. */
3
4
  export declare class BaseMongoSdk<T extends Document> {
5
+ /** The MongoDB SDK configuration for this instance. */
4
6
  config: BaseMongoSdkConfig;
5
7
  constructor(config: BaseMongoSdkConfig);
8
+ /** Returns the MongoDB connection URI, either from the config or constructed from individual credential fields. */
6
9
  get uri(): string;
10
+ /**
11
+ * Deletes all documents matching the filter.
12
+ * @param filter - The query filter to match documents for deletion
13
+ */
7
14
  deleteMany(filter: Filter<T>): Promise<DeleteResult>;
15
+ /**
16
+ * Deletes the first document matching the filter.
17
+ * @param filter - The query filter to match a document for deletion
18
+ */
8
19
  deleteOne(filter: Filter<T>): Promise<DeleteResult>;
20
+ /**
21
+ * Finds all documents matching the filter and returns a cursor.
22
+ * @param filter - The query filter
23
+ */
9
24
  find(filter: Filter<T>): Promise<FindCursor<WithId<T>>>;
25
+ /**
26
+ * Finds a single document matching the filter.
27
+ * @param filter - The query filter
28
+ * @returns The matched document, or `null` if not found
29
+ */
10
30
  findOne(filter: Filter<T>): Promise<WithId<T> | null>;
31
+ /**
32
+ * Inserts multiple documents into the collection.
33
+ * @param items - The documents to insert
34
+ * @param options - Optional bulk write options
35
+ */
11
36
  insertMany(items: OptionalUnlessRequiredId<T>[], options?: BulkWriteOptions): Promise<import("mongodb").InsertManyResult<T>>;
37
+ /**
38
+ * Inserts a single document into the collection.
39
+ * @param item - The document to insert
40
+ * @param options - Optional insert options
41
+ */
12
42
  insertOne(item: OptionalUnlessRequiredId<T>, options?: InsertOneOptions): Promise<import("mongodb").InsertOneResult<T>>;
43
+ /**
44
+ * Replaces a single document matching the filter.
45
+ * @param filter - The query filter to match the document
46
+ * @param item - The replacement document
47
+ * @param options - Optional replace options
48
+ */
13
49
  replaceOne(filter: Filter<T>, item: OptionalUnlessRequiredId<T>, options?: ReplaceOptions): Promise<import("mongodb").UpdateResult<T>>;
50
+ /**
51
+ * Updates a single document matching the filter without upserting.
52
+ * @param filter - The query filter to match the document
53
+ * @param fields - The update operations to apply
54
+ */
14
55
  updateOne(filter: Filter<T>, fields: UpdateFilter<T>): Promise<import("mongodb").UpdateResult<T>>;
56
+ /**
57
+ * Updates a single document matching the filter, inserting it if it does not exist.
58
+ * @param filter - The query filter to match the document
59
+ * @param fields - The update operations to apply
60
+ */
15
61
  upsertOne(filter: Filter<T>, fields: UpdateFilter<T>): Promise<import("mongodb").UpdateResult<T>>;
62
+ /**
63
+ * Executes a callback with access to the configured MongoDB collection.
64
+ * @param func - A callback receiving the typed collection
65
+ * @returns The result of the callback
66
+ */
16
67
  useCollection<R>(func: (collection: Collection<T>) => Promise<R> | R): Promise<R>;
68
+ /**
69
+ * Executes a callback with a connected MongoClient, handling connection and disconnection.
70
+ * @param func - A callback receiving the connected MongoClient
71
+ * @returns The result of the callback
72
+ */
17
73
  useMongo<R>(func: (client: MongoClient) => Promise<R> | R): Promise<R>;
18
74
  }
19
75
  //# sourceMappingURL=Base.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"Base.d.ts","sourceRoot":"","sources":["../../src/Base.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,gBAAgB,EAChB,UAAU,EACV,YAAY,EACZ,QAAQ,EACR,MAAM,EACN,UAAU,EACV,gBAAgB,EAChB,WAAW,EACX,wBAAwB,EACxB,cAAc,EACd,YAAY,EACZ,MAAM,EACP,MAAM,SAAS,CAAA;AAEhB,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAA;AAGrD,qBAAa,YAAY,CA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,CAAC,EAAE,IAAI,EAAE,CAAC,UAAU,EAAE,UAAU,CAAC,CAAC,CAAC,KAAK,OAAO,CAAC,CAAC,CAAC,GAAG,CAAC;IAM1E;;;;OAIG;IACG,QAAQ,CAAC,CAAC,EAAE,IAAI,EAAE,CAAC,MAAM,EAAE,WAAW,KAAK,OAAO,CAAC,CAAC,CAAC,GAAG,CAAC;CAUhE"}
@@ -1,14 +1,25 @@
1
+ /** Public configuration options for the Mongo SDK, safe to expose to clients. */
1
2
  export interface BaseMongoSdkPublicConfig {
3
+ /** Delay in milliseconds before closing idle connections. */
2
4
  closeDelay?: number;
5
+ /** The MongoDB collection name to operate on. */
3
6
  collection: string;
7
+ /** Maximum number of connections in the connection pool. */
4
8
  maxPoolSize?: number;
5
9
  }
10
+ /** Private configuration options for the Mongo SDK containing connection credentials. */
6
11
  export interface BaseMongoSdkPrivateConfig {
12
+ /** A full MongoDB connection string, used instead of individual credential fields. */
7
13
  dbConnectionString?: string;
14
+ /** The MongoDB Atlas cluster domain. */
8
15
  dbDomain?: string;
16
+ /** The database name to connect to. */
9
17
  dbName?: string;
18
+ /** The password for MongoDB authentication. */
10
19
  dbPassword?: string;
20
+ /** The username for MongoDB authentication. */
11
21
  dbUserName?: string;
12
22
  }
23
+ /** Combined public and private MongoDB SDK configuration. */
13
24
  export type BaseMongoSdkConfig = BaseMongoSdkPublicConfig & BaseMongoSdkPrivateConfig;
14
25
  //# sourceMappingURL=Config.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"Config.d.ts","sourceRoot":"","sources":["../../src/Config.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,wBAAwB;IACvC,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB,UAAU,EAAE,MAAM,CAAA;IAClB,WAAW,CAAC,EAAE,MAAM,CAAA;CACrB;AAED,MAAM,WAAW,yBAAyB;IACxC,kBAAkB,CAAC,EAAE,MAAM,CAAA;IAC3B,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB,UAAU,CAAC,EAAE,MAAM,CAAA;CACpB;AAED,MAAM,MAAM,kBAAkB,GAAG,wBAAwB,GAAG,yBAAyB,CAAA"}
1
+ {"version":3,"file":"Config.d.ts","sourceRoot":"","sources":["../../src/Config.ts"],"names":[],"mappings":"AAAA,iFAAiF;AACjF,MAAM,WAAW,wBAAwB;IACvC,6DAA6D;IAC7D,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB,iDAAiD;IACjD,UAAU,EAAE,MAAM,CAAA;IAClB,4DAA4D;IAC5D,WAAW,CAAC,EAAE,MAAM,CAAA;CACrB;AAED,yFAAyF;AACzF,MAAM,WAAW,yBAAyB;IACxC,sFAAsF;IACtF,kBAAkB,CAAC,EAAE,MAAM,CAAA;IAC3B,wCAAwC;IACxC,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,uCAAuC;IACvC,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,+CAA+C;IAC/C,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB,+CAA+C;IAC/C,UAAU,CAAC,EAAE,MAAM,CAAA;CACpB;AAED,6DAA6D;AAC7D,MAAM,MAAM,kBAAkB,GAAG,wBAAwB,GAAG,yBAAyB,CAAA"}
@@ -1,11 +1,23 @@
1
1
  import { MongoClient } from 'mongodb';
2
+ /** Manages a shared pool of MongoClient instances, reusing connections by URI. */
2
3
  export declare class MongoClientWrapper {
4
+ /** Global cache of wrapper instances keyed by connection URI. */
3
5
  static readonly clients: Map<string, MongoClientWrapper>;
4
6
  private client;
5
7
  constructor(uri: string, maxPoolSize?: number, closeDelay?: number);
8
+ /**
9
+ * Gets or creates a cached MongoClientWrapper for the given URI.
10
+ * @param uri - The MongoDB connection URI
11
+ * @param poolSize - Maximum connection pool size
12
+ * @param closeDelay - Delay in milliseconds before closing idle connections
13
+ * @returns A cached or newly created wrapper instance
14
+ */
6
15
  static get(uri: string, poolSize?: number, closeDelay?: number): MongoClientWrapper;
16
+ /** Connects to MongoDB and returns the underlying MongoClient. */
7
17
  connect(): Promise<MongoClient>;
18
+ /** Disconnects from MongoDB. */
8
19
  disconnect(): Promise<number>;
20
+ /** Initiates a graceful close of the connection. */
9
21
  initiateClose(): Promise<void>;
10
22
  }
11
23
  //# sourceMappingURL=Wrapper.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"Wrapper.d.ts","sourceRoot":"","sources":["../../src/Wrapper.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAE,MAAM,SAAS,CAAA;AAErC,qBAAa,kBAAkB;IAC7B,MAAM,CAAC,QAAQ,CAAC,OAAO,kCAAwC;IAE/D,OAAO,CAAC,MAAM,CAAa;gBAEf,GAAG,EAAE,MAAM,EAAE,WAAW,CAAC,EAAE,MAAM,EAAE,UAAU,CAAC,EAAE,MAAM;IAKlE,MAAM,CAAC,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,MAAM,EAAE,UAAU,CAAC,EAAE,MAAM;IASxD,OAAO;IAIP,UAAU;IAIV,aAAa;CAGpB"}
1
+ {"version":3,"file":"Wrapper.d.ts","sourceRoot":"","sources":["../../src/Wrapper.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAE,MAAM,SAAS,CAAA;AAErC,kFAAkF;AAClF,qBAAa,kBAAkB;IAC7B,iEAAiE;IACjE,MAAM,CAAC,QAAQ,CAAC,OAAO,kCAAwC;IAE/D,OAAO,CAAC,MAAM,CAAa;gBAEf,GAAG,EAAE,MAAM,EAAE,WAAW,CAAC,EAAE,MAAM,EAAE,UAAU,CAAC,EAAE,MAAM;IAKlE;;;;;;OAMG;IACH,MAAM,CAAC,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,MAAM,EAAE,UAAU,CAAC,EAAE,MAAM;IAS9D,kEAAkE;IAC5D,OAAO;IAIb,gCAAgC;IAC1B,UAAU;IAIhB,oDAAoD;IAC9C,aAAa;CAGpB"}
@@ -4,12 +4,20 @@ import { assertEx } from "@xylabs/assert";
4
4
  // src/Wrapper.ts
5
5
  import { MongoClient } from "mongodb";
6
6
  var MongoClientWrapper = class _MongoClientWrapper {
7
+ /** Global cache of wrapper instances keyed by connection URI. */
7
8
  static clients = /* @__PURE__ */ new Map();
8
9
  client;
9
10
  constructor(uri, maxPoolSize, closeDelay) {
10
11
  const options = { maxPoolSize, maxIdleTimeMS: closeDelay };
11
12
  this.client = new MongoClient(uri, options);
12
13
  }
14
+ /**
15
+ * Gets or creates a cached MongoClientWrapper for the given URI.
16
+ * @param uri - The MongoDB connection URI
17
+ * @param poolSize - Maximum connection pool size
18
+ * @param closeDelay - Delay in milliseconds before closing idle connections
19
+ * @returns A cached or newly created wrapper instance
20
+ */
13
21
  static get(uri, poolSize, closeDelay) {
14
22
  let client = this.clients.get(uri);
15
23
  if (!client) {
@@ -18,12 +26,15 @@ var MongoClientWrapper = class _MongoClientWrapper {
18
26
  }
19
27
  return client;
20
28
  }
29
+ /** Connects to MongoDB and returns the underlying MongoClient. */
21
30
  async connect() {
22
31
  return await Promise.resolve(this.client);
23
32
  }
33
+ /** Disconnects from MongoDB. */
24
34
  async disconnect() {
25
35
  return await Promise.resolve(0);
26
36
  }
37
+ /** Initiates a graceful close of the connection. */
27
38
  async initiateClose() {
28
39
  await Promise.resolve();
29
40
  }
@@ -31,63 +42,118 @@ var MongoClientWrapper = class _MongoClientWrapper {
31
42
 
32
43
  // src/Base.ts
33
44
  var BaseMongoSdk = class {
45
+ /** The MongoDB SDK configuration for this instance. */
34
46
  config;
35
47
  constructor(config) {
36
48
  this.config = config;
37
49
  }
50
+ /** Returns the MongoDB connection URI, either from the config or constructed from individual credential fields. */
38
51
  get uri() {
39
52
  return this.config.dbConnectionString ?? `mongodb+srv://${this.config.dbUserName}:${this.config.dbPassword}@${this.config.dbDomain}.mongodb.net/${this.config.dbName}?retryWrites=true&w=majority`;
40
53
  }
54
+ /**
55
+ * Deletes all documents matching the filter.
56
+ * @param filter - The query filter to match documents for deletion
57
+ */
41
58
  async deleteMany(filter) {
42
59
  return await this.useCollection(async (collection) => {
43
60
  return await collection.deleteMany(filter);
44
61
  });
45
62
  }
63
+ /**
64
+ * Deletes the first document matching the filter.
65
+ * @param filter - The query filter to match a document for deletion
66
+ */
46
67
  async deleteOne(filter) {
47
68
  return await this.useCollection(async (collection) => {
48
69
  return await collection.deleteOne(filter);
49
70
  });
50
71
  }
72
+ /**
73
+ * Finds all documents matching the filter and returns a cursor.
74
+ * @param filter - The query filter
75
+ */
51
76
  async find(filter) {
52
77
  return await this.useCollection((collection) => {
53
78
  return collection.find(filter);
54
79
  });
55
80
  }
81
+ /**
82
+ * Finds a single document matching the filter.
83
+ * @param filter - The query filter
84
+ * @returns The matched document, or `null` if not found
85
+ */
56
86
  async findOne(filter) {
57
87
  return await this.useCollection(async (collection) => {
58
88
  return await collection.findOne(filter);
59
89
  });
60
90
  }
91
+ /**
92
+ * Inserts multiple documents into the collection.
93
+ * @param items - The documents to insert
94
+ * @param options - Optional bulk write options
95
+ */
61
96
  async insertMany(items, options) {
62
97
  return await this.useCollection(async (collection) => {
63
98
  return await collection.insertMany(items, options);
64
99
  });
65
100
  }
101
+ /**
102
+ * Inserts a single document into the collection.
103
+ * @param item - The document to insert
104
+ * @param options - Optional insert options
105
+ */
66
106
  async insertOne(item, options) {
67
107
  return await this.useCollection(async (collection) => {
68
108
  return await collection.insertOne(item, options);
69
109
  });
70
110
  }
111
+ /**
112
+ * Replaces a single document matching the filter.
113
+ * @param filter - The query filter to match the document
114
+ * @param item - The replacement document
115
+ * @param options - Optional replace options
116
+ */
71
117
  async replaceOne(filter, item, options) {
72
118
  return await this.useCollection(async (collection) => {
73
119
  return await collection.replaceOne(filter, item, options);
74
120
  });
75
121
  }
122
+ /**
123
+ * Updates a single document matching the filter without upserting.
124
+ * @param filter - The query filter to match the document
125
+ * @param fields - The update operations to apply
126
+ */
76
127
  async updateOne(filter, fields) {
77
128
  return await this.useCollection(async (collection) => {
78
129
  return await collection.updateOne(filter, fields, { upsert: false });
79
130
  });
80
131
  }
132
+ /**
133
+ * Updates a single document matching the filter, inserting it if it does not exist.
134
+ * @param filter - The query filter to match the document
135
+ * @param fields - The update operations to apply
136
+ */
81
137
  async upsertOne(filter, fields) {
82
138
  return await this.useCollection(async (collection) => {
83
139
  return await collection.updateOne(filter, fields, { upsert: true });
84
140
  });
85
141
  }
142
+ /**
143
+ * Executes a callback with access to the configured MongoDB collection.
144
+ * @param func - A callback receiving the typed collection
145
+ * @returns The result of the callback
146
+ */
86
147
  async useCollection(func) {
87
148
  return await this.useMongo(async (client) => {
88
149
  return await func(client.db(this.config.dbName).collection(this.config.collection));
89
150
  });
90
151
  }
152
+ /**
153
+ * Executes a callback with a connected MongoClient, handling connection and disconnection.
154
+ * @param func - A callback receiving the connected MongoClient
155
+ * @returns The result of the callback
156
+ */
91
157
  async useMongo(func) {
92
158
  const wrapper = MongoClientWrapper.get(this.uri, this.config.maxPoolSize, this.config.closeDelay);
93
159
  const connection = await wrapper.connect();
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/Base.ts","../../src/Wrapper.ts"],"sourcesContent":["import { assertEx } from '@xylabs/assert'\nimport type {\n BulkWriteOptions,\n Collection,\n DeleteResult,\n Document,\n Filter,\n FindCursor,\n InsertOneOptions,\n MongoClient,\n OptionalUnlessRequiredId,\n ReplaceOptions,\n UpdateFilter,\n WithId,\n} from 'mongodb'\n\nimport type { BaseMongoSdkConfig } from './Config.js'\nimport { MongoClientWrapper } from './Wrapper.js'\n\nexport class BaseMongoSdk<T extends Document> {\n config: BaseMongoSdkConfig\n\n constructor(config: BaseMongoSdkConfig) {\n this.config = config\n }\n\n get uri() {\n return (\n this.config.dbConnectionString\n // eslint-disable-next-line @stylistic/max-len\n ?? `mongodb+srv://${this.config.dbUserName}:${this.config.dbPassword}@${this.config.dbDomain}.mongodb.net/${this.config.dbName}?retryWrites=true&w=majority`\n )\n }\n\n async deleteMany(filter: Filter<T>) {\n return await this.useCollection<DeleteResult>(async (collection: Collection<T>) => {\n return await collection.deleteMany(filter)\n })\n }\n\n async deleteOne(filter: Filter<T>) {\n return await this.useCollection<DeleteResult>(async (collection: Collection<T>) => {\n return await collection.deleteOne(filter)\n })\n }\n\n async find(filter: Filter<T>) {\n return await this.useCollection<FindCursor<WithId<T>>>((collection: Collection<T>) => {\n return collection.find(filter)\n })\n }\n\n async findOne(filter: Filter<T>) {\n return await this.useCollection<WithId<T> | null>(async (collection: Collection<T>) => {\n return await collection.findOne(filter)\n })\n }\n\n async insertMany(items: OptionalUnlessRequiredId<T>[], options?: BulkWriteOptions) {\n return await this.useCollection(async (collection: Collection<T>) => {\n return await collection.insertMany(items, options)\n })\n }\n\n async insertOne(item: OptionalUnlessRequiredId<T>, options?: InsertOneOptions) {\n return await this.useCollection(async (collection: Collection<T>) => {\n return await collection.insertOne(item, options)\n })\n }\n\n async replaceOne(filter: Filter<T>, item: OptionalUnlessRequiredId<T>, options?: ReplaceOptions) {\n return await this.useCollection(async (collection: Collection<T>) => {\n return await collection.replaceOne(filter, item, options)\n })\n }\n\n async updateOne(filter: Filter<T>, fields: UpdateFilter<T>) {\n return await this.useCollection(async (collection: Collection<T>) => {\n return await collection.updateOne(filter, fields, { upsert: false })\n })\n }\n\n async upsertOne(filter: Filter<T>, fields: UpdateFilter<T>) {\n return await this.useCollection(async (collection: Collection<T>) => {\n return await collection.updateOne(filter, fields, { upsert: true })\n })\n }\n\n async useCollection<R>(func: (collection: Collection<T>) => Promise<R> | R) {\n return await this.useMongo<R>(async (client: MongoClient) => {\n return await func(client.db(this.config.dbName).collection<T>(this.config.collection))\n })\n }\n\n async useMongo<R>(func: (client: MongoClient) => Promise<R> | R) {\n const wrapper = MongoClientWrapper.get(this.uri, this.config.maxPoolSize, this.config.closeDelay)\n const connection = await wrapper.connect()\n assertEx(connection, () => 'Connection failed')\n try {\n return await func(connection)\n } finally {\n await wrapper.disconnect()\n }\n }\n}\n","import type { MongoClientOptions } from 'mongodb'\nimport { MongoClient } from 'mongodb'\n\nexport class MongoClientWrapper {\n static readonly clients = new Map<string, MongoClientWrapper>()\n\n private client: MongoClient\n\n constructor(uri: string, maxPoolSize?: number, closeDelay?: number) {\n const options: MongoClientOptions = { maxPoolSize, maxIdleTimeMS: closeDelay }\n this.client = new MongoClient(uri, options)\n }\n\n static get(uri: string, poolSize?: number, closeDelay?: number) {\n let client = this.clients.get(uri)\n if (!client) {\n client = new MongoClientWrapper(uri, poolSize, closeDelay)\n this.clients.set(uri, client)\n }\n return client\n }\n\n async connect() {\n return await Promise.resolve(this.client)\n }\n\n async disconnect() {\n return await Promise.resolve(0)\n }\n\n async initiateClose() {\n await Promise.resolve()\n }\n}\n"],"mappings":";AAAA,SAAS,gBAAgB;;;ACCzB,SAAS,mBAAmB;AAErB,IAAM,qBAAN,MAAM,oBAAmB;AAAA,EAC9B,OAAgB,UAAU,oBAAI,IAAgC;AAAA,EAEtD;AAAA,EAER,YAAY,KAAa,aAAsB,YAAqB;AAClE,UAAM,UAA8B,EAAE,aAAa,eAAe,WAAW;AAC7E,SAAK,SAAS,IAAI,YAAY,KAAK,OAAO;AAAA,EAC5C;AAAA,EAEA,OAAO,IAAI,KAAa,UAAmB,YAAqB;AAC9D,QAAI,SAAS,KAAK,QAAQ,IAAI,GAAG;AACjC,QAAI,CAAC,QAAQ;AACX,eAAS,IAAI,oBAAmB,KAAK,UAAU,UAAU;AACzD,WAAK,QAAQ,IAAI,KAAK,MAAM;AAAA,IAC9B;AACA,WAAO;AAAA,EACT;AAAA,EAEA,MAAM,UAAU;AACd,WAAO,MAAM,QAAQ,QAAQ,KAAK,MAAM;AAAA,EAC1C;AAAA,EAEA,MAAM,aAAa;AACjB,WAAO,MAAM,QAAQ,QAAQ,CAAC;AAAA,EAChC;AAAA,EAEA,MAAM,gBAAgB;AACpB,UAAM,QAAQ,QAAQ;AAAA,EACxB;AACF;;;ADdO,IAAM,eAAN,MAAuC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Q,MAAM,OAAO;AAAA,IAC1D,CAAC;AAAA,EACH;AAAA,EAEA,MAAM,UAAU,QAAmB,QAAyB;AAC1D,WAAO,MAAM,KAAK,cAAc,OAAO,eAA8B;AACnE,aAAO,MAAM,WAAW,UAAU,QAAQ,QAAQ,EAAE,QAAQ,MAAM,CAAC;AAAA,IACrE,CAAC;AAAA,EACH;AAAA,EAEA,MAAM,UAAU,QAAmB,QAAyB;AAC1D,WAAO,MAAM,KAAK,cAAc,OAAO,eAA8B;AACnE,aAAO,MAAM,WAAW,UAAU,QAAQ,QAAQ,EAAE,QAAQ,KAAK,CAAC;AAAA,IACpE,CAAC;AAAA,EACH;AAAA,EAEA,MAAM,cAAiB,MAAqD;AAC1E,WAAO,MAAM,KAAK,SAAY,OAAO,WAAwB;AAC3D,aAAO,MAAM,KAAK,OAAO,GAAG,KAAK,OAAO,MAAM,EAAE,WAAc,KAAK,OAAO,UAAU,CAAC;AAAA,IACvF,CAAC;AAAA,EACH;AAAA,EAEA,MAAM,SAAY,MAA+C;AAC/D,UAAM,UAAU,mBAAmB,IAAI,KAAK,KAAK,KAAK,OAAO,aAAa,KAAK,OAAO,UAAU;AAChG,UAAM,aAAa,MAAM,QAAQ,QAAQ;AACzC,aAAS,YAAY,MAAM,mBAAmB;AAC9C,QAAI;AACF,aAAO,MAAM,KAAK,UAAU;AAAA,IAC9B,UAAE;AACA,YAAM,QAAQ,WAAW;AAAA,IAC3B;AAAA,EACF;AACF;","names":[]}
1
+ {"version":3,"sources":["../../src/Base.ts","../../src/Wrapper.ts"],"sourcesContent":["import { assertEx } from '@xylabs/assert'\nimport type {\n BulkWriteOptions,\n Collection,\n DeleteResult,\n Document,\n Filter,\n FindCursor,\n InsertOneOptions,\n MongoClient,\n OptionalUnlessRequiredId,\n ReplaceOptions,\n UpdateFilter,\n WithId,\n} from 'mongodb'\n\nimport type { BaseMongoSdkConfig } from './Config.js'\nimport { MongoClientWrapper } from './Wrapper.js'\n\n/** Provides a typed wrapper around common MongoDB collection operations. */\nexport class BaseMongoSdk<T extends Document> {\n /** The MongoDB SDK configuration for this instance. */\n config: BaseMongoSdkConfig\n\n constructor(config: BaseMongoSdkConfig) {\n this.config = config\n }\n\n /** Returns the MongoDB connection URI, either from the config or constructed from individual credential fields. */\n get uri() {\n return (\n this.config.dbConnectionString\n ?? `mongodb+srv://${this.config.dbUserName}:${this.config.dbPassword}@${this.config.dbDomain}.mongodb.net/${this.config.dbName}?retryWrites=true&w=majority`\n )\n }\n\n /**\n * Deletes all documents matching the filter.\n * @param filter - The query filter to match documents for deletion\n */\n async deleteMany(filter: Filter<T>) {\n return await this.useCollection<DeleteResult>(async (collection: Collection<T>) => {\n return await collection.deleteMany(filter)\n })\n }\n\n /**\n * Deletes the first document matching the filter.\n * @param filter - The query filter to match a document for deletion\n */\n async deleteOne(filter: Filter<T>) {\n return await this.useCollection<DeleteResult>(async (collection: Collection<T>) => {\n return await collection.deleteOne(filter)\n })\n }\n\n /**\n * Finds all documents matching the filter and returns a cursor.\n * @param filter - The query filter\n */\n async find(filter: Filter<T>) {\n return await this.useCollection<FindCursor<WithId<T>>>((collection: Collection<T>) => {\n return collection.find(filter)\n })\n }\n\n /**\n * Finds a single document matching the filter.\n * @param filter - The query filter\n * @returns The matched document, or `null` if not found\n */\n async findOne(filter: Filter<T>) {\n return await this.useCollection<WithId<T> | null>(async (collection: Collection<T>) => {\n return await collection.findOne(filter)\n })\n }\n\n /**\n * Inserts multiple documents into the collection.\n * @param items - The documents to insert\n * @param options - Optional bulk write options\n */\n async insertMany(items: OptionalUnlessRequiredId<T>[], options?: BulkWriteOptions) {\n return await this.useCollection(async (collection: Collection<T>) => {\n return await collection.insertMany(items, options)\n })\n }\n\n /**\n * Inserts a single document into the collection.\n * @param item - The document to insert\n * @param options - Optional insert options\n */\n async insertOne(item: OptionalUnlessRequiredId<T>, options?: InsertOneOptions) {\n return await this.useCollection(async (collection: Collection<T>) => {\n return await collection.insertOne(item, options)\n })\n }\n\n /**\n * Replaces a single document matching the filter.\n * @param filter - The query filter to match the document\n * @param item - The replacement document\n * @param options - Optional replace options\n */\n async replaceOne(filter: Filter<T>, item: OptionalUnlessRequiredId<T>, options?: ReplaceOptions) {\n return await this.useCollection(async (collection: Collection<T>) => {\n return await collection.replaceOne(filter, item, options)\n })\n }\n\n /**\n * Updates a single document matching the filter without upserting.\n * @param filter - The query filter to match the document\n * @param fields - The update operations to apply\n */\n async updateOne(filter: Filter<T>, fields: UpdateFilter<T>) {\n return await this.useCollection(async (collection: Collection<T>) => {\n return await collection.updateOne(filter, fields, { upsert: false })\n })\n }\n\n /**\n * Updates a single document matching the filter, inserting it if it does not exist.\n * @param filter - The query filter to match the document\n * @param fields - The update operations to apply\n */\n async upsertOne(filter: Filter<T>, fields: UpdateFilter<T>) {\n return await this.useCollection(async (collection: Collection<T>) => {\n return await collection.updateOne(filter, fields, { upsert: true })\n })\n }\n\n /**\n * Executes a callback with access to the configured MongoDB collection.\n * @param func - A callback receiving the typed collection\n * @returns The result of the callback\n */\n async useCollection<R>(func: (collection: Collection<T>) => Promise<R> | R) {\n return await this.useMongo<R>(async (client: MongoClient) => {\n return await func(client.db(this.config.dbName).collection<T>(this.config.collection))\n })\n }\n\n /**\n * Executes a callback with a connected MongoClient, handling connection and disconnection.\n * @param func - A callback receiving the connected MongoClient\n * @returns The result of the callback\n */\n async useMongo<R>(func: (client: MongoClient) => Promise<R> | R) {\n const wrapper = MongoClientWrapper.get(this.uri, this.config.maxPoolSize, this.config.closeDelay)\n const connection = await wrapper.connect()\n assertEx(connection, () => 'Connection failed')\n try {\n return await func(connection)\n } finally {\n await wrapper.disconnect()\n }\n }\n}\n","import type { MongoClientOptions } from 'mongodb'\nimport { MongoClient } from 'mongodb'\n\n/** Manages a shared pool of MongoClient instances, reusing connections by URI. */\nexport class MongoClientWrapper {\n /** Global cache of wrapper instances keyed by connection URI. */\n static readonly clients = new Map<string, MongoClientWrapper>()\n\n private client: MongoClient\n\n constructor(uri: string, maxPoolSize?: number, closeDelay?: number) {\n const options: MongoClientOptions = { maxPoolSize, maxIdleTimeMS: closeDelay }\n this.client = new MongoClient(uri, options)\n }\n\n /**\n * Gets or creates a cached MongoClientWrapper for the given URI.\n * @param uri - The MongoDB connection URI\n * @param poolSize - Maximum connection pool size\n * @param closeDelay - Delay in milliseconds before closing idle connections\n * @returns A cached or newly created wrapper instance\n */\n static get(uri: string, poolSize?: number, closeDelay?: number) {\n let client = this.clients.get(uri)\n if (!client) {\n client = new MongoClientWrapper(uri, poolSize, closeDelay)\n this.clients.set(uri, client)\n }\n return client\n }\n\n /** Connects to MongoDB and returns the underlying MongoClient. */\n async connect() {\n return await Promise.resolve(this.client)\n }\n\n /** Disconnects from MongoDB. */\n async disconnect() {\n return await Promise.resolve(0)\n }\n\n /** Initiates a graceful close of the connection. */\n async initiateClose() {\n await Promise.resolve()\n }\n}\n"],"mappings":";AAAA,SAAS,gBAAgB;;;ACCzB,SAAS,mBAAmB;AAGrB,IAAM,qBAAN,MAAM,oBAAmB;AAAA;AAAA,EAE9B,OAAgB,UAAU,oBAAI,IAAgC;AAAA,EAEtD;AAAA,EAER,YAAY,KAAa,aAAsB,YAAqB;AAClE,UAAM,UAA8B,EAAE,aAAa,eAAe,WAAW;AAC7E,SAAK,SAAS,IAAI,YAAY,KAAK,OAAO;AAAA,EAC5C;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASA,OAAO,IAAI,KAAa,UAAmB,YAAqB;AAC9D,QAAI,SAAS,KAAK,QAAQ,IAAI,GAAG;AACjC,QAAI,CAAC,QAAQ;AACX,eAAS,IAAI,oBAAmB,KAAK,UAAU,UAAU;AACzD,WAAK,QAAQ,IAAI,KAAK,MAAM;AAAA,IAC9B;AACA,WAAO;AAAA,EACT;AAAA;AAAA,EAGA,MAAM,UAAU;AACd,WAAO,MAAM,QAAQ,QAAQ,KAAK,MAAM;AAAA,EAC1C;AAAA;AAAA,EAGA,MAAM,aAAa;AACjB,WAAO,MAAM,QAAQ,QA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,MAAM,mBAAmB;AAC9C,QAAI;AACF,aAAO,MAAM,KAAK,UAAU;AAAA,IAC9B,UAAE;AACA,YAAM,QAAQ,WAAW;AAAA,IAC3B;AAAA,EACF;AACF;","names":[]}
@@ -1,19 +1,75 @@
1
1
  import type { BulkWriteOptions, Collection, DeleteResult, Document, Filter, FindCursor, InsertOneOptions, MongoClient, OptionalUnlessRequiredId, ReplaceOptions, UpdateFilter, WithId } from 'mongodb';
2
2
  import type { BaseMongoSdkConfig } from './Config.js';
3
+ /** Provides a typed wrapper around common MongoDB collection operations. */
3
4
  export declare class BaseMongoSdk<T extends Document> {
5
+ /** The MongoDB SDK configuration for this instance. */
4
6
  config: BaseMongoSdkConfig;
5
7
  constructor(config: BaseMongoSdkConfig);
8
+ /** Returns the MongoDB connection URI, either from the config or constructed from individual credential fields. */
6
9
  get uri(): string;
10
+ /**
11
+ * Deletes all documents matching the filter.
12
+ * @param filter - The query filter to match documents for deletion
13
+ */
7
14
  deleteMany(filter: Filter<T>): Promise<DeleteResult>;
15
+ /**
16
+ * Deletes the first document matching the filter.
17
+ * @param filter - The query filter to match a document for deletion
18
+ */
8
19
  deleteOne(filter: Filter<T>): Promise<DeleteResult>;
20
+ /**
21
+ * Finds all documents matching the filter and returns a cursor.
22
+ * @param filter - The query filter
23
+ */
9
24
  find(filter: Filter<T>): Promise<FindCursor<WithId<T>>>;
25
+ /**
26
+ * Finds a single document matching the filter.
27
+ * @param filter - The query filter
28
+ * @returns The matched document, or `null` if not found
29
+ */
10
30
  findOne(filter: Filter<T>): Promise<WithId<T> | null>;
31
+ /**
32
+ * Inserts multiple documents into the collection.
33
+ * @param items - The documents to insert
34
+ * @param options - Optional bulk write options
35
+ */
11
36
  insertMany(items: OptionalUnlessRequiredId<T>[], options?: BulkWriteOptions): Promise<import("mongodb").InsertManyResult<T>>;
37
+ /**
38
+ * Inserts a single document into the collection.
39
+ * @param item - The document to insert
40
+ * @param options - Optional insert options
41
+ */
12
42
  insertOne(item: OptionalUnlessRequiredId<T>, options?: InsertOneOptions): Promise<import("mongodb").InsertOneResult<T>>;
43
+ /**
44
+ * Replaces a single document matching the filter.
45
+ * @param filter - The query filter to match the document
46
+ * @param item - The replacement document
47
+ * @param options - Optional replace options
48
+ */
13
49
  replaceOne(filter: Filter<T>, item: OptionalUnlessRequiredId<T>, options?: ReplaceOptions): Promise<import("mongodb").UpdateResult<T>>;
50
+ /**
51
+ * Updates a single document matching the filter without upserting.
52
+ * @param filter - The query filter to match the document
53
+ * @param fields - The update operations to apply
54
+ */
14
55
  updateOne(filter: Filter<T>, fields: UpdateFilter<T>): Promise<import("mongodb").UpdateResult<T>>;
56
+ /**
57
+ * Updates a single document matching the filter, inserting it if it does not exist.
58
+ * @param filter - The query filter to match the document
59
+ * @param fields - The update operations to apply
60
+ */
15
61
  upsertOne(filter: Filter<T>, fields: UpdateFilter<T>): Promise<import("mongodb").UpdateResult<T>>;
62
+ /**
63
+ * Executes a callback with access to the configured MongoDB collection.
64
+ * @param func - A callback receiving the typed collection
65
+ * @returns The result of the callback
66
+ */
16
67
  useCollection<R>(func: (collection: Collection<T>) => Promise<R> | R): Promise<R>;
68
+ /**
69
+ * Executes a callback with a connected MongoClient, handling connection and disconnection.
70
+ * @param func - A callback receiving the connected MongoClient
71
+ * @returns The result of the callback
72
+ */
17
73
  useMongo<R>(func: (client: MongoClient) => Promise<R> | R): Promise<R>;
18
74
  }
19
75
  //# sourceMappingURL=Base.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"Base.d.ts","sourceRoot":"","sources":["../../src/Base.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,gBAAgB,EAChB,UAAU,EACV,YAAY,EACZ,QAAQ,EACR,MAAM,EACN,UAAU,EACV,gBAAgB,EAChB,WAAW,EACX,wBAAwB,EACxB,cAAc,EACd,YAAY,EACZ,MAAM,EACP,MAAM,SAAS,CAAA;AAEhB,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAA;AAGrD,qB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G,aAAa,CAAC,CAAC,EAAE,IAAI,EAAE,CAAC,UAAU,EAAE,UAAU,CAAC,CAAC,CAAC,KAAK,OAAO,CAAC,CAAC,CAAC,GAAG,CAAC;IAM1E;;;;OAIG;IACG,QAAQ,CAAC,CAAC,EAAE,IAAI,EAAE,CAAC,MAAM,EAAE,WAAW,KAAK,OAAO,CAAC,CAAC,CAAC,GAAG,CAAC;CAUhE"}
@@ -1,14 +1,25 @@
1
+ /** Public configuration options for the Mongo SDK, safe to expose to clients. */
1
2
  export interface BaseMongoSdkPublicConfig {
3
+ /** Delay in milliseconds before closing idle connections. */
2
4
  closeDelay?: number;
5
+ /** The MongoDB collection name to operate on. */
3
6
  collection: string;
7
+ /** Maximum number of connections in the connection pool. */
4
8
  maxPoolSize?: number;
5
9
  }
10
+ /** Private configuration options for the Mongo SDK containing connection credentials. */
6
11
  export interface BaseMongoSdkPrivateConfig {
12
+ /** A full MongoDB connection string, used instead of individual credential fields. */
7
13
  dbConnectionString?: string;
14
+ /** The MongoDB Atlas cluster domain. */
8
15
  dbDomain?: string;
16
+ /** The database name to connect to. */
9
17
  dbName?: string;
18
+ /** The password for MongoDB authentication. */
10
19
  dbPassword?: string;
20
+ /** The username for MongoDB authentication. */
11
21
  dbUserName?: string;
12
22
  }
23
+ /** Combined public and private MongoDB SDK configuration. */
13
24
  export type BaseMongoSdkConfig = BaseMongoSdkPublicConfig & BaseMongoSdkPrivateConfig;
14
25
  //# sourceMappingURL=Config.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"Config.d.ts","sourceRoot":"","sources":["../../src/Config.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,wBAAwB;IACvC,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB,UAAU,EAAE,MAAM,CAAA;IAClB,WAAW,CAAC,EAAE,MAAM,CAAA;CACrB;AAED,MAAM,WAAW,yBAAyB;IACxC,kBAAkB,CAAC,EAAE,MAAM,CAAA;IAC3B,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB,UAAU,CAAC,EAAE,MAAM,CAAA;CACpB;AAED,MAAM,MAAM,kBAAkB,GAAG,wBAAwB,GAAG,yBAAyB,CAAA"}
1
+ {"version":3,"file":"Config.d.ts","sourceRoot":"","sources":["../../src/Config.ts"],"names":[],"mappings":"AAAA,iFAAiF;AACjF,MAAM,WAAW,wBAAwB;IACvC,6DAA6D;IAC7D,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB,iDAAiD;IACjD,UAAU,EAAE,MAAM,CAAA;IAClB,4DAA4D;IAC5D,WAAW,CAAC,EAAE,MAAM,CAAA;CACrB;AAED,yFAAyF;AACzF,MAAM,WAAW,yBAAyB;IACxC,sFAAsF;IACtF,kBAAkB,CAAC,EAAE,MAAM,CAAA;IAC3B,wCAAwC;IACxC,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,uCAAuC;IACvC,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,+CAA+C;IAC/C,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB,+CAA+C;IAC/C,UAAU,CAAC,EAAE,MAAM,CAAA;CACpB;AAED,6DAA6D;AAC7D,MAAM,MAAM,kBAAkB,GAAG,wBAAwB,GAAG,yBAAyB,CAAA"}
@@ -1,11 +1,23 @@
1
1
  import { MongoClient } from 'mongodb';
2
+ /** Manages a shared pool of MongoClient instances, reusing connections by URI. */
2
3
  export declare class MongoClientWrapper {
4
+ /** Global cache of wrapper instances keyed by connection URI. */
3
5
  static readonly clients: Map<string, MongoClientWrapper>;
4
6
  private client;
5
7
  constructor(uri: string, maxPoolSize?: number, closeDelay?: number);
8
+ /**
9
+ * Gets or creates a cached MongoClientWrapper for the given URI.
10
+ * @param uri - The MongoDB connection URI
11
+ * @param poolSize - Maximum connection pool size
12
+ * @param closeDelay - Delay in milliseconds before closing idle connections
13
+ * @returns A cached or newly created wrapper instance
14
+ */
6
15
  static get(uri: string, poolSize?: number, closeDelay?: number): MongoClientWrapper;
16
+ /** Connects to MongoDB and returns the underlying MongoClient. */
7
17
  connect(): Promise<MongoClient>;
18
+ /** Disconnects from MongoDB. */
8
19
  disconnect(): Promise<number>;
20
+ /** Initiates a graceful close of the connection. */
9
21
  initiateClose(): Promise<void>;
10
22
  }
11
23
  //# sourceMappingURL=Wrapper.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"Wrapper.d.ts","sourceRoot":"","sources":["../../src/Wrapper.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAE,MAAM,SAAS,CAAA;AAErC,qBAAa,kBAAkB;IAC7B,MAAM,CAAC,QAAQ,CAAC,OAAO,kCAAwC;IAE/D,OAAO,CAAC,MAAM,CAAa;gBAEf,GAAG,EAAE,MAAM,EAAE,WAAW,CAAC,EAAE,MAAM,EAAE,UAAU,CAAC,EAAE,MAAM;IAKlE,MAAM,CAAC,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,MAAM,EAAE,UAAU,CAAC,EAAE,MAAM;IASxD,OAAO;IAIP,UAAU;IAIV,aAAa;CAGpB"}
1
+ {"version":3,"file":"Wrapper.d.ts","sourceRoot":"","sources":["../../src/Wrapper.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAE,MAAM,SAAS,CAAA;AAErC,kFAAkF;AAClF,qBAAa,kBAAkB;IAC7B,iEAAiE;IACjE,MAAM,CAAC,QAAQ,CAAC,OAAO,kCAAwC;IAE/D,OAAO,CAAC,MAAM,CAAa;gBAEf,GAAG,EAAE,MAAM,EAAE,WAAW,CAAC,EAAE,MAAM,EAAE,UAAU,CAAC,EAAE,MAAM;IAKlE;;;;;;OAMG;IACH,MAAM,CAAC,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,MAAM,EAAE,UAAU,CAAC,EAAE,MAAM;IAS9D,kEAAkE;IAC5D,OAAO;IAIb,gCAAgC;IAC1B,UAAU;IAIhB,oDAAoD;IAC9C,aAAa;CAGpB"}
@@ -4,12 +4,20 @@ import { assertEx } from "@xylabs/assert";
4
4
  // src/Wrapper.ts
5
5
  import { MongoClient } from "mongodb";
6
6
  var MongoClientWrapper = class _MongoClientWrapper {
7
+ /** Global cache of wrapper instances keyed by connection URI. */
7
8
  static clients = /* @__PURE__ */ new Map();
8
9
  client;
9
10
  constructor(uri, maxPoolSize, closeDelay) {
10
11
  const options = { maxPoolSize, maxIdleTimeMS: closeDelay };
11
12
  this.client = new MongoClient(uri, options);
12
13
  }
14
+ /**
15
+ * Gets or creates a cached MongoClientWrapper for the given URI.
16
+ * @param uri - The MongoDB connection URI
17
+ * @param poolSize - Maximum connection pool size
18
+ * @param closeDelay - Delay in milliseconds before closing idle connections
19
+ * @returns A cached or newly created wrapper instance
20
+ */
13
21
  static get(uri, poolSize, closeDelay) {
14
22
  let client = this.clients.get(uri);
15
23
  if (!client) {
@@ -18,12 +26,15 @@ var MongoClientWrapper = class _MongoClientWrapper {
18
26
  }
19
27
  return client;
20
28
  }
29
+ /** Connects to MongoDB and returns the underlying MongoClient. */
21
30
  async connect() {
22
31
  return await Promise.resolve(this.client);
23
32
  }
33
+ /** Disconnects from MongoDB. */
24
34
  async disconnect() {
25
35
  return await Promise.resolve(0);
26
36
  }
37
+ /** Initiates a graceful close of the connection. */
27
38
  async initiateClose() {
28
39
  await Promise.resolve();
29
40
  }
@@ -31,63 +42,118 @@ var MongoClientWrapper = class _MongoClientWrapper {
31
42
 
32
43
  // src/Base.ts
33
44
  var BaseMongoSdk = class {
45
+ /** The MongoDB SDK configuration for this instance. */
34
46
  config;
35
47
  constructor(config) {
36
48
  this.config = config;
37
49
  }
50
+ /** Returns the MongoDB connection URI, either from the config or constructed from individual credential fields. */
38
51
  get uri() {
39
52
  return this.config.dbConnectionString ?? `mongodb+srv://${this.config.dbUserName}:${this.config.dbPassword}@${this.config.dbDomain}.mongodb.net/${this.config.dbName}?retryWrites=true&w=majority`;
40
53
  }
54
+ /**
55
+ * Deletes all documents matching the filter.
56
+ * @param filter - The query filter to match documents for deletion
57
+ */
41
58
  async deleteMany(filter) {
42
59
  return await this.useCollection(async (collection) => {
43
60
  return await collection.deleteMany(filter);
44
61
  });
45
62
  }
63
+ /**
64
+ * Deletes the first document matching the filter.
65
+ * @param filter - The query filter to match a document for deletion
66
+ */
46
67
  async deleteOne(filter) {
47
68
  return await this.useCollection(async (collection) => {
48
69
  return await collection.deleteOne(filter);
49
70
  });
50
71
  }
72
+ /**
73
+ * Finds all documents matching the filter and returns a cursor.
74
+ * @param filter - The query filter
75
+ */
51
76
  async find(filter) {
52
77
  return await this.useCollection((collection) => {
53
78
  return collection.find(filter);
54
79
  });
55
80
  }
81
+ /**
82
+ * Finds a single document matching the filter.
83
+ * @param filter - The query filter
84
+ * @returns The matched document, or `null` if not found
85
+ */
56
86
  async findOne(filter) {
57
87
  return await this.useCollection(async (collection) => {
58
88
  return await collection.findOne(filter);
59
89
  });
60
90
  }
91
+ /**
92
+ * Inserts multiple documents into the collection.
93
+ * @param items - The documents to insert
94
+ * @param options - Optional bulk write options
95
+ */
61
96
  async insertMany(items, options) {
62
97
  return await this.useCollection(async (collection) => {
63
98
  return await collection.insertMany(items, options);
64
99
  });
65
100
  }
101
+ /**
102
+ * Inserts a single document into the collection.
103
+ * @param item - The document to insert
104
+ * @param options - Optional insert options
105
+ */
66
106
  async insertOne(item, options) {
67
107
  return await this.useCollection(async (collection) => {
68
108
  return await collection.insertOne(item, options);
69
109
  });
70
110
  }
111
+ /**
112
+ * Replaces a single document matching the filter.
113
+ * @param filter - The query filter to match the document
114
+ * @param item - The replacement document
115
+ * @param options - Optional replace options
116
+ */
71
117
  async replaceOne(filter, item, options) {
72
118
  return await this.useCollection(async (collection) => {
73
119
  return await collection.replaceOne(filter, item, options);
74
120
  });
75
121
  }
122
+ /**
123
+ * Updates a single document matching the filter without upserting.
124
+ * @param filter - The query filter to match the document
125
+ * @param fields - The update operations to apply
126
+ */
76
127
  async updateOne(filter, fields) {
77
128
  return await this.useCollection(async (collection) => {
78
129
  return await collection.updateOne(filter, fields, { upsert: false });
79
130
  });
80
131
  }
132
+ /**
133
+ * Updates a single document matching the filter, inserting it if it does not exist.
134
+ * @param filter - The query filter to match the document
135
+ * @param fields - The update operations to apply
136
+ */
81
137
  async upsertOne(filter, fields) {
82
138
  return await this.useCollection(async (collection) => {
83
139
  return await collection.updateOne(filter, fields, { upsert: true });
84
140
  });
85
141
  }
142
+ /**
143
+ * Executes a callback with access to the configured MongoDB collection.
144
+ * @param func - A callback receiving the typed collection
145
+ * @returns The result of the callback
146
+ */
86
147
  async useCollection(func) {
87
148
  return await this.useMongo(async (client) => {
88
149
  return await func(client.db(this.config.dbName).collection(this.config.collection));
89
150
  });
90
151
  }
152
+ /**
153
+ * Executes a callback with a connected MongoClient, handling connection and disconnection.
154
+ * @param func - A callback receiving the connected MongoClient
155
+ * @returns The result of the callback
156
+ */
91
157
  async useMongo(func) {
92
158
  const wrapper = MongoClientWrapper.get(this.uri, this.config.maxPoolSize, this.config.closeDelay);
93
159
  const connection = await wrapper.connect();
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/Base.ts","../../src/Wrapper.ts"],"sourcesContent":["import { assertEx } from '@xylabs/assert'\nimport type {\n BulkWriteOptions,\n Collection,\n DeleteResult,\n Document,\n Filter,\n FindCursor,\n InsertOneOptions,\n MongoClient,\n OptionalUnlessRequiredId,\n ReplaceOptions,\n UpdateFilter,\n WithId,\n} from 'mongodb'\n\nimport type { BaseMongoSdkConfig } from './Config.js'\nimport { MongoClientWrapper } from './Wrapper.js'\n\nexport class BaseMongoSdk<T extends Document> {\n config: BaseMongoSdkConfig\n\n constructor(config: BaseMongoSdkConfig) {\n this.config = config\n }\n\n get uri() {\n return (\n this.config.dbConnectionString\n // eslint-disable-next-line @stylistic/max-len\n ?? `mongodb+srv://${this.config.dbUserName}:${this.config.dbPassword}@${this.config.dbDomain}.mongodb.net/${this.config.dbName}?retryWrites=true&w=majority`\n )\n }\n\n async deleteMany(filter: Filter<T>) {\n return await this.useCollection<DeleteResult>(async (collection: Collection<T>) => {\n return await collection.deleteMany(filter)\n })\n }\n\n async deleteOne(filter: Filter<T>) {\n return await this.useCollection<DeleteResult>(async (collection: Collection<T>) => {\n return await collection.deleteOne(filter)\n })\n }\n\n async find(filter: Filter<T>) {\n return await this.useCollection<FindCursor<WithId<T>>>((collection: Collection<T>) => {\n return collection.find(filter)\n })\n }\n\n async findOne(filter: Filter<T>) {\n return await this.useCollection<WithId<T> | null>(async (collection: Collection<T>) => {\n return await collection.findOne(filter)\n })\n }\n\n async insertMany(items: OptionalUnlessRequiredId<T>[], options?: BulkWriteOptions) {\n return await this.useCollection(async (collection: Collection<T>) => {\n return await collection.insertMany(items, options)\n })\n }\n\n async insertOne(item: OptionalUnlessRequiredId<T>, options?: InsertOneOptions) {\n return await this.useCollection(async (collection: Collection<T>) => {\n return await collection.insertOne(item, options)\n })\n }\n\n async replaceOne(filter: Filter<T>, item: OptionalUnlessRequiredId<T>, options?: ReplaceOptions) {\n return await this.useCollection(async (collection: Collection<T>) => {\n return await collection.replaceOne(filter, item, options)\n })\n }\n\n async updateOne(filter: Filter<T>, fields: UpdateFilter<T>) {\n return await this.useCollection(async (collection: Collection<T>) => {\n return await collection.updateOne(filter, fields, { upsert: false })\n })\n }\n\n async upsertOne(filter: Filter<T>, fields: UpdateFilter<T>) {\n return await this.useCollection(async (collection: Collection<T>) => {\n return await collection.updateOne(filter, fields, { upsert: true })\n })\n }\n\n async useCollection<R>(func: (collection: Collection<T>) => Promise<R> | R) {\n return await this.useMongo<R>(async (client: MongoClient) => {\n return await func(client.db(this.config.dbName).collection<T>(this.config.collection))\n })\n }\n\n async useMongo<R>(func: (client: MongoClient) => Promise<R> | R) {\n const wrapper = MongoClientWrapper.get(this.uri, this.config.maxPoolSize, this.config.closeDelay)\n const connection = await wrapper.connect()\n assertEx(connection, () => 'Connection failed')\n try {\n return await func(connection)\n } finally {\n await wrapper.disconnect()\n }\n }\n}\n","import type { MongoClientOptions } from 'mongodb'\nimport { MongoClient } from 'mongodb'\n\nexport class MongoClientWrapper {\n static readonly clients = new Map<string, MongoClientWrapper>()\n\n private client: MongoClient\n\n constructor(uri: string, maxPoolSize?: number, closeDelay?: number) {\n const options: MongoClientOptions = { maxPoolSize, maxIdleTimeMS: closeDelay }\n this.client = new MongoClient(uri, options)\n }\n\n static get(uri: string, poolSize?: number, closeDelay?: number) {\n let client = this.clients.get(uri)\n if (!client) {\n client = new MongoClientWrapper(uri, poolSize, closeDelay)\n this.clients.set(uri, client)\n }\n return client\n }\n\n async connect() {\n return await Promise.resolve(this.client)\n }\n\n async disconnect() {\n return await Promise.resolve(0)\n }\n\n async initiateClose() {\n await Promise.resolve()\n }\n}\n"],"mappings":";AAAA,SAAS,gBAAgB;;;ACCzB,SAAS,mBAAmB;AAErB,IAAM,qBAAN,MAAM,oBAAmB;AAAA,EAC9B,OAAgB,UAAU,oBAAI,IAAgC;AAAA,EAEtD;AAAA,EAER,YAAY,KAAa,aAAsB,YAAqB;AAClE,UAAM,UAA8B,EAAE,aAAa,eAAe,WAAW;AAC7E,SAAK,SAAS,IAAI,YAAY,KAAK,OAAO;AAAA,EAC5C;AAAA,EAEA,OAAO,IAAI,KAAa,UAAmB,YAAqB;AAC9D,QAAI,SAAS,KAAK,QAAQ,IAAI,GAAG;AACjC,QAAI,CAAC,QAAQ;AACX,eAAS,IAAI,oBAAmB,KAAK,UAAU,UAAU;AACzD,WAAK,QAAQ,IAAI,KAAK,MAAM;AAAA,IAC9B;AACA,WAAO;AAAA,EACT;AAAA,EAEA,MAAM,UAAU;AACd,WAAO,MAAM,QAAQ,QAAQ,KAAK,MAAM;AAAA,EAC1C;AAAA,EAEA,MAAM,aAAa;AACjB,WAAO,MAAM,QAAQ,QAAQ,CAAC;AAAA,EAChC;AAAA,EAEA,MAAM,gBAAgB;AACpB,UAAM,QAAQ,QAAQ;AAAA,EACxB;AACF;;;ADdO,IAAM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AW,WAAW,QAAQ,MAAM,OAAO;AAAA,IAC1D,CAAC;AAAA,EACH;AAAA,EAEA,MAAM,UAAU,QAAmB,QAAyB;AAC1D,WAAO,MAAM,KAAK,cAAc,OAAO,eAA8B;AACnE,aAAO,MAAM,WAAW,UAAU,QAAQ,QAAQ,EAAE,QAAQ,MAAM,CAAC;AAAA,IACrE,CAAC;AAAA,EACH;AAAA,EAEA,MAAM,UAAU,QAAmB,QAAyB;AAC1D,WAAO,MAAM,KAAK,cAAc,OAAO,eAA8B;AACnE,aAAO,MAAM,WAAW,UAAU,QAAQ,QAAQ,EAAE,QAAQ,KAAK,CAAC;AAAA,IACpE,CAAC;AAAA,EACH;AAAA,EAEA,MAAM,cAAiB,MAAqD;AAC1E,WAAO,MAAM,KAAK,SAAY,OAAO,WAAwB;AAC3D,aAAO,MAAM,KAAK,OAAO,GAAG,KAAK,OAAO,MAAM,EAAE,WAAc,KAAK,OAAO,UAAU,CAAC;AAAA,IACvF,CAAC;AAAA,EACH;AAAA,EAEA,MAAM,SAAY,MAA+C;AAC/D,UAAM,UAAU,mBAAmB,IAAI,KAAK,KAAK,KAAK,OAAO,aAAa,KAAK,OAAO,UAAU;AAChG,UAAM,aAAa,MAAM,QAAQ,QAAQ;AACzC,aAAS,YAAY,MAAM,mBAAmB;AAC9C,QAAI;AACF,aAAO,MAAM,KAAK,UAAU;AAAA,IAC9B,UAAE;AACA,YAAM,QAAQ,WAAW;AAAA,IAC3B;AAAA,EACF;AACF;","names":[]}
1
+ {"version":3,"sources":["../../src/Base.ts","../../src/Wrapper.ts"],"sourcesContent":["import { assertEx } from '@xylabs/assert'\nimport type {\n BulkWriteOptions,\n Collection,\n DeleteResult,\n Document,\n Filter,\n FindCursor,\n InsertOneOptions,\n MongoClient,\n OptionalUnlessRequiredId,\n ReplaceOptions,\n UpdateFilter,\n WithId,\n} from 'mongodb'\n\nimport type { BaseMongoSdkConfig } from './Config.js'\nimport { MongoClientWrapper } from './Wrapper.js'\n\n/** Provides a typed wrapper around common MongoDB collection operations. */\nexport class BaseMongoSdk<T extends Document> {\n /** The MongoDB SDK configuration for this instance. */\n config: BaseMongoSdkConfig\n\n constructor(config: BaseMongoSdkConfig) {\n this.config = config\n }\n\n /** Returns the MongoDB connection URI, either from the config or constructed from individual credential fields. */\n get uri() {\n return (\n this.config.dbConnectionString\n ?? `mongodb+srv://${this.config.dbUserName}:${this.config.dbPassword}@${this.config.dbDomain}.mongodb.net/${this.config.dbName}?retryWrites=true&w=majority`\n )\n }\n\n /**\n * Deletes all documents matching the filter.\n * @param filter - The query filter to match documents for deletion\n */\n async deleteMany(filter: Filter<T>) {\n return await this.useCollection<DeleteResult>(async (collection: Collection<T>) => {\n return await collection.deleteMany(filter)\n })\n }\n\n /**\n * Deletes the first document matching the filter.\n * @param filter - The query filter to match a document for deletion\n */\n async deleteOne(filter: Filter<T>) {\n return await this.useCollection<DeleteResult>(async (collection: Collection<T>) => {\n return await collection.deleteOne(filter)\n })\n }\n\n /**\n * Finds all documents matching the filter and returns a cursor.\n * @param filter - The query filter\n */\n async find(filter: Filter<T>) {\n return await this.useCollection<FindCursor<WithId<T>>>((collection: Collection<T>) => {\n return collection.find(filter)\n })\n }\n\n /**\n * Finds a single document matching the filter.\n * @param filter - The query filter\n * @returns The matched document, or `null` if not found\n */\n async findOne(filter: Filter<T>) {\n return await this.useCollection<WithId<T> | null>(async (collection: Collection<T>) => {\n return await collection.findOne(filter)\n })\n }\n\n /**\n * Inserts multiple documents into the collection.\n * @param items - The documents to insert\n * @param options - Optional bulk write options\n */\n async insertMany(items: OptionalUnlessRequiredId<T>[], options?: BulkWriteOptions) {\n return await this.useCollection(async (collection: Collection<T>) => {\n return await collection.insertMany(items, options)\n })\n }\n\n /**\n * Inserts a single document into the collection.\n * @param item - The document to insert\n * @param options - Optional insert options\n */\n async insertOne(item: OptionalUnlessRequiredId<T>, options?: InsertOneOptions) {\n return await this.useCollection(async (collection: Collection<T>) => {\n return await collection.insertOne(item, options)\n })\n }\n\n /**\n * Replaces a single document matching the filter.\n * @param filter - The query filter to match the document\n * @param item - The replacement document\n * @param options - Optional replace options\n */\n async replaceOne(filter: Filter<T>, item: OptionalUnlessRequiredId<T>, options?: ReplaceOptions) {\n return await this.useCollection(async (collection: Collection<T>) => {\n return await collection.replaceOne(filter, item, options)\n })\n }\n\n /**\n * Updates a single document matching the filter without upserting.\n * @param filter - The query filter to match the document\n * @param fields - The update operations to apply\n */\n async updateOne(filter: Filter<T>, fields: UpdateFilter<T>) {\n return await this.useCollection(async (collection: Collection<T>) => {\n return await collection.updateOne(filter, fields, { upsert: false })\n })\n }\n\n /**\n * Updates a single document matching the filter, inserting it if it does not exist.\n * @param filter - The query filter to match the document\n * @param fields - The update operations to apply\n */\n async upsertOne(filter: Filter<T>, fields: UpdateFilter<T>) {\n return await this.useCollection(async (collection: Collection<T>) => {\n return await collection.updateOne(filter, fields, { upsert: true })\n })\n }\n\n /**\n * Executes a callback with access to the configured MongoDB collection.\n * @param func - A callback receiving the typed collection\n * @returns The result of the callback\n */\n async useCollection<R>(func: (collection: Collection<T>) => Promise<R> | R) {\n return await this.useMongo<R>(async (client: MongoClient) => {\n return await func(client.db(this.config.dbName).collection<T>(this.config.collection))\n })\n }\n\n /**\n * Executes a callback with a connected MongoClient, handling connection and disconnection.\n * @param func - A callback receiving the connected MongoClient\n * @returns The result of the callback\n */\n async useMongo<R>(func: (client: MongoClient) => Promise<R> | R) {\n const wrapper = MongoClientWrapper.get(this.uri, this.config.maxPoolSize, this.config.closeDelay)\n const connection = await wrapper.connect()\n assertEx(connection, () => 'Connection failed')\n try {\n return await func(connection)\n } finally {\n await wrapper.disconnect()\n }\n }\n}\n","import type { MongoClientOptions } from 'mongodb'\nimport { MongoClient } from 'mongodb'\n\n/** Manages a shared pool of MongoClient instances, reusing connections by URI. */\nexport class MongoClientWrapper {\n /** Global cache of wrapper instances keyed by connection URI. */\n static readonly clients = new Map<string, MongoClientWrapper>()\n\n private client: MongoClient\n\n constructor(uri: string, maxPoolSize?: number, closeDelay?: number) {\n const options: MongoClientOptions = { maxPoolSize, maxIdleTimeMS: closeDelay }\n this.client = new MongoClient(uri, options)\n }\n\n /**\n * Gets or creates a cached MongoClientWrapper for the given URI.\n * @param uri - The MongoDB connection URI\n * @param poolSize - Maximum connection pool size\n * @param closeDelay - Delay in milliseconds before closing idle connections\n * @returns A cached or newly created wrapper instance\n */\n static get(uri: string, poolSize?: number, closeDelay?: number) {\n let client = this.clients.get(uri)\n if (!client) {\n client = new MongoClientWrapper(uri, poolSize, closeDelay)\n this.clients.set(uri, client)\n }\n return client\n }\n\n /** Connects to MongoDB and returns the underlying MongoClient. */\n async connect() {\n return await Promise.resolve(this.client)\n }\n\n /** Disconnects from MongoDB. */\n async disconnect() {\n return await Promise.resolve(0)\n }\n\n /** Initiates a graceful close of the connection. */\n async initiateClose() {\n await Promise.resolve()\n }\n}\n"],"mappings":";AAAA,SAAS,gBAAgB;;;ACCzB,SAAS,mBAAmB;AAGrB,IAAM,qBAAN,MAAM,oBAAmB;AAAA;AAAA,EAE9B,OAAgB,UAAU,oBAAI,IAAgC;AAAA,EAEtD;AAAA,EAER,YAAY,KAAa,aAAsB,YAAqB;AAClE,UAAM,UAA8B,EAAE,aAAa,eAAe,WAAW;AAC7E,SAAK,SAAS,IAAI,YAAY,KAAK,OAAO;AAAA,EAC5C;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASA,OAAO,IAAI,KAAa,UAAmB,YAAqB;AAC9D,QAAI,SAAS,KAAK,QAAQ,IAAI,GAAG;AACjC,QAAI,CAAC,QAAQ;AACX,eAAS,IAAI,oBAAmB,KAAK,UAAU,UAAU;AACzD,WAAK,QAAQ,IAAI,KAAK,MAAM;AAAA,IAC9B;AACA,WAAO;AAAA,EACT;AAAA;AAAA,EAGA,MAAM,UAAU;AACd,WAAO,MAAM,QAAQ,QAAQ,KAAK,MAAM;AAAA,EAC1C;AAAA;AAAA,EAGA,MAAM,aAAa;AACjB,WAAO,MA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C,aAAS,YAAY,MAAM,mBAAmB;AAC9C,QAAI;AACF,aAAO,MAAM,KAAK,UAAU;AAAA,IAC9B,UAAE;AACA,YAAM,QAAQ,WAAW;AAAA,IAC3B;AAAA,EACF;AACF;","names":[]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xylabs/mongo",
3
- "version": "5.0.90",
3
+ "version": "5.0.91",
4
4
  "description": "Base functionality used throughout XYO TypeScript/JavaScript libraries that access Mongo DB",
5
5
  "keywords": [
6
6
  "mongo",
@@ -43,15 +43,17 @@
43
43
  "dist",
44
44
  "!**/*.bench.*",
45
45
  "!**/*.spec.*",
46
- "!**/*.test.*"
46
+ "!**/*.test.*",
47
+ "README.md"
47
48
  ],
48
49
  "dependencies": {
49
- "@xylabs/assert": "~5.0.89"
50
+ "@xylabs/assert": "~5.0.90"
50
51
  },
51
52
  "devDependencies": {
52
- "@xylabs/tsconfig": "~7.5.4",
53
+ "@xylabs/tsconfig": "~7.6.2",
53
54
  "mongodb": "^7.1.1",
54
- "typescript": "~5.9.3",
55
+ "typescript": "^5",
56
+ "vite": "^6.0.0 || ^7.0.0 || ^8.0.0",
55
57
  "vitest": "^4.1.2"
56
58
  },
57
59
  "peerDependencies": {