Skip to content

Commit

Permalink
Where not filtering (Joystream#491)
Browse files Browse the repository at this point in the history
* feat: support for \`where\` NOT filtering

affects: @joystream/bn-typeorm, @joystream/hydra-cli, @joystream/hydra-common,
@joystream/hydra-db-utils, @joystream/hydra-e2e-tests, @joystream/hydra-indexer-gateway, 
@joystream/hydra-indexer, @joystream/hydra-processor, sample
  • Loading branch information
ondratra authored May 26, 2022
1 parent 8b21a94 commit 9f0dbc1
Show file tree
Hide file tree
Showing 21 changed files with 220 additions and 400 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
"prettier": "2.0.2"
},
"resolutions": {
"typeorm": "0.2.34",
"@polkadot/types": "5.9.1"
},
"lint-staged": {
Expand Down
2 changes: 1 addition & 1 deletion packages/bn-typeorm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
},
"dependencies": {
"bn.js": "^5.1.3",
"typeorm": "^0.2.25"
"typeorm": "/~https://github.com/Joystream/typeorm/releases/download/0.3.5/typeorm-v0.3.5.tgz"
},
"devDependencies": {
"@types/bn.js": "^4.11.6",
Expand Down
4 changes: 2 additions & 2 deletions packages/hydra-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"@inquirer/input": "^0.0.13-alpha.0",
"@inquirer/password": "^0.0.12-alpha.0",
"@inquirer/select": "^0.0.13-alpha.0",
"@joystream/warthog": "^2.41.4",
"@joystream/warthog": "^2.41.5",
"@oclif/command": "^1.5.20",
"@oclif/config": "^1",
"@oclif/errors": "^1.3.3",
Expand Down Expand Up @@ -80,7 +80,7 @@
"lodash": "^4.17.20",
"pg": "^8.3.2",
"pg-listen": "^1.7.0",
"@joystream/warthog": "^2.41.4"
"@joystream/warthog": "^2.41.5"
},
"devDependencies": {
"@oclif/dev-cli": "^1",
Expand Down
4 changes: 2 additions & 2 deletions packages/hydra-cli/src/codegen/WarthogWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,10 +211,10 @@ export default class WarthogWrapper {
...extraDependencies,
}

// temporary fix for specific typeorm version needed (see /~https://github.com/Joystream/hydra/pull/453 for more info)
// resolutions can be used for fixing temporary package dependency issues
pkgFile.resolutions = {
...(pkgFile.resolutions || {}),
typeorm: '0.2.34',
// typeorm: '0.2.34', // example
}

debug(`Writing package.json: ${JSON.stringify(pkgFile, null, 2)}`)
Expand Down
3 changes: 1 addition & 2 deletions packages/hydra-cli/src/templates/entities/resolver.ts.mst
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {
Arg,
Args,
Mutation,
Query,
Root,
Resolver,
Expand All @@ -16,7 +15,7 @@ import {
import graphqlFields from 'graphql-fields';
import { Inject } from 'typedi';
import { Min } from 'class-validator'
import { Fields, StandardDeleteResponse, UserId, PageInfo, RawFields, NestedFields, BaseContext } from '@joystream/warthog';
import { Fields, PageInfo, BaseContext } from '@joystream/warthog';

import {
{{className}}CreateInput,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BaseService, BaseModel, WhereInput, LimitOffset, addQueryBuilderWhereItem } from '@joystream/warthog';
import { Brackets, SelectQueryBuilder } from 'typeorm';
import { BaseService, BaseModel, WhereInput, LimitOffset } from '@joystream/warthog';
import { SelectQueryBuilder } from 'typeorm';

type WhereExpression = {
AND?: WhereExpression[];
Expand Down
315 changes: 22 additions & 293 deletions packages/hydra-common/src/interfaces/typeorm.ts
Original file line number Diff line number Diff line change
@@ -1,293 +1,22 @@
/**
* TypeORM interfaces. Extracted into a separate file in order to avoid typeorm dependency
*/

/**
* Same as Partial<T> but goes deeper and makes Partial<T> all its properties and sub-properties.
*/
export declare type DeepPartial<T> = {
[P in keyof T]?: T[P] extends Array<infer U>
? Array<DeepPartial<U>>
: T[P] extends ReadonlyArray<infer U>
? ReadonlyArray<DeepPartial<U>>
: DeepPartial<T[P]> | T[P]
}

/**
* Interface of the simple literal object with any string keys.
*/
export interface ObjectLiteral {
[key: string]: any
}

/**
* Used to specify what entity relations should be loaded.
*
* Example:
* const options: JoinOptions = {
* alias: "photo",
* leftJoin: {
* author: "photo.author",
* categories: "categories",
* user: "categories.user",
* profile: "user.profile"
* },
* innerJoin: {
* author: "photo.author",
* categories: "categories",
* user: "categories.user",
* profile: "user.profile"
* },
* leftJoinAndSelect: {
* author: "photo.author",
* categories: "categories",
* user: "categories.user",
* profile: "user.profile"
* },
* innerJoinAndSelect: {
* author: "photo.author",
* categories: "categories",
* user: "categories.user",
* profile: "user.profile"
* }
* };
*/
export interface JoinOptions {
/**
* Alias of the main entity.
*/
alias: string
/**
* Object where each key represents the LEFT JOIN alias,
* and the corresponding value represents the relation path.
*
* The columns of the joined table are included in the selection.
*/
leftJoinAndSelect?: {
[key: string]: string
}
/**
* Object where each key represents the INNER JOIN alias,
* and the corresponding value represents the relation path.
*
* The columns of the joined table are included in the selection.
*/
innerJoinAndSelect?: {
[key: string]: string
}
/**
* Object where each key represents the LEFT JOIN alias,
* and the corresponding value represents the relation path.
*
* This method does not select the columns of the joined table.
*/
leftJoin?: {
[key: string]: string
}
/**
* Object where each key represents the INNER JOIN alias,
* and the corresponding value represents the relation path.
*
* This method does not select the columns of the joined table.
*/
innerJoin?: {
[key: string]: string
}
}

/**
* Used for find operations.
*/
export declare type FindConditions<T> = {
[P in keyof T]?: T[P] extends never
? FindConditions<T[P]> | FindOperator<FindConditions<T[P]>>
: FindConditions<T[P]> | FindOperator<FindConditions<T[P]>>
}

/**
* Find Operator used in Find Conditions.
*/
export declare class FindOperator<T> {
/**
* Operator type.
*/
private _type
/**
* Parameter value.
*/
private _value
/**
* ObjectLiteral parameters.
*/
private _objectLiteralParameters
/**
* Indicates if parameter is used or not for this operator.
*/
private _useParameter
/**
* Indicates if multiple parameters must be used for this operator.
*/
private _multipleParameters
/**
* SQL generator
*/
private _getSql
constructor(
type: FindOperatorType,
value: T | FindOperator<T>,
useParameter?: boolean,
multipleParameters?: boolean,
getSql?: SqlGeneratorType,
objectLiteralParameters?: ObjectLiteral
)

/**
* Indicates if parameter is used or not for this operator.
* Extracts final value if value is another find operator.
*/
get useParameter(): boolean

/**
* Indicates if multiple parameters must be used for this operator.
* Extracts final value if value is another find operator.
*/
get multipleParameters(): boolean

/**
* Gets the Type of this FindOperator
*/
get type(): string

/**
* Gets the final value needs to be used as parameter value.
*/
get value(): T

/**
* Gets ObjectLiteral parameters.
*/
get objectLiteralParameters(): ObjectLiteral | undefined

/**
* Gets the child FindOperator if it exists
*/
get child(): FindOperator<T> | undefined

/**
* Gets the SQL generator
*/
get getSql(): SqlGeneratorType | undefined
}

declare type SqlGeneratorType = (aliasPath: string) => string

/**
* List of types that FindOperator can be.
*/
export declare type FindOperatorType =
| 'not'
| 'lessThan'
| 'lessThanOrEqual'
| 'moreThan'
| 'moreThanOrEqual'
| 'equal'
| 'between'
| 'in'
| 'any'
| 'isNull'
| 'ilike'
| 'like'
| 'raw'

/**
* Defines a special criteria to find specific entity.
*/
export interface FindOneOptions<Entity = any> {
/**
* Specifies what columns should be retrieved.
*/
select?: (keyof Entity)[]
/**
* Simple condition that should be applied to match entities.
*/
where?:
| FindConditions<Entity>[]
| FindConditions<Entity>
| ObjectLiteral
| string
/**
* Indicates what relations of entity should be loaded (simplified left join form).
*/
relations?: string[]
/**
* Specifies what relations should be loaded.
*/
join?: JoinOptions
/**
* Order, in which entities should be ordered.
*/
order?: {
[P in EntityFieldsNames<Entity>]?: 'ASC' | 'DESC' | 1 | -1
}
/**
* Enables or disables query result caching.
*/
cache?:
| boolean
| number
| {
id: any
milliseconds: number
}
/**
* Indicates what locking mode should be used.
*
* Note: For lock tables, you must specify the table names and not the relation names
*/
lock?:
| {
mode: 'optimistic'
version: number | Date
}
| {
mode:
| 'pessimistic_read'
| 'pessimistic_write'
| 'dirty_read'
| 'pessimistic_partial_write'
| 'pessimistic_write_or_fail'
| 'for_no_key_update'
tables?: string[]
}
/**
* Indicates if soft-deleted rows should be included in entity result.
*/
withDeleted?: boolean
/**
* If sets to true then loads all relation ids of the entity and maps them into relation values (not relation objects).
* If array of strings is given then loads only relation ids of the given properties.
*/
loadRelationIds?:
| boolean
| {
relations?: string[]
disableMixedMap?: boolean
}
/**
* Indicates if eager relations should be loaded or not.
* By default they are loaded when find methods are used.
*/
loadEagerRelations?: boolean
/**
* If this is set to true, SELECT query in a `find` method will be executed in a transaction.
*/
transaction?: boolean
}

/**
* Interface of the entity fields names only (without functions)
*/
export declare type EntityFieldsNames<Entity = any> = {
// eslint-disable-next-line @typescript-eslint/ban-types
[P in keyof Entity]: Entity[P] extends Function ? never : P
}[keyof Entity]
export {
DeepPartial,
ObjectLiteral,
JoinOptions,
FindOperator,
// SqlGeneratorType,
FindOperatorType,
ObjectID,
FindOptionsSelectProperty,
FindOptionsSelect,
FindOptionsSelectByString,
FindOptionsRelationsProperty,
FindOptionsRelations,
FindOptionsRelationByString,
FindOptionsWhereProperty,
FindOptionsWhere,
EqualOperator,
FindOptionsOrderProperty,
FindOptionsOrder,
FindOptionsOrderValue,
FindOneOptions, // EntityFieldsNames,
} from 'typeorm'
2 changes: 1 addition & 1 deletion packages/hydra-db-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"bn.js": "^5.1.3",
"ioredis": "^4.17.3",
"lodash": "^4.17.20",
"typeorm": "^0.2.25"
"typeorm": "/~https://github.com/Joystream/typeorm/releases/download/0.3.5/typeorm-v0.3.5.tgz"
},
"devDependencies": {
"@types/bn.js": "^4.11.6",
Expand Down
Loading

0 comments on commit 9f0dbc1

Please sign in to comment.