@biorate/mongodb 2.0.2 → 2.1.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/README.md +111 -59
- package/dist/types/src/index.d.ts +1 -1
- package/package.json +7 -7
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)
|
|
@@ -7,7 +7,7 @@ export * from 'mongodb';
|
|
|
7
7
|
export declare class MongoDBConnector extends Connector<IMongoDBConfig, IMongoDBConnection> {
|
|
8
8
|
private '#connections';
|
|
9
9
|
private '#current';
|
|
10
|
-
protected readonly namespace
|
|
10
|
+
protected readonly namespace: string;
|
|
11
11
|
protected connect(config: IMongoDBConfig): Promise<import("mongoose").Connection>;
|
|
12
12
|
protected initialize(): Promise<void>;
|
|
13
13
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@biorate/mongodb",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.1.1",
|
|
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",
|
|
@@ -48,13 +48,13 @@
|
|
|
48
48
|
],
|
|
49
49
|
"author": "llevkin",
|
|
50
50
|
"license": "MIT",
|
|
51
|
-
"gitHead": "
|
|
51
|
+
"gitHead": "ff1dcf14a733ce8004ca582d2c421a2154ce97cb",
|
|
52
52
|
"dependencies": {
|
|
53
|
-
"@biorate/config": "3.1
|
|
54
|
-
"@biorate/connector": "3.
|
|
55
|
-
"@biorate/errors": "3.
|
|
56
|
-
"@biorate/inversion": "3.
|
|
57
|
-
"@biorate/tools": "3.
|
|
53
|
+
"@biorate/config": "3.2.1",
|
|
54
|
+
"@biorate/connector": "3.1.1",
|
|
55
|
+
"@biorate/errors": "3.1.1",
|
|
56
|
+
"@biorate/inversion": "3.1.1",
|
|
57
|
+
"@biorate/tools": "3.1.1",
|
|
58
58
|
"@typegoose/typegoose": "^11.0.2",
|
|
59
59
|
"mongodb": "^5.9.1",
|
|
60
60
|
"mongoose": "^7.0.4"
|