You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+59-1Lines changed: 59 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -57,7 +57,8 @@ options = {
57
57
filters: {}
58
58
},
59
59
use_page:false,
60
-
client_db:'mongodb'
60
+
client_db:'mongodb',
61
+
date_field:'created_at'
61
62
}
62
63
```
63
64
If the options are not provided, the default values will be used for the treatment of queries strings.
@@ -71,6 +72,7 @@ const app = express()
71
72
app.use(qs({
72
73
use_page:true,
73
74
client_db:'mongodb',
75
+
date_field:'created_at'
74
76
default: {
75
77
fields: {name:1 , age:1, number:1, _id:0},
76
78
sort: { created_at:'desc' },
@@ -96,11 +98,67 @@ app.use(qs({
96
98
* }
97
99
*/
98
100
```
101
+
102
+
### 3. Most Usage In Queries
103
+
#### 3.1 Partial Answers
104
+
| Query | Description | Result |
105
+
| ------ | ------ | ------ |
106
+
|`?fields=name,age`| Search where the user wants only the name and age of the user. |`{ fields: { name: 1, age: 1 } }`|
107
+
108
+
#### 3.2 Pagination
109
+
**3.2.1 Pagination With Skip**
110
+
111
+
| Query | Description | Result |
112
+
| ------ | ------ | ------ |
113
+
|`?limit=10&skip=0`| Search where the user wants only 10 results, without skip any. |`{ pagination: { limit: 10, skip: 0 } }`|
114
+
115
+
**3.2.2 Pagination With page**
116
+
117
+
| Query | Description | Result |
118
+
| ------ | ------ | ------ |
119
+
|`?limit=10&page=2`| Search where the user wants results only 10 results, but the second page (in this case, from the 11th to the 20th result). |`{ pagination: { limit: 10, page: 2 } }`|
120
+
121
+
#### 3.3 Ordination
122
+
| Query | Description | Result |
123
+
| ------ | ------ | ------ |
124
+
|`?sort=name,-age`| Search where the user wants results to be sorted in ascending order by name and in descending order by age. |`{ sort: { name: 'asc', age: 'desc' } }`|
125
+
126
+
#### 3.4 Filtering
127
+
| Query | Description | Result |
128
+
| ------ | ------ | ------ |
129
+
|`?name=elvis&age=80`| Search where the user wants results that name equals elvis and age equals 80. |`{ filters: { name: 'elvis', age: 80 } }`|
130
+
|`?name=elvis&name=john`| Search where the usar wants results that name equals elvis and john. |`{ filters: { $and: [ { name: 'elvis' }, { name: john } ] } }`|
131
+
|`?name=elvis,john`| Search where the user wants results that name equals elvis or john. |`{ filters: { $or: [ { name: 'elvis' }, { name: john } ] } }`|
132
+
|`?age=gt:30`| Search where the user wants results that age is greater than 30. |`{ filters: { age: { $gt: 30 } } }`|
133
+
|`?age=gte:30`| Search where the user wants results that age is greater or equal than 30. |`{ filters: { age: { $gte: 30 } } }`|
134
+
|`age=lt:30`| Search where the user wants results that age is lower than 30. |`{ filters: { age: { $lt: 30 } } }`|
135
+
|`age=lte:30`| Search where the user wants results that age is lower or equal than 30. |`{ filters: { age: { $lte: 30 } } }`|
136
+
137
+
#### 3.5 Data
138
+
| Query | Description | Result |
139
+
| ------ | ------ | ------ |
140
+
|`?date_start=2018-11-10&date_end=2018-12-10`| Search where the user wants results between 2018-12-10 and 2018-12-12. |`{ filters: { $and: [ { created_at: { $lt: '2018-12-10T00:00:00.000Z' } }, { created_at: { $gte: '2018-11-10T00:00:00.000Z' } } ] } }`|
141
+
|`?date_start=2018-12-10`| Search where the user wants results between 2018-12-10 and the current date. In this example, the current day is: 2018-12-12. |`{ filters: { $and: [ { created_at: { $lt: '2018-12-13T00:00:00.000Z' } }, { created_at: { $gte: '2018-12-10T00:00:00.000Z' } } ] } }`|
142
+
|`?date_end=2018-12-11&period=10d`| Search where the user wants results from 10 days before 2018-12-12. |`{ filters: { $and: [ { created_at: { $lt: '2018-12-11T00:00:00.000Z' } }, { created_at: { $gte: '2018-11-30T00:00:00.000Z' } } ] } }`|
143
+
|`?period=10d`| Search where the user wants results from 10 days before the current date. In this example, the current day is: 2018-12-12. |`{ filters: { $and: [ { created_at: { $lt: '2018-12-13T00:00:00.000Z' } }, { created_at: { $gte: '2018-12-02T00:00:00.000Z' } } ] } }`|
144
+
|`?date_end=2018-12-11&period=8w`| Search where the user wants results from 8 weeks before 2018-12-12. |`{ filters: { $and: [ { created_at: { $lt: '2018-12-11T00:00:00.000Z' } }, { created_at: { $gte: '2018-10-15T00:00:00.000Z' } } ] } }`|
145
+
|`?period=8w`| Search where the user wants results from 8 weeks before the current date. In this example, the current day is: 2018-12-12. |`{ filters: { $and: [ { created_at: { $lt: '2018-12-13T00:00:00.000Z' } }, { created_at: { $gte: '2018-10-17T00:00:00.000Z' } } ] } }`|
146
+
|`?date_end=2018-12-11&period=6m`| Search where the user wants results from 6 months before 2018-12-11. |`{ filters: { $and: [ { created_at: { $lt: '2018-12-11T00:00:00.000Z' } }, { created_at: { $gte: '2018-06-10T03:00:00.000Z' } } ] } }`|
147
+
|`?period=6m`| Search where the user wants results from 6 months before the current date. In this example, the current day is: 2018-12-12. |`{ filters: { $and: [ { created_at: { $lt: '2018-12-13T00:00:00.000Z' } }, { created_at: { $gte: '2018-06-12T03:00:00.000Z' } } ] } }`|
148
+
|`?date_end=2018-12-11&period=4y`| Search where the user wants results from 4 years before 2018-12-11. |`{ filters: { $and: [ { created_at: { $lt: '2018-12-11T00:00:00.000Z' } }, { created_at: { $gte: '2014-12-10T00:00:00.000Z' } } ] } }`|
149
+
|`?period=4y`| Search where the user wants results from 4 years before the current date. In this example, the current day is: 2018-12-12. |`{ filters: { $and: [ { created_at: { $lt: '2018-12-13T00:00:00.000Z' } }, { created_at: { $gte: '2014-12-12T00:00:00.000Z' } } ] } }`|
150
+
151
+
#### 3.6 Search
152
+
// TODO
153
+
99
154
**NOTES** :
100
155
* Default values are used only when they are not passed in the query string. For example, if you set the default value `?sort=-age`_(age in descending order)_ and your client makes a request with `?sort=name`_(name in ascending order)_, you will get in req.query the value `{ sort: { name: 'asc' } }`, since the values passed by the client will always have preference.
101
156
* Remember to set whether you will use the page or not in the options. If you do not set this setting, it will work without page param, even if you pass the page setup on paging.
102
157
* If you use custom configurations, the query configurations must be insert in json with name 'default'.
158
+
* The accepted date format of Data params is yyyy-MM-dd or yyyy-MM-dd: hh: mm: ss. Any date outside this format will not work correctly.
159
+
* The **date_field** parameter in the options is used in cases where you want to use filters with Date. If this value is not specified in the middleware configuration options, its default value will be 'created_at'.
103
160
* The configurations that you don't set in middleware options it will be the default settings.
161
+
104
162
### Query Support
105
163
106
164
This middleware supports the queries as follow the pattern bellow:
0 commit comments