@jsnw/nestjs-mongodb 0.0.1
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/lib/index.js +7 -0
- package/dist/lib/mongodb-core.module.js +92 -0
- package/dist/lib/mongodb.consts.js +0 -0
- package/dist/lib/mongodb.module.js +30 -0
- package/dist/lib/mongodb.types.js +2 -0
- package/dist/lib/mongodb.utils.js +15 -0
- package/dist/types/index.d.ts +3 -0
- package/dist/types/mongodb-core.module.d.ts +29 -0
- package/dist/types/mongodb.consts.d.ts +0 -0
- package/dist/types/mongodb.module.d.ts +9 -0
- package/dist/types/mongodb.types.d.ts +7 -0
- package/dist/types/mongodb.utils.d.ts +6 -0
- package/package.json +59 -0
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.coerceObjectId = exports.MongodbModule = void 0;
|
|
4
|
+
var mongodb_module_1 = require("./mongodb.module");
|
|
5
|
+
Object.defineProperty(exports, "MongodbModule", { enumerable: true, get: function () { return mongodb_module_1.MongodbModule; } });
|
|
6
|
+
var mongodb_utils_1 = require("./mongodb.utils");
|
|
7
|
+
Object.defineProperty(exports, "coerceObjectId", { enumerable: true, get: function () { return mongodb_utils_1.coerceObjectId; } });
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
var MongodbCoreModule_1;
|
|
12
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
|
+
exports.MongodbCoreModule = void 0;
|
|
14
|
+
const common_1 = require("@nestjs/common");
|
|
15
|
+
const mongodb_1 = require("mongodb");
|
|
16
|
+
let MongodbCoreModule = MongodbCoreModule_1 = class MongodbCoreModule {
|
|
17
|
+
client;
|
|
18
|
+
/**
|
|
19
|
+
* @return {DynamicModule}
|
|
20
|
+
*/
|
|
21
|
+
static forRoot(options) {
|
|
22
|
+
const mongoClientProvider = MongodbCoreModule_1.createMongoClientProvider(options), dbProvider = MongodbCoreModule_1.createDbProvider();
|
|
23
|
+
return {
|
|
24
|
+
module: MongodbCoreModule_1,
|
|
25
|
+
imports: [],
|
|
26
|
+
providers: [
|
|
27
|
+
mongoClientProvider,
|
|
28
|
+
dbProvider
|
|
29
|
+
],
|
|
30
|
+
exports: [
|
|
31
|
+
mongoClientProvider,
|
|
32
|
+
dbProvider
|
|
33
|
+
]
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* @param {MongodbOptions} options
|
|
38
|
+
* @return {FactoryProvider}
|
|
39
|
+
* @private
|
|
40
|
+
*/
|
|
41
|
+
static createMongoClientProvider(options) {
|
|
42
|
+
let client = null;
|
|
43
|
+
return {
|
|
44
|
+
provide: mongodb_1.MongoClient,
|
|
45
|
+
useFactory: async () => {
|
|
46
|
+
if (client)
|
|
47
|
+
return client;
|
|
48
|
+
client = new mongodb_1.MongoClient(`mongodb://${options.host}:${options.port}/${options.db}`, options.clientOptions);
|
|
49
|
+
await client.connect();
|
|
50
|
+
return client;
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* @return {FactoryProvider}
|
|
56
|
+
* @private
|
|
57
|
+
*/
|
|
58
|
+
static createDbProvider() {
|
|
59
|
+
let db = null;
|
|
60
|
+
return {
|
|
61
|
+
provide: mongodb_1.Db,
|
|
62
|
+
useFactory: (client) => {
|
|
63
|
+
if (!db)
|
|
64
|
+
db = client.db();
|
|
65
|
+
return db;
|
|
66
|
+
},
|
|
67
|
+
inject: [mongodb_1.MongoClient]
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* @param {MongoClient} client
|
|
72
|
+
*/
|
|
73
|
+
constructor(client) {
|
|
74
|
+
this.client = client;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* @return {Promise<void>}
|
|
78
|
+
*/
|
|
79
|
+
async onApplicationShutdown() {
|
|
80
|
+
if (this.client)
|
|
81
|
+
await this.client.close();
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
exports.MongodbCoreModule = MongodbCoreModule;
|
|
85
|
+
exports.MongodbCoreModule = MongodbCoreModule = MongodbCoreModule_1 = __decorate([
|
|
86
|
+
(0, common_1.Global)(),
|
|
87
|
+
(0, common_1.Module)({
|
|
88
|
+
imports: [],
|
|
89
|
+
providers: []
|
|
90
|
+
}),
|
|
91
|
+
__metadata("design:paramtypes", [mongodb_1.MongoClient])
|
|
92
|
+
], MongodbCoreModule);
|
|
File without changes
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var MongodbModule_1;
|
|
9
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
+
exports.MongodbModule = void 0;
|
|
11
|
+
const common_1 = require("@nestjs/common");
|
|
12
|
+
const mongodb_core_module_1 = require("./mongodb-core.module");
|
|
13
|
+
let MongodbModule = MongodbModule_1 = class MongodbModule {
|
|
14
|
+
/**
|
|
15
|
+
* @param {MongodbOptions} options
|
|
16
|
+
* @return {DynamicModule}
|
|
17
|
+
*/
|
|
18
|
+
static forRoot(options) {
|
|
19
|
+
return {
|
|
20
|
+
module: MongodbModule_1,
|
|
21
|
+
imports: [
|
|
22
|
+
mongodb_core_module_1.MongodbCoreModule.forRoot(options)
|
|
23
|
+
]
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
exports.MongodbModule = MongodbModule;
|
|
28
|
+
exports.MongodbModule = MongodbModule = MongodbModule_1 = __decorate([
|
|
29
|
+
(0, common_1.Module)({})
|
|
30
|
+
], MongodbModule);
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.coerceObjectId = coerceObjectId;
|
|
4
|
+
const mongodb_1 = require("mongodb");
|
|
5
|
+
/**
|
|
6
|
+
* @param {string | ObjectId} id
|
|
7
|
+
* @return {ObjectId}
|
|
8
|
+
*/
|
|
9
|
+
function coerceObjectId(id) {
|
|
10
|
+
if (id instanceof mongodb_1.ObjectId)
|
|
11
|
+
return id;
|
|
12
|
+
if (mongodb_1.ObjectId.isValid(id))
|
|
13
|
+
return new mongodb_1.ObjectId(id);
|
|
14
|
+
throw new TypeError(`Invalid argument "id". Expected string, ObjectId, Uint8Array, got: ${typeof id}`);
|
|
15
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { type DynamicModule, type OnApplicationShutdown } from '@nestjs/common';
|
|
2
|
+
import type { MongodbOptions } from './mongodb.types';
|
|
3
|
+
import { MongoClient } from 'mongodb';
|
|
4
|
+
export declare class MongodbCoreModule implements OnApplicationShutdown {
|
|
5
|
+
private readonly client;
|
|
6
|
+
/**
|
|
7
|
+
* @return {DynamicModule}
|
|
8
|
+
*/
|
|
9
|
+
static forRoot(options: MongodbOptions): DynamicModule;
|
|
10
|
+
/**
|
|
11
|
+
* @param {MongodbOptions} options
|
|
12
|
+
* @return {FactoryProvider}
|
|
13
|
+
* @private
|
|
14
|
+
*/
|
|
15
|
+
private static createMongoClientProvider;
|
|
16
|
+
/**
|
|
17
|
+
* @return {FactoryProvider}
|
|
18
|
+
* @private
|
|
19
|
+
*/
|
|
20
|
+
private static createDbProvider;
|
|
21
|
+
/**
|
|
22
|
+
* @param {MongoClient} client
|
|
23
|
+
*/
|
|
24
|
+
constructor(client: MongoClient);
|
|
25
|
+
/**
|
|
26
|
+
* @return {Promise<void>}
|
|
27
|
+
*/
|
|
28
|
+
onApplicationShutdown(): Promise<void>;
|
|
29
|
+
}
|
|
File without changes
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { type DynamicModule } from '@nestjs/common';
|
|
2
|
+
import type { MongodbOptions } from './mongodb.types';
|
|
3
|
+
export declare class MongodbModule {
|
|
4
|
+
/**
|
|
5
|
+
* @param {MongodbOptions} options
|
|
6
|
+
* @return {DynamicModule}
|
|
7
|
+
*/
|
|
8
|
+
static forRoot(options: MongodbOptions): DynamicModule;
|
|
9
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@jsnw/nestjs-mongodb",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "NestJS module for Mongodb integration",
|
|
5
|
+
"main": "./dist/lib/index.js",
|
|
6
|
+
"types": "./dist/types/index.d.ts",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"require": "./dist/lib/index.js",
|
|
10
|
+
"types": "./dist/types/index.d.ts"
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
"scripts": {
|
|
14
|
+
"clean": "rimraf ./dist",
|
|
15
|
+
"build": "npm run clean && tsc -p tsconfig.json",
|
|
16
|
+
"test": "jest --passWithNoTests",
|
|
17
|
+
"test:watch": "jest --watch",
|
|
18
|
+
"test:coverage": "jest --coverage",
|
|
19
|
+
"prepublishOnly": "npm run build"
|
|
20
|
+
},
|
|
21
|
+
"files": [
|
|
22
|
+
"./dist",
|
|
23
|
+
"README.md"
|
|
24
|
+
],
|
|
25
|
+
"repository": {
|
|
26
|
+
"type": "git",
|
|
27
|
+
"url": "git+ssh://git@github.com/pvbaliuk/jsnw-nestjs-mongodb"
|
|
28
|
+
},
|
|
29
|
+
"bugs": {
|
|
30
|
+
"url": "https://github.com/pvbaliuk/jsnw-nestjs-mongodb/issues"
|
|
31
|
+
},
|
|
32
|
+
"homepage": "https://github.com/pvbaliuk/jsnw-nestjs-mongodb#readme",
|
|
33
|
+
"keywords": [
|
|
34
|
+
"nestjs",
|
|
35
|
+
"mongodb"
|
|
36
|
+
],
|
|
37
|
+
"author": {
|
|
38
|
+
"name": "Pavlo Baliuk",
|
|
39
|
+
"email": "jsnow0177@gmail.com"
|
|
40
|
+
},
|
|
41
|
+
"license": "MIT",
|
|
42
|
+
"dependencies": {
|
|
43
|
+
"zod": "^4.3.6"
|
|
44
|
+
},
|
|
45
|
+
"peerDependencies": {
|
|
46
|
+
"@nestjs/common": "^11.1.14",
|
|
47
|
+
"@nestjs/core": "^11.1.14",
|
|
48
|
+
"mongodb": "^7.1.0"
|
|
49
|
+
},
|
|
50
|
+
"devDependencies": {
|
|
51
|
+
"@nestjs/common": "^11.1.14",
|
|
52
|
+
"@nestjs/core": "^11.1.14",
|
|
53
|
+
"jest": "^30.2.0",
|
|
54
|
+
"mongodb": "^7.1.0",
|
|
55
|
+
"rimraf": "^6.1.3",
|
|
56
|
+
"ts-jest": "^29.4.6",
|
|
57
|
+
"typescript": "^5.9.3"
|
|
58
|
+
}
|
|
59
|
+
}
|