diff --git a/src/packages/database/query/index.js b/src/packages/database/query/index.js index 008bf2e2..54ab9d22 100644 --- a/src/packages/database/query/index.js +++ b/src/packages/database/query/index.js @@ -3,6 +3,8 @@ import { camelize } from 'inflection'; import Model from '../model'; import { sql } from '../../logger'; +import initialize from './initialize'; + import entries from '../../../utils/entries'; import tryCatch from '../../../utils/try-catch'; import formatSelect from './utils/format-select'; @@ -11,7 +13,7 @@ import buildResults from './utils/build-results'; /** * @private */ -class Query extends Object { +class Query { /** * @private */ @@ -38,8 +40,6 @@ class Query extends Object { relationships: Object; constructor(model: typeof Model): Query { - super(); - Object.defineProperties(this, { model: { value: model, @@ -77,23 +77,7 @@ class Query extends Object { } }); - return new Proxy(this, { - get(instance: Query, key: string, receiver: Proxy): ?mixed | void { - if (model.hasScope(key)) { - const scope = model.scopes[key]; - - return (...args) => { - let { snapshots } = scope.apply(model, args); - snapshots = snapshots.map(snapshot => [...snapshot, key]); - - instance.snapshots.push(...snapshots); - return receiver; - }; - } else { - return instance[key]; - } - } - }); + return initialize(this); } all(): Query { diff --git a/src/packages/database/query/initialize.js b/src/packages/database/query/initialize.js new file mode 100644 index 00000000..7c1743f7 --- /dev/null +++ b/src/packages/database/query/initialize.js @@ -0,0 +1,26 @@ +import type Query from './index'; + +const TRAPS = { + get(target: Query, key: string, receiver: Proxy): ?mixed | void { + if (target.model.hasScope(key)) { + const scope = target.model.scopes[key]; + + return (...args) => { + let { snapshots } = scope.apply(target.model, args); + snapshots = snapshots.map(snapshot => [...snapshot, key]); + + target.snapshots.push(...snapshots); + return receiver; + }; + } else { + return target[key]; + } + } +}; + +/** + * @private + */ +export default function initialize(instance: Query): Query { + return new Proxy(instance, TRAPS); +}