Skip to content

Commit

Permalink
Merge pull request #9 from tswayne/tests-and-cleanup
Browse files Browse the repository at this point in the history
Tests and cleanup
  • Loading branch information
tswayne authored Jun 30, 2018
2 parents a37be75 + 817ca3e commit c117bb1
Show file tree
Hide file tree
Showing 10 changed files with 1,245 additions and 55 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change Log

## [1.0.2] - 06/25/2018
- Small refactor
- Add tests

## [1.0.1] - 04/07/2018
- Scope waterline initialization
- Remove unnecessary try/catch
Expand Down
14 changes: 6 additions & 8 deletions lib/fast-water.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
const Waterline = require('waterline');
const util = require('util')
const Waterline = require('./waterline-facade');
const fs = require('fs')
const readdir = util.promisify(fs.readdir)
const readdir = require('util').promisify(fs.readdir)
const waterlineConfig = require('./waterline-config')
const schema = require('./schema')
const deepmerge = require('deepmerge')
const fp = require('fastify-plugin')

module.exports = fp(async (fastify, opts) => {
module.exports = fp(async (fastify, opts, next) => {
const waterline = new Waterline();
const initialize = util.promisify(waterline.initialize)

const result = schema.validate(opts)
if (result.error) {
return next(Error(`fast-water config invalid: ${result.error}`))
}
const modelDefaults = opts.modelDefaults
const modelDefaults = opts.modelDefaults ? opts.modelDefaults : {}
const config = waterlineConfig(opts)
const modelDir = opts.modelPath
const decoratorName = opts.decoratorName ? opts.decoratorName : 'models'
Expand All @@ -26,10 +24,10 @@ module.exports = fp(async (fastify, opts) => {
const model = require(`${modelDir}/${modelName}`)
const decoratedModel = deepmerge(modelDefaults, model)

const wm = Waterline.Collection.extend(decoratedModel)
const wm = waterline.extend(decoratedModel)
waterline.registerModel(wm)
})

const ontology = await initialize(config)
const ontology = await waterline.initialize(config)
return fastify.decorate(decoratorName, ontology.collections)
})
28 changes: 28 additions & 0 deletions lib/waterline-facade.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const Waterline = require('waterline');

class WaterlineFacade {
constructor() {
this.waterline = new Waterline()
}

initialize(config) {
return new Promise((resolve, reject) => {
this.waterline.initialize(config, function(err, ontology) {
if (err) {
return reject(err)
}
return resolve(ontology)
})
})
}

registerModel(model) {
return this.waterline.registerModel(model)
}

extend(model) {
return Waterline.Collection.extend(model)
}
}

module.exports = WaterlineFacade
Loading

0 comments on commit c117bb1

Please sign in to comment.