Skip to content

Commit

Permalink
Merge branch 'release/4.0.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeffrey Yan committed Jul 8, 2020
2 parents eb426a4 + 93d86ff commit ae3ce50
Show file tree
Hide file tree
Showing 28 changed files with 2,226 additions and 1,078 deletions.
51 changes: 51 additions & 0 deletions .eslintrc.js
@@ -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.

3 changes: 2 additions & 1 deletion .gitignore
@@ -1,4 +1,5 @@
node_modules
dist
.nyc_output
coverage
coverage
.vscode
3 changes: 2 additions & 1 deletion .npmignore
@@ -1,3 +1,4 @@
src
coverage
.nyc_output
.nyc_output
.vscode
5 changes: 5 additions & 0 deletions .prettierrc.js
@@ -0,0 +1,5 @@
module.exports = {
bracketSpacing: true,
singleQuote: true,
trailingComma: 'none'
};
3 changes: 2 additions & 1 deletion .travis.yml
@@ -1,7 +1,8 @@
sudo: false
language: node_js
node_js:
- '8'
- '10'
- '12'
services:
- postgresql
- mysql
Expand Down
8 changes: 8 additions & 0 deletions doc/CHANGELOG.md
@@ -1,5 +1,13 @@
# Changelog

### 4.0.0

I've made this a major version bump due to the Typescript update, and the inclusion of typings. There are no new features additions though, so no need for migration.

- Refactor main codebase to [Typescript](https://www.typescriptlang.org/)
- Implement precommit hooks for eslint and [prettier](https://github.com/prettier/prettier)
- Updated some dev dependencies

### 3.0.0

* Update to [objection 2.1.2](https://github.com/tandg-digital/objection-filter/issues/28)
Expand Down
40 changes: 30 additions & 10 deletions package.json
@@ -1,19 +1,30 @@
{
"name": "objection-filter",
"version": "3.0.0",
"version": "4.0.0",
"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": "babel src --presets babel-preset-es2015 --out-dir dist",
"pretest": "eslint src/**/*.js",
"test": "cross-env NODE_ENV=test nyc mocha --exit --slow 10 --timeout 5000 --reporter spec test/**/*.test.js src/**/*.test.js",
"build": "tsc",
"pretest": "yarn run build",
"test": "cross-env NODE_ENV=test nyc mocha --exit --slow 10 --timeout 5000 --reporter spec test/*.test.js test/**/*.test.js",
"test-target": "cross-env NODE_ENV=test nyc mocha --slow 10 --timeout 5000 --reporter spec",
"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 @@ -34,22 +45,31 @@
}
],
"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",
"cross-env": "^7.0.2",
"eslint": "^7.3.1",
"eslint-config-airbnb-base": "^14.0.0",
"eslint-config-prettier": "^6.11.0",
"eslint-plugin-import": "^2.13.0",
"knex": "^0.20.10",
"husky": "^4.2.5",
"knex": "0.21.1",
"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",
"sqlite3": "^4.1.1"
"pg": "^8.2.1",
"prettier": "^2.0.5",
"sqlite3": "^5.0.0",
"typescript": "^3.9.5"
},
"dependencies": {
"debug": "^4.1.1",
Expand Down
3 changes: 0 additions & 3 deletions src/config.js

This file was deleted.

3 changes: 3 additions & 0 deletions src/config.ts
@@ -0,0 +1,3 @@
import _debug from 'debug';

export const debug = _debug('objection:filter');
12 changes: 0 additions & 12 deletions src/index.js

This file was deleted.

16 changes: 16 additions & 0 deletions src/index.ts
@@ -0,0 +1,16 @@
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';

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;
30 changes: 13 additions & 17 deletions src/lib/ExpressionBuilder.js → src/lib/ExpressionBuilder.ts
@@ -1,7 +1,5 @@
const _ = require('lodash');
const {
sliceRelation
} = require('./utils');
import { set, get, keys, map } from 'lodash';
import { sliceRelation } from './utils';

/**
* Takes a property name, and a tree of sub-relations and recursively builds
Expand All @@ -10,19 +8,22 @@ const {
* @param {Object} tree A tree of relations
* @param {String} relationName The top level relation
*/
const toRelationSubExpression = (tree, relationName) => {
function toRelationSubExpression(
tree: Record<string, unknown>,
relationName?: string
): string {
if (Object.keys(tree).length === 0) return relationName;

// Recursively apply to all attributes
const expression = _.map(tree, toRelationSubExpression).join(',');
const expression = map(tree, toRelationSubExpression).join(',');

// The first time this is called, there is no relationName
const prefix = relationName ? `${relationName}.` : '';

return _.keys(tree).length === 1
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 @@ -31,22 +32,17 @@ const toRelationSubExpression = (tree, relationName) => {
* expression = '[schema.[schemaAttributes,organization]]'
* @param {Array<String>} fields A list of fields
*/
const createRelationExpression = (fields) => {
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);
};
module.exports.createRelationExpression = createRelationExpression;
}

0 comments on commit ae3ce50

Please sign in to comment.