Skip to content

Commit

Permalink
Refactor username/password into options in databases CRUD
Browse files Browse the repository at this point in the history
  • Loading branch information
wolf4ood committed Jun 28, 2018
1 parent 2d20a8f commit e487c1f
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 75 deletions.
69 changes: 39 additions & 30 deletions lib/client/client.js
Expand Up @@ -122,48 +122,53 @@ class OrientDBClient extends EventEmitter {

/**
* Create a database with a given config
* @param {String} username Server user
* @param {String} password Server password
* @param {String|Object} config The database name, or configuration object.
* @param {String} config.name The database name
* @param {String} config.type The database type
* @param {String} config.storage The database storage type
* @param {Object} options The options object.
* @param {String} options.username Server user
* @param {String} options.password Server password
* @param {String} options.name The database name
* @param {String} options.type The database type
* @param {String} options.storage The database storage type
* @returns {Promise}
*/
createDatabase(username, password, config) {
config = config || "";

if (typeof config === "string" || typeof config === "number") {
config = {
name: "" + config,
createDatabase(options) {
options = options || {};
if (typeof options === "string" || typeof options === "number") {
options = {
username: "root",
password: "root",
name: "" + options,
type: "graph",
storage: "plocal"
};
} else {
config = {
name: config.name,
type: config.type || config.type,
storage: config.storage || config.storage || "plocal"
options = {
username: options.username || "root",
password: options.password || "root",
name: options.name,
type: options.type || options.type,
storage: options.storage || options.storage || "plocal"
};
}
if (!config.name) {
if (!options.name) {
return Promise.reject(
new Errors.Config("Cannot create database, no name specified.")
);
}
if (config.type !== "document" && config.type !== "graph") {
config.type = "graph";
if (options.type !== "document" && options.type !== "graph") {
options.type = "graph";
}
if (
config.storage !== "local" &&
config.storage !== "plocal" &&
config.storage !== "memory"
options.storage !== "local" &&
options.storage !== "plocal" &&
options.storage !== "memory"
) {
config.storage = "plocal";
options.storage = "plocal";
}
this.logger.debug("Creating database " + config.name);
this.logger.debug("Creating database " + options.name);

let ctx = {};
let { username, password } = options;

return this.cluster
.acquireFrom()
.then(resource => {
Expand All @@ -179,7 +184,7 @@ class OrientDBClient extends EventEmitter {
ctx.token = response.token;
return ctx.conn.send(
"db-create",
Object.assign({}, config, {
Object.assign({}, options, {
sessionId: ctx.sessionId,
token: ctx.token
})
Expand All @@ -199,14 +204,17 @@ class OrientDBClient extends EventEmitter {
/**
* Drop a database with a given config
* @param {String} username Server user
* @param {String}password Server password
* @param {String} password Server password
* @param {Object} options Options object
* @param {String} options.username Server user
* @param {String} options.password Server password
* @param {String} options.name Database name
* @param {String} options.storage Storage type
* @returns {Promise}
*/
dropDatabase(username, password, options) {
dropDatabase(options) {
let ctx = {};
let { username, password } = options;
return this.connect()
.then(() => {
return this.cluster.acquireFrom();
Expand Down Expand Up @@ -243,15 +251,16 @@ class OrientDBClient extends EventEmitter {

/**
* Determine whether a database exists with the given config.
* @param {String} username Server user
* @param {String}password Server password
* @param {Object} options Options object
* @param {String} options.username Server user
* @param {String} options.password Server password
* @param {String} options.name Database name
* @param {String} options.storage Storage type
* @returns {Promise}
*/
existsDatabase(username, password, options) {
existsDatabase(options) {
let ctx = {};
let { username, password } = options;
return this.connect()
.then(() => {
return this.cluster.acquireFrom();
Expand Down
96 changes: 56 additions & 40 deletions test/client/client-test.js
@@ -1,68 +1,84 @@
var Errors = require('../../lib/errors');
describe("Client API", function () {

before(CAN_RUN(37, function () {

}))
it('should create and connect the client', function (done) {
var Errors = require("../../lib/errors");
describe("Client API", function() {
before(CAN_RUN(37, function() {}));
it("should create and connect the client", function(done) {
this.client = new global.CLIENT(TEST_SERVER_CONFIG);
this.client.connect()
.then(() => {
done(0);
})
this.client.connect().then(() => {
done(0);
});
});

it('should fail to connect the client', function () {
this.client = new global.CLIENT(Object.assign({}, TEST_SERVER_CONFIG, {
port: 3535,
pool: {
acquireTimeoutMillis: 100
}
}));
return this.client.connect()
it("should fail to connect the client", function() {
this.client = new global.CLIENT(
Object.assign({}, TEST_SERVER_CONFIG, {
port: 3535,
pool: {
acquireTimeoutMillis: 100
}
})
);
return this.client
.connect()
.then(() => {
throw new Error('Should never happen!');
}).catch(err => {
err.should.be.an.instanceOf(Errors.Connection);
throw new Error("Should never happen!");
})
.catch(err => {
err.should.be.an.instanceOf(Errors.Connection);
});
});

it('should connect the client with multiple servers', function (done) {
it("should connect the client with multiple servers", function(done) {
var config = Object.assign({}, TEST_SERVER_CONFIG, {
port: 3535,
pool: {
acquireTimeoutMillis: 100
},
servers: [TEST_SERVER_CONFIG]
})
});
this.client = new global.CLIENT(config);
this.client.connect().then(() => {
done();
}).catch(err => {
throw new Error('Should never happen!');
})
this.client
.connect()
.then(() => {
done();
})
.catch(err => {
throw new Error("Should never happen!");
});
});

it('should connect the client and create/drop a database', function () {
it("should connect the client and create/drop a database", function() {
this.client = new global.CLIENT(TEST_SERVER_CONFIG);
var dbConfig = {name: "client_test_db_create", storage: "memory"};
return this.client.connect()
var dbConfig = {
name: "client_test_db_create",
storage: "memory",
username: TEST_SERVER_CONFIG.username,
password: TEST_SERVER_CONFIG.password
};
return this.client
.connect()
.then(() => {
return this.client.createDatabase(TEST_SERVER_CONFIG.username, TEST_SERVER_CONFIG.password, dbConfig);
return this.client.createDatabase(
dbConfig
);
})
.then(() => {
return this.client.existsDatabase(TEST_SERVER_CONFIG.username, TEST_SERVER_CONFIG.password, dbConfig);
return this.client.existsDatabase(
dbConfig
);
})
.then((response) => {
.then(response => {
response.should.be.eql(true);
return this.client.dropDatabase(TEST_SERVER_CONFIG.username, TEST_SERVER_CONFIG.password, dbConfig);
return this.client.dropDatabase(
dbConfig
);
})
.then(() => {
return this.client.existsDatabase(TEST_SERVER_CONFIG.username, TEST_SERVER_CONFIG.password, dbConfig);
return this.client.existsDatabase(
dbConfig
);
})
.then((response) => {
.then(response => {
response.should.be.eql(false);
})
});
});

});
14 changes: 9 additions & 5 deletions test/index.js
Expand Up @@ -136,19 +136,21 @@ function createDB(client, name, type) {
var username = TEST_SERVER_CONFIG.username;
var password = TEST_SERVER_CONFIG.password;
var cfg = {
username: username,
password: password,
name: name,
storage: type
};
return client
.existsDatabase(username, password, cfg)
.existsDatabase(cfg)
.then(exists => {
if (exists) {
return client.dropDatabase(username, password, cfg);
return client.dropDatabase(cfg);
}
return false;
})
.then(() => {
return client.createDatabase(username, password, cfg);
return client.createDatabase(cfg);
});
}

Expand All @@ -157,14 +159,16 @@ function dropDB(client, name, type) {
var username = TEST_SERVER_CONFIG.username;
var password = TEST_SERVER_CONFIG.password;
var cfg = {
username: username,
password: password,
name: name,
storage: type
};
return client
.existsDatabase(username, password, cfg)
.existsDatabase(cfg)
.then(exists => {
if (exists) {
return client.dropDatabase(username, password, cfg);
return client.dropDatabase(cfg);
}
return undefined;
})
Expand Down

0 comments on commit e487c1f

Please sign in to comment.