-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from fabrix-app/v1.1
[chore] update dependencies, add utils
- Loading branch information
Showing
10 changed files
with
246 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
}) | ||
}) |