-
Notifications
You must be signed in to change notification settings - Fork 9
docs: Add usage examples for Resource class methods #393
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
Merged
Merged
Changes from 6 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
84e08f5
Add usage examples for Resource class methods
baileydunning 9ac6171
prettier
baileydunning bb519de
Add usage examples for global vars in versioned documentation
baileydunning 9b1d6b7
rm usage examples
baileydunning f1f8ea4
Update Resource class documentation to include schema definition and …
baileydunning 944590a
format
baileydunning 1b433be
update
baileydunning e26f160
see if this fixes broken links
baileydunning ad096c7
format
baileydunning 15e3751
update globals page with new examples
baileydunning 97b1429
apply comments
baileydunning 15b95e7
try this
baileydunning 704270e
Update docs/reference/globals.md
baileydunning 1d9d294
Update docs/reference/globals.md
baileydunning e1a569a
Update docs/reference/globals.md
baileydunning 5e497b3
Update docs/reference/resources/index.md
baileydunning 8a6a8e0
Update docs/reference/resources/index.md
baileydunning ad9ad51
Update docs/reference/resources/index.md
baileydunning 682d7a7
apply ethan updates
baileydunning 4ebd3ff
format
baileydunning ec62174
make it just Product instead of ProductTable
baileydunning 3489c76
update example
baileydunning 2f49eeb
update put example
baileydunning 451d262
update again!
baileydunning 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 |
|---|---|---|
|
|
@@ -86,11 +86,65 @@ All Resource methods that are called from HTTP methods may directly return data | |
|
|
||
| ### `tables` | ||
|
|
||
| This is an object with all the tables in the default database (the default database is "data"). Each table that has been declared or created will be available as a (standard) property on this object, and the value will be the table class that can be used to interact with that table. The table classes implement the Resource API. | ||
| This is an object with all the tables in the default database (the default database is "data"). Each table that has been declared or created in your `schema.graphql` file will be available as a property on this object, and the value will be the table class that can be used to interact with that table. The table classes implement the Resource API. | ||
|
|
||
| **Schema Definition:** | ||
| Tables are defined in your `schema.graphql` file using the `@table` directive. For example: | ||
kriszyp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| ```graphql | ||
| type Product @table { | ||
| id: ID @primaryKey | ||
| name: String | ||
| price: Float | ||
| } | ||
| ``` | ||
|
|
||
| Once declared, `Product` will be available as `tables.Product` (or `databases.data.Product`). This mapping is automatic: every table defined in the default database in your schema will appear as a property on the `tables` object. | ||
|
|
||
| #### Example | ||
|
|
||
| ```js | ||
| const MyTable = tables.table_name; // Same as databases.data.MyTable | ||
|
||
|
|
||
| // Within your Resource class: | ||
| // Create a new record (ID generated) | ||
| const created = await MyTable.create({ name: 'Example', status: 'active' }); | ||
|
|
||
| // Retrieve by primary key | ||
| const record = await MyTable.get(created[MyTable.id]); | ||
baileydunning marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // Insert or replace by ID | ||
| await MyTable.put(created[MyTable.id], { ...record, status: 'inactive' }); | ||
|
|
||
| // Run a query | ||
| const query = { | ||
| conditions: [{ attribute: 'status', value: 'active' }], | ||
| limit: 50, | ||
| }; | ||
baileydunning marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| for await (const record of MyTable.search(query)) { | ||
| // Handle each row | ||
| } | ||
| ``` | ||
|
|
||
| ### `databases` | ||
|
|
||
| This is an object with all the databases that have been defined in Harper (in the running instance). Each database that has been declared or created will be available as a (standard) property on this object. The property values are an object with the tables in that database, where each property is a table, like the `tables` object. In fact, `databases.data === tables` should always be true. | ||
| This is an object with all the databases that have been defined in Harper (in the running instance). Each database that has been declared or created in your `schema.graphql` file will be available as a property on this object. The property values are objects containing the tables in that database, where each property is a table, just like the `tables` object. In fact, `databases.data === tables` should always be true. | ||
|
|
||
| #### Example | ||
|
|
||
| ```js | ||
| const MyTable = databases.data.MyTable; // Default database | ||
| const Events = databases.analytics.Events; // Another database | ||
|
|
||
| // Create a new event record | ||
| const event = await Events.create({ eventType: 'login', timestamp: Date.now() }); | ||
|
|
||
| // Query events | ||
| for await (const e of Events.search({ conditions: [{ attribute: 'eventType', value: 'login' }] })) { | ||
| // Handle each event | ||
| } | ||
| ``` | ||
|
|
||
| ### `Resource` | ||
|
|
||
|
|
||
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
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
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
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.
If we are going to add this, I wonder if it should be at line 58 before we first reference
tables.MyTable?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 just realized we have two "Globals" references in the docs. The top-level page and then this subsection. TBH i don't think we need to tackle reorganization within this pr. Just updating the sections in place with more details is helpful enough and then we'll make larger organization and flow changes later.