I’m making REST APIS with Express.js I have the following express route:
/api/customer
I added multiple query params to the route like this:
JavaScript
x
5
1
/api/customer?name=jake
2
/api/customer?country=america
3
/api/customer?name=jake&country=america
4
/api/customer?name=jake&limit=10
5
In my controllers I handle all of these with If and there are so many cases I feel that this method would not scale, is there a better way of handling this ?
This is the code for my controller, I’m using Sequelize to query the database:
JavaScript
1
79
79
1
async function getAllCustomer(queryLimit, page) {
2
const customers = await Customer.findAll({
3
limit: queryLimit ? parseInt(queryLimit) : null,
4
offset: page ? parseInt(queryLimit) * parseInt(page) : null
5
});
6
7
return customers;
8
}
9
10
async function getCustomerByFirstName(name, queryLimit, page) {
11
return await Customer.findAll({
12
where: {
13
firstName: name,
14
}
15
})
16
}
17
18
async function getCustomerByAddress(address) {
19
return await Customer.findAll({
20
where: {
21
customerAddress: address
22
}
23
})
24
}
25
26
async function getCustomerByNameAddress(name, address) {
27
return await Customer.findAll({
28
where: {
29
[Op.and]: [
30
{firstName: name},
31
{customerAddress: address}
32
]
33
}
34
})
35
}
36
37
async function getCustomer(req, res) {
38
const page = req.query.page;
39
const queryLimit = req.query.limit;
40
const name = req.query.name;
41
const address = req.query.address;
42
43
let customers;
44
/* User want to find first names */
45
if (name && !address) {
46
const names = name.split(",")
47
48
customers = await getCustomerByFirstName(names, queryLimit, page)
49
50
51
res.status(200).send(customers)
52
return;
53
}
54
55
/* User want to find addresses */
56
if (!name && address) {
57
const addresses = address.split(",")
58
customers = await getCustomerByAddress(addresses, queryLimit, page)
59
res.status(200).send(customers)
60
return;
61
}
62
63
/* User want to mix both */
64
if (name && address) {
65
const names = name.split(",")
66
const addresses = address.split(",")
67
customers = await getCustomerByNameAddress(names, addresses, queryLimit, page)
68
69
res.status(200).send(customers)
70
return;
71
}
72
73
if (!name && !address) {
74
customers = await getAllCustomer(queryLimit, page)
75
res.status(200).send(customers)
76
return;
77
}
78
}
79
Advertisement
Answer
You could do something like this:
JavaScript
1
30
30
1
async function getCustomer(req, res) {
2
const page = req.query.page;
3
const queryLimit = req.query.limit;
4
const name = req.query.name;
5
const address = req.query.address;
6
7
let query = { };
8
if(name) {
9
query.firstName = name;
10
}
11
12
if(address) {
13
query.address = address;
14
}
15
16
let customers = await getCustomers(query, queryLimit, page);
17
res.status(200).send(customers)
18
return;
19
}
20
21
async function getCustomers(query, queryLimit, page) {
22
const customers = await Customer.findAll({
23
where: query,
24
limit: queryLimit ? parseInt(queryLimit) : null,
25
offset: page ? parseInt(queryLimit) * parseInt(page) : null
26
});
27
28
return customers;
29
}
30
BTW, in your code, the functions getCustomerByFirstName
, getCustomerByAddress
and getCustomerByNameAddress
are expecting to receive name
and address
as string parameter, but you are passing names
and addresses
array. This might lead to errors…