Skip to content

Commit

Permalink
feat: add where"JSON" methods introduced by Knex 1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed May 8, 2022
1 parent 3880c8d commit f875828
Show file tree
Hide file tree
Showing 3 changed files with 2,038 additions and 1 deletion.
75 changes: 74 additions & 1 deletion adonis-typings/querybuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,52 @@ declare module '@ioc:Adonis/Lucid/Database' {
/**
* Key-value pair. The value can also be a subquery
*/
(key: string | RawQuery, value: StrictValues | ChainableContract): Builder
(key: string, value: StrictValues | ChainableContract): Builder
}

/**
* Possible signatures for adding a whereLike clause
*/
interface WhereJson<Builder extends ChainableContract> {
/**
* Key-value pair. The value can also be a subquery
*/
(
column: string,
value: Record<string, any> | ChainableContract | QueryCallback<Builder>
): Builder
}

interface WhereJsonPath<Builder extends ChainableContract> {
(
column: string,
jsonPath: string,
value:
| string
| number
| boolean
| string[]
| number[]
| boolean[]
| Record<string, any>
| ChainableContract
| QueryCallback<Builder>
): Builder
(
column: string,
jsonPath: string,
operator: string,
value:
| string
| number
| boolean
| string[]
| number[]
| boolean[]
| Record<string, any>
| ChainableContract
| QueryCallback<Builder>
): Builder
}

/**
Expand Down Expand Up @@ -531,6 +576,34 @@ declare module '@ioc:Adonis/Lucid/Database' {
orWhereILike: WhereLike<this>
andWhereILike: WhereLike<this>

whereJson: WhereJson<this>
orWhereJson: WhereJson<this>
andWhereJson: WhereJson<this>

whereNotJson: WhereJson<this>
orWhereNotJson: WhereJson<this>
andWhereNotJson: WhereJson<this>

whereJsonSuperset: WhereJson<this>
orWhereJsonSuperset: WhereJson<this>
andWhereJsonSuperset: WhereJson<this>

whereNotJsonSuperset: WhereJson<this>
orWhereNotJsonSuperset: WhereJson<this>
andWhereNotJsonSuperset: WhereJson<this>

whereJsonSubset: WhereJson<this>
orWhereJsonSubset: WhereJson<this>
andWhereJsonSubset: WhereJson<this>

whereNotJsonSubset: WhereJson<this>
orWhereNotJsonSubset: WhereJson<this>
andWhereNotJsonSubset: WhereJson<this>

whereJsonPath: WhereJsonPath<this>
orWhereJsonPath: WhereJsonPath<this>
andWhereJsonPath: WhereJsonPath<this>

join: Join<this>
innerJoin: Join<this>
leftJoin: Join<this>
Expand Down
282 changes: 282 additions & 0 deletions src/Database/QueryBuilder/Chainable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -892,6 +892,288 @@ export abstract class Chainable extends Macroable implements ChainableContract {
return this.andWhere(key, 'ilike', value)
}

/**
* Define a where clause with value that matches for JSON
*/
public whereJson(column: string, value: any) {
const whereClauses = this.getRecentStackItem()

whereClauses.push({
method: 'whereJsonObject',
args: [this.resolveColumn(column), this.transformValue(value)],
})

return this
}

/**
* Define a or where clause with value that matches for JSON
*/
public orWhereJson(column: string, value: any) {
const whereClauses = this.getRecentStackItem()

whereClauses.push({
method: 'orWhereJsonObject',
args: [this.resolveColumn(column), this.transformValue(value)],
})

return this
}

/**
* Define a where clause with value that matches for JSON
*
* @alias whereJson
*/
public andWhereJson(column: string, value: any) {
return this.whereJson(column, value)
}

/**
* Define a where clause with value that matches for JSON
*/
public whereNotJson(column: string, value: any) {
const whereClauses = this.getRecentStackItem()

whereClauses.push({
method: 'whereNotJsonObject',
args: [this.resolveColumn(column), this.transformValue(value)],
})

return this
}

/**
* Define a or where clause with value that matches for JSON
*/
public orWhereNotJson(column: string, value: any) {
const whereClauses = this.getRecentStackItem()

whereClauses.push({
method: 'orWhereNotJsonObject',
args: [this.resolveColumn(column), this.transformValue(value)],
})

return this
}

/**
* Define a where clause with value that matches for JSON
*
* @alias whereNotJson
*/
public andWhereNotJson(column: string, value: any) {
return this.whereNotJson(column, value)
}

/**
* Define a where clause with value that matches for a superset of
* JSON
*/
public whereJsonSuperset(column: string, value: any) {
const whereClauses = this.getRecentStackItem()

whereClauses.push({
method: 'whereJsonSupersetOf',
args: [this.resolveColumn(column), this.transformValue(value)],
})

return this
}

