You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The ```Mongo``` module is based on adapters. Included to the module, there is an adapter using mongoose and one using mongoose to manage gridfs.
105
+
The `Mongo` module is based on adapters. Included to the module, there is an adapter using mongoose and one using mongoose to manage gridfs.
106
106
107
-
But you can create your own adapters if you want by following some required steps describe belows.
107
+
But you can create your own adapters if you want by following some required steps described belows.
108
108
109
109
### Step 1
110
110
111
-
Your adapter should be a class which inherits from ```AbstractHapinessMongoAdapter```.
111
+
Your adapter should be a class which inherits from `AbstractHapinessMongoAdapter`.
112
112
113
113
### Step 2
114
114
115
-
You absolutely needs to implement a static function ```getInterfaceName```, which will return a uniq string identifier for your adapter (**NOTE**```mongoose``` and ```mongoose-gridfs``` are already use for included adapters of this module).
115
+
You absolutely need to implement a static function `getInterfaceName`, which will return a uniq string identifier for your adapter (**NOTE**`mongoose` and `mongoose-gridfs` are already used by adapters provided by this module).
116
116
117
117
### Step 3
118
118
119
119
You need to override 4 functions
120
120
121
-
```javascript
121
+
```typescript
122
122
_tryConnect(): Observable<void> { /* ... */ }
123
123
124
124
_afterConnect(): Observable<void> { /* ... */ }
@@ -132,7 +132,7 @@ You need to override 4 functions
**NOTE** DONT FORGET TO SET ```_isReady =true``` once you are done, else your adapter will never be ready.
224
+
**NOTE** DONT FORGET TO SET `_isReady = true` once you are done, else your adapter will never be ready.
225
+
226
+
You should also override:
227
+
228
+
```typescript
229
+
close(): Observalbe<void> { /**/ }
230
+
```
225
231
226
232
[Back to top](#table-of-contents)
227
233
228
234
## Registering adapters
229
235
230
-
When you want created your own adapters, you first need to tell the Mongo extension to register it. The Mongo extension will add your class and map it with the uniq identifier you put inside the static ```ddd```.
231
-
232
-
```javascript
236
+
When you want to create your own adapters, first you need to tell the Mongo extension to register it. The Mongo extension will add your classes and map it with the uniq identifier you put inside the static `ddd`.
To get your adapter and play with it, you need to inject the MongoClientService in your class and call the ```get()`` function to get an instance of the adapter manager.
362
-
363
-
Once you did it, you'll able to get your adapter with it's name only or with its name and options (if you have the same adapter in different db or host ...) calling the function ```getAdapter(...)```.
361
+
To get your adapter and play with it, you need to inject the MongoClientService in your class and call the `get()` function to get an instance of the adapter manager.
364
362
365
-
Example showing how to get mongoose adapter for ```my_database```:
363
+
Once you did it, you'll be able to get your adapter with its name only or with its name and options (if you have the same adapter in different DBs or host ...) calling the function `getAdapter(...)`.
366
364
367
-
```javascript
365
+
Example showing how to get mongoose adapter for `my_database`:
368
366
367
+
```typescript
369
368
@Injectable()
370
369
classMyModelDocument {
371
370
private _myModelConnection:any;
@@ -404,7 +403,7 @@ You can implement and register models in the adapter
404
403
405
404
Example:
406
405
407
-
```javascript
406
+
```typescript
408
407
@MongoModel({
409
408
adapter: 'mongoose',
410
409
collection: 'collection', // The name of the collection, will be pluralize using mongoose if no collectionName is explicitly given
@@ -452,23 +451,23 @@ class MyModule {}
452
451
453
452
## Helpers functions
454
453
455
-
The module gives you an helpers to perform some basic mongo-related operation.
454
+
The module gives you an helpers to perform some basic mongo-related operations.
456
455
457
456
Just import the class from the module
458
457
459
-
```
458
+
```typescript
460
459
import { MongoUtils } from'@hapiness/mongo'
461
460
```
462
461
463
462
There is 4 static functions (for now)
464
463
465
-
```public static prepareUpdateObject(dto: any): any```: You give to this function an object like ```{ "meta": { "key": "value" } }``` and it returns you the object ```{ "meta.key": "value" }```. Very usefull if you perform update operations!
464
+
`public static prepareUpdateObject(dto: any): any`: Give to this function an object like `{ "meta": { "key": "value" } }` and it returns you the object `{ "meta.key": "value" }`. Very usefull to perform update operations!
466
465
467
-
```public static toObjectId(id: string)```: it returns an ObjectID type from the given string (```null``` if the string is not a valid ObjectID)
466
+
`public static toObjectId(id: string)`: Returns an ObjectID type from the given string (`null` if the string is not a valid ObjectID)
468
467
469
-
```public static fieldsStringFromArray(fields: string[]): string```: If you want to select only some fields and you need for that to compute a string from an array of string, use this function
468
+
`public static fieldsStringFromArray(fields: string[]): string`: If you want to select only some fields and you need for that to compute a string from an array of string, use this function
470
469
471
-
```public static filterFindCondition(condition: any): any```: If you have a query object for mongo, this function will parse your condition, convert the field ```id``` into ```_id``` and then convert ```_id``` to an ```ObjectID``` before giving back the query object
470
+
`public static filterFindCondition(condition: any): any`: If you have a query object for mongo, this function will parse your condition, convert the field `id` into `_id` and then convert `_id` to an `ObjectID` before giving back the query object
472
471
473
472
[Back to top](#table-of-contents)
474
473
@@ -487,6 +486,11 @@ To set up your development environment:
487
486
488
487
## Change History
489
488
489
+
* v1.2.0 (2018-04-05)
490
+
* Updated packages' versions.
491
+
* added support of `OnShutdown` of `hapiness Core`
492
+
* added `close` functions to adapters.
493
+
* Fix typos in documentation.
490
494
* v1.1.3 (2018-01-16)
491
495
* Fix get adapter using adapter name
492
496
* v1.1.2 (2018-01-09)
@@ -523,12 +527,14 @@ To set up your development environment:
0 commit comments