Skip to content

Commit 64f0d6c

Browse files
committed
Replace databaseName with isAbsolute
1 parent fd5b3c5 commit 64f0d6c

File tree

6 files changed

+29
-23
lines changed

6 files changed

+29
-23
lines changed

CHANGELOG.md

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

4040
The behaviour depends on the load balancing strategy (see API docs).
4141

42+
* The `databaseName` config has been replaced with `isAbsolute`.
43+
44+
If you previously used `databaseName: false`, the same behaviour can now
45+
be achived using `isAbsolute: true`. If you want to use a specific
46+
database you can still switch databases with `db.useDatabase` at any time.
47+
4248
* Browser: maximum number of parallel connections behaves differently.
4349

4450
As keep-alive does not work reliably in the browser, the maximum number

README.md

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,9 @@ db.query({
196196

197197
// Using different databases
198198
const db = new Database({
199-
url: "http://localhost:8529",
200-
databaseName: "pancakes"
199+
url: "http://localhost:8529"
201200
});
201+
db.useDatabase("pancakes);
202202
db.useBasicAuth("root", "");
203203
// The database can be swapped at any time
204204
db.useDatabase("waffles");
@@ -207,7 +207,7 @@ db.useBasicAuth("admin", "maplesyrup");
207207
// Using ArangoDB behind a reverse proxy
208208
const db = new Database({
209209
url: "http://myproxy.local:8000",
210-
databaseName: false // don't automatically append database path to URL
210+
isAbsolute: true // don't automatically append database path to URL
211211
});
212212

213213
// Trigger ArangoDB 2.8 compatibility mode
@@ -460,13 +460,12 @@ If _config_ is a string, it will be interpreted as _config.url_.
460460
}
461461
```
462462

463-
* **databaseName**: `string | false` (Default: `_system`)
463+
* **isAbsolute**: `boolean` (Default: `false`)
464464

465-
Name of the active database.
466-
467-
If this option is explicitly set to `false`, the _url_ is expected to
468-
provide the database path and the _useDatabase_ method can not be used to
469-
switch databases.
465+
If this option is explicitly set to `true`, the _url_ will be treated as the
466+
absolute database path. This is an escape hatch to allow using arangojs with
467+
database APIs exposed with a reverse proxy and makes it impossible to switch
468+
databases with _useDatabase_ or using _acquireHostList_.
470469

471470
* **arangoVersion**: `number` (Default: `30000`)
472471

@@ -542,13 +541,17 @@ Updates the URL list by requesting a list of all coordinators in the cluster and
542541
543542
For long-running processes communicating with an ArangoDB cluster it is recommended to run this method repeatedly (e.g. once per hour) to make sure new coordinators are picked up correctly and can be used for fail-over or load balancing.
544543
544+
**Note**: This method can not be used when the arangojs instance was created with `isAbsolute: true`.
545+
545546
#### database.useDatabase
546547
547548
`database.useDatabase(databaseName): this`
548549
549550
Updates the _Database_ instance and its connection string to use the given
550551
_databaseName_, then returns itself.
551552
553+
**Note**: This method can not be used when the arangojs instance was created with `isAbsolute: true`.
554+
552555
**Arguments**
553556
554557
* **databaseName**: `string`

src/connection.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export type Config =
5454
| string[]
5555
| Partial<{
5656
url: string | string[];
57-
databaseName: string | false;
57+
isAbsolute: boolean;
5858
arangoVersion: number;
5959
loadBalancingStrategy: LoadBalancingStrategy;
6060
agent: Function;
@@ -84,8 +84,8 @@ export class Connection {
8484
if (config.arangoVersion !== undefined) {
8585
this._arangoVersion = config.arangoVersion;
8686
}
87-
if (config.databaseName !== undefined) {
88-
this._databaseName = config.databaseName;
87+
if (config.isAbsolute) {
88+
this._databaseName = false;
8989
}
9090
this._agent = config.agent;
9191
this._agentOptions = isBrowser

src/database.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ export class Database {
5454
}
5555

5656
async acquireHostList() {
57+
if (!this._connection.getDatabaseName()) {
58+
throw new Error("Cannot acquire host list with absolute URL");
59+
}
5760
const res = await this._api.request({
5861
path: "/_api/cluster/endpoints"
5962
});

src/test/00-basics.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,21 @@ import { expect } from "chai";
88

99
describe("Creating a Database", () => {
1010
describe("using the factory", () => {
11-
const db = arangojs({ databaseName: "potato" });
11+
const db = arangojs({ arangoVersion: 54321 });
1212
it("returns a Database instance", () => {
1313
expect(db).to.be.an.instanceof(Database);
1414
});
1515
it("passes any configs to the connection", () => {
16-
expect((db as any)._connection).to.have.property(
17-
"_databaseName",
18-
"potato"
19-
);
16+
expect((db as any)._connection).to.have.property("_arangoVersion", 54321);
2017
});
2118
});
2219
describe("using the constructor", () => {
23-
const db = new Database({ databaseName: "banana" });
20+
const db = new Database({ arangoVersion: 43210 });
2421
it("returns a Database instance", () => {
2522
expect(db).to.be.an.instanceof(Database);
2623
});
2724
it("passes any configs to the connection", () => {
28-
expect((db as any)._connection).to.have.property(
29-
"_databaseName",
30-
"banana"
31-
);
25+
expect((db as any)._connection).to.have.property("_arangoVersion", 43210);
3226
});
3327
});
3428
});

src/test/01-manipulating-databases.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ describe("Manipulating databases", () => {
101101
it("deletes the given database from the server", done => {
102102
db
103103
.dropDatabase(name)
104-
.then(() => new Database({ databaseName: name }).get())
104+
.then(() => new Database().useDatabase(name).get())
105105
.then(() => Promise.reject(new Error("Should not succeed")), () => null)
106106
.then(() => void done())
107107
.catch(done);

0 commit comments

Comments
 (0)