Skip to content

Commit 45ab6fc

Browse files
authored
Merge pull request #40 from hapinessjs/next
release(version): v1.2.0
2 parents a8a759e + 7b14b6e commit 45ab6fc

14 files changed

Lines changed: 1150 additions & 896 deletions

.nvmrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
9.3.0
1+
9.6.1

README.md

Lines changed: 52 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@
3333

3434
# Mongo Module
3535

36-
```Mongo``` module for the Hapiness framework including a ```mongoose``` adapter and a ```mongoose-gridfs``` one.
36+
`Mongo` module for the Hapiness framework including a `mongoose` adapter and a `mongoose-gridfs` one.
3737

3838
## Table of contents
3939

4040
* [Using your module inside Hapiness application](#using-your-module-inside-hapiness-application)
4141
* [`yarn` or `npm` it in your `package.json`](#yarn-or-npm-it-in-your-package)
4242
* [Importing `MongoModule` from the library](#importing-mongomodule-from-the-library)
43-
* [Creating ```Adapters```](#creating-adapters)
43+
* [Creating `Adapters`](#creating-adapters)
4444
* [Step 1](step-1)
4545
* [Step 2](step-2)
4646
* [Step 3](step-3)
@@ -67,7 +67,7 @@ or
6767
$ yarn add @hapiness/core @hapiness/mongo rxjs
6868
```
6969

70-
```javascript
70+
```json5
7171
"dependencies": {
7272
"@hapiness/core": "^1.3.0",
7373
"@hapiness/mongo": "^1.1.3",
@@ -79,9 +79,9 @@ $ yarn add @hapiness/core @hapiness/mongo rxjs
7979

8080
### Importing `MongoModule` from the library
8181

82-
This module provide an Hapiness extension for Mongo. To use it, simply register it during the ```bootstrap``` step of your project like that:
83-
```javascript
82+
This module provide an Hapiness extension for Mongo. To use it, simply register it during the `bootstrap` step of your project like that:
8483

84+
```typescript
8585
import { MongoModule, MongoClientExt } from '@hapiness/mongo';
8686

8787
@HapinessModule({
@@ -100,25 +100,25 @@ Hapiness.bootstrap(MyModule, [ MongoClientExt.setConfig(/* ... */) ]);
100100

101101
[Back to top](#table-of-contents)
102102

103-
## Creating ```Adapters```
103+
## Creating `Adapters`
104104

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.
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.
106106

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.
108108

109109
### Step 1
110110

111-
Your adapter should be a class which inherits from ```AbstractHapinessMongoAdapter```.
111+
Your adapter should be a class which inherits from `AbstractHapinessMongoAdapter`.
112112

113113
### Step 2
114114

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).
116116

117117
### Step 3
118118

119119
You need to override 4 functions
120120

121-
```javascript
121+
```typescript
122122
_tryConnect(): Observable<void> { /* ... */ }
123123

124124
_afterConnect(): Observable<void> { /* ... */ }
@@ -132,7 +132,7 @@ You need to override 4 functions
132132

133133
Example for mongoose
134134

135-
```javascript
135+
```typescript
136136
protected _tryConnect(): Observable<void> {
137137
return Observable
138138
.create(observer => {
@@ -163,11 +163,11 @@ protected _tryConnect(): Observable<void> {
163163
}
164164
```
165165

166-
*_afterConnect:* this function will be called just after ```_tryConnect``` if you want to manage some stuff once your connection is fine.
166+
*_afterConnect:* this function will be called just after `_tryConnect` if you want to manage some stuff once your connection is fine.
167167

168168
Example for mongoose:
169169

170-
```javascript
170+
```typescript
171171
protected _afterConnect(): Observable<void> {
172172
return Observable
173173
.create(observer => {
@@ -205,32 +205,37 @@ protected _afterConnect(): Observable<void> {
205205

206206
Example for mongoose:
207207

208-
```javascript
208+
```typescript
209209
public getLibrary(): any {
210210
return mongoose;
211211
}
212-
````
212+
```
213213

214214
*registerValue:* this will register your document and return a model value to get through the DI.
215215

216216
Example for mongoose:
217217

218-
```javascript
218+
```typescript
219219
public registerValue(document, collection): any {
220220
return this._connection.model(collection, document);
221221
}
222-
````
222+
```
223223

224-
**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+
```
225231

226232
[Back to top](#table-of-contents)
227233

228234
## Registering adapters
229235

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`.
233237

238+
```typescript
234239
class MyCustomAdapter extends AbstractHapinessMongoAdapter {
235240
public static getInterfaceName(): string {
236241
return 'custom-identifier-for-my-adapter';
@@ -252,21 +257,19 @@ Hapiness.bootstrap(
252257
)
253258
]
254259
);
255-
256260
```
257261

258-
Now, the mongo extension know that an Adapter with identifier ```custom-identifier-for-my-adapte``` exists.
262+
Now, the mongo extension knows that an Adapter with the identifier `custom-identifier-for-my-adapter` exists.
259263

260-
The two provided adapters dont needs to be registered as it is already done.
264+
The two provided adapters don't need to be registered as it is already done.
261265

262266
## Using a registered adapter
263267

264268
It will work the same for both custom adapters you made and provided adapters.
265269

266270
Just load them with the config you want to use:
267271

268-
```javascript
269-
272+
```typescript
270273
class MyCustomAdapter extends AbstractHapinessMongoAdapter {
271274
public static getInterfaceName(): string {
272275
return 'custom-identifier-for-my-adapter';
@@ -304,21 +307,19 @@ Hapiness.bootstrap(
304307
)
305308
]
306309
);
307-
308310
```
309311

310-
So you can load as much connection as you want and provide custom config for each adapter you load.
312+
So you can load as many connections as you want and provide custom configs for each adapter you load.
311313

312314
[Back to top](#table-of-contents)
313315

314316
## Configuration
315317

316-
When you load adapter (see previous section), you can provide config, but you have the possibility to not provide one every time.
317-
318-
Lets say you want two adapters pointing to the same database, you can for that use the ```common``` option.
318+
When you load adapters (see previous section), you can provide a config, but you have the possibility to not provide one every time.
319319

320-
```javascript
320+
Lets say you want two adapters pointing to the same database, you can for that use the `common` option.
321321

322+
```typescript
322323
class MyCustomAdapter extends AbstractHapinessMongoAdapter {
323324
public static getInterfaceName(): string {
324325
return 'custom-identifier-for-my-adapter';
@@ -351,21 +352,19 @@ Hapiness.bootstrap(
351352
)
352353
]
353354
);
354-
355355
```
356356

357357
[Back to top](#table-of-contents)
358358

359359
## Get your adapter anywhere
360360

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.
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.
364362

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(...)`.
366364

367-
```javascript
365+
Example showing how to get mongoose adapter for `my_database`:
368366

367+
```typescript
369368
@Injectable()
370369
class MyModelDocument {
371370
private _myModelConnection: any;
@@ -404,7 +403,7 @@ You can implement and register models in the adapter
404403

405404
Example:
406405

407-
```javascript
406+
```typescript
408407
@MongoModel({
409408
adapter: 'mongoose',
410409
collection: 'collection', // The name of the collection, will be pluralize using mongoose if no collectionName is explicitly given
@@ -452,23 +451,23 @@ class MyModule {}
452451

453452
## Helpers functions
454453

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.
456455

457456
Just import the class from the module
458457

459-
```
458+
```typescript
460459
import { MongoUtils } from '@hapiness/mongo'
461460
```
462461

463462
There is 4 static functions (for now)
464463

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!
466465

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)
468467

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
470469

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
472471

473472
[Back to top](#table-of-contents)
474473

@@ -487,6 +486,11 @@ To set up your development environment:
487486

488487
## Change History
489488

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.
490494
* v1.1.3 (2018-01-16)
491495
* Fix get adapter using adapter name
492496
* v1.1.2 (2018-01-09)
@@ -523,12 +527,14 @@ To set up your development environment:
523527
<td align="center"><a href="https://github.com/antoinegomez"><img src="https://avatars3.githubusercontent.com/u/997028?v=3&s=117" width="117"/></a></td>
524528
<td align="center"><a href="https://github.com/reptilbud"><img src="https://avatars3.githubusercontent.com/u/6841511?v=3&s=117" width="117"/></a></td>
525529
<td align="center"><a href="https://github.com/njl07"><img src="https://avatars3.githubusercontent.com/u/1673977?v=3&s=117" width="117"/></a></td>
530+
<td align="center"><a href="https://github.com/xmaIIoc"><img src="https://avatars2.githubusercontent.com/u/1898461?s=117&v=4" width="117"/></a></td>
526531
</tr>
527532
<tr>
528533
<td align="center"><a href="https://github.com/Juneil">Julien Fauville</a></td>
529534
<td align="center"><a href="https://github.com/antoinegomez">Antoine Gomez</a></td>
530535
<td align="center"><a href="https://github.com/reptilbud">Sébastien Ritz</a></td>
531536
<td align="center"><a href="https://github.com/njl07">Nicolas Jessel</a></td>
537+
<td align="center"><a href="https://github.com/xmaIIoc">Florent Bennani</a></td>
532538
</tr>
533539
</table>
534540

0 commit comments

Comments
 (0)