/**
* Define a or where clause with value that matches for a superset of
* JSON
*/
public orWhereJsonSuperset(column: string, value: any) {
const whereClauses = this.getRecentStackItem()

whereClauses.push({
method: 'orWhereJsonSupersetOf',
args: [this.resolveColumn(column), this.transformValue(value)],
})

return this
}

/**
* Define or where clause with value that matches for a superset of
* JSON
*
* @alias whereJsonSuperset
*/
public andWhereJsonSuperset(column: string, value: any) {
return this.whereJsonSuperset(column, value)
}

/**
* Define a where clause with value that matches for a superset of
* JSON
*/
public whereNotJsonSuperset(column: string, value: any) {
const whereClauses = this.getRecentStackItem()

whereClauses.push({
method: 'whereJsonNotSupersetOf',
args: [this.resolveColumn(column), this.transformValue(value)],
})

return this
}

/**
* Define a or where clause with value that matches for a superset of
* JSON
*/
public orWhereNotJsonSuperset(column: string, value: any) {
const whereClauses = this.getRecentStackItem()

whereClauses.push({
method: 'orWhereJsonNotSupersetOf',
args: [this.resolveColumn(column), this.transformValue(value)],
})

return this
}

/**
* Define or where clause with value that matches for a superset of
* JSON
*
* @alias whereNotJsonSuperset
*/
public andWhereNotJsonSuperset(column: string, value: any) {
return this.whereNotJsonSuperset(column, value)
}

/**
* Define a where clause with value that matches for a subset of
* JSON
*/
public whereJsonSubset(column: string, value: any) {
const whereClauses = this.getRecentStackItem()

whereClauses.push({
method: 'whereJsonSubsetOf',
args: [this.resolveColumn(column), this.transformValue(value)],
})

return this
}

/**
* Define a or where clause with value that matches for a subset of
* JSON
*/
public orWhereJsonSubset(column: string, value: any) {
const whereClauses = this.getRecentStackItem()

whereClauses.push({
method: 'orWhereJsonSubsetOf',
args: [this.resolveColumn(column), this.transformValue(value)],
})

return this
}

/**
* Define or where clause with value that matches for a subset of
* JSON
*
* @alias whereJsonSubset
*/
public andWhereJsonSubset(column: string, value: any) {
return this.whereJsonSubset(column, value)
}

/**
* Define a where clause with value that matches for a subset of
* JSON
*/
public whereNotJsonSubset(column: string, value: any) {
const whereClauses = this.getRecentStackItem()

whereClauses.push({
method: 'whereJsonNotSubsetOf',
args: [this.resolveColumn(column), this.transformValue(value)],
})

return this
}

/**
* Define a or where clause with value that matches for a subset of
* JSON
*/
public orWhereNotJsonSubset(column: string, value: any) {
const whereClauses = this.getRecentStackItem()

whereClauses.push({
method: 'orWhereJsonNotSubsetOf',
args: [this.resolveColumn(column), this.transformValue(value)],
})

return this
}

/**
* Define or where clause with value that matches for a subset of
* JSON
*
* @alias whereNotJsonSubset
*/
public andWhereNotJsonSubset(column: string, value: any) {
return this.whereNotJsonSubset(column, value)
}

/**
* Adds a where clause with comparison of a value returned
* by a JsonPath given an operator and a value.
*/
public whereJsonPath(column: string, jsonPath: string, operator: any, value?: any): this {
const whereClauses = this.getRecentStackItem()
if (!value) {
value = operator
operator = '='
}

whereClauses.push({
method: 'whereJsonPath',
args: [this.resolveColumn(column), jsonPath, operator, this.transformValue(value)],
})

return this
}

/**
* Adds a or where clause with comparison of a value returned
* by a JsonPath given an operator and a value.
*/
public orWhereJsonPath(column: string, jsonPath: string, operator: any, value?: any): this {
const whereClauses = this.getRecentStackItem()
if (!value) {
value = operator
operator = '='
}

whereClauses.push({
method: 'orWhereJsonPath',
args: [this.resolveColumn(column), jsonPath, operator, this.transformValue(value)],
})

return this
}

/**
* Adds a where clause with comparison of a value returned
* by a JsonPath given an operator and a value.
*
* @alias whereJsonPath
*/
public andWhereJsonPath(column: string, jsonPath: string, operator: any, value?: any): this {
return this.whereJsonPath(column, jsonPath, operator, value)
}

/**
* Add a join clause
*/
Expand Down
Loading

0 comments on commit f875828

Please sign in to comment.