Skip to content

Commit e717f75

Browse files
mpv1989pluma
authored andcommitted
Add DocumentSaveOptions (#546)
- Add type `DocumentSaveOptions` for `opts` in `DocumentCollection#save` - Add param `opts` of type `DocumentSaveOptions` in `EdgeCollection#save`
1 parent 97faea7 commit e717f75

File tree

4 files changed

+80
-6
lines changed

4 files changed

+80
-6
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
77

88
## [Unreleased]
99

10+
### Added
11+
12+
- Added type `DocumentSaveOptions` for `opts` in `DocumentCollection#save`
13+
14+
- Added param `opts` of type `DocumentSaveOptions` in `EdgeCollection#save`
15+
1016
## [6.3.0] - 2018-06-20
1117

1218
### Added

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,23 @@ the document's metadata.
7272
If set to `true`, return additionally the complete new documents under the
7373
attribute `new` in the result.
7474

75+
* **returnOld**: `boolean` (Default: `false)
76+
77+
If set to `true`, return additionally the complete old documents under the
78+
attribute `old` in the result.
79+
7580
* **silent**: `boolean` (Default: `false`)
7681

7782
If set to true, an empty object will be returned as response. No meta-data
7883
will be returned for the created document. This option can be used to save
7984
some network traffic.
8085

86+
* **overwrite**: `boolean` (Default: `false`)
87+
88+
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
90+
constraint violated but will replace the old document.
91+
8192
If a boolean is passed instead of an options object, it will be interpreted as
8293
the _returnNew_ option.
8394

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,39 @@ _data_ and returns an object containing the edge's metadata.
6262
document in the database, the `_key` of an edge in the collection, or a
6363
document (i.e. an object with an `_id` or `_key` property).
6464

65+
* **opts**: `Object` (optional)
66+
67+
If _opts_ is set, it must be an object with any of the following properties:
68+
69+
* **waitForSync**: `boolean` (Default: `false`)
70+
71+
Wait until document has been synced to disk.
72+
73+
* **returnNew**: `boolean` (Default: `false`)
74+
75+
If set to `true`, return additionally the complete new documents under the
76+
attribute `new` in the result.
77+
78+
* **returnOld**: `boolean` (Default: `false)
79+
80+
If set to `true`, return additionally the complete old documents under the
81+
attribute `old` in the result.
82+
83+
* **silent**: `boolean` (Default: `false`)
84+
85+
If set to true, an empty object will be returned as response. No meta-data
86+
will be returned for the created document. This option can be used to save
87+
some network traffic.
88+
89+
* **overwrite**: `boolean` (Default: `false`)
90+
91+
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
93+
constraint violated but will replace the old document.
94+
95+
If a boolean is passed instead of an options object, it will be interpreted as
96+
the _returnNew_ option.
97+
6598
**Examples**
6699

67100
```js

src/collection.ts

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,14 @@ export abstract class BaseCollection implements ArangoCollection {
634634
}
635635
}
636636

637+
export interface DocumentSaveOptions {
638+
waitForSync?: boolean;
639+
returnNew?: boolean;
640+
returnOld?: boolean;
641+
overwrite?: boolean;
642+
silent?: boolean;
643+
}
644+
637645
export class DocumentCollection extends BaseCollection {
638646
type = Types.DOCUMENT_COLLECTION;
639647
constructor(connection: Connection, name: string) {
@@ -651,7 +659,7 @@ export class DocumentCollection extends BaseCollection {
651659
);
652660
}
653661

654-
save(data: any, opts?: any) {
662+
save(data: any, opts?: DocumentSaveOptions | boolean) {
655663
if (typeof opts === "boolean") {
656664
opts = { returnNew: opts };
657665
}
@@ -704,12 +712,27 @@ export class EdgeCollection extends BaseCollection {
704712
);
705713
}
706714

707-
save(data: any): Promise<any>;
708-
save(data: any, fromId: DocumentHandle, toId: DocumentHandle): Promise<any>;
709-
save(data: any, fromId?: DocumentHandle, toId?: DocumentHandle) {
710-
if (fromId !== undefined) {
711-
data._from = this._documentHandle(fromId);
715+
save(data: any, opts?: DocumentSaveOptions | boolean): Promise<any>;
716+
save(
717+
data: any,
718+
fromId: DocumentHandle,
719+
toId: DocumentHandle,
720+
opts?: DocumentSaveOptions | boolean
721+
): Promise<any>;
722+
save(
723+
data: any,
724+
fromIdOrOpts?: DocumentHandle | DocumentSaveOptions | boolean,
725+
toId?: DocumentHandle,
726+
opts?: DocumentSaveOptions | boolean
727+
) {
728+
if (toId !== undefined) {
729+
data._from = this._documentHandle(fromIdOrOpts as DocumentHandle);
712730
data._to = this._documentHandle(toId!);
731+
} else if (fromIdOrOpts !== undefined) {
732+
opts = fromIdOrOpts as DocumentSaveOptions | boolean;
733+
}
734+
if (typeof opts === "boolean") {
735+
opts = { returnNew: opts };
713736
}
714737
if (this._connection.arangoMajor <= 2) {
715738
return this._connection.request(
@@ -718,6 +741,7 @@ export class EdgeCollection extends BaseCollection {
718741
path: "/_api/edge",
719742
body: data,
720743
qs: {
744+
...opts,
721745
collection: this.name,
722746
from: data._from,
723747
to: data._to

0 commit comments

Comments
 (0)