Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 25 additions & 28 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const QUERY_TYPES = {
$all: compareAll,

// Equality
$ne: compareNe,
$eq: compareEq,
$exists: compareExists
}
Expand Down Expand Up @@ -106,11 +107,7 @@ class Collection {
}

async update (query = {}, update = {}, options = {}) {
const {
upsert = false,
multi = false,
hint = null
} = options
const { upsert = false, multi = false, hint = null } = options

let nMatched = 0
let nUpserted = 0
Expand Down Expand Up @@ -147,7 +144,9 @@ class Collection {
for (const queryField of Object.keys(query)) {
const queryValue = query[queryField]
if ('$eq' in queryValue) initialDoc[queryField] = queryValue.$eq
else if (!isQueryObject(queryValue)) initialDoc[queryField] = queryValue
else if(!isQueryObject(queryValue)){
initialDoc[queryField] = queryValue
}
}

const newDoc = performUpdate(initialDoc, update)
Expand Down Expand Up @@ -268,12 +267,7 @@ class Collection {
}

class Cursor {
constructor (query = {}, collection, opts = {
limit: Infinity,
skip: 0,
sort: null,
hint: null
}) {
constructor (query = {}, collection, opts = { limit: Infinity, skip: 0, sort: null, hint: null }){
this.query = query
this.collection = collection
// TODO: Validate opts
Expand Down Expand Up @@ -324,17 +318,21 @@ class Cursor {
const eqS = existingFields.filter((name) => {
const queryValue = query[name]
if (!isQueryObject(queryValue)) return true
return ('$eq' in queryValue)
return '$eq' in queryValue
})

if (hint) {
const hintIndex = await this.collection.getIndex(hint)
const { fields } = hintIndex
if (sort) {
const sortIndex = fields.indexOf(sort.field)
if (sortIndex === -1) throw new Error("Hinted Index doesn't match required sort")
if (sortIndex === -1){
throw new Error("Hinted Index doesn't match required sort")
}
const consecutive = consecutiveSubset(fields, eqS)
if (consecutive !== sortIndex) throw new Error("Hinted index doesn't match required sort")
if (consecutive !== sortIndex){
throw new Error("Hinted index doesn't match required sort")
}
}

const prefixFields = fields.slice(0, consecutiveSubset(fields, eqS))
Expand Down Expand Up @@ -401,7 +399,7 @@ class Cursor {
}

async * [Symbol.asyncIterator] () {
if (this.query._id && (this.query._id instanceof ObjectID)) {
if (this.query._id && this.query._id instanceof ObjectID) {
// Doc IDs are unique, so we can query against them without doing a search
const key = this.query._id.id

Expand All @@ -422,11 +420,7 @@ class Cursor {
}
yield doc
} else {
const {
limit = Infinity,
skip = 0,
sort
} = this.opts
const { limit = Infinity, skip = 0, sort } = this.opts
const query = this.query
const seen = new Set()

Expand Down Expand Up @@ -482,7 +476,7 @@ class Cursor {
const gt = makeIndexKeyFromQuery(query, prefixFields, index.fields, makeIndexKey)

const opts = {
reverse: (sort?.direction === -1)
reverse: sort?.direction === -1
}
if (gt && gt.length) {
opts.gt = gt
Expand Down Expand Up @@ -569,7 +563,9 @@ function queryCompare (docValue, queryValue) {

function compareAll (docValue, queryValue) {
// TODO: Add query validator function to detect this early.
if (!Array.isArray(queryValue)) throw new Error('$all must be set to an array')
if (!Array.isArray(queryValue)){
throw new Error('$all must be set to an array')
}
if (Array.isArray(docValue)) {
return queryValue.every((fromQuery) => docValue.some((fromDoc) => compareEq(fromDoc, fromQuery)))
} else {
Expand All @@ -579,7 +575,9 @@ function compareAll (docValue, queryValue) {

function compareIn (docValue, queryValue) {
// TODO: Add query validator function to detect this early.
if (!Array.isArray(queryValue)) throw new Error('$in must be set to an array')
if (!Array.isArray(queryValue)){
throw new Error('$in must be set to an array')
}
if (Array.isArray(docValue)) {
return docValue.some((fromDoc) => queryValue.some((fromQuery) => compareEq(fromDoc, fromQuery)))
} else {
Expand Down Expand Up @@ -610,8 +608,7 @@ function ensureComparable (value) {

function compareEq (docValue, queryValue) {
if (Array.isArray(docValue)) {
return docValue
.some((item) => compareEq(item, queryValue))
return docValue.some((item) => compareEq(item, queryValue))
} else if (typeof docValue?.equals === 'function') {
return docValue.equals(queryValue)
} else {
Expand Down Expand Up @@ -731,7 +728,7 @@ function updateMul (doc, fields) {
}

function hasFields (doc, fields) {
return fields.every((field) => (field in doc) && (field !== undefined))
return fields.every((field) => field in doc && field !== undefined)
}

function makeIndexKeyV1 (doc, fields) {
Expand Down Expand Up @@ -875,7 +872,7 @@ function makeIndexKeyFromQuery (query, fields, indexFields, makeIndexKey) {
}

function isQueryObject (object) {
return (typeof object === 'object') && has$Keys(object)
return typeof object === 'object' && has$Keys(object)
}

function has$Keys (object) {
Expand Down