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.

70 lines
1.9 KiB
JavaScript

"use strict";
const splitFilter = require("split-filter");
const matchValue = require("match-value");
const operations = require("../../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}
*/
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((item) => {
console.log(item); // node, property
}));
return NoChange;
return createNode(name, operations.concat([ indexesObject ]));
} else {
return NoChange;
}
});
}
/*
[ createCollection, operations ]
[ _array, 0 ]
[ schemaFields, fields ]
[ _object, last_activity ]
*/
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)
}
};