Skip to content

Commit cb0c6ae

Browse files
update readme and fix some bugs
1 parent 2e2994e commit cb0c6ae

File tree

4 files changed

+71
-7
lines changed

4 files changed

+71
-7
lines changed

README.md

Lines changed: 67 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ Accept query parameters in express or similar libraries and convert them into sy
99
* Basic & Multiple-Column Sorting support. Query and sort your results by 1-or-more columns.
1010
* Parses string input to integers, floats, booleans and dates.
1111
* Accepts non-string values (though from express it will likely come through as a string type anyway).
12+
* Allows specified DATETIME fields to be compared on the DATE-part only.
13+
* Blacklisting.
14+
* Aliasing.
15+
* Custom handlers for specific column/query-key names.
1216

1317
### Operations
1418

@@ -33,6 +37,59 @@ _You can see the configuration section to see how the operations can be configur
3337

3438
_**Note:** I use `[Op.gte]` & `[Op.lte]` instead of `[Op.between]` to handle dates and other types better. I will investigate if the latter is a better option._
3539

40+
### Aliasing & Blacklisting
41+
By setting a column/property value into the Blacklist, you are telling the parser to simply ignore the key-value pair and not include it in the final output. With Aliasing, you can convert a specific incoming key-value pair to match the name of a known column. For example, converting incoming `personsAge=12` to `age=12`. Both of these can be configured in the initialise options.
42+
43+
```javascript
44+
const sequelizeDateQS = new SequelizeQS({
45+
blacklist: ['createdAt'],
46+
alias: {
47+
'personsAge': 'age'
48+
}
49+
});
50+
```
51+
52+
### Date Only
53+
You can now easily (as of v1.1.0) have specific Date-type columns converted to compare on the Date only instead of including the Timestamp. DATEONLY fields in Sqlite are still working as they did before though!
54+
55+
To prevent breaking changes from prior versions and to also not rely on guess working with the code, you will need to specify what columns in your Sequelize model are actual Date columns you want to be handled in this way. You must also enable this feature by setting the `dateOnlyCompare` property in the options to `true`.
56+
57+
```javascript
58+
const sequelizeDateQS = new SequelizeQS({
59+
dateOnlyCompare: true,
60+
dateFields: ['createdAt', 'updatedAt', 'lastLogin']
61+
});
62+
```
63+
64+
Now when we compare on the `lastLogin` column, our query can come out like;
65+
66+
```mysql
67+
SELECT count(*) AS `count` FROM `customers` AS `customers` WHERE date(`lastLogin`) LIKE '2021-%';
68+
```
69+
70+
By default, `dateOnlyCompare` will be `false` and the initial array value for `dateFields` will contain the original timestamp columns that sequelize typically adds to your models by default (createdAt & updatedAt).
71+
72+
### Custom Handlers
73+
A big improvement over the initial version is the ability to have the parser handle specific columns/query string properties in a specific way. This is configured through the initial options when setting up the parser.
74+
75+
Please note, that the value your custom function must return is one that would be interpreted by the Sequelize library. You can see an example below for more details;
76+
77+
```javascript
78+
const { Op } = require('sequelize');
79+
80+
const minimumAgeHandler = (column, value, options) => {
81+
return {
82+
'age': [Op.gte]: value
83+
}
84+
};
85+
86+
const sequelizeDateQS = new SequelizeQS({
87+
customHandlers: {
88+
minAge: minimumAgeHandler
89+
}
90+
});
91+
```
92+
3693
### Pagination
3794

3895
This library contains some basics for parsing pagination and setting default values. There is checks in place to ensure no negative pages are set and also that not *too many* records are pulled down. The `default maximum page size is 100` but this can be configured in the options. You can also omit `page` and `limit` from the parameters and no paging will take place. Though, setting 1 or both will have an effect or enabling pagination.
@@ -73,14 +130,20 @@ const sequelizeParser = SequelizeQS();
73130

74131
### Configuration Options
75132

76-
You can pass additional configuration options into the constructor for the parser. This will change how the parser operations. **This is currently pretty experimental and does not have all the options available. You should be cautious with the order of operations otherwise you might get some unintended results!**
133+
You can pass additional configuration options into the constructor for the parser. This will change how the parser operations. **As of Version 1.1, this have been a little more fleshed out. However, This is currently pretty experimental and does not have all the options available. You should be cautious with the order of operations otherwise you might get some unintended results!**
77134

78135
| property | decription | default |
79136
|------------------------|------------------------------------------------------------------------------|--------------|
80137
| ops | The available operations. | `['$in', '$nin', '$', '!', '\|', '^', '~', '>=', '>', '<=', '<']` |
138+
| alias | The alias matching for properties and db columns. | `{ }` |
139+
| blacklist | The list of columns that should be ignored and not compared on | `[ ]` |
140+
| customHandlers | List of available custom handlers for specific columns | `{ }` |
141+
| dateOnlyCompare | Tells the parser to convert recognised `datetime fields` to `date-only` for comparisons | `false` |
142+
| dateFields | List of date columns in your model that are datetime type fields | `['createdAt', 'updatedAt']` |
81143
| defaultPaginationLimit | Limit to default too when no limit is set or maximum size has been exceeded. | `25` |
82144
| maximumPageSize | The maximum page size allowed. | `100` |
83145

146+
84147
### Parse
85148

86149
`fetch me the first page of 25 where the age of the customer is between 20 & 30 and the order total is more or equal to £100`
@@ -111,9 +174,10 @@ This library was primarily built for myself when building multiple small API's a
111174
Some features that I have thought about but not necessarily implemented include;
112175

113176
* complex queries with AND & OR operators.
114-
* white & blacklist for specific parameter names.
115-
* custom functions that execute when a specific parameter is included.
177+
* ~~white & blacklist for specific parameter names.~~
178+
* ~~custom functions that execute when a specific parameter is included.~~
116179
* advanced sorting by functions (e.g. ordering by max(age))
180+
* ~~DATE-only comparison.~~
117181

118182
## Collaborators
119183

index.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,6 @@ class SequelizeQS {
116116

117117
for (const column of columns) {
118118
let key = this.alias[column] || column; // handles an alias
119-
console.log('column', column);
120119
let value = properties[key];
121120

122121
// check the blacklist
@@ -132,7 +131,7 @@ class SequelizeQS {
132131
let customOperation = this.customHandlers[key](key, value, this.opt);
133132
where = {
134133
...where,
135-
[key]: customOperation
134+
...customOperation
136135
};
137136

138137
break;

operations.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ const comparatorMap = {
2424

2525
const handleBasicComparator = (operationStr, column, value, options) => {
2626
// check & strip if we have the comparator at the start
27-
let strippedValue = (value && value.startsWith(operationStr))
27+
console.log('VALUE', value);
28+
let strippedValue = (value && typeof(value) === 'string' && value.startsWith(operationStr))
2829
? value.substring(operationStr.length)
2930
: value;
3031

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "sequelizeqp",
3-
"version": "1.0.1",
3+
"version": "1.1.0",
44
"description": "Small library to convert express query params (or objects in general) into a syntax recognised by the Sequelize library.",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)