Skip to content

Commit

Permalink
Refactor database crud operation within a single _runAsAdmin function
Browse files Browse the repository at this point in the history
  • Loading branch information
wolf4ood committed Jun 28, 2018
1 parent e487c1f commit 18a03f6
Showing 1 changed file with 78 additions and 99 deletions.
177 changes: 78 additions & 99 deletions lib/client/client.js
Expand Up @@ -157,48 +157,21 @@ class OrientDBClient extends EventEmitter {
if (options.type !== "document" && options.type !== "graph") {
options.type = "graph";
}
if (
options.storage !== "local" &&
options.storage !== "plocal" &&
options.storage !== "memory"
) {
if (options.storage !== "plocal" && options.storage !== "memory") {
options.storage = "plocal";
}
this.logger.debug("Creating database " + options.name);

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

return this.cluster
.acquireFrom()
.then(resource => {
ctx.conn = resource.connection;
return ctx.conn.send("connect", {
username: username,
password: password,
useToken: true
});
})
.then(response => {
ctx.sessionId = response.sessionId;
ctx.token = response.token;
return ctx.conn.send(
"db-create",
Object.assign({}, options, {
sessionId: ctx.sessionId,
token: ctx.token
})
);
})
.then(() => {
return ctx.conn.send("db-close", {
return this._runAsAdmin(options, ctx => {
return ctx.conn.send(
"db-create",
Object.assign({}, options, {
sessionId: ctx.sessionId,
token: ctx.token
});
})
.then(() => {
return ctx.conn.close();
});
})
);
});
}

/**
Expand All @@ -213,40 +186,22 @@ class OrientDBClient extends EventEmitter {
* @returns {Promise}
*/
dropDatabase(options) {
let ctx = {};
let { username, password } = options;
return this.connect()
.then(() => {
return this.cluster.acquireFrom();
})
.then(resource => {
ctx.conn = resource.connection;
return ctx.conn.send("connect", {
username: username,
password: password,
useToken: true
});
})
.then(response => {
ctx.sessionId = response.sessionId;
ctx.token = response.token;
return ctx.conn.send(
"db-delete",
Object.assign({}, options, {
sessionId: ctx.sessionId,
token: ctx.token
})
);
})
.then(() => {
return ctx.conn.send("db-close", {
return this._runAsAdmin(options, ctx => {
return ctx.conn.send(
"db-delete",
Object.assign({}, options, {
sessionId: ctx.sessionId,
token: ctx.token
});
})
.then(() => {
return ctx.conn.close();
});
})
);
});
}

_sendClose(ctx) {
return ctx.conn.send("db-close", {
sessionId: ctx.sessionId,
token: ctx.token
});
}

/**
Expand All @@ -259,44 +214,68 @@ class OrientDBClient extends EventEmitter {
* @returns {Promise}
*/
existsDatabase(options) {
let ctx = {};
let { username, password } = options;
return this.connect()
.then(() => {
return this.cluster.acquireFrom();
})
.then(resource => {
ctx.conn = resource.connection;
return ctx.conn.send("connect", {
username: username,
password: password,
useToken: true
});
})
.then(response => {
ctx.sessionId = response.sessionId;
ctx.token = response.token;
return ctx.conn.send(
return this._runAsAdmin(options, ctx => {
return ctx.conn
.send(
"db-exists",
Object.assign({}, options, {
sessionId: ctx.sessionId,
token: ctx.token
})
);
})
.then(response => {
ctx.exists = response.exists;
return ctx.conn.send("db-close", {
sessionId: ctx.sessionId,
token: ctx.token
)
.then(response => {
return response.exists;
});
})
.then(() => {
return ctx.conn.close();
})
.then(() => {
return ctx.exists;
});
});
}

_runAsAdmin(options, work) {
return new Promise((resolve, reject) => {
let ctx = { options: options };
let { username, password } = options;
this.connect()
.then(() => {
return this.cluster.acquireFrom();
})
.then(resource => {
ctx.conn = resource.connection;
return ctx.conn
.send("connect", {
username: username,
password: password,
useToken: true
})
.then(response => {
ctx.sessionId = response.sessionId;
ctx.token = response.token;
return work(ctx);
})
.then(response => {
ctx.response = response;
return this._sendClose(ctx);
})
.then(() => {
return ctx.conn.close();
})
.then(() => {
resolve(ctx.response);
})
.catch(err => {
let promises = [];
if (ctx.sessionId) {
promises.push(this._sendClose(ctx));
}
promises.push(ctx.conn.close());
Promise.all(promises)
.then(() => {
reject(err);
})
.catch(() => {
reject(err);
});
});
});
});
}

/**
Expand Down

0 comments on commit 18a03f6

Please sign in to comment.