lucis-api-query
Advanced tools
Comparing version 0.0.1 to 0.0.2
124
index.js
@@ -1,2 +0,2 @@ | ||
module.exports = exports = function getSearchParams (schema) { | ||
function pluginMongoose (schema) { | ||
@@ -156,2 +156,122 @@ schema.statics.getSearchParams = function(rawParams) { | ||
}; | ||
}; | ||
schema.statics.paginate = function(query, options, callback){ | ||
query = query || {}; | ||
options = Object.assign({}, paginate.options, options); | ||
let select = options.select; | ||
let sort = options.sort; | ||
let populate = options.populate; | ||
let lean = options.lean || false; | ||
let leanWithId = options.leanWithId ? options.leanWithId : true; | ||
let limit = options.limit ? options.limit : 10; | ||
let page, offset, skip, promises; | ||
if (options.offset) { | ||
offset = options.offset; | ||
skip = offset; | ||
} else if (options.page) { | ||
page = options.page; | ||
skip = (page - 1) * limit; | ||
} else { | ||
page = 1; | ||
offset = 0; | ||
skip = offset; | ||
} | ||
if (limit) { | ||
let docsQuery = this.find(query) | ||
.select(select) | ||
.sort(sort) | ||
.skip(skip) | ||
.limit(limit) | ||
.lean(lean); | ||
if (populate) { | ||
[].concat(populate).forEach((item) => { | ||
docsQuery.populate(item); | ||
}); | ||
} | ||
promises = { | ||
docs: docsQuery.exec(), | ||
count: this.count(query).exec() | ||
}; | ||
if (lean && leanWithId) { | ||
promises.docs = promises.docs.then((docs) => { | ||
docs.forEach((doc) => { | ||
doc.id = String(doc._id); | ||
}); | ||
return docs; | ||
}); | ||
} | ||
} | ||
promises = Object.keys(promises).map((x) => promises[x]); | ||
return Promise.all(promises).then((data) => { | ||
let result = { | ||
docs: data.docs, | ||
total: data.count, | ||
limit: limit | ||
}; | ||
if (offset !== undefined) { | ||
result.offset = offset; | ||
} | ||
if (page !== undefined) { | ||
result.page = page; | ||
result.pages = Math.ceil(data.count / limit) || 1; | ||
} | ||
if (typeof callback === 'function') { | ||
return callback(null, result); | ||
} | ||
let promise = new Promise(); | ||
promise.resolve(result); | ||
return promise; | ||
}); | ||
} | ||
}; | ||
// TODO: tornar ou tudo inglês ou portugues para não precisar aquelas funções util | ||
function middlewareExpress (req, res, next) { | ||
req.query.pagina = (typeof req.query.pagina === 'string') ? parseInt(req.query.pagina, 10) || 1 : 1; | ||
req.query.resultadosPorPagina = (typeof req.query.resultadosPorPagina === 'string') ? parseInt(req.query.resultadosPorPagina, 10) || 0 : 10; | ||
if (req.query.resultadosPorPagina > 50) | ||
req.query.resultadosPorPagina = 50; | ||
if (req.query.pagina < 1) | ||
req.query.pagina = 1; | ||
if (req.query.resultadosPorPagina < 0) | ||
req.query.resultadosPorPagina = 0; | ||
req.skip = req.offset = (req.query.pagina * req.query.resultadosPorPagina) - req.query.resultadosPorPagina; | ||
res.locals.paginacao = {}; | ||
res.locals.paginacao.skip = req.skip; | ||
res.locals.paginacao.pagina = req.query.pagina; | ||
res.locals.paginacao.resultadosPorPagina = req.query.resultadosPorPagina | ||
delete req.query.pagina; | ||
delete req.query.resultadosPorPagina; | ||
next(); | ||
}; | ||
function montaEntidadePaginacao(resposta){ | ||
return { | ||
totalDeEntidades: resposta.total, | ||
entidades: resposta.docs, | ||
pagina: resposta.page, | ||
resultadosPorPagina: resposta.limit, | ||
totalDePaginas: resposta.pages | ||
} | ||
}; | ||
function montaConfigPaginacao(paginacao, campos, populate){ | ||
return { | ||
page: paginacao.pagina, | ||
limit: paginacao.resultadosPorPagina, | ||
lean: true, | ||
select: campos, | ||
populate: populate | ||
}; | ||
}; | ||
const biblioteca = {pluginMongoose, middlewareExpress, montaEntidadePaginacao, montaConfigPaginacao}; | ||
module.exports = exports = biblioteca; |
{ | ||
"name": "lucis-api-query", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "Adaptação do mongoose-api-query para necessidades mais comuns", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
Found 1 instance in 1 package
12935
3
246
1
142