respond-framework
Version:
create as fast you think
94 lines (91 loc) • 3.2 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.toProject = exports.toObjectIdsSelector = exports.toObjectIds = exports.toIdSelector = exports.resolveId = exports.isForeignOrLocalKey = exports.fromObjectIds = void 0;
var _mongodb = require("mongodb");
const toObjectIds = (doc, key) => {
if (doc instanceof _mongodb.ObjectId || doc instanceof Date || doc instanceof RegExp) {
return doc;
} else if (key && isForeignOrLocalKey(key) && _mongodb.ObjectId.isValid(doc) && doc.length === 24) {
return new _mongodb.ObjectId(doc);
} else if (Array.isArray(doc)) {
return isArrayOfIds(doc[0]) ? doc.map(v => new _mongodb.ObjectId(v)) : doc.map(v => toObjectIds(v));
} else if (typeof doc === 'object' && doc !== null) {
return Object.keys(doc).reduce((acc, k) => {
acc[k] = toObjectIds(doc[k], k);
return acc;
}, {});
}
return doc; // primitive
};
exports.toObjectIds = toObjectIds;
const toObjectIdsSelector = selector => {
if (selector === undefined) return;
if (typeof selector === 'string') {
selector = {
_id: selector
};
} else {
selector = {
...selector
};
}
if (selector.id) {
selector._id = selector.id; // replace top level id with _id
delete selector.id;
}
return toObjectIds(selector);
};
exports.toObjectIdsSelector = toObjectIdsSelector;
const resolveId = field => {
if (field === 'id') {
return '_id';
} else if (field === 'forceId') {
return 'id'; // id can be used on actual mongo docs, such as with collections that have both an _id and id field
}
return field;
};
exports.resolveId = resolveId;
const isForeignOrLocalKey = key => endsWithIdReg.test(key) || key === '_id' || key === 'id';
exports.isForeignOrLocalKey = isForeignOrLocalKey;
const isArrayOfIds = firstElement => typeof firstElement === 'string' && _mongodb.ObjectId.isValid(firstElement) && firstElement.length === 24; // valid 24 char hex string -- must check if string, cuz ObjectId.isValid(123) is true; NOTE: your app can't use 24 hex strings for any other purpose!
const endsWithIdReg = /(Id|\.id)$/;
const fromObjectIds = doc => {
if (doc instanceof _mongodb.ObjectId) {
return doc.toString();
} else if (doc instanceof Date || doc instanceof RegExp) {
return doc;
} else if (Array.isArray(doc)) {
return doc.map(v => fromObjectIds(v));
} else if (doc && typeof doc === 'object') {
return Object.keys(doc).reduce((acc, k) => {
acc[k] = fromObjectIds(doc[k]);
return acc;
}, {});
}
return doc; // primitive
};
exports.fromObjectIds = fromObjectIds;
const toProject = project => {
if (project?.id !== undefined) {
const {
id,
...proj
} = project;
return {
...proj,
_id: id
};
}
return project;
};
// only for use for index.id.js which assumes a 3rd party api with id keys, which you would like to keep while storing in Mongo with _id at the same time
exports.toProject = toProject;
const toIdSelector = (selector = {}) => {
const id = typeof selector === 'string' ? selector : selector.id;
return id ? {
id
} : toObjectIds(selector);
};
exports.toIdSelector = toIdSelector;