1.0.1 • Published 12 months ago

imjano_get_query_from_url_mw v1.0.1

Weekly downloads
-
License
ISC
Repository
github
Last release
12 months ago

Get query from URL

The function extracts construct a URL object from the request parameters. It then creates a query object based on the URL query string, which will be used for querying a database using mongoose. The constructed query object is then inserted into the req parameter for further processing in subsequent middleware or route handlers.

Installation

npm install get_query_from_url_imjano

Usage

import the middleware get_query_from_url_imjano

const GetQueryFromURLMiddleware = require('get_query_from_url_imjano')

Now you can configure acording to your project and insert it before your routes

const getQueryFromURL = GetQueryFromURLMiddleware.configure({
	defaultFields: {
		id: '_id',
		timestampCreatedAt: 'createdAt',
		timestampUpdatedAt: 'updatedAt',
	},
})
app.use(getQueryFromURL)

let's suppose that the path of the request ends as follows

'example?from=2023-05-10'&param=name&equalTo=alex

req.queryFromURL has been injected with query objects

{
	query: { createdAt: { '$gte': '2023-05-10' }, name: 'alex' },
	sort: { createdAt: -1 },
	limit: 100
}

Now you can execute the query

app.get('/example', async (req, res, next) => {
	const { query, sort, limit } = req.queryFromURL
	let users = await UserModel.find(query).sort(sort).limit(limit)
	res.json(users).status(200)
})