"use strict"; const matchValue = require("match-value"); const operations = require("../../operations"); const internalOperations = require("../../internal-operations"); const NoChange = require("../util/no-change"); const ConsumeNode = require("../util/consume-node"); /* Translate index modifiers on a single field into a top-level (non-composite) index element { type: "index"|"removeIndex", indexType, isComposite: true|false, field|fields} */ // MARKER: Filter out ConsumeNode from result (doublecheck that their presence doesn't signify a bug!), figure out autogeneration for index names, improve error for localColumn to explicitly say "don't provide a column name" (via partial match followed by error, eg. forbidden + wrapError) function handleCollection(node, { registerStateHandler, defer }) { let createNode = matchValue.literal(node.type, { createCollectionCommand: operations.createCollection, changeCollectionCommand: operations.changeCollection }); let indexNodes = []; registerStateHandler("encounteredLocalIndex", (item) => { // FIXME: Make named index // FIXME: Default name generation for indexes indexNodes.push(item); }); return defer(() => { if (indexNodes.length > 0) { let indexesObject = operations.indexes(indexNodes.map(({ node, key }) => { // FIXME: Why is it allowed to call operations.index with the same arguments? That should be disallowed. return internalOperations._index({ indexType: node.indexType, field: operations.field(key), clauses: node.clauses }); })); return createNode(node.name, node.operations.concat([ indexesObject ])); } else { return NoChange; } }); } module.exports = { name: "move-out-indexes", category: [ "normalization" ], visitors: { localIndex: (node, { setState, findNearestStep }) => { let { key } = findNearestStep("$object"); setState("encounteredLocalIndex", { node, key }); return ConsumeNode; }, createCollectionCommand: handleCollection, changeCollectionCommand: handleCollection // MARKER: Move indexes from column definitions to table index definition, with auto-generated name; may need some way to select "only within node of type X" (where X = fields) // IDEA: propertyPath and typePath arguments, for the visitor to determine whether it is appearing in the correct place (otherwise NoChange) } };