Skip to content

refactor!: format sort in cursor and in sort builder - #2573

Merged
reggi merged 11 commits into
masterfrom
NODE-2458/cursor-sort
Oct 19, 2020
Merged

refactor!: format sort in cursor and in sort builder#2573
reggi merged 11 commits into
masterfrom
NODE-2458/cursor-sort

Conversation

@reggi

@reggi reggi commented Oct 9, 2020

Copy link
Copy Markdown
Contributor

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 a cmd 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.

@reggi
reggi requested review from emadum, mbroadst and nbbeeken and removed request for mbroadst October 9, 2020 20:46
Comment thread src/operations/common_functions.ts Outdated
}

// Get the next available document from the cursor, returns null if no more documents are available.
export function nextObject(cursor: Cursor, callback: Callback): void {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dead code?

Comment thread src/sort.ts Outdated
}

/** converts a Sort type into a type that is valid for the server (SortForCmd) */
export const formatSort = SortDigest.prepare;

Copy link
Copy Markdown
Contributor Author

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.

Copy link
Copy Markdown
Contributor

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.

Copy link
Copy Markdown
Contributor Author

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);

Copy link
Copy Markdown
Contributor Author

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.

Comment thread test/functional/cursor.test.js
});
cursor = collection.find();
try {
cursor.sort(25);

Copy link
Copy Markdown
Contributor Author

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?

Copy link
Copy Markdown
Contributor

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.

Base automatically changed from NODE-2820/remove-readable-from-cursor to master October 13, 2020 14:56
@reggi
reggi changed the base branch from master to NODE-2839/remove-core-cursor October 13, 2020 15:47
Base automatically changed from NODE-2839/remove-core-cursor to NODE-2839/remove-core-cursor-clean October 13, 2020 15:55
@reggi
reggi changed the base branch from NODE-2839/remove-core-cursor-clean to master October 13, 2020 16:05
@reggi
reggi changed the base branch from master to NODE-2839/remove-core-cursor-clean October 13, 2020 16:08
@reggi
reggi changed the base branch from NODE-2839/remove-core-cursor-clean to master October 13, 2020 17:05
@reggi
reggi force-pushed the NODE-2458/cursor-sort branch from cb3c039 to 2f4131d Compare October 13, 2020 17:07

@nbbeeken nbbeeken left a comment

Copy link
Copy Markdown
Contributor

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

Comment thread src/sort.ts Outdated
type SortForCmd = { [key: string]: SortDirectionForCmd };

/** @internal */
class SortDigest {

Copy link
Copy Markdown
Contributor

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?

Copy link
Copy Markdown
Contributor Author

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.

Copy link
Copy Markdown
Contributor

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done!

Comment thread src/sort.ts Outdated
Comment on lines +66 to +68
return t.reduce((acq, i) => {
return { ...acq, ...SortDigest.pairToObject(i) };
}, {});

Copy link
Copy Markdown
Contributor

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.

Copy link
Copy Markdown
Contributor Author

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}?

Copy link
Copy Markdown
Contributor

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cool, done

Comment thread src/sort.ts Outdated
Comment on lines +82 to +83
if (Array.isArray(sort) && !sort.length) return undefined;
if (typeof sort === 'object' && !Object.keys(sort).length) return undefined;

Copy link
Copy Markdown
Contributor

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 🧅

Copy link
Copy Markdown
Contributor Author

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 => undefined
  • Object.keys({ 'neal': true }).length => 1

Copy link
Copy Markdown
Contributor

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.

Copy link
Copy Markdown
Contributor Author

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?

Comment thread test/functional/cursor.test.js
Comment thread test/functional/cursor.test.js Outdated
@reggi
reggi requested a review from nbbeeken October 15, 2020 17:59
Comment thread src/sort.ts
Comment thread src/sort.ts Outdated

@nbbeeken nbbeeken left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀

@emadum emadum left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

@reggi
reggi merged commit 8aad134 into master Oct 19, 2020
@reggi
reggi deleted the NODE-2458/cursor-sort branch October 19, 2020 21:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants