Skip to content

Instantly share code, notes, and snippets.

@Ivannnnn
Created December 16, 2021 10:36
Show Gist options
  • Select an option

  • Save Ivannnnn/54fef420edcbeecc5ff3eea71585a0f3 to your computer and use it in GitHub Desktop.

Select an option

Save Ivannnnn/54fef420edcbeecc5ff3eea71585a0f3 to your computer and use it in GitHub Desktop.
Chainable
function chainable(obj) {
const proxy = new Proxy(obj, {
get(target, prop) {
return typeof target[prop] === 'function'
? (...args) => target[prop](...args) || proxy
: target[prop]
},
})
return proxy
}
const builder = chainable({
query: {},
with(a) {
this.query.with = a
},
where(a) {
this.query.where = a
},
get() {
return this.query
},
})
console.log(builder.with('tasks').where({ projectId: 'abc' }).get())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment