Skip to content

Commit

Permalink
Merge pull request #2 from fabrix-app/v1.6
Browse files Browse the repository at this point in the history
[feat] upgrade deps, add validator to schema.
  • Loading branch information
scott-wyatt authored Jun 10, 2019
2 parents 4c2690d + e2fdc88 commit 68946b8
Show file tree
Hide file tree
Showing 15 changed files with 2,183 additions and 1,965 deletions.
11 changes: 9 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,15 @@ version: 2
defaults: &defaults
working_directory: ~/fabrix
docker:
- image: circleci/node:10.0.0
- image: circleci/elasticsearch:latest
- image: circleci/node:12.4.0
- image: docker.elastic.co/elasticsearch/elasticsearch:6.2.2
environment:
- cluster.name: elasticsearch
- xpack.security.enabled: false
- transport.host: localhost
- network.host: 127.0.0.1
- http.port: 9200
- discovery.type: single-node

jobs:
test:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export class SomeController extends Controller {
}
```

More information about Elasticsearch client could be found here: https://github.com/elastic/elasticsearch-js
More information about Elasticsearch client could be found here: [Elasticsearch-js](https://github.com/elastic/elasticsearch-js)

## License
[MIT](https://github.com/fabrix-app/spool-elasticsearch/blob/master/LICENSE)
Expand Down
File renamed without changes.
41 changes: 36 additions & 5 deletions lib/ElasticsearchSpool.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Spool } from '@fabrix/fabrix/dist/common'
import { clone, isObject, isFunction } from 'lodash'
import { Validator } from './validator'
import * as elasticsearch from 'elasticsearch'

import * as config from './config/index'
Expand Down Expand Up @@ -37,12 +38,26 @@ export class ElasticsearchSpool extends Spool {
*/
async validate() {
if (!isObject(this.app.config.get('elasticsearch'))) {
return Promise.reject(new Error('No configuration found at config.elasticsearch !'))
return Promise.reject(new Error('No configuration found at config.elasticsearch!'))
}

if (!isObject(this.app.config.get('elasticsearch.connection'))) {
return Promise.reject(new Error('No connection configuration defined !'))
return Promise.reject(new Error('No connection configuration defined!'))
}

const stores = this.app.config.get('stores')
if (stores && Object.keys(stores).length === 0) {
this.app.log.warn('No store configured at config.stores, models will be ignored')
}
const models = this.app.config.get('models')
if (models && Object.keys(models).length === 0) {
this.app.log.warn('No models configured at config.models, models will be ignored')
}
return Promise.all([
Validator.validateStoresConfig(stores),
Validator.validateModelsConfig(models),
Validator.validateElasticConfig(this.app.config.get('elasticsearch'))
])
}

configure() {
Expand All @@ -56,14 +71,15 @@ export class ElasticsearchSpool extends Spool {
// super.initialize()

// Notice !!!
// Elastic try to change given config onject. So do not remove `clone`
// Elastic tries to change given config onject. So do not remove `clone`
// Otherwise Fabrix will pass readonly object and Elasticsearch wouldn't
// be able to connect
this.client = new elasticsearch.Client(clone(this.app.config.get('elasticsearch.connection')))

// If no need to validate connection - exit
if (!this.app.config.get('elasticsearch.validateConnection')) {
return Promise.resolve()
// Migrate the connections and/or models by their migration strategy
return this.migrate()
}

// validating connection using ping command
Expand All @@ -72,7 +88,14 @@ export class ElasticsearchSpool extends Spool {
if (err) {
return reject(err)
}
resolve()
// Migrate the connections and/or models by their migration strategy
return this.migrate()
.then(() => {
resolve()
})
.catch(_err => {
reject(_err)
})
})
})
}
Expand All @@ -89,4 +112,12 @@ export class ElasticsearchSpool extends Spool {
return this.app.elasticClient.close()
}

/**
* Migrate the database connections
*/
async migrate() {
const SchemaMigrationService = this.app.services.ElasticsearchSchemaMigrationService
return SchemaMigrationService.migrateDB([this.app.elasticClient])
}

}
102 changes: 102 additions & 0 deletions lib/api/services/ElasticsearchSchemaMigrationService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import { FabrixService as Service } from '@fabrix/fabrix/dist/common'
/**
* @module SchemaMigrationService
* @description Schema Migrations
*/
export class ElasticsearchSchemaMigrationService extends Service {

/**
* Drop collection
*/
async dropIndex(index, connection) {
// const dialect = connection.dialect.connectionManager.dialectName
// return index.sequelize.query(dialect === 'sqlite' ? 'PRAGMA foreign_keys = OFF' : 'SET FOREIGN_KEY_CHECKS = 0')
// .then(() => {
// return index.sync({force: true})
// })
// .then(() => {
// return index.sequelize.query(dialect === 'sqlite' ? 'PRAGMA foreign_keys = ON' : 'SET FOREIGN_KEY_CHECKS = 1')
// })
// .catch(err => {
// return index.sync({force: true})
// })
}

/**
* Alter an existing schema
*/
async alterIndex(index, connection) {
// const dialect = connection.dialect.connectionManager.dialectName
// return connection.sync(index)
// return index.sync()
}

migrateIndexs(indexs, connection) {
// let promises = []
// Object.entries(indexs).forEach(([ _, index ]: [ any, {[key: string]: any}]) => {
// if (index.migrate === 'drop') {
// promises.push(this.dropIndex(index, connection))
// }
// else if (index.migrate === 'alter') {
// promises.push(this.alterIndex(index, connection))
// }
// else if (index.migrate === 'none') {
// return
// }
// else {
// return
// }
// })
// return promises
}

/**
* Drop collections in current connection
* @param connection connection object
*/
async dropDB(connection) {
// const dialect = connection.dialect.connectionManager.dialectName
// return connection.query(dialect === 'sqlite' ? 'PRAGMA foreign_keys = OFF' : 'SET FOREIGN_KEY_CHECKS = 0')
// .then(() => {
// return connection.sync({force: true})
// })
// .then(() => {
// return connection.query(dialect === 'sqlite' ? 'PRAGMA foreign_keys = ON' : 'SET FOREIGN_KEY_CHECKS = 1')
// })
// .catch(err => {
// return connection.sync({force: true})
// })
}

/**
* Alter an existing database
*/
async alterDB(connection) {
// return connection.sync()
}

/**
* Migrate the DB
* Checks the connection level instances first and the reverts to index level migration strategy
*/
async migrateDB(connections) {
let promises = []

// Object.entries(connections).forEach(([ _, store ]: [ any, {[key: string]: any}]) => {
// if (store.migrate === 'drop') {
// promises.push(this.dropDB(store))
// }
// else if (store.migrate === 'alter') {
// promises.push(this.alterDB(store))
// }
// else if (store.migrate === 'none') {
// return
// }
// else {
// promises = [...promises, ...this.migrateIndexs(store.indexs, store)]
// }
// })

return Promise.all(promises)
}
}
5 changes: 0 additions & 5 deletions lib/api/services/ElasticsearchService.ts

This file was deleted.

2 changes: 1 addition & 1 deletion lib/api/services/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { ElasticsearchService } from './ElasticsearchService'
export { ElasticsearchSchemaMigrationService } from './ElasticsearchSchemaMigrationService'
2 changes: 2 additions & 0 deletions lib/config/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
export { spool } from './spool'
export { elasticsearch } from './elasticsearch'
export { stores } from './stores'
export { models } from './models'
1 change: 1 addition & 0 deletions lib/config/models.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const models = {}
1 change: 1 addition & 0 deletions lib/config/stores.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const stores = {}
21 changes: 21 additions & 0 deletions lib/schemas.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict'
const joi = require('joi')

export const Schemas = {
storesConfig: joi.object().keys({

}).unknown(),
modelsConfig: joi.object().keys({

}).unknown(),
pluginsConfig: joi.object().keys({

}).unknown(),

elasticConfig: joi.object().keys({
connection: joi.object().keys({
log: joi.string()
}).unknown(),
validateConnection: joi.boolean()
}).unknown(),
}
35 changes: 35 additions & 0 deletions lib/validator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import * as joi from 'joi'
import { Schemas } from './schemas'

export const Validator = {
validateStoresConfig (config) {
return new Promise((resolve, reject) => {
joi.validate(config, Schemas.storesConfig, (err, value) => {
if (err) {
return reject(err)
}
return resolve(value)
})
})
},
validateModelsConfig (config) {
return new Promise((resolve, reject) => {
joi.validate(config, Schemas.modelsConfig, (err, value) => {
if (err) {
return reject(err)
}
return resolve(value)
})
})
},
validateElasticConfig (config) {
return new Promise((resolve, reject) => {
joi.validate(config, Schemas.elasticConfig, (err, value) => {
if (err) {
return reject(err)
}
return resolve(value)
})
})
}
}
Loading

0 comments on commit 68946b8

Please sign in to comment.