-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor!: format sort in cursor and in sort builder #2573
Conversation
src/operations/common_functions.ts
Outdated
@@ -106,28 +99,6 @@ export function prepareDocs( | |||
}); | |||
} | |||
|
|||
// Get the next available document from the cursor, returns null if no more documents are available. | |||
export function nextObject(cursor: Cursor, callback: Callback): void { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
dead code?
src/sort.ts
Outdated
} | ||
|
||
/** converts a Sort type into a type that is valid for the server (SortForCmd) */ | ||
export const formatSort = SortDigest.prepare; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thought it would be good to keep this as a black box.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense! since we just export prepare
, maybe we should name it formatSort
this goes along with my comment of pulling the functions to the top level of the module, my thinking its a little more ctrl-F friendly this way.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done!
} | ||
}; | ||
|
||
var cursor = collection.find().sort(['a', 1]); | ||
test.deepEqual(['a', 1], cursor.sortValue); | ||
finished(); | ||
test.deepEqual({ a: 1 }, cursor.sortValue); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
these had to change, it no longer just places the value it formats & validates it.
}); | ||
cursor = collection.find(); | ||
try { | ||
cursor.sort(25); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This now throws a sync error, which is a breaking change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is fine, but we should probably add a !
to the commit and make a note that sort direction is now being validated more strictly.
cb3c039
to
2f4131d
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great start, a few comments
src/sort.ts
Outdated
type SortForCmd = { [key: string]: SortDirectionForCmd }; | ||
|
||
/** @internal */ | ||
class SortDigest { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since all the methods are static so whats the motivation for making this a class?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer to house the utilities used as static methods on a class, but they could be flat functions in this file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we haven't done it elsewhere I think its best to leave them as top level functions, the fact that they live in their own file I think already provides the boxing you're looking for.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done!
src/sort.ts
Outdated
return t.reduce((acq, i) => { | ||
return { ...acq, ...SortDigest.pairToObject(i) }; | ||
}, {}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's unlikely that anyone will have large sort specifiers but nonetheless this nested looping feels like something to avoid despite the potentially small input.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm confused, this seems essential, how else would you convert [['a', 1], ['b', 'desc']]
to {a: 1, b: -1}
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't this be equivalent without the inner loop?
let sortObject = {};
for (const [name, value] of t) {
sortObject[name] = prepareDirection(value);
}
return sortObject
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cool, done
src/sort.ts
Outdated
if (Array.isArray(sort) && !sort.length) return undefined; | ||
if (typeof sort === 'object' && !Object.keys(sort).length) return undefined; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[1000% nit]: I would use x.length !== 0
but that is a total 🧅
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How would this proposal work?
({ 'neal': true }).length
=> undefinedObject.keys({ 'neal': true }).length
=> 1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I should clarify I meant in addition to what you have.
if you already check Array.isArray(sort)
is true then sort will have a length prop that is a number.
Object.keys(sort)
returns an array so you can check the length === 0 on that too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmmm, I'm 100% down to do this, not sure I'm fully understanding, can you give me a suggestion using the github feature / code sample?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🚀
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM 👍
NODE-2458
This PR attempts to disambiguate what the difference between the
Sort
that the user enters in, and the$sort
that is sent to the server, and to format accordingly. Even though acmd
is any / document, that's not really true, it still takes specified key / value types, and it's not necessary apparent what the difference is.