-
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
Changes from 12 commits
84e08f5
9ac6171
bb519de
9b1d6b7
f1f8ea4
944590a
1b433be
e26f160
ad096c7
15e3751
97b1429
15b95e7
704270e
1d9d294
e1a569a
5e497b3
8a6a8e0
ad9ad51
682d7a7
4ebd3ff
ec62174
3489c76
2f49eeb
451d262
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
| 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. For more info, read our complete [guide on defining schemas](../developers/applications/defining-schemas). | ||
|
|
||
| #### Example | ||
|
|
||
| ```js | ||
| const ProductTable = tables.Product; // Same as databases.data.Product | ||
|
|
||
| // Within your Resource class: | ||
| // Create a new record (ID generated) | ||
| const created = await ProductTable.create({ name: 'Example', status: 'active' }); | ||
baileydunning marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // Retrieve by primary key | ||
| const record = await ProductTable.get(created.id); | ||
|
|
||
| // Insert or replace by ID | ||
| await ProductTable.put(created.id, { ...record, status: 'inactive' }); | ||
baileydunning marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // 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 ProductTable.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 ProductTable = databases.data.Product; // Default database | ||
baileydunning marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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` | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.