Skip to content

Commit

Permalink
Merge pull request #4 from fabrix-app/v1.1
Browse files Browse the repository at this point in the history
[chore] update dependencies, add utils
  • Loading branch information
scott-wyatt authored Jul 23, 2018
2 parents bb1e7cb + d5f9554 commit 10c01c3
Show file tree
Hide file tree
Showing 10 changed files with 246 additions and 24 deletions.
6 changes: 6 additions & 0 deletions lib/SequelizeResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import { Transformer } from './transformer'

export class SequelizeResolver extends FabrixResolver {
private _connection
private _sequelize
private _sequelizeModel

constructor (model: FabrixModel, datastore?: Sequelize) {
super(model)
if (!model) {
throw new RangeError('Resolver must be given a Model to bind to')
}
this._sequelize = datastore
}

get connection() {
Expand All @@ -25,6 +27,10 @@ export class SequelizeResolver extends FabrixResolver {
return this._sequelizeModel
}

get sequelize() {
return this._sequelize
}

public connect(modelName, schema, options) {
this._sequelizeModel = this._connection.define(modelName, schema, options)
this._sequelizeModel.app = this.app
Expand Down
123 changes: 123 additions & 0 deletions lib/api/services/SequelizeService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import { FabrixService as Service } from '@fabrix/fabrix/dist/common'

import { defaults, omitBy, isArray, isObject, isString, isNil, merge } from 'lodash'

export class SequelizeService extends Service {
/**
*
* @param options
* @returns {*|{}}
*/
mergeOptionDefaults(...options) {
let wheres = {}
let limits = null
let offsets = null
let includes = []
let orders = []
let newOptions

for (const option of options) {
if (!(option instanceof Object)) {
throw new Error(`Option must be an object, type of option was ${ typeof option }`)
}
includes = this.mergeOptionIncludes(includes, option.include)
orders = this.mergeOptionOrders(orders, option.order)
wheres = this.mergeOptionWheres(wheres, option.where)
limits = this.mergeOptionLimits(limits, option.limit)
offsets = this.mergeOptionOffsets(offsets, option.offset)
}

newOptions = {
include: includes,
order: orders,
where: wheres,
limit: limits,
offset: offsets
}

for (const option of options.reverse()) {
newOptions = defaults({}, newOptions, option)
}
return omitBy(newOptions, isNil)
}

/**
*
* @param defaults
* @param overrides
* @returns {*|Array}
*/
mergeOptionIncludes(originals = [], overrides = []) {
const includes = originals

if (!isArray(originals) || !isArray(overrides)) {
throw new Error('include must be an array')
}

overrides.map(include => {
const inIncludes = includes.findIndex(i => i.model === include.model)
if (inIncludes !== -1 && includes[inIncludes]['as'] === include.as) {
includes[inIncludes] = include
}
else {
includes.push(include)
}
})
return includes
}

/**
*
*/
mergeOptionOrders(originals = [], overrides = []) {

if (isString(originals)) {
originals = [originals.trim().split(' ')]
}

let order = originals

if (isString(overrides)) {
order.push(overrides.trim().split(' '))
}
else if (isArray(overrides)) {
order = order.concat(overrides)
}
else if (isObject(overrides)) {
order.push([overrides])
}


return order // = defaultsDeep(order, overrides)
}

/**
*
*/
mergeOptionWheres(originals = {}, overrides = {}) {
const where = merge(originals, overrides)
return where
}

/**
*
*/
mergeOptionOffsets(originals, overrides) {
let offset = originals
if (overrides) {
offset = overrides
}
return offset
}

/**
*
*/
mergeOptionLimits(originals, overrides) {
let limit = originals
if (overrides) {
limit = overrides
}
return limit
}
}
1 change: 1 addition & 0 deletions lib/api/services/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { SchemaMigrationService } from './SchemaMigrationService'
export { SequelizeService } from './SequelizeService'
export { TapestryService } from './TapestryService'
20 changes: 10 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@fabrix/spool-sequelize",
"version": "1.1.0",
"version": "1.1.1",
"description": "Spool - Datastore Spool for Sequelize.js http://sequelizejs.com",
"scripts": {
"build": "tsc -p ./lib/tsconfig.release.json",
Expand Down Expand Up @@ -52,10 +52,10 @@
"sequelize": "^4.37.10"
},
"devDependencies": {
"@fabrix/fabrix": "^1.1.0",
"@fabrix/fabrix": "^1.1.1",
"@fabrix/lint": "^1.0.0-alpha.3",
"@fabrix/spool-router": "^1.1.0",
"@fabrix/spool-tapestries": "^1.1.0",
"@fabrix/spool-router": "^1.1.2",
"@fabrix/spool-tapestries": "^1.1.1",
"@types/lodash": "^4.14.109",
"@types/node": "~10.3.4",
"mocha": "^5",
Expand All @@ -70,8 +70,8 @@
"typescript": "~2.8.1"
},
"peerDependencies": {
"@fabrix/fabrix": "^1.1.0",
"@fabrix/spool-router": "^1.1.0"
"@fabrix/fabrix": "^1.1.1",
"@fabrix/spool-router": "^1.1.2"
},
"engines": {
"node": ">= 7.6.0"
Expand Down
8 changes: 3 additions & 5 deletions test/app.js → test/fixtures/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@

const _ = require('lodash')
const smokesignals = require('smokesignals')
const testModel = require('./fixtures/testmodel')
const SequelizeResolver = require('../dist').SequelizeResolver
const testModel = require('./testmodel')
const SequelizeResolver = require('../../dist/index').SequelizeResolver

// require('@fabrix/fabrix')
const Model = require('@fabrix/fabrix/dist/common').FabrixModel


const App = {
pkg: {
name: 'spool-sequelize-test'
Expand Down Expand Up @@ -205,7 +203,7 @@ const App = {
spools: [
require('@fabrix/spool-router').RouterSpool,
require('@fabrix/spool-tapestries').TapestriesSpool,
require('../dist').SequelizeSpool // spool-sequelize
require('../../dist/index').SequelizeSpool // spool-sequelize
]
},
stores: {
Expand Down
2 changes: 1 addition & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const FabrixApp = require('@fabrix/fabrix').FabrixApp

before(() => {
global.app = new FabrixApp(require('./app'))
global.app = new FabrixApp(require('./fixtures/app'))
return global.app.start().catch(global.app.stop)
})

Expand Down
2 changes: 1 addition & 1 deletion test/unit/lib/transformer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const FabrixApp = require('@fabrix/fabrix').FabrixApp
describe('lib.Transformer', () => {
let app
beforeEach(() => {
app = new FabrixApp(require('../../app'))
app = new FabrixApp(require('../../fixtures/app'))
})

describe('#getConnections', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/unit/lib/validator.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const assert = require('assert')
const app = require('../../app')
const app = require('../../fixtures/app')
const lib = require('../../../dist/index')

describe('lib.Validator', () => {
Expand Down
94 changes: 94 additions & 0 deletions test/unit/services/SequelizeService.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
'use strict'

const assert = require('assert')

describe('api.services.SequelizeService', () => {
it.skip('should do stringify sort', (done) => {
const sort = [['created_at','ASC']]
const s = global.app.services.SequelizeService.sortToString(sort)
console.log('STRING', s)
done()
})
it.skip('should do stringify a bad sort', (done) => {
const sort = []
const s = global.app.services.SequelizeService.sortToString(sort)
console.log('STRING', s)
done()
})
it('should merge includes', (done) => {
const newOptions = global.app.services.SequelizeService.mergeOptionDefaults({
include: [{ model: 'hello' }]
}, {
include: [{ model: 'world'}]
})
assert.equal(newOptions.include.length, 2)
assert.equal(newOptions.include[0].model, 'hello')
assert.equal(newOptions.include[1].model, 'world')
done()
})
it('should merge duplicate includes', (done) => {
const newOptions = global.app.services.SequelizeService.mergeOptionDefaults({
include: [{ model: 'hello', as: 'world' }]
}, {
include: [{ model: 'hello', as: 'world' }]
})
assert.equal(newOptions.include.length, 1)
assert.equal(newOptions.include[0].model, 'hello')
done()
})
it('should merge includes with same model', (done) => {
const newOptions = global.app.services.SequelizeService.mergeOptionDefaults({
include: [{ model: 'hello', as: 'world' }]
}, {
include: [{ model: 'hello', as: 'planet' }]
})
assert.equal(newOptions.include.length, 2)
assert.equal(newOptions.include[0].model, 'hello')
assert.equal(newOptions.include[1].model, 'hello')
done()
})
it('should merge order and fix incorrect instances', (done) => {
const newOptions = global.app.services.SequelizeService.mergeOptionDefaults({
order: [['created_at','ASC']]
}, {
order: 'updated_at DESC'
})
assert.equal(newOptions.order.length, 2)
done()
})
it('should merge wheres', (done) => {
const newOptions = global.app.services.SequelizeService.mergeOptionDefaults({
where: { name: 'hello'}
}, {
where: { created_at: 'now' }
})
assert.equal(newOptions.where.name, 'hello')
assert.equal(newOptions.where.created_at, 'now')
done()
})
it('should merge limit', (done) => {
const newOptions = global.app.services.SequelizeService.mergeOptionDefaults({
limit: null
}, {
limit: 10
})
assert.equal(newOptions.limit, 10)
done()
})
it('should merge offset', (done) => {
const newOptions = global.app.services.SequelizeService.mergeOptionDefaults({
offset: null
}, {
offset: 10
})
assert.equal(newOptions.offset, 10)
done()
})
it('should merge with unknown variables', (done) => {
const newOptions = global.app.services.SequelizeService.mergeOptionDefaults({
hello: 'world'
}, {})
assert.equal(newOptions.hello, 'world')
done()
})
})

0 comments on commit 10c01c3

Please sign in to comment.