You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 lines
1.3 KiB
JavaScript

"use strict";
// Simple data type to represent a query path and corresponding schema path tied together, because these are basically always used together, and it would bloat up the implementation code otherwise
function createInstance({ queryPath, schemaPath, queryObject, schemaObject }) {
return {
queryPath: queryPath,
schemaPath: schemaPath,
query: queryObject,
schema: schemaObject,
child: function (queryKey, schemaKey, { queryOverride, schemaOverride } = {}) {
return createInstance({
queryPath: (queryKey != null)
? queryPath.concat([ queryKey ])
: queryPath,
schemaPath: (schemaKey != null)
? schemaPath.concat([ schemaKey ])
: schemaPath,
queryObject: queryOverride ?? queryObject[queryKey],
schemaObject: schemaOverride ?? schemaObject[schemaKey]
});
},
toPathString: function () {
return queryPath
.map((segment, i) => {
if (segment === schemaPath[i]) {
return segment;
} else {
// This is used for representing aliases, showing the original schema key in brackets
return `${segment} [${schemaPath[i]}]`;
}
})
.join(" -> ");
}
};
}
module.exports = function createCursor({ query, schema }) {
return createInstance({
queryPath: [],
schemaPath: [],
queryObject: query,
schemaObject: schema
});
};