Skip to content

Commit d0747bf

Browse files
docs: Add usage examples for Resource class methods
2 parents 8f4f71c + 451d262 commit d0747bf

File tree

8 files changed

+425
-53
lines changed

8 files changed

+425
-53
lines changed

docs/reference/globals.md

Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,25 +28,64 @@ The global variables include:
2828

2929
## `tables`
3030

31-
This is an object that holds all the tables for the default database (called `data`) as properties. Each of these property values is a table class that subclasses the Resource interface and provides access to the table through the Resource interface. For example, you can get a record from a table (in the default database) called 'my-table' with:
31+
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.
3232

33-
```javascript
34-
import { tables } from 'harperdb';
35-
const { MyTable } = tables;
36-
async function getRecord() {
37-
let record = await MyTable.get(recordId);
33+
**Schema Definition:**
34+
Tables are defined in your `schema.graphql` file using the `@table` directive. For example:
35+
36+
```graphql
37+
type Product @table {
38+
id: ID @primaryKey
39+
name: String
40+
price: Float
3841
}
3942
```
4043

41-
It is recommended that you [define a database](../developers/applications/defining-schemas) for all the tables that are required to exist in your application. This will ensure that the tables exist on the `tables` object. Also note that the property names follow a CamelCase convention for use in JavaScript and in the GraphQL Schemas, but these are translated to snake_case for the actual table names, and converted back to CamelCase when added to the `tables` object.
44+
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).
45+
46+
### Example
47+
48+
```js
49+
const Product = tables.Product; // Same as databases.data.Product
50+
51+
// Create a new record (`id` is automatically generated when using `.create()`)
52+
const created = await Product.create({ name: 'Shirt', price: 9.5 });
53+
54+
// Modify the record
55+
await Product.patch(created.id, { price: Math.round(created.price * 0.8 * 100) / 100 }); // 20% off!
56+
57+
// Retrieve by primary key
58+
const record = await Product.get(created.id);
59+
60+
logger.info('New price:', record.price);
61+
62+
// Query for all products with a `price` less than `8.00`
63+
const query = {
64+
conditions: [{ attribute: 'price', comparator: 'less_than', value: 8.0 }],
65+
};
66+
67+
for await (const record of Product.search(query)) {
68+
// ...
69+
}
70+
```
4271

4372
## `databases`
4473

45-
This is an object that holds all the databases in Harper, and can be used to explicitly access a table by database name. Each database will be a property on this object, each of these property values will be an object with the set of all tables in that database. The default database, `databases.data` should equal the `tables` export. For example, if you want to access the "dog" table in the "dev" database, you could do so:
74+
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.
4675

47-
```javascript
48-
import { databases } from 'harperdb';
49-
const { Dog } = databases.dev;
76+
### Example
77+
78+
```js
79+
const Product = databases.data.Product; // Default database
80+
const Events = databases.analytics.Events; // Another database
81+
82+
// Create a new event record
83+
const event = await Events.create({ eventType: 'login', timestamp: Date.now() });
84+
85+
// Query events
86+
for await (const e of Events.search({ conditions: [{ attribute: 'eventType', value: 'login' }] })) {
87+
// Handle each event
88+
}
5089
```
5190

5291
## `Resource`

docs/reference/resources/index.md

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,65 @@ All Resource methods that are called from HTTP methods may directly return data
8686

8787
### `tables`
8888

89-
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.
89+
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.
90+
91+
**Schema Definition:**
92+
Tables are defined in your `schema.graphql` file using the `@table` directive. For example:
93+
94+
```graphql
95+
type Product @table {
96+
id: ID @primaryKey
97+
name: String
98+
price: Float
99+
}
100+
```
101+
102+
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).
103+
104+
#### Example
105+
106+
```js
107+
const Product = tables.Product; // Same as databases.data.Product
108+
109+
// Create a new record (`id` is automatically generated when using `.create()`)
110+
const created = await Product.create({ name: 'Shirt', price: 9.5 });
111+
112+
// Modify the record
113+
await Product.patch(created.id, { price: Math.round(created.price * 0.8 * 100) / 100 }); // 20% off!
114+
115+
// Retrieve by primary key
116+
const record = await Product.get(created.id);
117+
118+
logger.info('New price:', record.price);
119+
120+
// Query for all products with a `price` less than `8.00`
121+
const query = {
122+
conditions: [{ attribute: 'price', comparator: 'less_than', value: 8.0 }],
123+
};
124+
125+
for await (const record of Product.search(query)) {
126+
// ...
127+
}
128+
```
90129

