Skip to content

Commit 340a9d3

Browse files
committed
Documentation fixes
1 parent e323358 commit 340a9d3

File tree

4 files changed

+85
-85
lines changed

4 files changed

+85
-85
lines changed

docs/developers/applications/README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -321,12 +321,13 @@ export class CustomDog extends Dog {
321321
async post(target, data) {
322322
if (data.action === 'add-trick') {
323323
const context = this.getContext();
324+
// if we want to skip the default permission checks, we can turn off checkPermissions:
325+
target.checkPermissions = false;
326+
const record = this.update(target);
327+
// and do our own/custom permission check:
324328
if (record.owner !== context.user?.username) {
325329
throw new Error('Can not update this record');
326330
}
327-
// if we now want to skip the default permission checks, we can turn that off:
328-
target.checkPermissions = false;
329-
const record = this.update(target);
330331
record.tricks.push(data.trick);
331332
}
332333
}

docs/technical-details/reference/resource-instance-binding.md

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
# Resource Class with Resource Instance Binding behavior
22

3-
This document describes the legacy behavior of the Resource class using the instance binding behavior. It is recommended that [updated behavior](./resource.md) instead, but this legacy API is preserved for backwards compatibility.
3+
This document describes the legacy behavior of the Resource class using the instance binding behavior. It is recommended that you use the [updated behavior of the Resource API](./resource.md) instead, but this legacy API is preserved for backwards compatibility.
44
## Resource Class
55

66
```javascript
77
export class MyExternalData extends Resource {
88
static loadAsInstance = true;
9-
async get() {
10-
// fetch data from an external source, using our id
11-
let response = await this.fetch(this.id);
12-
// do something with the response
13-
}
14-
put(data) {
9+
async get() {
10+
// fetch data from an external source, using our id
11+
let response = await this.fetch(this.id);
12+
// do something with the response
13+
}
14+
put(data) {
1515
// send the data into the external source
1616
}
1717
delete() {
@@ -138,8 +138,8 @@ The `query` argument is used to represent any additional query parameters that w
138138
139139
```javascript
140140
put(data, query) {
141-
let param1 = query?.get?.('param1'); // returns 'value'
142-
...
141+
let param1 = query?.get?.('param1'); // returns 'value'
142+
...
143143
}
144144
```
145145
@@ -340,18 +340,18 @@ This will define the function to use for a computed attribute. To use this, the
340340
341341
```javascript
342342
MyTable.setComputedAttribute('computedAttribute', (record) => {
343-
return record.attribute1 + record.attribute2;
343+
return record.attribute1 + record.attribute2;
344344
});
345345
```
346346
347347
For a schema like:
348348
349349
```graphql
350350
type MyTable @table {
351-
id: ID @primaryKey
352-
attribute1: Int
353-
attribute2: Int
354-
computedAttribute: Int @computed
351+
id: ID @primaryKey
352+
attribute1: Int
353+
attribute2: Int
354+
computedAttribute: Int @computed
355355
}
356356
```
357357
@@ -382,7 +382,7 @@ If the source resource implements subscription support, real-time invalidation c
382382
This property can be set to force the direct URL request target to be mapped to the resource primary key. Normally, URL resource targets are parsed, where the path is mapped to the primary key of the resource (and decoded using standard URL decoding), and any query string parameters are used to query that resource. But if this is turned on, the full URL is used as the primary key. For example:
383383
```javascript
384384
export class MyTable extends tables.MyTable {
385-
static directURLMapping = true;
385+
static directURLMapping = true;
386386
}
387387
```
388388
```http request

docs/technical-details/reference/resource-migration.md

Lines changed: 53 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ The updated Resource API is enabled on a per-class basis, by setting static `loa
66
* The `get` method (both static and instance) will directly return the record, a frozen enumerable object with direct properties, instead of a Resource instance.
77
* When instance methods are called, there will not be any record preloaded beforehand. And the resource instance will not have properties mapped to a record.
88
* All instance methods accept a `target`, an instance of `RequestTarget`, as the first argument, which identifies the target record or query.
9-
* The `target` will have an `id` property identifying the target resource, along with target information. The `getId()` method will return `undefined`.
10-
* The `target` will provide access to query parameters, search operators, and other directives.
11-
* A `target` property of `checkPermission` indicates that a method should check the permission before of request before proceeding. The default instance methods provide the default authorization behavior.
12-
* This supplants the need for `allowRead`, `allowUpdate`, `allowCreate`, and `allowDelete` methods, which shouldn't need to be used (and don't provide the id of the target record).
9+
* The `target` will have an `id` property identifying the target resource, along with target information. The `getId()` method will return `undefined`.
10+
* The `target` will provide access to query parameters, search operators, and other directives.
11+
* A `target` property of `checkPermission` indicates that a method should check the permission before of request before proceeding. The default instance methods provide the default authorization behavior.
12+
* This supplants the need for `allowRead`, `allowUpdate`, `allowCreate`, and `allowDelete` methods, which shouldn't need to be used (and don't provide the id of the target record).
1313
* Any data from a POST, PUT, and PATCH request will be available in the second argument. This reverses the order of the arguments to `put`, `post`, and `patch` compared to the legacy Resource API.
1414
* Context is tracked using asynchronous context tracking, and will automatically be available to calls to other resources. This can be disabled by setting `static explicitContext = true`, which can improve performance.
1515
* The `update` method will return an `Updatable` object (instead of a Resource instance), which provides properties mapped to a record, but these properties can be updated and changes will be saved when the transaction is committed.
@@ -18,37 +18,37 @@ Here are examples of how to convert/upgrade to the non-instance binding Resource
1818
Previous code with a `get` method:
1919
```javascript
2020
export class MyData extends tables.MyData {
21-
async get(query) {
22-
let id = this.getId(); // get the id
23-
if (query?.size > 0) { // check number of query parameters
24-
let idWithQuery = id + query.toString(); // add query parameters
25-
let resource = await tables.MyData.get(idWithQuery, this); // retrieve another record
26-
resource.newProperty = 'value'; // assign a new value to the returned resource instance
27-
return resource;
28-
} else {
29-
this.newProperty = 'value'; // assign a new value to this instance
30-
return super.get(query);
31-
}
32-
}
21+
async get(query) {
22+
let id = this.getId(); // get the id
23+
if (query?.size > 0) { // check number of query parameters
24+
let idWithQuery = id + query.toString(); // add query parameters
25+
let resource = await tables.MyData.get(idWithQuery, this); // retrieve another record
26+
resource.newProperty = 'value'; // assign a new value to the returned resource instance
27+
return resource;
28+
} else {
29+
this.newProperty = 'value'; // assign a new value to this instance
30+
return super.get(query);
31+
}
32+
}
3333
}
3434
```
3535
Updated code:
3636
```javascript
3737
export class MyData extends tables.MyData {
38-
static loadAsInstance = false; // opt in to updated behavior
39-
async get(target) {
40-
let id = target.id; // get the id
41-
let record;
42-
if (target.size > 0) { // check number of query parameters
43-
let idWithQuery = target.toString(); // this is the full target with the path query parameters
44-
// we can retrieve another record from this table directly with this.get/super.get or with tables.MyData.get
45-
record = await super.get(idWithQuery);
46-
} else {
47-
record = await super.get(target); // we can just directly use the target as well
48-
}
49-
// the record itself is frozen, but we can copy/assign to a new object with additional properties if we want
50-
return { ...record, newProperty: 'value' };
51-
}
38+
static loadAsInstance = false; // opt in to updated behavior
39+
async get(target) {
40+
let id = target.id; // get the id
41+
let record;
42+
if (target.size > 0) { // check number of query parameters
43+
let idWithQuery = target.toString(); // this is the full target with the path query parameters
44+
// we can retrieve another record from this table directly with this.get/super.get or with tables.MyData.get
45+
record = await super.get(idWithQuery);
46+
} else {
47+
record = await super.get(target); // we can just directly use the target as well
48+
}
49+
// the record itself is frozen, but we can copy/assign to a new object with additional properties if we want
50+
return { ...record, newProperty: 'value' };
51+
}
5252
}
5353
```
5454
Here is an example of the preferred approach for authorization:
@@ -83,34 +83,34 @@ Here is an example of how to convert/upgrade an implementation of a `post` metho
8383
Previous code with a `post` method:
8484
```javascript
8585
export class MyData extends tables.MyData {
86-
async post(data, query) {
87-
let resource = await tables.MyData.get(data.id, this);
88-
if (resource) { // update a property
89-
resource.someProperty = 'value';
90-
// or
91-
tables.MyData.patch(data.id, { someProperty: 'value' }, this);
92-
} else { // create a new record
93-
MyData.create(data, this);
94-
}
95-
}
86+
async post(data, query) {
87+
let resource = await tables.MyData.get(data.id, this);
88+
if (resource) { // update a property
89+
resource.someProperty = 'value';
90+
// or
91+
tables.MyData.patch(data.id, { someProperty: 'value' }, this);
92+
} else { // create a new record
93+
MyData.create(data, this);
94+
}
95+
}
9696
}
9797

9898
```
9999
Updated code:
100100
```javascript
101101
export class MyData extends tables.MyData {
102-
static loadAsInstance = false; // opt in to updated behavior
103-
// IMPORTANT: arguments are reversed:
104-
async post(target, data) {
105-
let record = await this.get(data.id);
106-
if (record) { // update a property
107-
const updatable = await this.update(data.id); // we can alternately pass a target to update
108-
updatable.someProperty = 'value';
109-
// or
110-
this.patch(data.id, { someProperty: 'value' }, this);
111-
} else { // create a new record
112-
MyData.create(data, this);
113-
}
114-
}
102+
static loadAsInstance = false; // opt in to updated behavior
103+
// IMPORTANT: arguments are reversed:
104+
async post(target, data) {
105+
let record = await this.get(data.id);
106+
if (record) { // update a property
107+
const updatable = await this.update(data.id); // we can alternately pass a target to update
108+
updatable.someProperty = 'value';
109+
// or
110+
this.patch(data.id, { someProperty: 'value' });
111+
} else { // create a new record
112+
this.create(data);
113+
}
114+
}
115115
}
116116
```

docs/technical-details/reference/resource.md

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ You can create classes that extend `Resource` to define your own data sources, t
3232
```javascript
3333
export class MyExternalData extends Resource {
3434
static loadAsInstance = false; // enable the updated API
35-
async get(target) {
36-
// fetch data from an external source, using our id
37-
let response = await this.fetch(target.id);
38-
// do something with the response
39-
}
40-
put(target, data) {
35+
async get(target) {
36+
// fetch data from an external source, using our id
37+
let response = await this.fetch(target.id);
38+
// do something with the response
39+
}
40+
put(target, data) {
4141
// send the data into the external source
4242
}
4343
delete(target) {
@@ -136,7 +136,6 @@ The `target` object represents the target of a request and can be used to access
136136
class extends Resource {
137137
static loadAsInstance = false;
138138
get(target) {
139-
// note that query will only exist (as an object) if there is a query string
140139
let param1 = target.get('param1'); // returns 'value'
141140
let id = target.id; // returns 'some-id'
142141
let path = target.pathname; // returns /some-id
@@ -351,18 +350,18 @@ This will define the function to use for a computed attribute. To use this, the
351350
352351
```javascript
353352
MyTable.setComputedAttribute('computedAttribute', (record) => {
354-
return record.attribute1 + record.attribute2;
353+
return record.attribute1 + record.attribute2;
355354
});
356355
```
357356
358357
For a schema like:
359358
360359
```graphql
361360
type MyTable @table {
362-
id: ID @primaryKey
363-
attribute1: Int
364-
attribute2: Int
365-
computedAttribute: Int @computed
361+
id: ID @primaryKey
362+
attribute1: Int
363+
attribute2: Int
364+
computedAttribute: Int @computed
366365
}
367366
```
368367
@@ -393,7 +392,7 @@ If the source resource implements subscription support, real-time invalidation c
393392
This property can be set to force the direct URL request target to be mapped to the resource primary key. Normally, URL resource targets are parsed, where the path is mapped to the primary key of the resource (and decoded using standard URL decoding), and any query string parameters are used to query that resource. But if this is turned on, the full URL is used as the primary key. For example:
394393
```javascript
395394
export class MyTable extends tables.MyTable {
396-
static directURLMapping = true;
395+
static directURLMapping = true;
397396
}
398397
```
399398
```http request
@@ -646,7 +645,7 @@ export class CustomProduct extends Product {
646645
post(target, data) {
647646
let record = this.update(target);
648647
record.name = data.name;
649-
record.description = data.description;
648+
record.description = data.description;
650649
// both of these changes will be saved automatically as this transaction commits
651650
}
652651
}

0 commit comments

Comments
 (0)