Skip to content

Commit

Permalink
Fix TS types (#73)
Browse files Browse the repository at this point in the history
  • Loading branch information
kibertoad authored Oct 26, 2020
1 parent 9786c82 commit 9078f00
Show file tree
Hide file tree
Showing 17 changed files with 1,748 additions and 967 deletions.
15 changes: 15 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,25 @@ jobs:
POSTGRES_HOST_AUTH_METHOD: trust
steps:
- test-nodejs
node-v15:
docker:
- image: circleci/node:15
- image: circleci/mysql
command: [--default-authentication-plugin=mysql_native_password]
environment:
MYSQL_ALLOW_EMPTY_PASSWORD: true
MYSQL_ROOT_PASSWORD: pass
- image: circleci/postgres
environment:
POSTGRES_USER: postgres
POSTGRES_HOST_AUTH_METHOD: trust
steps:
- test-nodejs
workflows:
version: 2
node-multi-build:
jobs:
- node-v10
- node-v12
- node-v14
- node-v15
7 changes: 5 additions & 2 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@
"parserOptions": {
"ecmaVersion": 2018
},
"extends": ["eslint:recommended", "prettier"],
"plugins": ["prettier"],
"extends": ["eslint:recommended", "plugin:@typescript-eslint/recommended","prettier"],
"plugins": ["@typescript-eslint", "prettier"],
"rules": {
"prettier/prettier": "error",
"@typescript-eslint/no-unused-vars": "off",
"@typescript-eslint/no-var-requires": "off",
"@typescript-eslint/no-this-alias": "off",
"no-unused-vars": "off"
},
"env": {
Expand Down
16 changes: 8 additions & 8 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ instance. Just pass an [objection.js](https://github.com/Vincit/objection.js/) m
and start building the query.

```js
var findQuery = require('objection-find');
var Person = require('./models/Person');
var builder = findQuery(Person);
const findQuery = require('objection-find');
const Person = require('./models/Person');
const builder = findQuery(Person);
```

`findQuery(Person)` is just a shortcut for [new findQuery.FindQueryBuilder(Person)](#new-findquerybuilderobjectionmodelconstructor---findquerybuilder)
Expand Down Expand Up @@ -53,9 +53,9 @@ and whatnot.
The constructor function.

```js
var FindQueryBuilder = require('objection-find').FindQueryBuilder;
var Person = require('./models/Person');
var findQueryBuilder = new FindQueryBuilder(Person);
const FindQueryBuilder = require('objection-find').FindQueryBuilder;
const Person = require('./models/Person');
const findQueryBuilder = new FindQueryBuilder(Person);
```

<br>
Expand All @@ -65,7 +65,7 @@ var findQueryBuilder = new FindQueryBuilder(Person);
Creates a subclass of the FindQueryBuilder.

```js
var FindQueryBuilder = require('objection-find').FindQueryBuilder;
const FindQueryBuilder = require('objection-find').FindQueryBuilder;

function MyFindQueryBuilder() {
FindQueryBuilder.apply(this, arguments);
Expand Down Expand Up @@ -171,7 +171,7 @@ The `method` must be the name of one of the objection.js where methods. `args` i
of arguments for the method. The filter is invoked somewhat like this:

```js
var filter = lowercaseEq(propertyRef, value, modelClass);
const filter = lowercaseEq(propertyRef, value, modelClass);
objectionQueryBuilder[filter.method].apply(objectionQueryBuilder, filter.args);
```

Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ expressions.
Using objection-find in an [express](http://expressjs.com/) route is as easy as this:

```js
var findQuery = require('objection-find');
const findQuery = require('objection-find');
// Our objection.js model.
var Person = require('../models/Person');
const Person = require('../models/Person');

expressApp.get('/api/persons', function (req, res, next) {
findQuery(Person)
Expand Down Expand Up @@ -80,7 +80,7 @@ $http({
'rangeEnd': 4
}
}).then(function (res) {
var persons = res.data.results;
const persons = res.data.results;

console.log(persons.length); // --> 5
console.log(persons[0].children);
Expand Down Expand Up @@ -113,7 +113,7 @@ Easiest way to get started is to use [the objection.js example project](https://
and copy paste this to the `api.js` file:

```js
var findQuery = require('objection-find');
const findQuery = require('objection-find');

app.get('/persons/search', function (req, res, next) {
findQuery(Person).build(req.query).then(function (persons) {
Expand Down
48 changes: 30 additions & 18 deletions typings/index.d.ts → index.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { Model, ModelClass, Page, RelationExpression } from 'objection';
import { Constructor, Model, ModelClass, Page, RelationExpression } from 'objection';

interface FilterFn<M extends Model> {
(propertyRef: PropertyRef<M>, value: string, modelClass: ModelClass<M>): { method: string; args: any[] };
(propertyRef: PropertyRef<M>, value: string, modelClass: ModelClass<M>): {
method: string;
// eslint-disable-next-line
args: any[];
};
}

export class FindQueryBuilder<M extends Model, R = M[]> {
Expand All @@ -22,20 +26,26 @@ export class FindQueryBuilder<M extends Model, R = M[]> {
* findQuery(Person).allow('firstName', 'parent.firstName', 'pets.name');
* ```
*/
allow(...args: string[]): this['SingleQueryBuilderType'] & M['QueryBuilderType']['SingleQueryBuilderType'];
allow(
...args: string[]
): this['SingleQueryBuilderType'] & M['QueryBuilderType']['SingleQueryBuilderType'];

/**
* Allow all property references. This is true by default.
*/
allowAll(bool: boolean): this['SingleQueryBuilderType'] & M['QueryBuilderType']['SingleQueryBuilderType'];
allowAll(
bool: boolean
): this['SingleQueryBuilderType'] & M['QueryBuilderType']['SingleQueryBuilderType'];

/**
* Sets/gets the allowed eager expression.
*
* Calls the `allowEager` method of a objection.js `QueryBuilder`. See the objection.js
* documentation for more information.
*/
allowEager(exp: RelationExpression<M>): this['SingleQueryBuilderType'] & M['QueryBuilderType']['SingleQueryBuilderType'] | null;
allowEager(
exp: RelationExpression<M>
): (this['SingleQueryBuilderType'] & M['QueryBuilderType']['SingleQueryBuilderType']) | null;

/**
* Registers a filter function.
Expand Down Expand Up @@ -91,7 +101,10 @@ export class FindQueryBuilder<M extends Model, R = M[]> {
*
* Now you could use your filter in the query parameters like this `someProperty:leq=Hello`.
*/
registerFilter(filterName: string, filter: FilterFn<M>): this['SingleQueryBuilderType'] & M['QueryBuilderType']['SingleQueryBuilderType'];
registerFilter(
filterName: string,
filter: FilterFn<M>
): this['SingleQueryBuilderType'] & M['QueryBuilderType']['SingleQueryBuilderType'];

/**
* Give names for the special parameters.
Expand All @@ -104,7 +117,10 @@ export class FindQueryBuilder<M extends Model, R = M[]> {
* builder.specialParameter('eager', 'withRelated');
* ```
*/
specialParameter(name: string, parameterName: string): this['SingleQueryBuilderType'] & M['QueryBuilderType']['SingleQueryBuilderType'];
specialParameter(
name: string,
parameterName: string
): this['SingleQueryBuilderType'] & M['QueryBuilderType']['SingleQueryBuilderType'];

/**
* Builds the find query for the given query parameters.
Expand All @@ -120,7 +136,11 @@ export class FindQueryBuilder<M extends Model, R = M[]> {
* });
* ```
*/
build(params: object, builder?: this['SingleQueryBuilderType'] & M['QueryBuilderType']['SingleQueryBuilderType']): this['SingleQueryBuilderType'] & M['QueryBuilderType']['SingleQueryBuilderType'];
build(
// eslint-disable-next-line
params: Record<string, any>,
builder?: this['SingleQueryBuilderType'] & M['QueryBuilderType']['SingleQueryBuilderType']
): this['SingleQueryBuilderType'] & M['QueryBuilderType']['SingleQueryBuilderType'];
}

export class PropertyRef<M extends Model> {
Expand Down Expand Up @@ -154,13 +174,5 @@ export class PropertyRef<M extends Model> {
buildFilter(param: string, builder: FindQueryBuilder<M>, boolOp?: string): void;
}

interface FindStatic<T extends typeof Model> {
QueryBuilder: typeof FindQueryBuilder;
new(): FindInstance<T> & T['prototype'];
}

interface FindInstance<T extends typeof Model> {
QueryBuilderType: FindQueryBuilder<this & T['prototype']>;
}

export default function findQuery<T extends typeof Model>(subClass: T): FindStatic<T> & Omit<T, 'new'> & T['prototype'];
export function findQuery<T extends Model>(modelClass: Constructor<T>): FindQueryBuilder<T>;
export default findQuery;
24 changes: 24 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'use strict';

const FindQueryBuilder = require('./lib/FindQueryBuilder');
const QueryParameter = require('./lib/QueryParameter');
const PropertyRef = require('./lib/PropertyRef');

const findQuery = (modelClass) => new FindQueryBuilder(modelClass);

/**
* These export configurations enable JS and TS developers
* to consume objection-find in whatever way best suits their needs.
* Some examples of supported import syntax includes:
* - `const findQuery = require('objection-find')`
* - `const { findQuery } = require('objection-find')`
* - `import * as findQuery from 'objection-find'`
* - `import { findQuery } from 'objection-find'`
* - `import findQuery from 'objection-find'`
*/
findQuery.findQuery = findQuery;
findQuery.FindQueryBuilder = FindQueryBuilder;
findQuery.QueryParameter = QueryParameter;
findQuery.PropertyRef = PropertyRef;
findQuery.default = findQuery;
module.exports = findQuery;
10 changes: 0 additions & 10 deletions objection-find.js

This file was deleted.

Loading

0 comments on commit 9078f00

Please sign in to comment.