mirror of
https://github.com/overleaf/overleaf.git
synced 2024-10-31 21:21:03 -04:00
1be43911b4
Set Prettier's "trailingComma" setting to "es5" GitOrigin-RevId: 9f14150511929a855b27467ad17be6ab262fe5d5
51 lines
1.2 KiB
JavaScript
51 lines
1.2 KiB
JavaScript
const OError = require('@overleaf/o-error')
|
|
const { ObjectId } = require('mongodb')
|
|
const { ObjectId: MongooseObjectId } = require('mongoose').mongo
|
|
|
|
function _getObjectIdInstance(id) {
|
|
if (typeof id === 'string') {
|
|
return ObjectId(id)
|
|
} else if (id instanceof ObjectId) {
|
|
return id
|
|
} else if (id instanceof MongooseObjectId) {
|
|
return ObjectId(id.toString())
|
|
} else {
|
|
throw new OError('unexpected object id', { id })
|
|
}
|
|
}
|
|
|
|
function normalizeQuery(query) {
|
|
if (!query) {
|
|
throw new Error('no query provided')
|
|
}
|
|
if (
|
|
typeof query === 'string' ||
|
|
query instanceof ObjectId ||
|
|
query instanceof MongooseObjectId
|
|
) {
|
|
return { _id: _getObjectIdInstance(query) }
|
|
} else if (typeof query._id === 'string') {
|
|
query._id = ObjectId(query._id)
|
|
return query
|
|
} else {
|
|
return query
|
|
}
|
|
}
|
|
|
|
function normalizeMultiQuery(query) {
|
|
if (Array.isArray(query)) {
|
|
return { _id: { $in: query.map(id => _getObjectIdInstance(id)) } }
|
|
} else {
|
|
return normalizeQuery(query)
|
|
}
|
|
}
|
|
|
|
function isObjectIdInstance(id) {
|
|
return id instanceof ObjectId || id instanceof MongooseObjectId
|
|
}
|
|
|
|
module.exports = {
|
|
isObjectIdInstance,
|
|
normalizeQuery,
|
|
normalizeMultiQuery,
|
|
}
|