Skip to content

Commit 567ac76

Browse files
committed
Document handles can now be document objects.
1 parent 90bad53 commit 567ac76

File tree

2 files changed

+64
-18
lines changed

2 files changed

+64
-18
lines changed

README.md

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1413,10 +1413,29 @@ If *opts* is set, it must be an object with any of the following properties:
14131413
* if *policy* is set to `"last"`, the document will be replaced regardless of the revision.
14141414
* if *policy* is set to `"error"` or not set, the replacement will fail with an error.
14151415

1416-
The *documentHandle* can be either the `_id` or the `_key` of a document in the collection.
1416+
The *documentHandle* can be either the `_id` or the `_key` of a document in the collection, or a document (i.e. an object with an `_id` or `_key` property).
14171417

14181418
For more information on the *opts* object, see [the HTTP API documentation for working with documents](https://docs.arangodb.com/HttpDocument/WorkingWithDocuments.html).
14191419

1420+
*Examples*
1421+
1422+
```js
1423+
var db = require('arangojs')();
1424+
db.collection('some-collection', function (err, collection) {
1425+
if (err) return console.error(err);
1426+
collection.save({number: 1, hello: 'world'}, function (err, doc) {
1427+
if (err) return console.error(err);
1428+
collection.replace(doc, {number: 2}, function (err, doc2) {
1429+
if (err) return console.error(err);
1430+
doc2._id === doc._id;
1431+
doc2._rev !== doc._rev;
1432+
doc2.number === 2;
1433+
doc2.hello === undefined;
1434+
});
1435+
});
1436+
});
1437+
```
1438+
14201439
#### collection.update(documentHandle, data, [opts,] callback)
14211440

14221441
Updates (merges) the content of the document with the given *documentHandle* with the given *data*.
@@ -1431,10 +1450,29 @@ If *opts* is set, it must be an object with any of the following properties:
14311450
* if *policy* is set to `"last"`, the document will be replaced regardless of the revision.
14321451
* if *policy* is set to `"error"` or not set, the replacement will fail with an error.
14331452

1434-
The *documentHandle* can be either the `_id` or the `_key` of a document in the collection.
1453+
The *documentHandle* can be either the `_id` or the `_key` of a document in the collection, or a document (i.e. an object with an `_id` or `_key` property).
14351454

14361455
For more information on the *opts* object, see [the HTTP API documentation for working with documents](https://docs.arangodb.com/HttpDocument/WorkingWithDocuments.html).
14371456

1457+
*Examples*
1458+
1459+
```js
1460+
var db = require('arangojs')();
1461+
db.collection('some-collection', function (err, collection) {
1462+
if (err) return console.error(err);
1463+
collection.save({number: 1, hello: 'world'}, function (err, doc) {
1464+
if (err) return console.error(err);
1465+
collection.update(doc, {number: 2}, function (err, doc2) {
1466+
if (err) return console.error(err);
1467+
doc2._id === doc._id;
1468+
doc2._rev !== doc._rev;
1469+
doc2.number === 2;
1470+
doc2.hello === doc.hello;
1471+
});
1472+
});
1473+
});
1474+
```
1475+
14381476
#### collection.remove(documentHandle, [opts,] callback)
14391477

14401478
Deletes the document with the given *documentHandle* from the collection.
@@ -1447,7 +1485,7 @@ If *opts* is set, it must be an object with any of the following properties:
14471485
* if *policy* is set to `"last"`, the document will be replaced regardless of the revision.
14481486
* if *policy* is set to `"error"` or not set, the replacement will fail with an error.
14491487

1450-
The *documentHandle* can be either the `_id` or the `_key` of a document in the collection.
1488+
The *documentHandle* can be either the `_id` or the `_key` of a document in the collection, or a document (i.e. an object with an `_id` or `_key` property).
14511489

14521490
For more information on the *opts* object, see [the HTTP API documentation for working with documents](https://docs.arangodb.com/HttpDocument/WorkingWithDocuments.html).
14531491

@@ -1487,7 +1525,7 @@ The *DocumentCollection API* extends the [*Collection API* (see above)](#collect
14871525

14881526
Retrieves the document with the given *documentHandle* from the collection.
14891527

1490-
The *documentHandle* can be either the `_id` or the `_key` of a document in the collection.
1528+
The *documentHandle* can be either the `_id` or the `_key` of a document in the collection, or a document (i.e. an object with an `_id` or `_key` property).
14911529

14921530
*Examples*
14931531

@@ -1542,7 +1580,7 @@ The *EdgeCollection API* extends the [*Collection API* (see above)](#collection-
15421580

15431581
Retrieves the edge with the given *documentHandle* from the collection.
15441582

1545-
The *documentHandle* can be either the `_id` or the `_key` of an edge in the collection.
1583+
The *documentHandle* can be either the `_id` or the `_key` of an edge in the collection, or an edge (i.e. an object with an `_id` or `_key` property).
15461584

15471585
*Examples*
15481586

@@ -1598,6 +1636,8 @@ db.createEdgeCollection('my-edges', function (err, collection) {
15981636

15991637
Retrieves a list of all edges of the document with the given *documentHandle*.
16001638

1639+
The *documentHandle* can be either the `_id` or the `_key` of a document in any collection, or a document (i.e. an object with an `_id` or `_key` property).
1640+
16011641
*Examples*
16021642

16031643
```js
@@ -1625,6 +1665,8 @@ db.createEdgeCollection('my-edges', function (err, collection) {
16251665

16261666
Retrieves a list of all incoming edges of the document with the given *documentHandle*.
16271667

1668+
The *documentHandle* can be either the `_id` or the `_key` of a document in any collection, or a document (i.e. an object with an `_id` or `_key` property).
1669+
16281670
*Examples*
16291671

16301672
```js
@@ -1652,6 +1694,8 @@ db.createEdgeCollection('my-edges', function (err, collection) {
16521694

16531695
Retrieves a list of all outgoing edges of the document with the given *documentHandle*.
16541696

1697+
The *documentHandle* can be either the `_id` or the `_key` of a document in any collection, or a document (i.e. an object with an `_id` or `_key` property).
1698+
16551699
*Examples*
16561700

16571701
```js
@@ -1771,7 +1815,7 @@ The *GraphVertexCollection API* extends the [*Collection API* (see above)](#coll
17711815

17721816
Retrieves the vertex with the given *documentHandle* from the collection.
17731817

1774-
The *documentHandle* can be either the `_id` or the `_key` of a vertex in the collection.
1818+
The *documentHandle* can be either the `_id` or the `_key` of a vertex in the collection, or a vertex (i.e. an object with an `_id` or `_key` property).
17751819

17761820
#### graphVertexCollection.save(data, callback)
17771821

@@ -1785,7 +1829,7 @@ The *GraphEdgeCollection API* extends the *Collection API* (see above) with the
17851829

17861830
Retrieves the edge with the given *documentHandle* from the collection.
17871831

1788-
The *documentHandle* can be either the `_id` or the `_key` of an edge in the collection.
1832+
The *documentHandle* can be either the `_id` or the `_key` of an edge in the collection, or an edge (i.e. an object with an `_id` or `_key` property).
17891833

17901834
#### graphEdgeCollection.save(data, fromId, toId, callback)
17911835

lib/collection.js

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,18 @@ function BaseCollection(connection, body) {
2626

2727
extend(BaseCollection.prototype, {
2828
_documentPath: function (documentHandle) {
29+
return (this.type === 3 ? 'edge/' : 'document/') + this._documentHandle(documentHandle);
30+
},
31+
_documentHandle: function (documentHandle) {
32+
if (documentHandle._id) {
33+
documentHandle = documentHandle._id;
34+
} else if (documentHandle._key) {
35+
documentHandle = documentHandle._key;
36+
}
2937
if (documentHandle.indexOf('/') === -1) {
3038
documentHandle = this.name + '/' + documentHandle;
3139
}
32-
return (this.type === 3 ? 'edge/' : 'document/') + documentHandle;
40+
return documentHandle;
3341
},
3442
_get: function (path, update, opts, callback) {
3543
if (typeof opts === 'function') {
@@ -186,10 +194,7 @@ inherits(DocumentCollection, BaseCollection);
186194
extend(DocumentCollection.prototype, {
187195
document: function (documentHandle, callback) {
188196
if (!callback) callback = noop;
189-
if (documentHandle.indexOf('/') === -1) {
190-
documentHandle = this.name + '/' + documentHandle;
191-
}
192-
this._api.get('document/' + documentHandle, function (err, body) {
197+
this._api.get('document/' + this._documentHandle(documentHandle), function (err, body) {
193198
if (err) callback(err);
194199
else callback(null, body);
195200
});
@@ -214,10 +219,7 @@ inherits(EdgeCollection, BaseCollection);
214219
extend(EdgeCollection.prototype, {
215220
edge: function (documentHandle, callback) {
216221
if (!callback) callback = noop;
217-
if (documentHandle.indexOf('/') === -1) {
218-
documentHandle = this.name + '/' + documentHandle;
219-
}
220-
this._api.get('edge/' + documentHandle, function (err, body) {
222+
this._api.get('edge/' + this._documentHandle(documentHandle), function (err, body) {
221223
if (err) callback(err);
222224
else callback(null, body);
223225
});
@@ -233,10 +235,10 @@ extend(EdgeCollection.prototype, {
233235
else callback(null, body);
234236
});
235237
},
236-
_edges: function (vertex, direction, callback) {
238+
_edges: function (documentHandle, direction, callback) {
237239
if (!callback) callback = noop;
238240
this._api.get('edges/' + this.name, {
239-
vertex: vertex,
241+
vertex: this._documentHandle(documentHandle),
240242
direction: direction
241243
}, function (err, body) {
242244
if (err) callback(err);

0 commit comments

Comments
 (0)