91130
### `databases`
92131

93-
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.
132+
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.
133+
134+
#### Example
135+
136+
```js
137+
const Product = databases.data.Product; // Default database
138+
const Events = databases.analytics.Events; // Another database
139+
140+
// Create a new event record
141+
const event = await Events.create({ eventType: 'login', timestamp: Date.now() });
142+
143+
// Query events
144+
for await (const e of Events.search({ conditions: [{ attribute: 'eventType', value: 'login' }] })) {
145+
// Handle each event
146+
}
147+
```
94148

95149
### `Resource`
96150

versioned_docs/version-4.5/reference/globals.md

Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,25 +28,64 @@ The global variables include:
2828

2929
## `tables`
3030

31-
This is an object that holds all the tables for the default database (called `data`) as properties. Each of these property values is a table class that subclasses the Resource interface and provides access to the table through the Resource interface. For example, you can get a record from a table (in the default database) called 'my-table' with:
31+
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.
3232

33-
```javascript
34-
import { tables } from 'harperdb';
35-
const { MyTable } = tables;
36-
async function getRecord() {
37-
let record = await MyTable.get(recordId);
33+
**Schema Definition:**
34+
Tables are defined in your `schema.graphql` file using the `@table` directive. For example:
35+
36+
```graphql
37+
type Product @table {
38+
id: ID @primaryKey
39+
name: String
40+
price: Float
3841
}
3942
```
4043

41-
It is recommended that you [define a database](../getting-started/quickstart) for all the tables that are required to exist in your application. This will ensure that the tables exist on the `tables` object. Also note that the property names follow a CamelCase convention for use in JavaScript and in the GraphQL Schemas, but these are translated to snake_case for the actual table names, and converted back to CamelCase when added to the `tables` object.
44+
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).
45+
46+
### Example
47+
48+
```js
49+
const Product = tables.Product; // Same as databases.data.Product
50+
51+
// Create a new record (`id` is automatically generated when using `.create()`)
52+
const created = await Product.create({ name: 'Shirt', price: 9.5 });
53+
54+
// Modify the record
55+
await Product.patch(created.id, { price: Math.round(created.price * 0.8 * 100) / 100 }); // 20% off!
56+
57+
// Retrieve by primary key
58+
const record = await Product.get(created.id);
59+
60+
logger.info('New price:', record.price);
61+
62+
// Query for all products with a `price` less than `8.00`
63+
const query = {
64+
conditions: [{ attribute: 'price', comparator: 'less_than', value: 8.0 }],
65+
};
66+
67+
for await (const record of Product.search(query)) {
68+
// ...
69+
}
70+
```
4271

4372
## `databases`
4473

45-
This is an object that holds all the databases in Harper, and can be used to explicitly access a table by database name. Each database will be a property on this object, each of these property values will be an object with the set of all tables in that database. The default database, `databases.data` should equal the `tables` export. For example, if you want to access the "dog" table in the "dev" database, you could do so:
74+
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.
4675

47-
```javascript
48-
import { databases } from 'harperdb';
49-
const { Dog } = databases.dev;
76+
### Example
77+
78+
```js
79+
const Product = databases.data.Product; // Default database
80+
const Events = databases.analytics.Events; // Another database
81+
82+
// Create a new event record
83+
const event = await Events.create({ eventType: 'login', timestamp: Date.now() });
84+
85+
// Query events
86+
for await (const e of Events.search({ conditions: [{ attribute: 'eventType', value: 'login' }] })) {
87+
// Handle each event
88+
}
5089
```
5190

5291
## `Resource`

versioned_docs/version-4.5/reference/resource.md

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,65 @@ All Resource methods that are called from HTTP methods may directly return data
8585

8686
### `tables`
8787

