Skip to content
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

Merged
merged 11 commits into from Oct 19, 2020

Conversation

reggi
Copy link
Contributor

@reggi reggi commented Oct 9, 2020

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 mbroadst, nbbeeken and emadum and removed request for mbroadst October 9, 2020 20:46
@@ -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 {
Copy link
Contributor Author

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;
Copy link
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
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
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
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.

});
cursor = collection.find();
try {
cursor.sort(25);
Copy link
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
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
Copy link
Contributor

@nbbeeken nbbeeken left a 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 {
Copy link
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
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
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
Contributor Author

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
Comment on lines 66 to 68
return t.reduce((acq, i) => {
return { ...acq, ...SortDigest.pairToObject(i) };
}, {});
Copy link
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
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
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
Contributor Author

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

test/functional/cursor.test.js Show resolved Hide resolved
test/functional/cursor.test.js Outdated Show resolved Hide resolved
@reggi reggi requested a review from nbbeeken October 15, 2020 17:59
src/sort.ts Outdated Show resolved Hide resolved
src/sort.ts Outdated Show resolved Hide resolved
Copy link
Contributor

@nbbeeken nbbeeken left a comment

Choose a reason for hiding this comment

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

🚀

Copy link
Contributor

@emadum emadum left a comment

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
3 participants