Skip to content

Commit

Permalink
Merge branch 'prettier' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeffrey Yan committed Jul 2, 2020
2 parents fb39cbe + 51df2e6 commit 5224727
Show file tree
Hide file tree
Showing 13 changed files with 832 additions and 357 deletions.
51 changes: 51 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
module.exports = {
root: true,
parser: '@typescript-eslint/parser',
plugins: [
'@typescript-eslint',
],
extends: [
'airbnb-base',
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
'prettier'
],
settings: {
'import/resolver': {
node: {
extensions: ['.js', '.ts']
}
}
},
rules: {
'max-len': 'error',
'no-underscore-dangle': 'off',
'comma-dangle': 'off',
'space-before-function-paren': 'off',
'func-names': 'off',
'object-shorthand': 'off',
'prefer-arrow-callback': 'off',
'consistent-return': 'off',
'no-use-before-define': 'off',
'guard-for-in': 'off',
'no-restricted-syntax': 'off',
'no-continue': 'off',
'prefer-template': 'off',
'newline-per-chained-call': 'off',
'linebreak-style': 'off',
'arrow-parens': 'off',
'no-unused-expressions': 'off',
'no-param-reassign': 'off',
'no-nested-ternary': 'off',
'one-var-declaration-per-line': 'off',
'one-var': 'off',
'prefer-destructuring': 'off',
'prefer-object-spread': 'off',
'import/extensions': 'off',
'import/prefer-default-export': 'off'
},
env: {
node: true,
mocha: true
}
}
33 changes: 0 additions & 33 deletions .eslintrc.json

This file was deleted.

5 changes: 5 additions & 0 deletions .prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
bracketSpacing: true,
singleQuote: true,
trailingComma: 'none'
};
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
sudo: false
language: node_js
node_js:
- '8'
- '10'
- '12'
services:
- postgresql
- mysql
Expand Down
21 changes: 19 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "A filter module for objection.js",
"main": "./dist/index.js",
"engines": {
"node": ">=8.0.0"
"node": ">=10.0.0 <=12.x.x"
},
"scripts": {
"build": "tsc",
Expand All @@ -14,6 +14,17 @@
"prepublish": "yarn run build",
"coverage": "nyc report --reporter=text-lcov | coveralls"
},
"husky": {
"hooks": {
"pre-commit": "lint-staged"
}
},
"lint-staged": {
"*.ts": [
"eslint",
"prettier --write"
]
},
"repository": {
"type": "git",
"url": "https://github.com/tandg-digital/objection-filter"
Expand All @@ -36,21 +47,27 @@
"devDependencies": {
"@types/lodash": "^4.14.155",
"@types/node": "^14.0.13",
"@typescript-eslint/eslint-plugin": "^3.4.0",
"@typescript-eslint/parser": "^3.4.0",
"babel-cli": "^6.26.0",
"babel-preset-es2015": "^6.24.1",
"bluebird": "^3.5.1",
"chai": "^4.1.2",
"coveralls": "^3.0.9",
"cross-env": "^5.1.3",
"eslint": "^6.8.0",
"eslint": "^7.3.1",
"eslint-config-airbnb-base": "^14.0.0",
"eslint-config-prettier": "^6.11.0",
"eslint-plugin-import": "^2.13.0",
"husky": "^4.2.5",
"knex": "0.20.15",
"lint-staged": "^10.2.11",
"mocha": "^7.0.1",
"mysql": "^2.16.0",
"nyc": "^15.0.0",
"objection": "^2.1.2",
"pg": "^7.4.3",
"prettier": "^2.0.5",
"sqlite3": "^4.1.1",
"typescript": "^3.9.5"
},
Expand Down
3 changes: 2 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
import { debug as _debug} from 'debug';
import _debug from 'debug';

export const debug = _debug('objection:filter');
15 changes: 6 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
import { default as _FilterQueryBuilder } from './lib/FilterQueryBuilder';
import { Transaction, Model } from 'objection';
import _FilterQueryBuilder from './lib/FilterQueryBuilder';
import { sliceRelation as _sliceRelation } from './lib/utils';
import { createRelationExpression as _createRelationExpression } from './lib/ExpressionBuilder';
import {
BaseModel,
FilterQueryBuilderOptions
} from './lib/types';
import { Transaction, Model } from 'objection';
import { BaseModel, FilterQueryBuilderOptions } from './lib/types';

export function buildFilter<M extends BaseModel, K extends typeof Model>(
modelClass: K,
trx: Transaction,
options: FilterQueryBuilderOptions<M>
) {
): _FilterQueryBuilder<M, K> {
return new FilterQueryBuilder(modelClass, trx, options);
};
}
export const FilterQueryBuilder = _FilterQueryBuilder;
export const sliceRelation = _sliceRelation;
export const createRelationExpression = _createRelationExpression;
export const createRelationExpression = _createRelationExpression;
19 changes: 9 additions & 10 deletions src/lib/ExpressionBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { set, get, keys, map} from 'lodash';
import { set, get, keys, map } from 'lodash';
import { sliceRelation } from './utils';

/**
Expand All @@ -8,7 +8,10 @@ import { sliceRelation } from './utils';
* @param {Object} tree A tree of relations
* @param {String} relationName The top level relation
*/
function toRelationSubExpression(tree: object, relationName?: string): string {
function toRelationSubExpression(
tree: Record<string, unknown>,
relationName?: string
): string {
if (Object.keys(tree).length === 0) return relationName;

// Recursively apply to all attributes
Expand All @@ -20,7 +23,7 @@ function toRelationSubExpression(tree: object, relationName?: string): string {
return keys(tree).length === 1
? `${prefix}${expression}`
: `${prefix}[${expression}]`;
};
}

/**
* Takes an array of fully qualified field names, and concatenates them into a single
Expand All @@ -32,18 +35,14 @@ function toRelationSubExpression(tree: object, relationName?: string): string {
export function createRelationExpression(fields: string[]): string {
// For each field, set some arbitrarily deep property
const tree = {};
fields.forEach(field => {
fields.forEach((field) => {
const { relationName } = sliceRelation(field);
if (!relationName) return;

// Set the node of the tree if it doesn't exist yet
set(
tree,
relationName,
get(tree, relationName, {})
);
set(tree, relationName, get(tree, relationName, {}));
});

// Reduce the property map into a nested expression
return toRelationSubExpression(tree);
};
}

0 comments on commit 5224727

Please sign in to comment.