88-
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.
88+
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.
89+
90+
**Schema Definition:**
91+
Tables are defined in your `schema.graphql` file using the `@table` directive. For example:
92+
93+
```graphql
94+
type Product @table {
95+
id: ID @primaryKey
96+
name: String
97+
price: Float
98+
}
99+
```
100+
101+
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).
102+
103+
#### Example
104+
105+
```js
106+
const Product = tables.Product; // Same as databases.data.Product
107+
108+
// Create a new record (`id` is automatically generated when using `.create()`)
109+
const created = await Product.create({ name: 'Shirt', price: 9.5 });
110+
111+
// Modify the record
112+
await Product.patch(created.id, { price: Math.round(created.price * 0.8 * 100) / 100 }); // 20% off!
113+
114+
// Retrieve by primary key
115+
const record = await Product.get(created.id);
116+
117+
logger.info('New price:', record.price);
118+
119+
// Query for all products with a `price` less than `8.00`
120+
const query = {
121+
conditions: [{ attribute: 'price', comparator: 'less_than', value: 8.0 }],
122+
};
123+
124+
for await (const record of Product.search(query)) {
125+
// ...
126+
}
127+
```
89128

90129
### `databases`
91130

92-
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.
131+
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.
132+
133+
#### Example
134+
135+
```js
136+
const Product = databases.data.Product; // Default database
137+
const Events = databases.analytics.Events; // Another database
138+
139+
// Create a new event record
140+
const event = await Events.create({ eventType: 'login', timestamp: Date.now() });
141+
142+
// Query events
143+
for await (const e of Events.search({ conditions: [{ attribute: 'eventType', value: 'login' }] })) {
144+
// Handle each event
145+
}
146+
```
93147

94148
### `Resource`
95149

versioned_docs/version-4.6/reference/globals.md

Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,25 +28,64 @@ The global variables include:
2828

2929
## `tables`
3030

31-
This is an object that holds all the tables for the default database (called `data`) as properties. Each of these property values is a table class that subclasses the Resource interface and provides access to the table through the Resource interface. For example, you can get a record from a table (in the default database) called 'my-table' with:
31+
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.
3232

33-
```javascript
34-
import { tables } from 'harperdb';
35-
const { MyTable } = tables;
36-
async function getRecord() {
37-
let record = await MyTable.get(recordId);
33+
**Schema Definition:**
34+
Tables are defined in your `schema.graphql` file using the `@table` directive. For example:
35+
36+
```graphql
37+
type Product @table {
38+
id: ID @primaryKey
39+
name: String
40+
price: Float
3841
}
3942
```
4043

41-
It is recommended that you [define a database](../developers/applications/defining-schemas) for all the tables that are required to exist in your application. This will ensure that the tables exist on the `tables` object. Also note that the property names follow a CamelCase convention for use in JavaScript and in the GraphQL Schemas, but these are translated to snake_case for the actual table names, and converted back to CamelCase when added to the `tables` object.
44+
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).
45+
46+
### Example
47+
48+
```js
49+
const Product = tables.Product; // Same as databases.data.Product
50+
51+
// Create a new record (`id` is automatically generated when using `.create()`)
52+
const created = await Product.create({ name: 'Shirt', price: 9.5 });
53+
54+
// Modify the record
55+
await Product.patch(created.id, { price: Math.round(created.price * 0.8 * 100) / 100 }); // 20% off!
56+
57+
// Retrieve by primary key
58+
const record = await Product.get(created.id);
59+
60+
logger.info('New price:', record.price);
61+
62+
// Query for all products with a `price` less than `8.00`
63+
const query = {
64+
conditions: [{ attribute: 'price', comparator: 'less_than', value: 8.0 }],
65+
};
66+
67+
for await (const record of Product.search(query)) {
68+
// ...
69+
}
70+
```
4271

4372
## `databases`
4473

45-
This is an object that holds all the databases in Harper, and can be used to explicitly access a table by database name. Each database will be a property on this object, each of these property values will be an object with the set of all tables in that database. The default database, `databases.data` should equal the `tables` export. For example, if you want to access the "dog" table in the "dev" database, you could do so:
74+
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.
4675

47-
```javascript
48-
import { databases } from 'harperdb';
49-
const { Dog } = databases.dev;
76+
### Example
77+
78+
```js
79+
const Product = databases.data.Product; // Default database
80+
const Events = databases.analytics.Events; // Another database
81+
82+
// Create a new event record
83+
const event = await Events.create({ eventType: 'login', timestamp: Date.now() });
84+
85+
// Query events
86+
for await (const e of Events.search({ conditions: [{ attribute: 'eventType', value: 'login' }] })) {
87+
// Handle each event
88+
}
5089
```
5190

5291
## `Resource`

0 commit comments

Comments
 (0)