Skip to content

Commit

Permalink
Merge pull request #2 from paralect/ezhivitsa-extendable-service
Browse files Browse the repository at this point in the history
Extendable services
  • Loading branch information
anorsich authored Oct 26, 2017
2 parents d1ef732 + 3f9dc16 commit 3952b6a
Show file tree
Hide file tree
Showing 10 changed files with 444 additions and 623 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## v0.3.0 (2017-10-24)

* Add ability to create custom methods for service and query service.
* Add tests.

## v0.2.0 (2017-10-12)

* Add support of the [joi](https://github.com/hapijs/joi) for validating data schema.
Expand Down
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ There are few reasons, why we think this layer could be helpful to many projects

1. Every update method emits `*.updated`, `*.created`, `*.removed` events, which allow to listen for the database changes and perform business logic based on this updates. That could help keep your entities weakly coupled with each other.
2. Implements more high level api, such as paging.
3. Implements database schema validation based on [jsonschema](https://github.com/tdegrunt/jsonschema). See examples below for more details.
3. Implements database schema validation based on [joi](https://github.com/hapijs/joi). See examples below for more details.
4. Allows you to add custom methods for services that are needed on a particular project. See examples below for more details.

## Usage example

Expand Down Expand Up @@ -196,3 +197,20 @@ module.exports = (obj) => validator.validate(obj, companySchema);
const schema = require('./user.schema')
const usersService = db.createService('users', schema);
```

In order to add your own methods to the services, you can use the functions `setServiceMethod` and `setQueryServiceMethod` of the object `db`. This function takes the function name as the first parameter and the custom function for the service as the second parameter. The custom function takes the service itself as the first parameter, and the remaining parameters are the parameters that are passed when this custom function is called.

```javascript
const connectionString = `mongodb://localhost:27017/home-db`;
const db = require('./').connect(connectionString);

db.setQueryServiceMethod('findById', (service, id) => {
return service.findOne({ _id: id });
});

// Create entity service
const usersQueryService = db.createQueryService('users');

// find user by id
const user = await usersQueryService.findById('123')
```
4 changes: 2 additions & 2 deletions bin/run.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/bin/sh
npm i --quiet
NODE_ENV=test npm run test:eslint
npm install --quiet
Node_ENV=test npm run test:eslint
Loading

0 comments on commit 3952b6a

Please sign in to comment.