Skip to content
71 changes: 35 additions & 36 deletions core/database/foxx/api/query_router.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ module.exports = router;

router
.post("/create", function (req, res) {
let client = undefined;
let result = undefined;
try {
g_db._executeTransaction({
Expand All @@ -26,9 +25,9 @@ router
write: ["q", "owner"],
},
action: function () {
client = g_lib.getUserFromClientID(req.queryParams.client);
const client = g_lib.getUserFromClientID(req.queryParams.client);
logger.logRequestStarted({
client: client?._id,
client: req.queryParams.client,
correlationId: req.headers["x-correlation-id"],
httpVerb: "POST",
routePath: basePath + "/create",
Expand Down Expand Up @@ -93,7 +92,7 @@ router

res.send(result);
logger.logRequestSuccess({
client: client?._id,
client: req.queryParams.client,
correlationId: req.headers["x-correlation-id"],
httpVerb: "POST",
routePath: basePath + "/create",
Expand All @@ -103,7 +102,7 @@ router
});
} catch (e) {
logger.logRequestFailure({
client: client?._id,
client: req.queryParams.client,
correlationId: req.headers["x-correlation-id"],
httpVerb: "POST",
routePath: basePath + "/create",
Expand Down Expand Up @@ -136,7 +135,6 @@ router

router
.post("/update", function (req, res) {
let client = undefined;
let result = undefined;
try {
g_db._executeTransaction({
Expand All @@ -145,9 +143,9 @@ router
write: ["q", "owner"],
},
action: function () {
client = g_lib.getUserFromClientID(req.queryParams.client);
const client = g_lib.getUserFromClientID(req.queryParams.client);
logger.logRequestStarted({
client: client?._id,
client: req.queryParams.client,
correlationId: req.headers["x-correlation-id"],
httpVerb: "POST",
routePath: basePath + "/update",
Expand Down Expand Up @@ -200,7 +198,7 @@ router
});
res.send(result);
logger.logRequestSuccess({
client: client?._id,
client: req.queryParams.client,
correlationId: req.headers["x-correlation-id"],
httpVerb: "POST",
routePath: basePath + "/update",
Expand All @@ -210,7 +208,7 @@ router
});
} catch (e) {
logger.logRequestFailure({
client: client?._id,
client: req.queryParams.client,
correlationId: req.headers["x-correlation-id"],
httpVerb: "POST",
routePath: basePath + "/update",
Expand Down Expand Up @@ -243,12 +241,12 @@ router

router
.get("/view", function (req, res) {
let client = undefined;
let qry = undefined;
let client = null;
try {
client = g_lib.getUserFromClientID(req.queryParams.client);
logger.logRequestStarted({
client: client?._id,
client: req.queryParams.client,
correlationId: req.headers["x-correlation-id"],
httpVerb: "GET",
routePath: basePath + "/view",
Expand All @@ -273,7 +271,7 @@ router

res.send(qry);
logger.logRequestSuccess({
client: client?._id,
client: req.queryParams.client,
correlationId: req.headers["x-correlation-id"],
httpVerb: "GET",
routePath: basePath + "/view",
Expand All @@ -283,7 +281,7 @@ router
});
} catch (e) {
logger.logRequestFailure({
client: client?._id,
client: req.queryParams.client,
correlationId: req.headers["x-correlation-id"],
httpVerb: "GET",
routePath: basePath + "/view",
Expand All @@ -303,12 +301,11 @@ router

router
.get("/delete", function (req, res) {
let client = undefined;
try {
client = g_lib.getUserFromClientID(req.queryParams.client);
const client = g_lib.getUserFromClientID(req.queryParams.client);
var owner;
logger.logRequestStarted({
client: client?._id,
client: req.queryParams.client,
correlationId: req.headers["x-correlation-id"],
httpVerb: "GET",
routePath: basePath + "/delete",
Expand Down Expand Up @@ -340,7 +337,7 @@ router

g_graph.q.remove(owner._from);
logger.logRequestSuccess({
client: client?._id,
client: req.queryParams.client,
correlationId: req.headers["x-correlation-id"],
httpVerb: "GET",
routePath: basePath + "/delete",
Expand All @@ -351,7 +348,7 @@ router
}
} catch (e) {
logger.logRequestFailure({
client: client?._id,
client: req.queryParams.client,
correlationId: req.headers["x-correlation-id"],
httpVerb: "GET",
routePath: basePath + "/delete",
Expand All @@ -371,12 +368,11 @@ router

router
.get("/list", function (req, res) {
let client = undefined;
let result = undefined;
try {
client = g_lib.getUserFromClientID(req.queryParams.client);
const client = g_lib.getUserFromClientID(req.queryParams.client);
logger.logRequestStarted({
client: client?._id,
client: req.queryParams.client,
correlationId: req.headers["x-correlation-id"],
httpVerb: "GET",
routePath: basePath + "/list",
Expand Down Expand Up @@ -418,7 +414,7 @@ router

res.send(result);
logger.logRequestSuccess({
client: client?._id,
client: req.queryParams.client,
correlationId: req.headers["x-correlation-id"],
httpVerb: "GET",
routePath: basePath + "/list",
Expand All @@ -431,7 +427,7 @@ router
});
} catch (e) {
logger.logRequestFailure({
client: client?._id,
client: req.queryParams.client,
correlationId: req.headers["x-correlation-id"],
httpVerb: "GET",
routePath: basePath + "/list",
Expand Down Expand Up @@ -651,12 +647,11 @@ function execQuery(client, mode, published, orig_query) {

router
.get("/exec", function (req, res) {
let client = undefined;
let results = undefined;
try {
client = g_lib.getUserFromClientID(req.queryParams.client);
let client = g_lib.getUserFromClientID(req.queryParams.client);
logger.logRequestStarted({
client: client?._id,
client: req.queryParams.client,
correlationId: req.headers["x-correlation-id"],
httpVerb: "GET",
routePath: basePath + "/exec",
Expand All @@ -679,7 +674,7 @@ router

res.send(results);
logger.logRequestSuccess({
client: client?._id,
client: req.queryParams.client,
correlationId: req.headers["x-correlation-id"],
httpVerb: "GET",
routePath: basePath + "/exec",
Expand All @@ -689,7 +684,7 @@ router
});
} catch (e) {
logger.logRequestFailure({
client: client?._id,
client: req.queryParams.client,
correlationId: req.headers["x-correlation-id"],
httpVerb: "GET",
routePath: basePath + "/exec",
Expand All @@ -711,18 +706,18 @@ router
router
.post("/exec/direct", function (req, res) {
let results = undefined;
let client = undefined;
try {
client = g_lib.getUserFromClientID_noexcept(req.queryParams.client);
logger.logRequestStarted({
client: client?._id,
client: req.queryParams.client,
correlationId: req.headers["x-correlation-id"],
httpVerb: "POST",
routePath: basePath + "/exec/direct",
status: "Started",
description: "Execute published data search query",
});

let client = g_lib.getUserFromClientID_noexcept(req.queryParams.client);

const query = {
...req.body,
params: JSON.parse(req.body.params),
Expand All @@ -731,23 +726,27 @@ router

res.send(results);
logger.logRequestSuccess({
client: client?._id,
client: req.queryParams.client,
correlationId: req.headers["x-correlation-id"],
httpVerb: "POST",
routePath: basePath + "/exec/direct",
status: "Success",
description: "Execute published data search query",
extra: results,
extra: {
count: Array.isArray(results) ? results.length : undefined,
},
});
} catch (e) {
logger.logRequestFailure({
client: client?._id,
client: req.queryParams.client,
correlationId: req.headers["x-correlation-id"],
httpVerb: "POST",
routePath: basePath + "/exec/direct",
status: "Failure",
description: "Execute published data search query",
extra: results,
extra: {
count: Array.isArray(results) ? results.length : undefined,
},
error: e,
});
g_lib.handleException(e, res);
Expand Down
51 changes: 49 additions & 2 deletions core/database/foxx/tests/query_router.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ const qry_base_url = `${baseUrl}/qry`;

describe("unit_query_router: the Foxx microservice qry_router endpoints", () => {
after(function () {
const collections = ["u", "qry"];
const collections = ["u", "qry", "c", "note", "fake"];
collections.forEach((name) => {
let col = db._collection(name);
if (col) col.truncate();
});
});

beforeEach(() => {
const collections = ["u", "qry"];
const collections = ["u", "qry", "c", "note", "fake"];
collections.forEach((name) => {
let col = db._collection(name);
if (col) {
Expand Down Expand Up @@ -163,4 +163,51 @@ describe("unit_query_router: the Foxx microservice qry_router endpoints", () =>
expect(parsed).to.be.an("array");
expect(parsed.length).to.be.greaterThan(0);
});

it("should execute a query directly", () => {
// arrange
const fakeUser = {
_key: "fakeUser",
_id: "u/fakeUser",
name: "Fake User",
email: "fakeuser@datadev.org",
is_admin: true,
max_coll: 5,
max_proj: 5,
max_sav_qry: 10,
};

const fakeCol = {
_key: "fakeCol",
_id: "col/fakeCol",
title: "fakeCol",
desc: "This is a fake col",
};

db.u.save(fakeUser);

// Save the query and the edge between the query and the user
var request_string = `${qry_base_url}/exec/direct?client=u/fakeUser&owner=u/fakeUser&cols=c/fakeCol&cnt=1&off=0`;
var body = {
qry_begin: "FOR i in fake filter i.owner == @owner ",
qry_end: " sort @off,@cnt RETURN distinct i",
qry_filter: "",
params: '{ "cnt": 1, "off": 0, "owner": "u/fakeUser"}',
limit: 10,
mode: 1,
published: false,
};

// act
var response = request.post(request_string, {
json: true,
body: body,
headers: {
"x-correlation-id": "test-correlation-id",
},
});

// Assert
expect(response.status).to.equal(200);
});
});