getRelatedIndexes.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. 'use strict';
  2. function getRelatedSchemaIndexes(model, schemaIndexes) {
  3. return getRelatedIndexes({
  4. baseModelName: model.baseModelName,
  5. discriminatorMapping: model.schema.discriminatorMapping,
  6. indexes: schemaIndexes,
  7. indexesType: 'schema'
  8. });
  9. }
  10. function getRelatedDBIndexes(model, dbIndexes) {
  11. return getRelatedIndexes({
  12. baseModelName: model.baseModelName,
  13. discriminatorMapping: model.schema.discriminatorMapping,
  14. indexes: dbIndexes,
  15. indexesType: 'db'
  16. });
  17. }
  18. module.exports = {
  19. getRelatedSchemaIndexes,
  20. getRelatedDBIndexes
  21. };
  22. function getRelatedIndexes({
  23. baseModelName,
  24. discriminatorMapping,
  25. indexes,
  26. indexesType
  27. }) {
  28. const discriminatorKey = discriminatorMapping && discriminatorMapping.key;
  29. const discriminatorValue = discriminatorMapping && discriminatorMapping.value;
  30. if (!discriminatorKey) {
  31. return indexes;
  32. }
  33. const isChildDiscriminatorModel = Boolean(baseModelName);
  34. if (isChildDiscriminatorModel) {
  35. return indexes.filter(index => {
  36. const partialFilterExpression = getPartialFilterExpression(index, indexesType);
  37. return partialFilterExpression && partialFilterExpression[discriminatorKey] === discriminatorValue;
  38. });
  39. }
  40. return indexes.filter(index => {
  41. const partialFilterExpression = getPartialFilterExpression(index, indexesType);
  42. return !partialFilterExpression || !partialFilterExpression[discriminatorKey];
  43. });
  44. }
  45. function getPartialFilterExpression(index, indexesType) {
  46. if (indexesType === 'schema') {
  47. const options = index[1];
  48. return options && options.partialFilterExpression;
  49. }
  50. return index.partialFilterExpression;
  51. }