Skip to content

Commit

Permalink
Fixed newClients databaseKey
Browse files Browse the repository at this point in the history
  • Loading branch information
Nataniel López committed May 22, 2020
1 parent ffdf237 commit 189fbe8
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
## [Unreleased]
### Added
- `databaseSettings` parameter for `postSaveHook()` method
- `newClientsDatabaseKey` setting for new clients database config

### Changed
- `Client` Model now set the new clients databaseKey from config
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ You should configure the database config in your service, in order to get the co
}
},
"clients": {
"databaseKey": "newClients", // The new clients config databaseKey,
"newClientsDatabaseKey": "newClients", // The new clients config databaseKey ("newClients" by default)
"databaseKey": "newClients", // The databaseKey where store the new clients ("core" by default)
"table": "clients" // The clients table where create the clients ("clients" by default)
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/api-create.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class ClientCreateAPI extends API {

async process({ clients: clientCodes } = this.data) {

const databaseSettings = Settings.get('database')[this.clientModel.databaseKey];
const databaseSettings = Settings.get('database')[this.clientModel.newClientsDatabaseKey];

const clientDatabase = {
dbHost: databaseSettings.host
Expand Down
2 changes: 1 addition & 1 deletion lib/listener-created.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class ClientCreatedListener extends EventListener {

async process(clientCode = this.eventId) {

const databaseSettings = Settings.get('database')[this.clientModel.databaseKey];
const databaseSettings = Settings.get('database')[this.clientModel.newClientsDatabaseKey];

const clientDatabase = {
dbHost: databaseSettings.host,
Expand Down
4 changes: 4 additions & 0 deletions lib/model-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ class Client extends Model {
return this.constructor.settings.databaseKey || 'core';
}

get newClientsDatabaseKey() {
return this.constructor.settings.newClientsDatabaseKey || 'newClients';
}

static get table() {
return this.settings.table || 'clients';
}
Expand Down
2 changes: 1 addition & 1 deletion tests/api-create.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ describe('APIs', () => {
}
},
clients: {
databaseKey: 'newClients'
newClientsDatabaseKey: 'newClients'
}
};

Expand Down
2 changes: 1 addition & 1 deletion tests/listener-created.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ describe('Client Created Listener', async () => {
}
},
clients: {
databaseKey: 'newClients'
newClientsDatabaseKey: 'newClients'
}
};

Expand Down
22 changes: 22 additions & 0 deletions tests/model-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,28 @@ describe('ClientModel', () => {
assert.deepStrictEqual(clientModel.databaseKey, 'some-databaseKey');
});

it('should return the newClients as databaseKey for new clients when is not set in settings', () => {

sandbox.stub(Settings, 'get')
.returns({});

const clientModel = new ClientModel();

assert.deepStrictEqual(clientModel.newClientsDatabaseKey, 'newClients');
});

it('should return the new clients databaseKey setted in settings', () => {

sandbox.stub(Settings, 'get')
.returns({
newClientsDatabaseKey: 'some-databaseKey'
});

const clientModel = new ClientModel();

assert.deepStrictEqual(clientModel.newClientsDatabaseKey, 'some-databaseKey');
});

it('should return clients as table name when is not set in settings', () => {

sandbox.stub(Settings, 'get')
Expand Down

0 comments on commit 189fbe8

Please sign in to comment.