Skip to content

Commit

Permalink
feature: support skip and limit options in search
Browse files Browse the repository at this point in the history
  • Loading branch information
onurcipe committed Sep 30, 2022
1 parent f505bc0 commit 5f77622
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 27 deletions.
28 changes: 17 additions & 11 deletions lib/controllers/CrudController.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ class CrudController extends Controller
hooks = utility.init(hooks, {});
hooks.bearer = utility.init(hooks.bearer, {});

let query, searchValue;
let value, query, options;

switch (this._apiType)
{
Expand All @@ -115,8 +115,8 @@ class CrudController extends Controller
CrudController._extractAndAuthorizeHeaders(request, allowedPropertiesForRequestElements.headers, false);
CrudController._extractAndAuthorizePathParameters(request, allowedPropertiesForRequestElements.pathParameters, false);
const queryString = CrudController._extractAndAuthorizeQueryString(request, allowedPropertiesForRequestElements.queryString);
value = queryString.value;
query = queryString.query;
searchValue = queryString.search;
CrudController._extractAndAuthorizeBody(request, allowedPropertiesForRequestElements.body, false);
break;
}
Expand All @@ -126,31 +126,37 @@ class CrudController extends Controller
CrudController._extractAndAuthorizePathParameters(request, allowedPropertiesForRequestElements.pathParameters, false);
CrudController._extractAndAuthorizeQueryString(request, allowedPropertiesForRequestElements.queryString, false);
const body = CrudController._extractAndAuthorizeBody(request, allowedPropertiesForRequestElements.body);
value = body.value;
query = body.query;
searchValue = body.search;
options = body.options;

break;
}
}

if (!_.isString(searchValue) ||
!utility.isInitialized(searchValue))
if (!_.isString(value))
{
throw new BadRequestError(ErrorSafe.get().INVALID_SEARCH_VALUE);
}

utility.isExist(hooks.value) ? await hooks.value(value) : undefined;
utility.isExist(hooks.query) ? await hooks.query(query) : undefined;
utility.isExist(hooks.search) ? await hooks.search(searchValue) : undefined;
utility.isExist(hooks.options) ? await hooks.options(options) : undefined;

let documents;
let result; // Contains documents and count.
const {session} = SessionManager.generateSessionsForController(hooks);
await SessionManager.exec(async () =>
{
utility.isExist(hooks.before) ? await hooks.before(query, searchValue, session) : undefined;
documents = await this._controllerService.search(query, searchValue, searchFields, undefined, session, {bearer: hooks.bearer});
utility.isExist(hooks.after) ? await hooks.after(documents, session) : undefined;
utility.isExist(hooks.before) ? await hooks.before(value, query, options, session) : undefined;
result = !utility.isInitialized(value)
? await this._controllerService.read({}, options, session, {bearer: hooks.bearer})
: await this._controllerService.search(value, query, searchFields, options, session, {bearer: hooks.bearer});
utility.isExist(hooks.after) ? await hooks.after(result.documents, result.count, session) : undefined;
}, undefined, session);

await this._sendResponse(request, response, 200, {documents});
console.log(result);

await this._sendResponse(request, response, 200, result);
}
catch (error)
{
Expand Down
32 changes: 17 additions & 15 deletions lib/services/ControllerService.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,31 +106,29 @@ class ControllerService extends Service
/**
* Fetches the documents that match the specified search value on each specified fields and query.
*
* @param {string} value
* @param {Object} query
* @param {string} searchValue
* @param {Array<string>} searchFields
* @param {Object} [options]
* @param {Object} [options.sort]
* @param {ClientSession} [externalSession]
* @param {Object} [hooks]
* @returns {Promise<Array>} - The fetched documents.
* @returns {Promise<{documents: Array, count: number}>} - The fetched documents.
*/
async search (query, searchValue, searchFields, options = undefined, externalSession = undefined, hooks = undefined)
async search (value, query, searchFields, options = undefined, externalSession = undefined, hooks = undefined)
{
query = utility.init(query, {});
options = utility.init(options, {});
options.sort = utility.init(options.sort, {});
hooks = utility.init(hooks, {});
hooks.bearer = utility.init(hooks.bearer, {});

this._validateParameterQuery(query);

if (!_.isString(searchValue) ||
!utility.isInitialized(searchValue))
if (!_.isString(value) ||
!utility.isInitialized(value))
{
throw new InvalidArgumentsError(ErrorSafe.get().DEV_1);
}

this._validateParameterQuery(query);

if (!_.isArray(searchFields))
{
throw new InvalidArgumentsError(ErrorSafe.get().DEV_1);
Expand Down Expand Up @@ -162,26 +160,30 @@ class ControllerService extends Service
for (const searchField of searchFields)
{
const searchElement = {};
searchElement[searchField] = {$regex: searchValue, $options: "i"};
searchElement[searchField] = {$regex: value, $options: "i"};

query.$or.push(searchElement);
}

utility.isExist(hooks.query) ? await hooks.query(query) : undefined;
utility.isExist(hooks.sort) ? await hooks.sort(options.sort) : undefined;
utility.isExist(hooks.options) ? await hooks.options(options) : undefined;

let documents;
let documents, count;
const {session, internalSession} = SessionManager.generateSessionsForService(externalSession, hooks);
await SessionManager.exec(async () =>
{
utility.isExist(hooks.before) ? await hooks.before(query, session) : undefined;
utility.isExist(hooks.before) ? await hooks.before(query, options, session) : undefined;
let convertedQuery = mongoDotNotation.flatten(query);
convertedQuery = utility.isExist(convertedQuery.$set) ? convertedQuery.$set : {};
documents = await this.dbOperation.read(convertedQuery, {...options, session});
documents = await this.dbOperation.read(convertedQuery, options, session, {bearer: hooks.bearer});
count = await this.dbOperation.count(query, {bearer: hooks.bearer});
utility.isExist(hooks.after) ? await hooks.after(documents, session) : undefined;
}, externalSession, internalSession);

return documents;
return {
documents,
count
};
}

/**
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@thedolphinos/cell",
"version": "0.184.1",
"version": "0.185.0",
"description": "A framework to build server side applications using MongoDB, Node.js and Express.",
"keywords": [
"dolphinos",
Expand Down

0 comments on commit 5f77622

Please sign in to comment.