Skip to content

Commit

Permalink
Merge pull request #107 from thevahidal/105_add_support_for_filter_op…
Browse files Browse the repository at this point in the history
…erators

Add support for filter operators
  • Loading branch information
thevahidal authored Jul 20, 2023
2 parents b313b67 + 5d44509 commit a721df7
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 29 deletions.
4 changes: 2 additions & 2 deletions core/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "soul-cli",
"version": "0.4.0",
"version": "0.5.0",
"description": "A SQLite REST and Realtime server",
"main": "src/server.js",
"bin": {
Expand Down
42 changes: 37 additions & 5 deletions core/src/controllers/rows.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ const quotePrimaryKeys = (pks) => {
return quotedPks;
};

const operators = {
eq: '=',
lt: '<',
gt: '>',
lte: '<=',
gte: '>=',
neq: '!=',
};

// Return paginated rows of a table
const listTableRows = async (req, res) => {
/*
Expand Down Expand Up @@ -64,17 +73,40 @@ const listTableRows = async (req, res) => {
// filtering is case insensitive
// e.g. ?_filters=name:John,age:20
// will filter by name like '%John%' and age like '%20%'
let filters = [];
try {
filters = _filters.split(',').map((filter) => {
let [key, value] = filter.split(':');

let field = key.split('__')[0];
let fieldOperator = key.split('__')[1];

if (!fieldOperator) {
fieldOperator = 'eq';
} else if (!operators[fieldOperator]) {
throw new Error(
`Invalid field operator "${fieldOperator}" for field "${field}". You can only use the following operators after the "${field}" field: __lt, __gt, __lte, __gte, __eq, __neq.`
);
}

const filters = _filters.split(',').map((filter) => {
const [field, value] = filter.split(':');
return { field, value };
});
let operator = operators[fieldOperator];
return { field, operator, value };
});
} catch (error) {
return res.status(400).json({
message: error.message,
error: error,
});
}

let whereString = '';
if (_filters !== '') {
whereString += ' WHERE ';
whereString += filters
.map((filter) => `${tableName}.${filter.field} = '${filter.value}'`)
.map(
(filter) =>
`${tableName}.${filter.field} ${filter.operator} '${filter.value}'`
)
.join(' AND ');
}

Expand Down
40 changes: 19 additions & 21 deletions docs/api/rows-examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ Response
- `_schema` e.g. `?_schema=Title,ArtistId`, to get only the Title and ArtistId columns.
- `_extend` e.g. `?_extend=ArtistId`, to get the Artist object related to the Album.
- `_filters` e.g. `?_filters=ArtistId:1,Title:Rock`, to get only the rows where the ArtistId is 1 and the Title is Rock.
NOTE: If you want to use comparison operators in the filter, you can use these operators after the field: `__eq, __neq, __lt, __gt, __lte, __gte` . For example, you can use `/invoices/rows?_filters=InvoiceId__neq:1,Total__gte:5`

Example with query params

Expand Down Expand Up @@ -115,11 +116,11 @@ Response

```json
{
"data": {
"AlbumId": 1,
"Title": "For Those About To Rock We Salute You",
"ArtistId": 1
}
"data": {
"AlbumId": 1,
"Title": "For Those About To Rock We Salute You",
"ArtistId": 1
}
}
```

Expand All @@ -143,17 +144,16 @@ Response

```json
{
"data": {
"Title": "Facelift",
"ArtistId_data": {
"ArtistId": 5,
"Name": "Alice In Chains"
}
}
"data": {
"Title": "Facelift",
"ArtistId_data": {
"ArtistId": 5,
"Name": "Alice In Chains"
}
}
}
```


### 4. Update Row(s) by ID

To update a row call `/tables/<table-name>/rows/<lookup-values>/` endpoint with `PUT` method.
Expand All @@ -173,7 +173,7 @@ Response

```json
{
"message": "Row updated"
"message": "Row updated"
}
```

Expand All @@ -195,7 +195,6 @@ Response
}
```


### 5. Delete Row(s) by ID

To delete a row call `/tables/<table-name>/rows/<lookup-values>/` endpoint with `DELETE` method.
Expand All @@ -209,19 +208,18 @@ Response

```json
{
"message": "Row deleted",
"data": {
"changes": 3290,
"lastInsertRowid": 0
}
"message": "Row deleted",
"data": {
"changes": 3290,
"lastInsertRowid": 0
}
}
```

#### Path Params

- `lookup-values` e.g. `1`, to delete the row with the AlbumId 1. or comma-separated values e.g. `1,2`, to delete the row with the AlbumId 1 and ArtistId 2. (Bulk Delete)


#### Query Params

- `_lookup_field` e.g. `?_lookup_field=ArtistId`, to delete the row by the ArtistId field. If not provided, the default lookup field is the primary key of the table.

0 comments on commit a721df7

Please sign in to comment.