Skip to content

Commit

Permalink
feat(QueryBuilder): pass self to callIfNecessary callbacks (#94)
Browse files Browse the repository at this point in the history
see #88
  • Loading branch information
mathroc authored and benjie committed Oct 27, 2017
1 parent db30a95 commit e169d33
Showing 1 changed file with 28 additions and 14 deletions.
42 changes: 28 additions & 14 deletions packages/graphile-build-pg/src/QueryBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,25 @@ import isSafeInteger from "lodash/isSafeInteger";

const isDev = ["test", "development"].indexOf(process.env.NODE_ENV) >= 0;

type Gen<T> = () => T;
type GenContext = {
queryBuilder: QueryBuilder,
};
type Gen<T> = (context: GenContext) => T;

function callIfNecessary<T>(o: Gen<T> | T): T {
function callIfNecessary<T>(o: Gen<T> | T, context: GenContext): T {
if (typeof o === "function") {
return o();
return o(context);
} else {
return o;
}
}

function callIfNecessaryArray<T>(o: Array<Gen<T> | T>): Array<T> {
function callIfNecessaryArray<T>(
o: Array<Gen<T> | T>,
context: GenContext
): Array<T> {
if (Array.isArray(o)) {
return o.map(callIfNecessary);
return o.map(v => callIfNecessary(v, context));
} else {
return o;
}
Expand Down Expand Up @@ -483,6 +489,9 @@ class QueryBuilder {
}
lock(type: string) {
if (this.locks[type]) return;
const getContext = () => ({
queryBuilder: this,
});
const beforeLocks = this.data.beforeLock[type];
this.data.beforeLock[type] = [];
for (const fn of beforeLocks || []) {
Expand All @@ -495,38 +504,43 @@ class QueryBuilder {
} else if (type === "whereBound") {
// Handle properties separately
this.compiledData[type].lower = callIfNecessaryArray(
this.data[type].lower
this.data[type].lower,
getContext()
);
this.compiledData[type].upper = callIfNecessaryArray(
this.data[type].upper
this.data[type].upper,
getContext()
);
} else if (type === "select") {
this.compiledData[type] = this.data[type].map(([a, b]) => [
callIfNecessary(a),
callIfNecessary(a, getContext()),
b,
]);
} else if (type === "orderBy") {
this.compiledData[type] = this.data[type].map(([a, b]) => [
callIfNecessary(a),
callIfNecessary(a, getContext()),
b,
]);
} else if (type === "from") {
if (this.data.from) {
const f = this.data.from;
this.compiledData.from = [callIfNecessary(f[0]), f[1]];
this.compiledData.from = [callIfNecessary(f[0], getContext()), f[1]];
}
} else if (type === "join" || type === "where") {
this.compiledData[type] = callIfNecessaryArray(this.data[type]);
this.compiledData[type] = callIfNecessaryArray(
this.data[type],
getContext()
);
} else if (type === "selectCursor") {
this.compiledData[type] = callIfNecessary(this.data[type]);
this.compiledData[type] = callIfNecessary(this.data[type], getContext());
} else if (type === "cursorPrefix") {
this.compiledData[type] = this.data[type];
} else if (type === "orderIsUnique") {
this.compiledData[type] = this.data[type];
} else if (type === "limit") {
this.compiledData[type] = callIfNecessary(this.data[type]);
this.compiledData[type] = callIfNecessary(this.data[type], getContext());
} else if (type === "offset") {
this.compiledData[type] = callIfNecessary(this.data[type]);
this.compiledData[type] = callIfNecessary(this.data[type], getContext());
} else if (type === "first") {
this.compiledData[type] = this.data[type];
} else if (type === "last") {
Expand Down

0 comments on commit e169d33

Please sign in to comment.