Skip to content

Commit 77de284

Browse files
authored
Implement graceful document fetching (#557)
* Implement graceful document fetching * Add to CHANGELOG * Add document aliases * Fold _documentPath definition into Base * Add documentExists method
1 parent 017dfc3 commit 77de284

15 files changed

+519
-215
lines changed

CHANGELOG.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,26 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1414

1515
This behavior can be controlled using the `maxRetries` option.
1616

17+
- Renamed `EdgeCollection#edge` to `EdgeCollection#document`
18+
19+
`EdgeCollection#edge` is now an alias for the `document` method.
20+
21+
- Renamed `GraphEdgeCollection#edge` to `GraphEdgeCollection#document`
22+
23+
`GraphEdgeCollection#edge` is now an alias for the `document` method.
24+
25+
- Renamed `GraphVertexCollection#vertex` to `GraphVertexCollection#document`
26+
27+
`GraphVertexCollection#vertex` is now an alias for the `document` method.
28+
1729
### Added
1830

1931
- Added `maxRetries` option to configuration to control retry behavior
2032

33+
- Added `collection.documentExists` method
34+
35+
- Added `graceful` option to `collection.document`
36+
2137
## [6.4.0] - 2018-07-06
2238

2339
### Changed

docs/Drivers/JS/Reference/Collection/DocumentCollection.md

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,23 @@ The _DocumentCollection API_ extends the
55

66
## documentCollection.document
77

8-
`async documentCollection.document(documentHandle): Object`
8+
`async documentCollection.document(documentHandle, [graceful]): Object`
99

1010
Retrieves the document with the given _documentHandle_ from the collection.
1111

1212
**Arguments**
1313

14-
* **documentHandle**: `string`
14+
- **documentHandle**: `string`
1515

1616
The handle of the document to retrieve. This can be either the `_id` or the
1717
`_key` of a document in the collection, or a document (i.e. an object with an
1818
`_id` or `_key` property).
1919

20+
- **graceful**: `boolean` (Default: `false`)
21+
22+
If set to `true`, the method will return `null` instead of throwing an error
23+
if the document does not exist.
24+
2025
**Examples**
2126

2227
```js
@@ -44,6 +49,39 @@ try {
4449
// something went wrong or
4550
// the document does not exist
4651
}
52+
53+
// -- or --
54+
55+
const doc = await collection.document('some-key', true);
56+
if (doc === null) {
57+
// the document does not exist
58+
}
59+
```
60+
61+
## documentCollection.documentExists
62+
63+
`async documentCollection.documentExists(documentHandle): boolean`
64+
65+
Checks whether the document with the given _documentHandle_ exists.
66+
67+
**Arguments**
68+
69+
- **documentHandle**: `string`
70+
71+
The handle of the document to retrieve. This can be either the `_id` or the
72+
`_key` of a document in the collection, or a document (i.e. an object with an
73+
`_id` or `_key` property).
74+
75+
**Examples**
76+
77+
```js
78+
const db = new Database();
79+
const collection = db.collection('my-docs');
80+
81+
const exists = await collection.documentExists('some-key');
82+
if (exists === false) {
83+
// the document does not exist
84+
}
4785
```
4886

4987
## documentCollection.save
@@ -55,38 +93,38 @@ the document's metadata.
5593

5694
**Arguments**
5795

58-
* **data**: `Object`
96+
- **data**: `Object`
5997

6098
The data of the new document, may include a `_key`.
6199

62-
* **opts**: `Object` (optional)
100+
- **opts**: `Object` (optional)
63101

64102
If _opts_ is set, it must be an object with any of the following properties:
65103

66-
* **waitForSync**: `boolean` (Default: `false`)
104+
- **waitForSync**: `boolean` (Default: `false`)
67105

68106
Wait until document has been synced to disk.
69107

70-
* **returnNew**: `boolean` (Default: `false`)
108+
- **returnNew**: `boolean` (Default: `false`)
71109

72110
If set to `true`, return additionally the complete new documents under the
73111
attribute `new` in the result.
74112

75-
* **returnOld**: `boolean` (Default: `false`)
113+
- **returnOld**: `boolean` (Default: `false`)
76114

77115
If set to `true`, return additionally the complete old documents under the
78116
attribute `old` in the result.
79117

80-
* **silent**: `boolean` (Default: `false`)
118+
- **silent**: `boolean` (Default: `false`)
81119

82120
If set to true, an empty object will be returned as response. No meta-data
83121
will be returned for the created document. This option can be used to save
84122
some network traffic.
85123

86-
* **overwrite**: `boolean` (Default: `false`)
124+
- **overwrite**: `boolean` (Default: `false`)
87125

88126
If set to true, the insert becomes a replace-insert. If a document with the
89-
same _key already exists the new document is not rejected with unique
127+
same \_key already exists the new document is not rejected with unique
90128
constraint violated but will replace the old document.
91129

92130
If a boolean is passed instead of an options object, it will be interpreted as

docs/Drivers/JS/Reference/Collection/EdgeCollection.md

Lines changed: 60 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,77 @@
33
The _EdgeCollection API_ extends the
44
[_Collection API_](README.md) with the following methods.
55

6-
## edgeCollection.edge
6+
## edgeCollection.document
77

8-
`async edgeCollection.edge(documentHandle): Object`
8+
`async edgeCollection.document(documentHandle, [graceful]): Object`
9+
10+
Alias: `edgeCollection.edge`.
911

1012
Retrieves the edge with the given _documentHandle_ from the collection.
1113

1214
**Arguments**
1315

14-
* **documentHandle**: `string`
16+
- **documentHandle**: `string`
1517

1618
The handle of the edge to retrieve. This can be either the `_id` or the `_key`
1719
of an edge in the collection, or an edge (i.e. an object with an `_id` or
1820
`_key` property).
1921

22+
* **graceful**: `boolean` (Default: `false`)
23+
24+
If set to `true`, the method will return `null` instead of throwing an error
25+
if the edge does not exist.
26+
2027
**Examples**
2128

2229
```js
2330
const db = new Database();
2431
const collection = db.edgeCollection('edges');
2532

26-
const edge = await collection.edge('some-key');
33+
const edge = await collection.document('some-key');
2734
// the edge exists
2835
assert.equal(edge._key, 'some-key');
2936
assert.equal(edge._id, 'edges/some-key');
3037

3138
// -- or --
3239

33-
const edge = await collection.edge('edges/some-key');
40+
const edge = await collection.document('edges/some-key');
3441
// the edge exists
3542
assert.equal(edge._key, 'some-key');
3643
assert.equal(edge._id, 'edges/some-key');
44+
45+
// -- or --
46+
47+
const edge = await collection.document('some-key', true);
48+
if (edge === null) {
49+
// the edge does not exist
50+
}
51+
```
52+
53+
## edgeCollection.documentExists
54+
55+
`async edgeCollection.documentExists(documentHandle): boolean`
56+
57+
Checks whether the edge with the given _documentHandle_ exists.
58+
59+
**Arguments**
60+
61+
- **documentHandle**: `string`
62+
63+
The handle of the edge to retrieve. This can be either the `_id` or the
64+
`_key` of a edge in the collection, or an edge (i.e. an object with an
65+
`_id` or `_key` property).
66+
67+
**Examples**
68+
69+
```js
70+
const db = new Database();
71+
const collection = db.edgeCollection('my-docs');
72+
73+
const exists = await collection.documentExists('some-key');
74+
if (exists === false) {
75+
// the edge does not exist
76+
}
3777
```
3878

3979
## edgeCollection.save
@@ -45,51 +85,51 @@ _data_ and returns an object containing the edge's metadata.
4585

4686
**Arguments**
4787

48-
* **data**: `Object`
88+
- **data**: `Object`
4989

5090
The data of the new edge. If _fromId_ and _toId_ are not specified, the _data_
5191
needs to contain the properties `_from` and `_to`.
5292

53-
* **fromId**: `string` (optional)
93+
- **fromId**: `string` (optional)
5494

5595
The handle of the start vertex of this edge. This can be either the `_id` of a
5696
document in the database, the `_key` of an edge in the collection, or a
5797
document (i.e. an object with an `_id` or `_key` property).
5898

59-
* **toId**: `string` (optional)
99+
- **toId**: `string` (optional)
60100

61101
The handle of the end vertex of this edge. This can be either the `_id` of a
62102
document in the database, the `_key` of an edge in the collection, or a
63103
document (i.e. an object with an `_id` or `_key` property).
64104

65-
* **opts**: `Object` (optional)
105+
- **opts**: `Object` (optional)
66106

67107
If _opts_ is set, it must be an object with any of the following properties:
68108

69-
* **waitForSync**: `boolean` (Default: `false`)
109+
- **waitForSync**: `boolean` (Default: `false`)
70110

71111
Wait until document has been synced to disk.
72112

73-
* **returnNew**: `boolean` (Default: `false`)
113+
- **returnNew**: `boolean` (Default: `false`)
74114

75115
If set to `true`, return additionally the complete new documents under the
76116
attribute `new` in the result.
77117

78-
* **returnOld**: `boolean` (Default: `false`)
118+
- **returnOld**: `boolean` (Default: `false`)
79119

80120
If set to `true`, return additionally the complete old documents under the
81121
attribute `old` in the result.
82122

83-
* **silent**: `boolean` (Default: `false`)
123+
- **silent**: `boolean` (Default: `false`)
84124

85125
If set to true, an empty object will be returned as response. No meta-data
86126
will be returned for the created document. This option can be used to save
87127
some network traffic.
88128

89-
* **overwrite**: `boolean` (Default: `false`)
129+
- **overwrite**: `boolean` (Default: `false`)
90130

91131
If set to true, the insert becomes a replace-insert. If a document with the
92-
same _key already exists the new document is not rejected with unique
132+
same \_key already exists the new document is not rejected with unique
93133
constraint violated but will replace the old document.
94134

95135
If a boolean is passed instead of an options object, it will be interpreted as
@@ -133,7 +173,7 @@ Retrieves a list of all edges of the document with the given _documentHandle_.
133173

134174
**Arguments**
135175

136-
* **documentHandle**: `string`
176+
- **documentHandle**: `string`
137177

138178
The handle of the document to retrieve the edges of. This can be either the
139179
`_id` of a document in the database, the `_key` of an edge in the collection,
@@ -164,7 +204,7 @@ _documentHandle_.
164204

165205
**Arguments**
166206

167-
* **documentHandle**: `string`
207+
- **documentHandle**: `string`
168208

169209
The handle of the document to retrieve the edges of. This can be either the
170210
`_id` of a document in the database, the `_key` of an edge in the collection,
@@ -195,7 +235,7 @@ _documentHandle_.
195235

196236
**Arguments**
197237

198-
* **documentHandle**: `string`
238+
- **documentHandle**: `string`
199239

200240
The handle of the document to retrieve the edges of. This can be either the
201241
`_id` of a document in the database, the `_key` of an edge in the collection,
@@ -226,13 +266,13 @@ contained in this edge collection.
226266

227267
**Arguments**
228268

229-
* **startVertex**: `string`
269+
- **startVertex**: `string`
230270

231271
The handle of the start vertex. This can be either the `_id` of a document in
232272
the database, the `_key` of an edge in the collection, or a document (i.e. an
233273
object with an `_id` or `_key` property).
234274

235-
* **opts**: `Object`
275+
- **opts**: `Object`
236276

237277
See
238278
[the HTTP API documentation](https://docs.arangodb.com/latest/HTTP/Traversal/index.html)

0 commit comments

Comments
 (0)