some progress

This commit is contained in:
Jonas_Jones 2023-03-30 20:40:42 +02:00
parent aea93a5527
commit e3c15bd288
1388 changed files with 306946 additions and 68323 deletions

View file

@ -0,0 +1,13 @@
'use strict';
const isTextIndex = require('./isTextIndex');
module.exports = function applySchemaCollation(indexKeys, indexOptions, schemaOptions) {
if (isTextIndex(indexKeys)) {
return;
}
if (schemaOptions.hasOwnProperty('collation') && !indexOptions.hasOwnProperty('collation')) {
indexOptions.collation = schemaOptions.collation;
}
};

View file

@ -0,0 +1,14 @@
'use strict';
module.exports = function decorateDiscriminatorIndexOptions(schema, indexOptions) {
// If the model is a discriminator and has an index, add a
// partialFilterExpression by default so the index will only apply
// to that discriminator.
const discriminatorName = schema.discriminatorMapping && schema.discriminatorMapping.value;
if (discriminatorName && !('sparse' in indexOptions)) {
const discriminatorKey = schema.options.discriminatorKey;
indexOptions.partialFilterExpression = indexOptions.partialFilterExpression || {};
indexOptions.partialFilterExpression[discriminatorKey] = discriminatorName;
}
return indexOptions;
};

View file

@ -0,0 +1,59 @@
'use strict';
function getRelatedSchemaIndexes(model, schemaIndexes) {
return getRelatedIndexes({
baseModelName: model.baseModelName,
discriminatorMapping: model.schema.discriminatorMapping,
indexes: schemaIndexes,
indexesType: 'schema'
});
}
function getRelatedDBIndexes(model, dbIndexes) {
return getRelatedIndexes({
baseModelName: model.baseModelName,
discriminatorMapping: model.schema.discriminatorMapping,
indexes: dbIndexes,
indexesType: 'db'
});
}
module.exports = {
getRelatedSchemaIndexes,
getRelatedDBIndexes
};
function getRelatedIndexes({
baseModelName,
discriminatorMapping,
indexes,
indexesType
}) {
const discriminatorKey = discriminatorMapping && discriminatorMapping.key;
const discriminatorValue = discriminatorMapping && discriminatorMapping.value;
if (!discriminatorKey) {
return indexes;
}
const isChildDiscriminatorModel = Boolean(baseModelName);
if (isChildDiscriminatorModel) {
return indexes.filter(index => {
const partialFilterExpression = getPartialFilterExpression(index, indexesType);
return partialFilterExpression && partialFilterExpression[discriminatorKey] === discriminatorValue;
});
}
return indexes.filter(index => {
const partialFilterExpression = getPartialFilterExpression(index, indexesType);
return !partialFilterExpression || !partialFilterExpression[discriminatorKey];
});
}
function getPartialFilterExpression(index, indexesType) {
if (indexesType === 'schema') {
const options = index[1];
return options && options.partialFilterExpression;
}
return index.partialFilterExpression;
}

View file

@ -0,0 +1,18 @@
'use strict';
const get = require('../get');
module.exports = function isDefaultIdIndex(index) {
if (Array.isArray(index)) {
// Mongoose syntax
const keys = Object.keys(index[0]);
return keys.length === 1 && keys[0] === '_id' && index[0]._id !== 'hashed';
}
if (typeof index !== 'object') {
return false;
}
const key = get(index, 'key', {});
return Object.keys(key).length === 1 && key.hasOwnProperty('_id');
};

View file

@ -0,0 +1,96 @@
'use strict';
const get = require('../get');
const utils = require('../../utils');
/**
* Given a Mongoose index definition (key + options objects) and a MongoDB server
* index definition, determine if the two indexes are equal.
*
* @param {Object} schemaIndexKeysObject the Mongoose index spec
* @param {Object} options the Mongoose index definition's options
* @param {Object} dbIndex the index in MongoDB as returned by `listIndexes()`
* @api private
*/
module.exports = function isIndexEqual(schemaIndexKeysObject, options, dbIndex) {
// Special case: text indexes have a special format in the db. For example,
// `{ name: 'text' }` becomes:
// {
// v: 2,
// key: { _fts: 'text', _ftsx: 1 },
// name: 'name_text',
// ns: 'test.tests',
// background: true,
// weights: { name: 1 },
// default_language: 'english',
// language_override: 'language',
// textIndexVersion: 3
// }
if (dbIndex.textIndexVersion != null) {
delete dbIndex.key._fts;
delete dbIndex.key._ftsx;
const weights = { ...dbIndex.weights, ...dbIndex.key };
if (Object.keys(weights).length !== Object.keys(schemaIndexKeysObject).length) {
return false;
}
for (const prop of Object.keys(weights)) {
if (!(prop in schemaIndexKeysObject)) {
return false;
}
const weight = weights[prop];
if (weight !== get(options, 'weights.' + prop) && !(weight === 1 && get(options, 'weights.' + prop) == null)) {
return false;
}
}
if (options['default_language'] !== dbIndex['default_language']) {
return dbIndex['default_language'] === 'english' && options['default_language'] == null;
}
return true;
}
const optionKeys = [
'unique',
'partialFilterExpression',
'sparse',
'expireAfterSeconds',
'collation'
];
for (const key of optionKeys) {
if (!(key in options) && !(key in dbIndex)) {
continue;
}
if (key === 'collation') {
if (options[key] == null || dbIndex[key] == null) {
return options[key] == null && dbIndex[key] == null;
}
const definedKeys = Object.keys(options.collation);
const schemaCollation = options.collation;
const dbCollation = dbIndex.collation;
for (const opt of definedKeys) {
if (get(schemaCollation, opt) !== get(dbCollation, opt)) {
return false;
}
}
} else if (!utils.deepEqual(options[key], dbIndex[key])) {
return false;
}
}
const schemaIndexKeys = Object.keys(schemaIndexKeysObject);
const dbIndexKeys = Object.keys(dbIndex.key);
if (schemaIndexKeys.length !== dbIndexKeys.length) {
return false;
}
for (let i = 0; i < schemaIndexKeys.length; ++i) {
if (schemaIndexKeys[i] !== dbIndexKeys[i]) {
return false;
}
if (!utils.deepEqual(schemaIndexKeysObject[schemaIndexKeys[i]], dbIndex.key[dbIndexKeys[i]])) {
return false;
}
}
return true;
};

View file

@ -0,0 +1,16 @@
'use strict';
/**
* Returns `true` if the given index options have a `text` option.
*/
module.exports = function isTextIndex(indexKeys) {
let isTextIndex = false;
for (const key of Object.keys(indexKeys)) {
if (indexKeys[key] === 'text') {
isTextIndex = true;
}
}
return isTextIndex;
};