Skip to content
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
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 93 additions & 0 deletions docs/sql-helpers.md
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
Copy link
Member

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?

4 changes: 2 additions & 2 deletions src/server/modules/user/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import settings from '../../../../settings';

export default pubsub => ({
Query: {
users: withAuth(['user:view:all'], (obj, { orderBy, filter }, context) => {
return context.User.getUsers(orderBy, filter);
users: withAuth(['user:view:all'], (obj, { orderBy, filter, limit, offset }, context) => {
return context.User.getUsers(orderBy, filter, limit, offset);
}),
user: withAuth(
(obj, args, context) => {
Expand Down
7 changes: 6 additions & 1 deletion src/server/modules/user/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,12 @@ type Tokens {

extend type Query {
# Get all users ordered by: OrderByUserInput add filtered by: FilterUserInput
users(orderBy: OrderByUserInput, filter: FilterUserInput): [User]
users(
orderBy: OrderByUserInput
filter: FilterUserInput
limit: Int
offset: Int
): [User]
# Get user by id
user(id: Int!): User
# Get current user
Expand Down
49 changes: 13 additions & 36 deletions src/server/modules/user/sql.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
// Helpers
import { camelizeKeys, decamelizeKeys, decamelize } from 'humps';
import { has } from 'lodash';
import { camelizeKeys, decamelizeKeys } from 'humps';
import bcrypt from 'bcryptjs';
import knex from '../../../server/sql/connector';

import { ordering } from '../../../server/sql/ordering';
import paging from '../../../server/sql/paging';
import { currentFilter } from '../../../server/sql/filters';

// Actual query fetching and transformation in DB
export default class User {
async getUsers(orderBy, filter) {
const queryBuilder = knex
async getUsers(orderBy, filter, limit, offset) {
let queryBuilder = knex
.select(
'u.id as id',
'u.username',
Expand All @@ -28,40 +31,14 @@ export default class User {
.leftJoin('auth_facebook AS fa', 'fa.user_id', 'u.id')
.leftJoin('auth_google AS ga', 'ga.user_id', 'u.id');

// add order by
if (orderBy && orderBy.column) {
let column = orderBy.column;
let order = 'asc';
if (orderBy.order) {
order = orderBy.order;
}

queryBuilder.orderBy(decamelize(column), order);
}
// Add filters
queryBuilder = currentFilter(queryBuilder, filter);

// add filter conditions
if (filter) {
if (has(filter, 'role') && filter.role !== '') {
queryBuilder.where(function() {
this.where('u.role', filter.role);
});
}
// Add limit / offset
queryBuilder = paging(queryBuilder, { limit, offset });

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}%`);
});
}
}
// add result ordering
queryBuilder = ordering(queryBuilder, [orderBy]);

return camelizeKeys(await queryBuilder);
}
Expand Down
135 changes: 135 additions & 0 deletions src/server/sql/filters.js
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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this function is specific to user module. Why is it here, then?

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;
}
40 changes: 40 additions & 0 deletions src/server/sql/ordering.js
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;
}
13 changes: 13 additions & 0 deletions src/server/sql/paging.js
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;
}