Skip to content

Commit 182c11a

Browse files
committed
Add database/collection/graph#exists
1 parent f849d9c commit 182c11a

File tree

6 files changed

+92
-15
lines changed

6 files changed

+92
-15
lines changed

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,21 @@ See
1616
[the HTTP API documentation](https://docs.arangodb.com/latest/HTTP/Collection/Getting.html)
1717
for details.
1818

19+
## collection.exists
20+
21+
`async collection.exists(): boolean`
22+
23+
Checks whether the collection exists.
24+
25+
**Examples**
26+
27+
```js
28+
const db = new Database();
29+
const collection = db.collection('some-collection');
30+
const result = await collection.exists();
31+
// result indicates whether the collection exists
32+
```
33+
1934
### collection.get
2035

2136
`async collection.get(): Object`
@@ -99,7 +114,7 @@ Retrieves the collection checksum.
99114

100115
**Arguments**
101116

102-
* **opts**: `Object` (optional)
117+
- **opts**: `Object` (optional)
103118

104119
For information on the possible options see
105120
[the HTTP API for getting collection information](https://docs.arangodb.com/latest/HTTP/Collection/Getting.html).

docs/Drivers/JS/Reference/Database/DatabaseManipulation.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,20 @@ const info = await db.createDatabase('mydb', [{username: 'root'}]);
166166
// the database has been created
167167
```
168168

169+
## database.exists
170+
171+
`async database.exists(): boolean`
172+
173+
Checks whether the database exists.
174+
175+
**Examples**
176+
177+
```js
178+
const db = new Database();
179+
const result = await db.exists();
180+
// result indicates whether the database exists
181+
```
182+
169183
## database.get
170184

171185
`async database.get(): Object`

docs/Drivers/JS/Reference/Graph/README.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,21 @@
33
These functions implement the
44
[HTTP API for manipulating graphs](https://docs.arangodb.com/latest/HTTP/Gharial/index.html).
55

6+
## graph.exists
7+
8+
`async graph.exists(): boolean`
9+
10+
Checks whether the graph exists.
11+
12+
**Examples**
13+
14+
```js
15+
const db = new Database();
16+
const graph = db.graph('some-graph');
17+
const result = await graph.exists();
18+
// result indicates whether the graph exists
19+
```
20+
621
## graph.get
722

823
`async graph.get(): Object`
@@ -27,7 +42,7 @@ the server response.
2742

2843
**Arguments**
2944

30-
* **properties**: `Object`
45+
- **properties**: `Object`
3146

3247
For more information on the _properties_ object, see
3348
[the HTTP API documentation for creating graphs](https://docs.arangodb.com/latest/HTTP/Gharial/Management.html).
@@ -55,7 +70,7 @@ Deletes the graph from the database.
5570

5671
**Arguments**
5772

58-
* **dropCollections**: `boolean` (optional)
73+
- **dropCollections**: `boolean` (optional)
5974

6075
If set to `true`, the collections associated with the graph will also be
6176
deleted.

src/collection.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { Connection } from "./connection";
22
import { ArrayCursor } from "./cursor";
3-
import { ArangoError } from "./error";
43

54
export enum Types {
65
DOCUMENT_COLLECTION = 2,
@@ -31,6 +30,7 @@ export interface ArangoCollection {
3130
name: string;
3231
}
3332

33+
const COLLECTION_NOT_FOUND = 1203;
3434
export abstract class BaseCollection implements ArangoCollection {
3535
isArangoCollection: true = true;
3636
name: string;
@@ -108,12 +108,15 @@ export abstract class BaseCollection implements ArangoCollection {
108108
}
109109

110110
exists(): Promise<boolean> {
111-
return this._connection
112-
.request({ path: `/_api/collection/${this.name}` }, () => true)
113-
.catch((e: ArangoError) => {
114-
if (e.errorNum !== 1203) throw e;
111+
return this.get().then(
112+
() => true,
113+
err => {
114+
if (err.errorNum !== COLLECTION_NOT_FOUND) {
115+
throw err;
116+
}
115117
return false;
116-
});
118+
}
119+
);
117120
}
118121

119122
create(properties?: any) {

src/database.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
import { AqlLiteral, AqlQuery, isAqlLiteral, isAqlQuery } from "./aql-query";
2-
import { ArangoCollection, constructCollection, DocumentCollection, EdgeCollection, isArangoCollection } from "./collection";
2+
import {
3+
ArangoCollection,
4+
constructCollection,
5+
DocumentCollection,
6+
EdgeCollection,
7+
isArangoCollection
8+
} from "./collection";
39
import { Config, Connection } from "./connection";
410
import { ArrayCursor } from "./cursor";
511
import { Graph } from "./graph";
612
import { Route } from "./route";
713
import { btoa } from "./util/btoa";
814
import { toForm } from "./util/multipart";
915

10-
1116
function colToString(collection: string | ArangoCollection): string {
1217
if (isArangoCollection(collection)) {
1318
return String(collection.name);
@@ -37,6 +42,7 @@ export type ServiceOptions = {
3742
dependencies?: { [key: string]: any };
3843
};
3944

45+
const DATABASE_NOT_FOUND = 1228;
4046
export class Database {
4147
private _connection: Connection;
4248

@@ -91,6 +97,18 @@ export class Database {
9197
);
9298
}
9399

100+
exists(): Promise<boolean> {
101+
return this.get().then(
102+
() => true,
103+
err => {
104+
if (err.errorNum !== DATABASE_NOT_FOUND) {
105+
throw err;
106+
}
107+
return false;
108+
}
109+
);
110+
}
111+
94112
createDatabase(databaseName: string, users?: string[]) {
95113
return this._connection.request(
96114
{
@@ -687,7 +705,7 @@ export class Database {
687705
);
688706
}
689707

690-
login(username: string = 'root', password: string = ''): Promise<string> {
708+
login(username: string = "root", password: string = ""): Promise<string> {
691709
return this._connection.request(
692710
{
693711
method: "POST",

src/graph.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@ import {
33
BaseCollection,
44
DocumentHandle,
55
EdgeCollection,
6-
Types,
7-
isArangoCollection
6+
isArangoCollection,
7+
Types
88
} from "./collection";
9-
109
import { Connection } from "./connection";
1110

1211
export class GraphVertexCollection extends BaseCollection {
@@ -244,6 +243,7 @@ export class GraphEdgeCollection extends EdgeCollection {
244243
}
245244
}
246245

246+
const GRAPH_NOT_FOUND = 1924;
247247
export class Graph {
248248
name: string;
249249

@@ -261,6 +261,18 @@ export class Graph {
261261
);
262262
}
263263

264+
exists(): Promise<boolean> {
265+
return this.get().then(
266+
() => true,
267+
err => {
268+
if (err.errorNum !== GRAPH_NOT_FOUND) {
269+
throw err;
270+
}
271+
return false;
272+
}
273+
);
274+
}
275+
264276
create(properties: any = {}, opts?: { waitForSync?: boolean }) {
265277
return this._connection.request(
266278
{

0 commit comments

Comments
 (0)