database and ajax test added
This commit is contained in:
+16
@@ -0,0 +1,16 @@
|
||||
'use strict';
|
||||
|
||||
const get = require('../get');
|
||||
|
||||
module.exports = function applyWriteConcern(schema, options) {
|
||||
const writeConcern = get(schema, 'options.writeConcern', {});
|
||||
if (!('w' in options) && writeConcern.w != null) {
|
||||
options.w = writeConcern.w;
|
||||
}
|
||||
if (!('j' in options) && writeConcern.j != null) {
|
||||
options.j = writeConcern.j;
|
||||
}
|
||||
if (!('wtimeout' in options) && writeConcern.wtimeout != null) {
|
||||
options.wtimeout = writeConcern.wtimeout;
|
||||
}
|
||||
};
|
||||
+124
@@ -0,0 +1,124 @@
|
||||
'use strict';
|
||||
|
||||
const get = require('../get');
|
||||
const utils = require('../../utils');
|
||||
|
||||
/*!
|
||||
* Gather all indexes defined in the schema, including single nested,
|
||||
* document arrays, and embedded discriminators.
|
||||
*/
|
||||
|
||||
module.exports = function getIndexes(schema) {
|
||||
let indexes = [];
|
||||
const schemaStack = new WeakMap();
|
||||
const indexTypes = schema.constructor.indexTypes;
|
||||
|
||||
const collectIndexes = function(schema, prefix) {
|
||||
// Ignore infinitely nested schemas, if we've already seen this schema
|
||||
// along this path there must be a cycle
|
||||
if (schemaStack.has(schema)) {
|
||||
return;
|
||||
}
|
||||
schemaStack.set(schema, true);
|
||||
|
||||
prefix = prefix || '';
|
||||
const keys = Object.keys(schema.paths);
|
||||
const length = keys.length;
|
||||
|
||||
for (let i = 0; i < length; ++i) {
|
||||
const key = keys[i];
|
||||
const path = schema.paths[key];
|
||||
|
||||
if (path.$isMongooseDocumentArray || path.$isSingleNested) {
|
||||
if (get(path, 'options.excludeIndexes') !== true &&
|
||||
get(path, 'schemaOptions.excludeIndexes') !== true) {
|
||||
collectIndexes(path.schema, prefix + key + '.');
|
||||
}
|
||||
|
||||
if (path.schema.discriminators != null) {
|
||||
const discriminators = path.schema.discriminators;
|
||||
const discriminatorKeys = Object.keys(discriminators);
|
||||
for (const discriminatorKey of discriminatorKeys) {
|
||||
collectIndexes(discriminators[discriminatorKey]._originalSchema,
|
||||
prefix + key + '.');
|
||||
}
|
||||
}
|
||||
|
||||
// Retained to minimize risk of backwards breaking changes due to
|
||||
// gh-6113
|
||||
if (path.$isMongooseDocumentArray) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
const index = path._index || (path.caster && path.caster._index);
|
||||
|
||||
if (index !== false && index !== null && index !== undefined) {
|
||||
const field = {};
|
||||
const isObject = utils.isObject(index);
|
||||
const options = isObject ? index : {};
|
||||
const type = typeof index === 'string' ? index :
|
||||
isObject ? index.type :
|
||||
false;
|
||||
|
||||
if (type && indexTypes.indexOf(type) !== -1) {
|
||||
field[prefix + key] = type;
|
||||
} else if (options.text) {
|
||||
field[prefix + key] = 'text';
|
||||
delete options.text;
|
||||
} else {
|
||||
field[prefix + key] = 1;
|
||||
}
|
||||
|
||||
delete options.type;
|
||||
if (!('background' in options)) {
|
||||
options.background = true;
|
||||
}
|
||||
|
||||
indexes.push([field, options]);
|
||||
}
|
||||
}
|
||||
|
||||
schemaStack.delete(schema);
|
||||
|
||||
if (prefix) {
|
||||
fixSubIndexPaths(schema, prefix);
|
||||
} else {
|
||||
schema._indexes.forEach(function(index) {
|
||||
if (!('background' in index[1])) {
|
||||
index[1].background = true;
|
||||
}
|
||||
});
|
||||
indexes = indexes.concat(schema._indexes);
|
||||
}
|
||||
};
|
||||
|
||||
collectIndexes(schema);
|
||||
return indexes;
|
||||
|
||||
/*!
|
||||
* Checks for indexes added to subdocs using Schema.index().
|
||||
* These indexes need their paths prefixed properly.
|
||||
*
|
||||
* schema._indexes = [ [indexObj, options], [indexObj, options] ..]
|
||||
*/
|
||||
|
||||
function fixSubIndexPaths(schema, prefix) {
|
||||
const subindexes = schema._indexes;
|
||||
const len = subindexes.length;
|
||||
for (let i = 0; i < len; ++i) {
|
||||
const indexObj = subindexes[i][0];
|
||||
const keys = Object.keys(indexObj);
|
||||
const klen = keys.length;
|
||||
const newindex = {};
|
||||
|
||||
// use forward iteration, order matters
|
||||
for (let j = 0; j < klen; ++j) {
|
||||
const key = keys[j];
|
||||
newindex[prefix + key] = indexObj[key];
|
||||
}
|
||||
|
||||
indexes.push([newindex, subindexes[i][1]]);
|
||||
}
|
||||
}
|
||||
};
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = handleTimestampOption;
|
||||
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
|
||||
function handleTimestampOption(arg, prop) {
|
||||
if (arg == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (typeof arg === 'boolean') {
|
||||
return prop;
|
||||
}
|
||||
if (typeof arg[prop] === 'boolean') {
|
||||
return arg[prop] ? prop : null;
|
||||
}
|
||||
if (!(prop in arg)) {
|
||||
return prop;
|
||||
}
|
||||
return arg[prop];
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = function merge(s1, s2) {
|
||||
s1.add(s2.obj);
|
||||
|
||||
s1.callQueue = s1.callQueue.concat(s2.callQueue);
|
||||
s1.method(s2.methods);
|
||||
s1.static(s2.statics);
|
||||
|
||||
for (const query in s2.query) {
|
||||
s1.query[query] = s2.query[query];
|
||||
}
|
||||
|
||||
for (const virtual in s2.virtuals) {
|
||||
s1.virtual[virtual] = s2.virtual[virtual].clone();
|
||||
}
|
||||
|
||||
s1.s.hooks.merge(s2.s.hooks, false);
|
||||
};
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
'use strict';
|
||||
|
||||
/*!
|
||||
* Set `$parentSchema` on all schema types, and `$schemaType` on single
|
||||
* nested docs.
|
||||
*
|
||||
* This is a slow path function, should only run when model is compiled
|
||||
*/
|
||||
|
||||
module.exports = function setParentPointers(schema, skipRecursion) {
|
||||
for (const path of Object.keys(schema.paths)) {
|
||||
const schemaType = schema.paths[path];
|
||||
if (schemaType.schema != null) {
|
||||
Object.defineProperty(schemaType.schema, '$schemaType', {
|
||||
configurable: true,
|
||||
writable: false,
|
||||
enumerable: false,
|
||||
value: schemaType
|
||||
});
|
||||
}
|
||||
Object.defineProperty(schemaType, '$parentSchema', {
|
||||
configurable: true,
|
||||
writable: false,
|
||||
enumerable: false,
|
||||
value: schema
|
||||
});
|
||||
}
|
||||
|
||||
// `childSchemas` contains all descendant schemas, so no need to recurse
|
||||
// further.
|
||||
if (skipRecursion) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (const obj of schema.childSchemas) {
|
||||
setParentPointers(obj.schema, true);
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user