2021-06-24 19:16:48 +02:00
|
|
|
import { Knex, knex } from "knex"
|
2021-06-03 17:31:24 +02:00
|
|
|
const BASE_LIMIT = 5000
|
2021-06-24 19:17:26 +02:00
|
|
|
import {
|
|
|
|
QueryJson,
|
|
|
|
SearchFilters,
|
|
|
|
QueryOptions,
|
|
|
|
SortDirection,
|
|
|
|
Operation,
|
2021-06-25 19:13:11 +02:00
|
|
|
RelationshipsJson,
|
2021-06-27 00:09:46 +02:00
|
|
|
} from "../../definitions/datasource"
|
2021-07-12 11:51:30 +02:00
|
|
|
import { isIsoDateString } from "../utils"
|
2021-06-24 19:16:48 +02:00
|
|
|
|
2021-06-25 19:13:11 +02:00
|
|
|
type KnexQuery = Knex.QueryBuilder | Knex
|
2021-06-03 17:31:24 +02:00
|
|
|
|
2021-07-12 11:51:30 +02:00
|
|
|
function parseBody(body: any) {
|
|
|
|
for (let [key, value] of Object.entries(body)) {
|
|
|
|
if (typeof value !== "string") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if (isIsoDateString(value)) {
|
|
|
|
body[key] = new Date(value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return body
|
|
|
|
}
|
|
|
|
|
2021-06-30 19:31:16 +02:00
|
|
|
// right now we only do filters on the specific table being queried
|
2021-06-24 19:17:26 +02:00
|
|
|
function addFilters(
|
2021-06-30 19:31:16 +02:00
|
|
|
tableName: string,
|
2021-06-25 19:13:11 +02:00
|
|
|
query: KnexQuery,
|
2021-06-24 19:17:26 +02:00
|
|
|
filters: SearchFilters | undefined
|
2021-06-25 19:13:11 +02:00
|
|
|
): KnexQuery {
|
2021-06-24 19:17:26 +02:00
|
|
|
function iterate(
|
|
|
|
structure: { [key: string]: any },
|
|
|
|
fn: (key: string, value: any) => void
|
|
|
|
) {
|
2021-06-03 17:31:24 +02:00
|
|
|
for (let [key, value] of Object.entries(structure)) {
|
2021-06-30 19:31:16 +02:00
|
|
|
fn(`${tableName}.${key}`, value)
|
2021-06-03 17:31:24 +02:00
|
|
|
}
|
|
|
|
}
|
2021-06-04 16:16:15 +02:00
|
|
|
if (!filters) {
|
|
|
|
return query
|
|
|
|
}
|
2021-06-15 14:50:41 +02:00
|
|
|
// if all or specified in filters, then everything is an or
|
2021-06-24 19:16:48 +02:00
|
|
|
const allOr = filters.allOr
|
2021-07-01 15:10:44 +02:00
|
|
|
if (filters.oneOf) {
|
|
|
|
iterate(filters.oneOf, (key, array) => {
|
|
|
|
const fnc = allOr ? "orWhereIn" : "whereIn"
|
|
|
|
query = query[fnc](key, array)
|
|
|
|
})
|
|
|
|
}
|
2021-06-03 17:31:24 +02:00
|
|
|
if (filters.string) {
|
|
|
|
iterate(filters.string, (key, value) => {
|
2021-06-15 14:03:55 +02:00
|
|
|
const fnc = allOr ? "orWhere" : "where"
|
|
|
|
query = query[fnc](key, "like", `${value}%`)
|
2021-06-03 17:31:24 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
if (filters.range) {
|
|
|
|
iterate(filters.range, (key, value) => {
|
|
|
|
if (!value.high || !value.low) {
|
|
|
|
return
|
|
|
|
}
|
2021-06-15 14:03:55 +02:00
|
|
|
const fnc = allOr ? "orWhereBetween" : "whereBetween"
|
|
|
|
query = query[fnc](key, [value.low, value.high])
|
2021-06-03 17:31:24 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
if (filters.equal) {
|
|
|
|
iterate(filters.equal, (key, value) => {
|
2021-06-15 14:03:55 +02:00
|
|
|
const fnc = allOr ? "orWhere" : "where"
|
|
|
|
query = query[fnc]({ [key]: value })
|
2021-06-03 17:31:24 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
if (filters.notEqual) {
|
|
|
|
iterate(filters.notEqual, (key, value) => {
|
2021-06-15 14:03:55 +02:00
|
|
|
const fnc = allOr ? "orWhereNot" : "whereNot"
|
|
|
|
query = query[fnc]({ [key]: value })
|
2021-06-03 17:31:24 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
if (filters.empty) {
|
|
|
|
iterate(filters.empty, key => {
|
2021-06-15 14:03:55 +02:00
|
|
|
const fnc = allOr ? "orWhereNull" : "whereNull"
|
|
|
|
query = query[fnc](key)
|
2021-06-03 17:31:24 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
if (filters.notEmpty) {
|
|
|
|
iterate(filters.notEmpty, key => {
|
2021-06-15 14:03:55 +02:00
|
|
|
const fnc = allOr ? "orWhereNotNull" : "whereNotNull"
|
|
|
|
query = query[fnc](key)
|
2021-06-03 17:31:24 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
return query
|
|
|
|
}
|
|
|
|
|
2021-06-25 19:34:21 +02:00
|
|
|
function addRelationships(
|
|
|
|
query: KnexQuery,
|
|
|
|
fromTable: string,
|
|
|
|
relationships: RelationshipsJson[] | undefined
|
|
|
|
): KnexQuery {
|
2021-06-23 20:05:32 +02:00
|
|
|
if (!relationships) {
|
|
|
|
return query
|
|
|
|
}
|
|
|
|
for (let relationship of relationships) {
|
2021-06-29 18:42:46 +02:00
|
|
|
const from = relationship.from,
|
|
|
|
to = relationship.to,
|
|
|
|
toTable = relationship.tableName
|
2021-06-23 20:05:32 +02:00
|
|
|
if (!relationship.through) {
|
2021-06-25 19:13:11 +02:00
|
|
|
// @ts-ignore
|
2021-06-29 19:38:27 +02:00
|
|
|
query = query.leftJoin(
|
2021-06-29 18:42:46 +02:00
|
|
|
toTable,
|
|
|
|
`${fromTable}.${from}`,
|
|
|
|
`${relationship.tableName}.${to}`
|
|
|
|
)
|
2021-06-23 20:05:32 +02:00
|
|
|
} else {
|
2021-06-29 18:42:46 +02:00
|
|
|
const throughTable = relationship.through
|
2021-06-23 20:05:32 +02:00
|
|
|
query = query
|
2021-06-25 19:13:11 +02:00
|
|
|
// @ts-ignore
|
2021-06-29 19:38:27 +02:00
|
|
|
.leftJoin(
|
|
|
|
throughTable,
|
|
|
|
`${fromTable}.${from}`,
|
|
|
|
`${throughTable}.${from}`
|
|
|
|
)
|
|
|
|
.leftJoin(toTable, `${toTable}.${to}`, `${throughTable}.${to}`)
|
2021-06-23 20:05:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return query
|
|
|
|
}
|
|
|
|
|
2021-06-25 19:34:21 +02:00
|
|
|
function buildCreate(
|
|
|
|
knex: Knex,
|
|
|
|
json: QueryJson,
|
|
|
|
opts: QueryOptions
|
|
|
|
): KnexQuery {
|
2021-06-03 17:31:24 +02:00
|
|
|
const { endpoint, body } = json
|
2021-06-25 19:13:11 +02:00
|
|
|
let query: KnexQuery = knex(endpoint.entityId)
|
2021-07-12 11:51:30 +02:00
|
|
|
const parsedBody = parseBody(body)
|
2021-06-18 14:14:45 +02:00
|
|
|
// mysql can't use returning
|
|
|
|
if (opts.disableReturning) {
|
2021-07-12 11:51:30 +02:00
|
|
|
return query.insert(parsedBody)
|
2021-06-18 14:14:45 +02:00
|
|
|
} else {
|
2021-07-12 11:51:30 +02:00
|
|
|
return query.insert(parsedBody).returning("*")
|
2021-06-18 14:14:45 +02:00
|
|
|
}
|
2021-06-03 17:31:24 +02:00
|
|
|
}
|
|
|
|
|
2021-06-25 19:13:11 +02:00
|
|
|
function buildRead(knex: Knex, json: QueryJson, limit: number): KnexQuery {
|
2021-06-23 20:05:32 +02:00
|
|
|
let { endpoint, resource, filters, sort, paginate, relationships } = json
|
|
|
|
const tableName = endpoint.entityId
|
2021-06-25 19:13:11 +02:00
|
|
|
let query: KnexQuery = knex(tableName)
|
2021-06-18 14:14:45 +02:00
|
|
|
// select all if not specified
|
|
|
|
if (!resource) {
|
|
|
|
resource = { fields: [] }
|
|
|
|
}
|
2021-06-03 17:31:24 +02:00
|
|
|
// handle select
|
|
|
|
if (resource.fields && resource.fields.length > 0) {
|
2021-08-05 20:24:29 +02:00
|
|
|
query = query.select(resource.fields.map(field => `${field} as ${field}`))
|
2021-06-03 17:31:24 +02:00
|
|
|
} else {
|
|
|
|
query = query.select("*")
|
|
|
|
}
|
|
|
|
// handle where
|
2021-06-30 19:31:16 +02:00
|
|
|
query = addFilters(tableName, query, filters)
|
2021-06-23 20:05:32 +02:00
|
|
|
// handle join
|
|
|
|
query = addRelationships(query, tableName, relationships)
|
2021-06-03 17:31:24 +02:00
|
|
|
// handle sorting
|
|
|
|
if (sort) {
|
|
|
|
for (let [key, value] of Object.entries(sort)) {
|
|
|
|
const direction = value === SortDirection.ASCENDING ? "asc" : "desc"
|
|
|
|
query = query.orderBy(key, direction)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// handle pagination
|
2021-06-04 16:16:15 +02:00
|
|
|
if (paginate && paginate.page && paginate.limit) {
|
2021-06-24 19:16:48 +02:00
|
|
|
// @ts-ignore
|
2021-06-03 17:31:24 +02:00
|
|
|
const page = paginate.page <= 1 ? 0 : paginate.page - 1
|
|
|
|
const offset = page * paginate.limit
|
|
|
|
query = query.offset(offset).limit(paginate.limit)
|
2021-06-04 16:16:15 +02:00
|
|
|
} else if (paginate && paginate.limit) {
|
2021-06-03 17:31:24 +02:00
|
|
|
query = query.limit(paginate.limit)
|
|
|
|
} else {
|
|
|
|
query.limit(limit)
|
|
|
|
}
|
2021-06-03 18:45:19 +02:00
|
|
|
return query
|
2021-06-03 17:31:24 +02:00
|
|
|
}
|
|
|
|
|
2021-06-25 19:34:21 +02:00
|
|
|
function buildUpdate(
|
|
|
|
knex: Knex,
|
|
|
|
json: QueryJson,
|
|
|
|
opts: QueryOptions
|
|
|
|
): KnexQuery {
|
2021-06-03 17:31:24 +02:00
|
|
|
const { endpoint, body, filters } = json
|
2021-06-25 19:13:11 +02:00
|
|
|
let query: KnexQuery = knex(endpoint.entityId)
|
2021-07-12 11:51:30 +02:00
|
|
|
const parsedBody = parseBody(body)
|
2021-06-30 19:31:16 +02:00
|
|
|
query = addFilters(endpoint.entityId, query, filters)
|
2021-06-18 14:14:45 +02:00
|
|
|
// mysql can't use returning
|
|
|
|
if (opts.disableReturning) {
|
2021-07-12 11:51:30 +02:00
|
|
|
return query.update(parsedBody)
|
2021-06-18 14:14:45 +02:00
|
|
|
} else {
|
2021-07-12 11:51:30 +02:00
|
|
|
return query.update(parsedBody).returning("*")
|
2021-06-18 14:14:45 +02:00
|
|
|
}
|
2021-06-03 17:31:24 +02:00
|
|
|
}
|
|
|
|
|
2021-06-25 19:34:21 +02:00
|
|
|
function buildDelete(
|
|
|
|
knex: Knex,
|
|
|
|
json: QueryJson,
|
|
|
|
opts: QueryOptions
|
|
|
|
): KnexQuery {
|
2021-06-03 17:31:24 +02:00
|
|
|
const { endpoint, filters } = json
|
2021-06-25 19:13:11 +02:00
|
|
|
let query: KnexQuery = knex(endpoint.entityId)
|
2021-06-30 19:31:16 +02:00
|
|
|
query = addFilters(endpoint.entityId, query, filters)
|
2021-06-18 14:14:45 +02:00
|
|
|
// mysql can't use returning
|
|
|
|
if (opts.disableReturning) {
|
|
|
|
return query.delete()
|
|
|
|
} else {
|
|
|
|
return query.delete().returning("*")
|
|
|
|
}
|
2021-06-03 17:31:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class SqlQueryBuilder {
|
2021-06-25 14:46:02 +02:00
|
|
|
private readonly sqlClient: string
|
2021-06-24 19:16:48 +02:00
|
|
|
private readonly limit: number
|
2021-06-03 17:31:24 +02:00
|
|
|
// pass through client to get flavour of SQL
|
2021-06-25 14:46:02 +02:00
|
|
|
constructor(client: string, limit: number = BASE_LIMIT) {
|
|
|
|
this.sqlClient = client
|
2021-06-24 19:16:48 +02:00
|
|
|
this.limit = limit
|
2021-06-03 17:31:24 +02:00
|
|
|
}
|
|
|
|
|
2021-06-18 14:14:45 +02:00
|
|
|
/**
|
|
|
|
* @param json the input JSON structure from which an SQL query will be built.
|
|
|
|
* @return {string} the operation that was found in the JSON.
|
|
|
|
*/
|
2021-06-24 19:16:48 +02:00
|
|
|
_operation(json: QueryJson): Operation {
|
2021-06-03 19:48:04 +02:00
|
|
|
return json.endpoint.operation
|
|
|
|
}
|
|
|
|
|
2021-06-18 14:14:45 +02:00
|
|
|
/**
|
|
|
|
* @param json The JSON query DSL which is to be converted to SQL.
|
|
|
|
* @param opts extra options which are to be passed into the query builder, e.g. disableReturning
|
|
|
|
* which for the sake of mySQL stops adding the returning statement to inserts, updates and deletes.
|
|
|
|
* @return {{ sql: string, bindings: object }} the query ready to be passed to the driver.
|
|
|
|
*/
|
2021-06-24 19:16:48 +02:00
|
|
|
_query(json: QueryJson, opts: QueryOptions = {}) {
|
2021-06-25 14:46:02 +02:00
|
|
|
const client = knex({ client: this.sqlClient })
|
2021-06-03 18:45:19 +02:00
|
|
|
let query
|
2021-06-03 19:48:04 +02:00
|
|
|
switch (this._operation(json)) {
|
2021-06-24 19:16:48 +02:00
|
|
|
case Operation.CREATE:
|
|
|
|
query = buildCreate(client, json, opts)
|
2021-06-03 18:45:19 +02:00
|
|
|
break
|
2021-06-24 19:16:48 +02:00
|
|
|
case Operation.READ:
|
|
|
|
query = buildRead(client, json, this.limit)
|
2021-06-03 18:45:19 +02:00
|
|
|
break
|
2021-06-24 19:16:48 +02:00
|
|
|
case Operation.UPDATE:
|
|
|
|
query = buildUpdate(client, json, opts)
|
2021-06-03 18:45:19 +02:00
|
|
|
break
|
2021-06-24 19:16:48 +02:00
|
|
|
case Operation.DELETE:
|
|
|
|
query = buildDelete(client, json, opts)
|
2021-06-03 18:45:19 +02:00
|
|
|
break
|
2021-06-03 17:31:24 +02:00
|
|
|
default:
|
2021-06-04 15:53:49 +02:00
|
|
|
throw `Operation type is not supported by SQL query builder`
|
2021-06-03 17:31:24 +02:00
|
|
|
}
|
2021-06-25 19:13:11 +02:00
|
|
|
|
|
|
|
// @ts-ignore
|
2021-06-03 18:45:19 +02:00
|
|
|
return query.toSQL().toNative()
|
2021-06-03 17:31:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = SqlQueryBuilder
|