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

adding ability to select fields, on single resource only for now #39

Merged
merged 3 commits into from
Feb 26, 2019
Merged
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
87 changes: 55 additions & 32 deletions src/processors/knex-processor.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,50 @@
import { camelize } from "ember-cli-string-utils";
import * as Knex from "knex";
import * as pluralize from "pluralize";
import { camelize } from 'ember-cli-string-utils';

import Resource from "../resource";
import { KnexRecord, Operation, ResourceConstructor } from "../types";

import OperationProcessor from "./operation-processor";

const operators = {
eq: '=',
ne: '!=',
lt: '<',
gt: '>',
le: '<=',
ge: '>=',
like: 'like',
in: 'in',
nin: 'not in'
eq: "=",
ne: "!=",
lt: "<",
gt: ">",
le: "<=",
ge: ">=",
like: "like",
in: "in",
nin: "not in"
};

const getOperator = (paramValue: string): string =>
operators[Object.keys(operators).find(operator => paramValue.indexOf(`${operator}:`) === 0)];

const buildSortClause = (sort) => sort.split(',').map(criteria => {
if (criteria.startsWith('-')) {
return { field: camelize(criteria.substr(1)), direction: 'DESC' };
operators[
Object.keys(operators).find(
operator => paramValue.indexOf(`${operator}:`) === 0
)
];

const buildSortClause = sort =>
sort.split(",").map(criteria => {
if (criteria.startsWith("-")) {
return { field: camelize(criteria.substr(1)), direction: "DESC" };
}

return { field: camelize(criteria), direction: 'ASC' };
return { field: camelize(criteria), direction: "ASC" };
});

const getAttributes = (attributes, fields, type): [] => {
if (Object.entries(fields).length === 0 && fields.constructor === Object) {
return attributes;
}

return attributes.filter(attribute =>
fields[pluralize(type)].includes(attribute)
);
};

export default class KnexProcessor<
ResourceT = Resource
> extends OperationProcessor<ResourceT> {
Expand All @@ -42,15 +57,21 @@ export default class KnexProcessor<
}

async get(op: Operation): Promise<ResourceT[]> {
const { id, type } = op.ref;
const { params, ref } = op;
const { id, type } = ref;
const tableName = this.typeToTableName(type);
const filters = op.params ? { id, ...(op.params.filter || {}) } : { id };
const Resource = Object.create(this.resourceFor(type));
const attributes = Object.keys(Resource.__proto__.attributes);
const filters = params ? { id, ...(params.filter || {}) } : { id };
const resource = this.resourceFor(type);
const fields = params ? { ...params.fields } : {};
const attributes = getAttributes(
Object.keys(resource.prototype.attributes),
fields,
type
);

const records: KnexRecord[] = await this.knex(tableName)
.where(queryBuilder => this.filtersToKnex(queryBuilder, filters))
.select(...attributes, 'id')
.select(...attributes, "id")
.modify(queryBuilder => this.optionsBuilder(queryBuilder, op));

return this.convertToResources(type, records);
Expand Down Expand Up @@ -113,30 +134,32 @@ export default class KnexProcessor<
const processedFilters = [];

Object.keys(filters).forEach(
key => filters[key] === undefined && delete filters[key],
key => filters[key] === undefined && delete filters[key]
);

Object.keys(filters).forEach((key) => {

Object.keys(filters).forEach(key => {
let value = filters[key];
let operator = getOperator(filters[key]) || '=';
const operator = getOperator(filters[key]) || "=";

if (value.substring(value.indexOf(':') + 1)) {
value = value.substring(value.indexOf(':') + 1)
if (value.substring(value.indexOf(":") + 1)) {
value = value.substring(value.indexOf(":") + 1);
}

value = value !== 'null' ? value : 0;

value = value !== "null" ? value : 0;

processedFilters.push({
value,
operator,
column: camelize(key),
column: camelize(key)
});
});

return processedFilters.forEach((filter) => {
return queryBuilder.andWhere(filter.column, filter.operator, filter.value);
return processedFilters.forEach(filter => {
return queryBuilder.andWhere(
filter.column,
filter.operator,
filter.value
);
});
}

Expand Down