@tmlmobilidade/mongo 20251202.1817.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +39 -0
- package/dist/index.js +75 -0
- package/package.json +48 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { type Collection, type Db, type DbOptions, type Document, MongoClient, type MongoClientOptions } from 'mongodb';
|
|
2
|
+
export declare class MongoConnector {
|
|
3
|
+
/**
|
|
4
|
+
* Get the MongoClient instance.
|
|
5
|
+
*/
|
|
6
|
+
get client(): MongoClient;
|
|
7
|
+
private _client;
|
|
8
|
+
constructor(uri: string, options?: MongoClientOptions);
|
|
9
|
+
/**
|
|
10
|
+
* Aggregates data from a collection.
|
|
11
|
+
*/
|
|
12
|
+
aggregate<T extends Document>(collection: Collection<T>, pipeline: Document[]): Promise<Document[]>;
|
|
13
|
+
/**
|
|
14
|
+
* Connect to MongoDB and return the database instance.
|
|
15
|
+
*/
|
|
16
|
+
connect(): Promise<MongoClient>;
|
|
17
|
+
/**
|
|
18
|
+
* Create a new Db instance sharing the current socket connections.
|
|
19
|
+
*
|
|
20
|
+
* @param dbName - The name of the database we want to use. If not provided, use database name from connection string.
|
|
21
|
+
* @param options - Optional settings for Db construction
|
|
22
|
+
*/
|
|
23
|
+
db(dbName?: string, options?: DbOptions): Db;
|
|
24
|
+
/**
|
|
25
|
+
* Close the MongoDB connection.
|
|
26
|
+
*/
|
|
27
|
+
disconnect(): Promise<void>;
|
|
28
|
+
/**
|
|
29
|
+
* Get a specific collection by name.
|
|
30
|
+
* @param db - The database instance.
|
|
31
|
+
* @param collectionName - The name of the collection to retrieve.
|
|
32
|
+
* @returns The collection instance.
|
|
33
|
+
*/
|
|
34
|
+
getCollection<T extends Document>(db: Db, collectionName: string): Promise<Collection<T>>;
|
|
35
|
+
/**
|
|
36
|
+
* Watches a collection for changes.
|
|
37
|
+
*/
|
|
38
|
+
watch<T extends Document>(collection: Collection<T>): Promise<import("mongodb").ChangeStream<T, import("mongodb").ChangeStreamDocument<T>>>;
|
|
39
|
+
}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/* * */
|
|
2
|
+
import { MongoClient } from 'mongodb';
|
|
3
|
+
/* * */
|
|
4
|
+
export class MongoConnector {
|
|
5
|
+
/**
|
|
6
|
+
* Get the MongoClient instance.
|
|
7
|
+
*/
|
|
8
|
+
get client() {
|
|
9
|
+
return this._client;
|
|
10
|
+
}
|
|
11
|
+
_client;
|
|
12
|
+
constructor(uri, options) {
|
|
13
|
+
this._client = new MongoClient(uri, options);
|
|
14
|
+
this._client.on('close', () => {
|
|
15
|
+
console.warn('MongoDB connection closed unexpectedly.');
|
|
16
|
+
});
|
|
17
|
+
this._client.on('reconnect', () => {
|
|
18
|
+
console.log('MongoDB reconnected.');
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Aggregates data from a collection.
|
|
23
|
+
*/
|
|
24
|
+
async aggregate(collection, pipeline) {
|
|
25
|
+
return collection.aggregate(pipeline).toArray();
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Connect to MongoDB and return the database instance.
|
|
29
|
+
*/
|
|
30
|
+
async connect() {
|
|
31
|
+
if (!this._client) {
|
|
32
|
+
throw new Error('MongoClient not initialized');
|
|
33
|
+
}
|
|
34
|
+
try {
|
|
35
|
+
await this._client.connect();
|
|
36
|
+
return this._client;
|
|
37
|
+
}
|
|
38
|
+
catch (error) {
|
|
39
|
+
throw new Error('Error connecting to MongoDB', { cause: error });
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Create a new Db instance sharing the current socket connections.
|
|
44
|
+
*
|
|
45
|
+
* @param dbName - The name of the database we want to use. If not provided, use database name from connection string.
|
|
46
|
+
* @param options - Optional settings for Db construction
|
|
47
|
+
*/
|
|
48
|
+
db(dbName, options) {
|
|
49
|
+
return this._client.db(dbName, options);
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Close the MongoDB connection.
|
|
53
|
+
*/
|
|
54
|
+
async disconnect() {
|
|
55
|
+
if (this._client) {
|
|
56
|
+
await this._client.close();
|
|
57
|
+
console.log('⤷ Disconnected from MongoDB.');
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Get a specific collection by name.
|
|
62
|
+
* @param db - The database instance.
|
|
63
|
+
* @param collectionName - The name of the collection to retrieve.
|
|
64
|
+
* @returns The collection instance.
|
|
65
|
+
*/
|
|
66
|
+
async getCollection(db, collectionName) {
|
|
67
|
+
return db.collection(collectionName);
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Watches a collection for changes.
|
|
71
|
+
*/
|
|
72
|
+
async watch(collection) {
|
|
73
|
+
return collection.watch();
|
|
74
|
+
}
|
|
75
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@tmlmobilidade/mongo",
|
|
3
|
+
"version": "20251202.1817.5",
|
|
4
|
+
"author": {
|
|
5
|
+
"email": "iso@tmlmobilidade.pt",
|
|
6
|
+
"name": "TML-ISO"
|
|
7
|
+
},
|
|
8
|
+
"license": "AGPL-3.0-or-later",
|
|
9
|
+
"homepage": "https://github.com/tmlmobilidade/go#readme",
|
|
10
|
+
"bugs": {
|
|
11
|
+
"url": "https://github.com/tmlmobilidade/go/issues"
|
|
12
|
+
},
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "git+https://github.com/tmlmobilidade/go.git"
|
|
16
|
+
},
|
|
17
|
+
"keywords": [
|
|
18
|
+
"public transit",
|
|
19
|
+
"tml",
|
|
20
|
+
"transportes metropolitanos de lisboa",
|
|
21
|
+
"go"
|
|
22
|
+
],
|
|
23
|
+
"publishConfig": {
|
|
24
|
+
"access": "public"
|
|
25
|
+
},
|
|
26
|
+
"type": "module",
|
|
27
|
+
"files": [
|
|
28
|
+
"dist"
|
|
29
|
+
],
|
|
30
|
+
"main": "./dist/index.js",
|
|
31
|
+
"types": "./dist/index.d.ts",
|
|
32
|
+
"scripts": {
|
|
33
|
+
"build": "tsc && resolve-tspaths",
|
|
34
|
+
"lint": "eslint ./src/ && tsc --noEmit",
|
|
35
|
+
"lint:fix": "eslint ./src/ --fix",
|
|
36
|
+
"watch": "tsc-watch --onSuccess 'resolve-tspaths'"
|
|
37
|
+
},
|
|
38
|
+
"dependencies": {
|
|
39
|
+
"mongodb": "7.0.0"
|
|
40
|
+
},
|
|
41
|
+
"devDependencies": {
|
|
42
|
+
"@tmlmobilidade/tsconfig": "*",
|
|
43
|
+
"@types/node": "24.10.1",
|
|
44
|
+
"resolve-tspaths": "0.8.23",
|
|
45
|
+
"tsc-watch": "7.2.0",
|
|
46
|
+
"typescript": "5.9.3"
|
|
47
|
+
}
|
|
48
|
+
}
|