Properly integrate pagination plugin

This commit is contained in:
Elliot DeNolf
2019-03-16 15:37:31 -04:00
parent d0bb871cc8
commit 826bfafb38
14 changed files with 117 additions and 39 deletions

5
.idea/codeStyles/codeStyleConfig.xml generated Normal file
View File

@@ -0,0 +1,5 @@
<component name="ProjectCodeStyleConfiguration">
<state>
<option name="PREFERRED_PROJECT_CODE_STYLE" value="Default" />
</state>
</component>

4
.idea/encodings.xml generated Normal file
View File

@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding" addBOMForNewFiles="with NO BOM" />
</project>

View File

@@ -0,0 +1,11 @@
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="Eslint" enabled="true" level="ERROR" enabled_by_default="true" />
<inspection_tool class="SpellCheckingInspection" enabled="false" level="TYPO" enabled_by_default="false">
<option name="processCode" value="true" />
<option name="processLiterals" value="true" />
<option name="processComments" value="true" />
</inspection_tool>
</profile>
</component>

7
.idea/jsLibraryMappings.xml generated Normal file
View File

@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="JavaScriptLibraryMappings">
<file url="PROJECT" libraries="{@types/express, @types/jest, @types/mongoose, @types/passport}" />
<includedPredefinedLibrary name="Node.js Core" />
</component>
</project>

9
.idea/misc.xml generated Normal file
View File

@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="JavaScriptSettings">
<option name="languageLevel" value="JSX" />
</component>
<component name="ProjectDictionaryState">
<dictionary name="denol" />
</component>
</project>

8
.idea/modules.xml generated Normal file
View File

@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/payload.iml" filepath="$PROJECT_DIR$/.idea/payload.iml" />
</modules>
</component>
</project>

16
.idea/payload.iml generated Normal file
View File

@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/.tmp" />
<excludeFolder url="file://$MODULE_DIR$/temp" />
<excludeFolder url="file://$MODULE_DIR$/tmp" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="@types/express" level="application" />
<orderEntry type="library" name="@types/jest" level="application" />
<orderEntry type="library" name="@types/passport" level="application" />
<orderEntry type="library" name="@types/mongoose" level="application" />
</component>
</module>

6
.idea/vcs.xml generated Normal file
View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

View File

