-
Notifications
You must be signed in to change notification settings - Fork 36
Add more expansive #db.f cursor documentation
#573
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
Draft
Fayti1703
wants to merge
8
commits into
comcode-org:main
Choose a base branch
from
Fayti1703:db/refine-cursor
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
5eeaf49
Snapshot 2025-11-12T24:30:00
Fayti1703 16a8063
Add page for the each family
Fayti1703 7916a10
Add page for distinct_and_keep_open
Fayti1703 2e877cc
Add page for count_and_keep_open
Fayti1703 951c5d3
Add page for sort
Fayti1703 1945e7a
Add idempotent notice to cursor.close
Fayti1703 532703b
Add page for array_and_keep_open
Fayti1703 40a6001
Run a prettier pass
Fayti1703 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| --- | ||
| title: array(_and_close) | ||
| alias: array_and_close | ||
| --- | ||
|
|
||
| Retrieve all documents of the cursor, and close it. | ||
|
|
||
| ## Syntax | ||
|
|
||
| ``` | ||
| let cursor = #db.f(query, projection) | ||
| cursor.array() | ||
| ``` | ||
|
|
||
| or | ||
|
|
||
| ``` | ||
| let cursor = #db.f(query, projection) | ||
| cursor.array_and_close() | ||
| ``` | ||
|
|
||
| ### Parameters | ||
|
|
||
| No parameters. | ||
|
|
||
| ### Return | ||
|
|
||
| Returns all documents from the cursor, as an array of objects. | ||
|
|
||
| ## Cursor State | ||
|
|
||
| This function closes the cursor. If this is undesirable, try [array_and_keep_open](./array_and_keep_open). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| Retrieve all documents of the cursor, keeping it open. | ||
|
|
||
| ## Syntax | ||
|
|
||
| ``` | ||
| let cursor = #db.f(query, projection) | ||
| cursor.array_and_keep_open() | ||
| ``` | ||
|
|
||
| ### Parameters | ||
|
|
||
| No parameters. | ||
|
|
||
| ### Return | ||
|
|
||
| Returns all documents from the cursor, as an array of objects. | ||
|
|
||
| ## Cursor State | ||
|
|
||
| This function keeps the cursor open. | ||
|
|
||
| Try [array](./array) for a variant of this function that automatically closes the cursor. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| Close the cursor. | ||
|
|
||
| This method is ((%Fidempotent%)): | ||
| calling it multiple times results in identical behavior to calling it once. | ||
|
|
||
| ## Syntax | ||
|
|
||
| ``` | ||
| let cursor = #db.f(query, projection) | ||
| cursor.close() | ||
| ``` | ||
|
|
||
| ### Parameters | ||
|
|
||
| No parameters. | ||
|
|
||
| ### Return | ||
|
|
||
| Always returns ((%Vnull%)). | ||
|
|
||
| ## Cursor State | ||
|
|
||
| This function closes the cursor. | ||
|
|
||
| ## Example | ||
|
|
||
| ```js | ||
| function(context, args) { | ||
| let cursor = #db.f({ type: "my_data" }); | ||
|
|
||
| // removing the following line causes an error: | ||
| cursor.close(); | ||
| } | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| --- | ||
| title: count(_and_close) | ||
| alias: count_and_close | ||
| --- | ||
|
|
||
| Retrieve the total number of documents of the cursor, and close it. | ||
|
|
||
| This method ignores `skip` and `limit` settings. | ||
|
|
||
| ## Syntax | ||
|
|
||
| ``` | ||
| let cursor = #db.f(query, projection) | ||
| cursor.count() | ||
| ``` | ||
|
|
||
| or | ||
|
|
||
| ``` | ||
| let cursor = #db.f(query, projection) | ||
| cursor.count_and_close() | ||
| ``` | ||
|
|
||
| ### Parameters | ||
|
|
||
| No parameters. | ||
|
|
||
| ### Return | ||
|
|
||
| The total number of documents of this cursor, as a number. | ||
|
|
||
| ## Cursor State | ||
|
|
||
| This function closes the cursor. If this is undesirable, try [count_and_keep_open](./count_and_keep_open). | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| Retrieve the total number of documents of the cursor, keeping it open. | ||
|
|
||
| This method ignores `skip` and `limit` settings. | ||
|
|
||
| ## Syntax | ||
|
|
||
| ``` | ||
| let cursor = #db.f(query, projection) | ||
| cursor.count_and_keep_open() | ||
| ``` | ||
|
|
||
| ### Parameters | ||
|
|
||
| No parameters. | ||
|
|
||
| ### Return | ||
|
|
||
| The total number of documents of this cursor, as a number. | ||
|
|
||
| ## Cursor State | ||
|
|
||
| This function keeps the cursor open. | ||
|
|
||
| Try [count](./count) for a variant of this function that automatically closes the cursor. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| --- | ||
| title: distinct(_and_close) | ||
| alias: distinct_and_close | ||
| --- | ||
|
|
||
| Retrieve the unique values of a given field for all documents of the cursor, and close it. | ||
|
|
||
| This method ignores `skip` and `limit` settings. | ||
|
|
||
| ## Syntax | ||
|
|
||
| ``` | ||
| let cursor = #db.f(query, projection) | ||
| cursor.distinct(field) | ||
| ``` | ||
|
|
||
| or | ||
|
|
||
| ``` | ||
| let cursor = #db.f(query, projection) | ||
| cursor.distinct_and_close(field) | ||
| ``` | ||
|
|
||
| ### Parameters | ||
|
|
||
| #### field | ||
|
|
||
| A string naming the field to pull unique values from. | ||
|
|
||
| ### Return | ||
|
|
||
| The unique values of the given field from all documents of the cursor, as an array. | ||
|
|
||
| ## Cursor State | ||
|
|
||
| This function closes the cursor. If this is undesirable, try [distinct_and_keep_open](./distinct_and_keep_open). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| Retrieve the unique values of a given field for all documents of the cursor, keeping it open. | ||
|
|
||
| This method ignores `skip` and `limit` settings. | ||
|
|
||
| ## Syntax | ||
|
|
||
| ``` | ||
| let cursor = #db.f(query, projection) | ||
| cursor.distinct_and_keep_open(field) | ||
| ``` | ||
|
|
||
| ### Parameters | ||
|
|
||
| #### field | ||
|
|
||
| A string naming the field to pull unique values from. | ||
|
|
||
| ### Return | ||
|
|
||
| The unique values of the given field from all documents of the cursor, as an array. | ||
|
|
||
| ## Cursor State | ||
|
|
||
| This function keeps the cursor open. | ||
|
|
||
| Try [distinct](./distinct) for a variant of this function that automatically closes the cursor. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| --- | ||
| title: each(_and_close) | ||
| alias: each_and_close | ||
| --- | ||
|
|
||
| Call a function on all documents of the cursor, and close it. | ||
|
|
||
| ## Syntax | ||
|
|
||
| ``` | ||
| let cursor = #db.f(query, projection) | ||
| cursor.each(fn) | ||
| ``` | ||
|
|
||
| or | ||
|
|
||
| ``` | ||
| let cursor = #db.f(query, projection) | ||
| cursor.each_and_close(fn) | ||
| ``` | ||
|
|
||
| ### Parameters | ||
|
|
||
| #### fn | ||
|
|
||
| The function to call. This function is called once per document, with the document passed as the only argument. | ||
|
|
||
| ### Return | ||
|
|
||
| Always returns ((%Vundefined%)). | ||
|
|
||
| ## Cursor state | ||
|
|
||
| This function closes the cursor. If this is undesirable, try [each_and_keep_open](./each_and_keep_open). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| Call a function on all documents of the cursor, keeping it open. | ||
|
|
||
| ## Syntax | ||
|
|
||
| ``` | ||
| let cursor = #db.f(query, projection) | ||
| cursor.each_and_keep_open(fn) | ||
| ``` | ||
|
|
||
| ### Parameters | ||
|
|
||
| #### fn | ||
|
|
||
| The function to call. This function is called once per document, with the document passed as the only argument. | ||
|
|
||
| ### Return | ||
|
|
||
| Always returns ((%Vundefined%)). | ||
|
|
||
| ## Cursor state | ||
|
|
||
| This function keeps the cursor open. | ||
|
|
||
| Try [each](./each) for a variant of this function that automatically closes the cursor. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| --- | ||
| title: first(_and_close) | ||
| alias: first_and_close | ||
| --- | ||
|
|
||
| Retrieve the first document of the cursor, and close it. | ||
|
|
||
| ## Syntax | ||
|
|
||
| ``` | ||
| let cursor = #db.f(query, projection) | ||
| cursor.first() | ||
| ``` | ||
|
|
||
| or | ||
|
|
||
| ``` | ||
| let cursor = #db.f(query, projection) | ||
| cursor.first_and_close() | ||
| ``` | ||
|
|
||
| ### Parameters | ||
|
|
||
| No parameters. | ||
|
|
||
| ### Return | ||
|
|
||
| Returns the first document from the cursor as an object. | ||
| If the cursor is empty, returns ((%Vnull%)). | ||
|
|
||
| ## Cursor State | ||
|
|
||
| This function closes the cursor. If this is undesirable, try [first_and_keep_open](./first_and_keep_open). | ||
|
|
||
| ## Example | ||
|
|
||
| For the following examples, assume the database contains the following documents, inserted in that order: | ||
|
|
||
| ``` | ||
| { type: "my_data", my_key: "foo", order: 2 } | ||
| { type: "my_data", my_key: "bar", order: 1 } | ||
| ``` | ||
|
|
||
| ```js | ||
| function(context, args) { | ||
| let data = #db.f({ type: "my_data" }).first(); | ||
| return data; // { type: "my_data", my_key: "foo", order: 2 } | ||
| } | ||
| ``` | ||
|
|
||
| ```js | ||
| function(context, args) { | ||
| let data = #db.f({ type: "my_data" }).sort({ order: 1 }).first(); | ||
| return data; // { type: "my_data", my_key: "bar", order: 1 } | ||
| } | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| Retrieve the first document of the cursor, keeping it open. | ||
|
|
||
| ## Syntax | ||
|
|
||
| ``` | ||
| let cursor = #db.f(query, projection) | ||
| cursor.first_and_keep_open() | ||
| // continue using `cursor`... | ||
| ``` | ||
|
|
||
| ### Parameters | ||
|
|
||
| No parameters. | ||
|
|
||
| ### Return | ||
|
|
||
| Returns the first document from the cursor as an object. | ||
| If the cursor is empty, returns ((%Vnull%)). | ||
|
|
||
| ## Cursor State | ||
|
|
||
| This function keeps the cursor open. | ||
|
|
||
| Try [first](./first) for a variant of this function that automatically closes the cursor. | ||
|
|
||
| ## Example | ||
|
|
||
| For the following example, assume the database contains the following documents, inserted in that order: | ||
|
|
||
| ``` | ||
| { type: "my_data", my_key: "foo", order: 2 } | ||
| { type: "my_data", my_key: "bar", order: 1 } | ||
| ``` | ||
|
|
||
| ```js | ||
| function(context, args) { | ||
| let cursor = #db.f({ type: "my_data" }); | ||
| let a = cursor.skip(1).first_and_keep_open(); | ||
| let b = cursor.skip(0).first_and_close(); | ||
| return [ a.my_key, b.my_key ]; // [ "bar", "foo" ] | ||
| } | ||
| ``` |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 page does not exist yet
Uh oh!
There was an error while loading. Please reload this page.
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's a few missing pages:
count_and_keep_open(which you mentioned)distinct_and_keep_openeach(_and_close)each_and_keep_opensortThere 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.
Pages added:
2e877cc
7916a10
16a8063
951c5d3