@biorate/mongodb 2.1.0 → 2.1.2
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/README.md +111 -59
- package/package.json +24 -23
package/README.md
CHANGED
|
@@ -1,88 +1,142 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @biorate/mongodb
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
MongoDB ORM connector — connection manager for Mongoose with `@typegoose/typegoose` model injection.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **Auto-connect** — creates Mongoose `Connection` on `@init()` via config namespace `MongoDB`.
|
|
8
|
+
- **`@model()` decorator** — inject a typed TypeGoose model bound to a connection.
|
|
9
|
+
- **`@typegoose/typegoose` re-exports** — `@Prop()`, `@modelOptions()`, `Severity`, `ReturnModelType`, etc.
|
|
10
|
+
- **Connection verification** — waits for `open` event.
|
|
11
|
+
- **Multi-connection** — multiple connections, `@model()` optional name parameter.
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
pnpm add @biorate/mongodb
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
Requires `@biorate/connector`, `@biorate/inversion`, `@biorate/config`, `@biorate/tools`, `mongoose`, `@typegoose/typegoose`.
|
|
20
|
+
|
|
21
|
+
## Quick start
|
|
6
22
|
|
|
7
23
|
```ts
|
|
8
24
|
import { inject, container, Types, Core } from '@biorate/inversion';
|
|
9
25
|
import { IConfig, Config } from '@biorate/config';
|
|
10
26
|
import {
|
|
11
|
-
Severity,
|
|
12
|
-
|
|
13
|
-
Prop,
|
|
14
|
-
MongoDBConnector,
|
|
15
|
-
IMongoDBConnector,
|
|
16
|
-
model,
|
|
17
|
-
ReturnModelType,
|
|
27
|
+
Severity, modelOptions, Prop,
|
|
28
|
+
MongoDBConnector, model, ReturnModelType,
|
|
18
29
|
} from '@biorate/mongodb';
|
|
19
30
|
|
|
20
|
-
// Define models
|
|
21
31
|
@modelOptions({
|
|
22
|
-
options: {
|
|
23
|
-
allowMixed: Severity.ALLOW,
|
|
24
|
-
},
|
|
32
|
+
options: { allowMixed: Severity.ALLOW },
|
|
25
33
|
schemaOptions: { collection: 'test', versionKey: false },
|
|
26
34
|
})
|
|
27
|
-
|
|
28
|
-
@Prop()
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
@Prop()
|
|
32
|
-
lastName: string;
|
|
33
|
-
|
|
34
|
-
@Prop()
|
|
35
|
-
age: number;
|
|
35
|
+
class TestModel {
|
|
36
|
+
@Prop() firstName: string;
|
|
37
|
+
@Prop() lastName: string;
|
|
38
|
+
@Prop() age: number;
|
|
36
39
|
}
|
|
37
40
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
@inject(MongoDBConnector) public connector: IMongoDBConnector;
|
|
41
|
+
class Root extends Core() {
|
|
42
|
+
@inject(MongoDBConnector) public connector: MongoDBConnector;
|
|
41
43
|
@model(TestModel) public test: ReturnModelType<typeof TestModel>;
|
|
42
44
|
}
|
|
43
45
|
|
|
44
|
-
// Bind dependencies
|
|
45
46
|
container.bind<IConfig>(Types.Config).to(Config).inSingletonScope();
|
|
46
|
-
container.bind<
|
|
47
|
+
container.bind<MongoDBConnector>(MongoDBConnector).toSelf().inSingletonScope();
|
|
47
48
|
container.bind<Root>(Root).toSelf().inSingletonScope();
|
|
48
49
|
|
|
49
|
-
// Configure
|
|
50
50
|
container.get<IConfig>(Types.Config).merge({
|
|
51
|
-
MongoDB: [
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
useNewUrlParser: true,
|
|
57
|
-
useUnifiedTopology: true,
|
|
58
|
-
dbName: 'test',
|
|
59
|
-
},
|
|
60
|
-
},
|
|
61
|
-
],
|
|
51
|
+
MongoDB: [{
|
|
52
|
+
name: 'connection',
|
|
53
|
+
host: 'mongodb://localhost:27017/',
|
|
54
|
+
options: { dbName: 'test' },
|
|
55
|
+
}],
|
|
62
56
|
});
|
|
63
57
|
|
|
64
58
|
(async () => {
|
|
65
59
|
const root = container.get<Root>(Root);
|
|
66
60
|
await root.$run();
|
|
67
|
-
await root.
|
|
61
|
+
await new root.test({ firstName: 'Vasya', lastName: 'Pupkin', age: 36 }).save();
|
|
62
|
+
const data = await root.test.find({ firstName: 'Vasya' }, { _id: 0 });
|
|
63
|
+
console.log(data); // [{ firstName: 'Vasya', lastName: 'Pupkin', age: 36 }]
|
|
64
|
+
})();
|
|
65
|
+
```
|
|
68
66
|
|
|
69
|
-
|
|
70
|
-
console.log(connection);
|
|
67
|
+
## API Reference
|
|
71
68
|
|
|
72
|
-
|
|
73
|
-
firstName: 'Vasya',
|
|
74
|
-
lastName: 'Pupkin',
|
|
75
|
-
age: 36,
|
|
76
|
-
}).save(); // insert data into test collection
|
|
69
|
+
### `MongoDBConnector`
|
|
77
70
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
71
|
+
| Member | Type | Description |
|
|
72
|
+
|------------------|---------------------------------------------|------------------------------------------|
|
|
73
|
+
| `namespace` | `'MongoDB'` | Config key for connection definitions. |
|
|
74
|
+
| `connect(config)` | `(config) => Promise<IMongoDBConnection>` | Creates Mongoose connection via `createConnection`. |
|
|
75
|
+
|
|
76
|
+
### `@model()` decorator
|
|
77
|
+
|
|
78
|
+
```ts
|
|
79
|
+
@model(ModelClass, connectionName?, options?)
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
Binds a TypeGoose model class to a specific named connection (or the first connection if omitted).
|
|
83
|
+
|
|
84
|
+
### Config
|
|
85
|
+
|
|
86
|
+
```ts
|
|
87
|
+
interface IMongoDBConfig extends IConnectorConfig {
|
|
88
|
+
host: string; // mongodb://host:port/
|
|
89
|
+
options: ConnectOptions; // dbName, auth, ssl, etc.
|
|
90
|
+
}
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
### Errors
|
|
94
|
+
|
|
95
|
+
| Error | Condition |
|
|
96
|
+
|------------------------------------|------------------------------------------------|
|
|
97
|
+
| `MongoDBCantConnectError` | `createConnection()` or `open` event fails. |
|
|
98
|
+
| `MongoDBConnectionNotExistsError` | `@model()` references a non-existent connection. |
|
|
99
|
+
|
|
100
|
+
## Usage patterns
|
|
101
|
+
|
|
102
|
+
### Multi-connection with named models
|
|
103
|
+
|
|
104
|
+
```ts
|
|
105
|
+
config: {
|
|
106
|
+
MongoDB: [
|
|
107
|
+
{ name: 'primary', host: 'mongodb://primary:27017/', options: { dbName: 'app' } },
|
|
108
|
+
{ name: 'archive', host: 'mongodb://archive:27017/', options: { dbName: 'archive' } },
|
|
109
|
+
],
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
class Root extends Core() {
|
|
113
|
+
@model(UserModel, 'primary') public users: ReturnModelType<typeof UserModel>;
|
|
114
|
+
@model(LogModel, 'archive') public logs: ReturnModelType<typeof LogModel>;
|
|
115
|
+
}
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
### Raw connection access
|
|
119
|
+
|
|
120
|
+
```ts
|
|
121
|
+
const conn = root.connector.connection('primary');
|
|
122
|
+
// conn is a mongoose.Connection — use conn.collection(), conn.model(), etc.
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
## Architecture
|
|
126
|
+
|
|
127
|
+
```
|
|
128
|
+
MongoDBConnector extends Connector<IMongoDBConfig, IMongoDBConnection>
|
|
129
|
+
│
|
|
130
|
+
├── namespace = 'MongoDB'
|
|
131
|
+
├── connect(config) → createConnection(host, options)
|
|
132
|
+
│ └── await events.once(connection, 'open')
|
|
133
|
+
│
|
|
134
|
+
├── @init() initialize()
|
|
135
|
+
│ ├── store connections reference globally (for @model decorator)
|
|
136
|
+
│ └── super.initialize() — creates all connections from config
|
|
137
|
+
│
|
|
138
|
+
└── model(Model, connectionName?, options?) → property decorator
|
|
139
|
+
└── getter → getModelForClass(Model, { existingConnection })
|
|
86
140
|
```
|
|
87
141
|
|
|
88
142
|
### Learn
|
|
@@ -93,8 +147,6 @@ container.get<IConfig>(Types.Config).merge({
|
|
|
93
147
|
|
|
94
148
|
See the [CHANGELOG](https://github.com/biorate/core/blob/master/packages/%40biorate/mongodb/CHANGELOG.md)
|
|
95
149
|
|
|
96
|
-
|
|
150
|
+
## License
|
|
97
151
|
|
|
98
152
|
[MIT](https://github.com/biorate/core/blob/master/packages/%40biorate/mongodb/LICENSE)
|
|
99
|
-
|
|
100
|
-
Copyright (c) 2021-present [Leonid Levkin (llevkin)](mailto:llevkin@yandex.ru)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@biorate/mongodb",
|
|
3
|
-
"version": "2.1.
|
|
3
|
+
"version": "2.1.2",
|
|
4
4
|
"description": "Mongodb ORM connector based on mongoose and typegoose",
|
|
5
5
|
"main": "./dist/cjs/index.js",
|
|
6
6
|
"module": "./dist/esm/index.js",
|
|
@@ -18,6 +18,21 @@
|
|
|
18
18
|
"README.md",
|
|
19
19
|
"LICENSE"
|
|
20
20
|
],
|
|
21
|
+
"scripts": {
|
|
22
|
+
"clean": "cleanup dist",
|
|
23
|
+
"build:cjs": "npx tsc -p ./tsconfig.build.cjs.json",
|
|
24
|
+
"build:esm": "npx tsc -p ./tsconfig.build.esm.json",
|
|
25
|
+
"build:types": "npx tsc -p ./tsconfig.build.types.json",
|
|
26
|
+
"build": "pnpm run clean && pnpm run build:cjs && pnpm run build:esm && pnpm run build:types",
|
|
27
|
+
"postbuild:cjs": "node ../../../.scripts/write-package-type.js dist/cjs commonjs",
|
|
28
|
+
"postbuild:esm": "tsc-esm-fix --tsconfig=./tsconfig.build.esm.json --target=./dist/esm && node ../../../.scripts/write-package-type.js dist/esm module",
|
|
29
|
+
"format": "prettier --check src",
|
|
30
|
+
"lint": "eslint src --ext .ts",
|
|
31
|
+
"format:fix": "prettier --write src",
|
|
32
|
+
"lint:fix": "eslint src --ext .ts --fix",
|
|
33
|
+
"test": "npx nyc --nycrc-path ../../../.nycrc.json -- npx mocha tests/**/*.spec.ts",
|
|
34
|
+
"prepublishOnly": "pnpm run build"
|
|
35
|
+
},
|
|
21
36
|
"repository": {
|
|
22
37
|
"type": "git",
|
|
23
38
|
"url": "git+https://github.com/biorate/core.git",
|
|
@@ -33,29 +48,15 @@
|
|
|
33
48
|
],
|
|
34
49
|
"author": "llevkin",
|
|
35
50
|
"license": "MIT",
|
|
36
|
-
"gitHead": "
|
|
51
|
+
"gitHead": "e17a03900999e261a0eb04c6afb09b9535cad952",
|
|
37
52
|
"dependencies": {
|
|
53
|
+
"@biorate/config": "3.2.2",
|
|
54
|
+
"@biorate/connector": "3.1.2",
|
|
55
|
+
"@biorate/errors": "3.1.2",
|
|
56
|
+
"@biorate/inversion": "3.1.2",
|
|
57
|
+
"@biorate/tools": "3.1.2",
|
|
38
58
|
"@typegoose/typegoose": "^11.0.2",
|
|
39
59
|
"mongodb": "^5.9.1",
|
|
40
|
-
"mongoose": "^7.0.4"
|
|
41
|
-
"@biorate/config": "3.2.0",
|
|
42
|
-
"@biorate/inversion": "3.1.0",
|
|
43
|
-
"@biorate/errors": "3.1.0",
|
|
44
|
-
"@biorate/connector": "3.1.0",
|
|
45
|
-
"@biorate/tools": "3.1.0"
|
|
46
|
-
},
|
|
47
|
-
"scripts": {
|
|
48
|
-
"clean": "cleanup dist",
|
|
49
|
-
"build:cjs": "npx tsc -p ./tsconfig.build.cjs.json",
|
|
50
|
-
"build:esm": "npx tsc -p ./tsconfig.build.esm.json",
|
|
51
|
-
"build:types": "npx tsc -p ./tsconfig.build.types.json",
|
|
52
|
-
"build": "pnpm run clean && pnpm run build:cjs && pnpm run build:esm && pnpm run build:types",
|
|
53
|
-
"postbuild:cjs": "node ../../../.scripts/write-package-type.js dist/cjs commonjs",
|
|
54
|
-
"postbuild:esm": "tsc-esm-fix --tsconfig=./tsconfig.build.esm.json --target=./dist/esm && node ../../../.scripts/write-package-type.js dist/esm module",
|
|
55
|
-
"format": "prettier --check src",
|
|
56
|
-
"lint": "eslint src --ext .ts",
|
|
57
|
-
"format:fix": "prettier --write src",
|
|
58
|
-
"lint:fix": "eslint src --ext .ts --fix",
|
|
59
|
-
"test": "npx nyc --nycrc-path ../../../.nycrc.json -- npx mocha tests/**/*.spec.ts"
|
|
60
|
+
"mongoose": "^7.0.4"
|
|
60
61
|
}
|
|
61
|
-
}
|
|
62
|
+
}
|