-
-
Notifications
You must be signed in to change notification settings - Fork 323
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
pull common SQL adapters and helpers into reusable functions #595
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
# SQL helpers | ||
|
||
Files can be found in [src/server/sql](/src/server/sql). | ||
|
||
These are functions useful in ETL and reshaping operations | ||
for database -> resolver data shapes. | ||
|
||
### orderedFor | ||
|
||
In `helpers.js` | ||
|
||
``` | ||
orderedFor( rows, collection, field, singleObject ) | ||
``` | ||
|
||
__rows__ is the rows returned from the database, | ||
there are multiple rows for each element of the collection. | ||
|
||
__collection__ is the set of items you are getting multiple rows for from the database. | ||
|
||
__field__ is the object filed in the rows to group the results by | ||
and then associate the elements of the collection. | ||
|
||
__singleObject__ is a boolean which determines if each collection | ||
entry is a single object (1-1 relationship) or not (1-N) relationship. | ||
|
||
It returns an array of objects in the first case, and | ||
an array of arrays of objects in the second case. | ||
|
||
--- | ||
|
||
The adapters are a work in progress and | ||
require some changes to how arguments | ||
to the graphql query are passed around in the server. | ||
The query also has more parameters, though maybe they should | ||
be combined into a single object with subfields | ||
for the different capabilities...? | ||
|
||
### paging | ||
|
||
In `paging.js`, adds limit and offset | ||
|
||
``` | ||
paging(queryBuilder, args) | ||
``` | ||
|
||
__queryBuilder__ is a knex instance. | ||
|
||
__args__ is an object with fields `{limit, offset}` | ||
|
||
The function returns the queryBuilder object after checking for and adding | ||
limit and/or offset knex calls. | ||
|
||
### cursor | ||
|
||
TBD... cursor based paging | ||
|
||
|
||
### currentOrdering | ||
|
||
In `ordering.js` | ||
uses the current code an orderBy argument | ||
|
||
|
||
### ordering | ||
|
||
In `ordering.js`, looks for an array of OrderBy GraphQL type | ||
|
||
An orderby object looks like `{column, table, order}`, only column is required. | ||
|
||
|
||
``` | ||
ordering(queryBuilder, args) | ||
``` | ||
|
||
__queryBuilder__ is a knex instance. | ||
|
||
__args__ is an array named `orderBys` containing objects with the fields above. | ||
|
||
The function returns the queryBuilder object after checking for | ||
and adding `orderBy` knex calls. | ||
|
||
### currentFilter | ||
|
||
In `filters.js` | ||
uses the current code and filter argument | ||
|
||
|
||
### filterBuilder | ||
|
||
uses a more advanced filtering input and processing. | ||
|
||
See https://github.com/sysgears/apollo-universal-starter-kit/issues/569 | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
import { decamelize } from 'humps'; | ||
import { has } from 'lodash'; | ||
|
||
export function currentFilter(queryBuilder, filter) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like this function is specific to |
||
if (filter) { | ||
if (has(filter, 'role') && filter.role !== '') { | ||
queryBuilder.where(function() { | ||
this.where('u.role', filter.role); | ||
}); | ||
} | ||
|
||
if (has(filter, 'isActive') && filter.isActive !== null) { | ||
queryBuilder.where(function() { | ||
this.where('u.is_active', filter.isActive); | ||
}); | ||
} | ||
|
||
if (has(filter, 'searchText') && filter.searchText !== '') { | ||
queryBuilder.where(function() { | ||
this.where('u.username', 'like', `%${filter.searchText}%`) | ||
.orWhere('u.email', 'like', `%${filter.searchText}%`) | ||
.orWhere('up.first_name', 'like', `%${filter.searchText}%`) | ||
.orWhere('up.last_name', 'like', `%${filter.searchText}%`); | ||
}); | ||
} | ||
} | ||
|
||
return queryBuilder; | ||
} | ||
|
||
export function filterBuilder(queryBuilder, args) { | ||
let { filters } = args; | ||
|
||
console.log('FILTERS', filters); | ||
|
||
// add filter conditions | ||
if (filters) { | ||
let first = true; | ||
for (let filter of filters) { | ||
// Pre Filters Recursion | ||
if (filter.prefilters) { | ||
if (first) { | ||
first = false; | ||
queryBuilder.where(function() { | ||
filterBuilder(this, { filters: filter.prefilters }); | ||
}); | ||
} else { | ||
if (filter.filterBool === 'and') { | ||
queryBuilder.andWhere(function() { | ||
filterBuilder(this, { filters: filter.prefilters }); | ||
}); | ||
} else if (filter.filterBool === 'or') { | ||
queryBuilder.orWhere(function() { | ||
filterBuilder(this, { filters: filter.prefilters }); | ||
}); | ||
} else { | ||
// Default to OR | ||
queryBuilder.orWhere(function() { | ||
filterBuilder(this, { filters: filter.prefilters }); | ||
}); | ||
} | ||
} | ||
} | ||
|
||
// This Filter Visitation | ||
if (filter.field) { | ||
let column = filter.field; | ||
if (filter.table) { | ||
column = filter.table + '.' + column; | ||
} | ||
column = decamelize(column); | ||
|
||
let compare = '='; | ||
if (filter.compare) { | ||
compare = filter.compare; | ||
} | ||
|
||
let value = filter.value ? filter.value : filter.values; | ||
if (!value) { | ||
value = filter.timeValue ? filter.timeValue : filter.timeValues; | ||
if (!value) { | ||
value = filter.intValue ? filter.intValue : filter.intValues; | ||
} | ||
if (!value) { | ||
value = filter.floatValue ? filter.floatValue : filter.floatValues; | ||
} | ||
if (!value) { | ||
value = filter.boolValue ? filter.boolValue : filter.boolValues; | ||
} | ||
} | ||
|
||
if (first) { | ||
first = false; | ||
queryBuilder.where(column, compare, value); | ||
} else { | ||
if (filter.bool === 'and') { | ||
queryBuilder.andWhere(column, compare, value); | ||
} else if (filter.bool === 'or') { | ||
queryBuilder.orWhere(column, compare, value); | ||
} else { | ||
// Default to OR | ||
queryBuilder.orWhere(column, compare, value); | ||
} | ||
} | ||
} | ||
|
||
// Post Filters Recursion | ||
if (filter.postfilters) { | ||
if (first) { | ||
first = false; | ||
queryBuilder.where(function() { | ||
filterBuilder(this, { filters: filter.postfilters }); | ||
}); | ||
} else { | ||
if (filter.filterBool === 'and') { | ||
queryBuilder.andWhere(function() { | ||
filterBuilder(this, { filters: filter.postfilters }); | ||
}); | ||
} else if (filter.filterBool === 'or') { | ||
queryBuilder.orWhere(function() { | ||
filterBuilder(this, { filters: filter.postfilters }); | ||
}); | ||
} else { | ||
// Default to OR | ||
queryBuilder.orWhere(function() { | ||
filterBuilder(this, { filters: filter.postfilters }); | ||
}); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
return queryBuilder; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { decamelize } from 'humps'; | ||
|
||
export function currentOrdering(queryBuilder, orderBy) { | ||
if (orderBy && orderBy.column) { | ||
let column = orderBy.column; | ||
let order = 'asc'; | ||
if (orderBy.order) { | ||
order = orderBy.order; | ||
} | ||
|
||
queryBuilder.orderBy(decamelize(column), order); | ||
} | ||
|
||
return queryBuilder; | ||
} | ||
|
||
export function ordering(queryBuilder, args) { | ||
let { orderBys } = args; | ||
|
||
// add order by | ||
if (orderBys) { | ||
for (let orderBy of orderBys) { | ||
if (orderBy && orderBy.column) { | ||
let column = orderBy.column; | ||
if (orderBy.table) { | ||
column = orderBy.table + '.' + column; | ||
} | ||
column = decamelize(column); | ||
|
||
let order = 'asc'; | ||
if (orderBy.order) { | ||
order = orderBy.order; | ||
} | ||
queryBuilder.orderBy(column, order); | ||
} | ||
} | ||
} | ||
|
||
return queryBuilder; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
export default function paging(queryBuilder, args) { | ||
const { offset, limit } = args; | ||
|
||
if (offset) { | ||
queryBuilder.offset(offset); | ||
} | ||
|
||
if (limit) { | ||
queryBuilder.limit(limit); | ||
} | ||
|
||
return queryBuilder; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it okay to send user to the PR in the docs?