@@ -16,7 +16,7 @@ const PageSchema = new mongoose.Schema({
{ timestamps: true }
);
// PageSchema.plugin(paginate);
PageSchema.plugin(paginate);
PageSchema.plugin(buildQuery);
PageSchema.plugin(internationalization, payloadConfig.localization);

View File

@@ -6,18 +6,14 @@ export default function buildQuery(schema) {
const model = this;
const params = paramParser(this, rawParams, locale);
// Create the Mongoose Query object.
let query = model
.find(params.searchParams);
if (params.sort)
query = query.sort(params.sort);
if (cb) {
query.exec(cb);
} else {
return query;
model
.find(params.searchParams)
.exec(cb);
}
return params.searchParams;
};
}
@@ -93,8 +89,7 @@ function parseParam(key, val, model, query, locale) {
query.searchParams['_id'] = { $ne: val };
} else if (lcKey === 'locale') {
// Do nothing
}
else {
} else {
query = parseSchemaForKey(model.schema, query, '', lcKey, val, operator, locale);
}
return query;
@@ -114,7 +109,7 @@ function parseSchemaForKey(schema, query, keyPrefix, lcKey, val, operator, local
// This wasn't handled in the original package but seems to work
paramType = schema.paths[matches[1]].schema.paths.name.instance;
}
} else if (typeof schema === 'object' ) {
} else if (typeof schema === 'object') {
if (schema.obj[lcKey].intl) {
key = `${key}.${locale}`;
paramType = 'String'

View File

@@ -22,28 +22,28 @@ function paginate(query, options, callback) {
options = Object.assign({}, paginate.options, options);
options.customLabels = options.customLabels ? options.customLabels : {};
var defaultLimit = 10;
let defaultLimit = 10;
var select = options.select;
var sort = options.sort;
var collation = options.collation || {};
var populate = options.populate;
var lean = options.lean || false;
var leanWithId = options.hasOwnProperty('leanWithId') ? options.leanWithId : true;
var limit = options.hasOwnProperty('limit') ? parseInt(options.limit) : defaultLimit;
var skip;
var offset;
var page;
let select = options.select;
let sort = options.sort;
let collation = options.collation || {};
let populate = options.populate;
let lean = options.lean || false;
let leanWithId = options.hasOwnProperty('leanWithId') ? options.leanWithId : true;
let limit = options.hasOwnProperty('limit') ? parseInt(options.limit) : defaultLimit;
let skip;
let offset;
let page;
// Custom Labels
var labelTotal = options.customLabels.totalDocs ? options.customLabels.totalDocs : 'totalDocs';
var labelLimit = options.customLabels.limit ? options.customLabels.limit : 'limit';
var labelPage = options.customLabels.page ? options.customLabels.page : 'page';
var labelTotalPages = options.customLabels.totalPages ? options.customLabels.totalPages : 'totalPages';
var labelDocs = options.customLabels.docs ? options.customLabels.docs : 'docs';
var labelNextPage = options.customLabels.nextPage ? options.customLabels.nextPage : 'nextPage';
var labelPrevPage = options.customLabels.prevPage ? options.customLabels.prevPage : 'prevPage';
var labelPagingCounter = options.customLabels.pagingCounter ? options.customLabels.pagingCounter : 'pagingCounter';
let labelTotal = options.customLabels.totalDocs ? options.customLabels.totalDocs : 'totalDocs';
let labelLimit = options.customLabels.limit ? options.customLabels.limit : 'limit';
let labelPage = options.customLabels.page ? options.customLabels.page : 'page';
let labelTotalPages = options.customLabels.totalPages ? options.customLabels.totalPages : 'totalPages';
let labelDocs = options.customLabels.docs ? options.customLabels.docs : 'docs';
let labelNextPage = options.customLabels.nextPage ? options.customLabels.nextPage : 'nextPage';
let labelPrevPage = options.customLabels.prevPage ? options.customLabels.prevPage : 'prevPage';
let labelPagingCounter = options.customLabels.pagingCounter ? options.customLabels.pagingCounter : 'pagingCounter';
if (options.hasOwnProperty('offset')) {
offset = parseInt(options.offset);
@@ -78,7 +78,7 @@ function paginate(query, options, callback) {
model.populate(populate);
}
var docs = model.exec();
let docs = model.exec();
if (lean && leanWithId) {
docs = docs.then(function (docs) {
@@ -92,7 +92,7 @@ function paginate(query, options, callback) {
return Promise.all([count, docs])
.then(function (values) {
var result = {
let result = {
[labelDocs]: values[1],
[labelTotal]: values[0],
[labelLimit]: limit

View File

@@ -1,20 +1,22 @@
import httpStatus from 'http-status';
const query = (req, res) => {
req.model.apiQuery(req.query, req.locale, (err, result) => {
req.model.paginate(req.model.apiQuery(req.query, req.locale), req.query, (err, result) => {
if (err) {
return res.status(httpStatus.INTERNAL_SERVER_ERROR).json({ error: err });
}
return res.json(
result.map(doc => { //TODO: 'result.docs' will need to be used for the pagination plugin
return res.json({
...result,
docs: result.docs.map(doc => {
if (req.locale) {
doc.setLocale(req.locale, req.query['fallback-locale']);
}
return doc.toJSON({ virtuals: true })
})
);
});
});
})
};
export default query;

View File

@@ -0,0 +1,7 @@
describe('troubleshooting', () => {
describe('plugins', () => {
it('should return all records', () => {
});
});
});

8
toggle_mongo.cmd Normal file
View File

@@ -0,0 +1,8 @@
@ECHO OFF
sc query "MongoDB" | findstr "RUNNING"
if errorlevel 1 (
net start "MongoDB Server"
) else (
net stop "MongoDB Server"
)