swaf/src/db/ModelQuery.ts

421 lines
14 KiB
TypeScript
Raw Normal View History

2020-04-22 15:52:17 +02:00
import {query, QueryResult} from "./MysqlConnectionManager";
import {Connection} from "mysql";
2020-07-24 12:13:28 +02:00
import Model from "./Model";
import Pagination from "../Pagination";
import ModelRelation, {RelationDatabaseProperties} from "./ModelRelation";
2020-07-24 12:13:28 +02:00
import ModelFactory from "./ModelFactory";
2020-04-22 15:52:17 +02:00
2020-09-04 22:15:55 +02:00
export default class ModelQuery<M extends Model> implements WhereFieldConsumer<M> {
2020-09-05 15:33:51 +02:00
public static select<M extends Model>(factory: ModelFactory<M>, ...fields: SelectFields): ModelQuery<M> {
fields = fields.map(v => v === '' ? new SelectFieldValue('none', 1, true) : v);
2020-07-24 12:13:28 +02:00
return new ModelQuery(QueryType.SELECT, factory, fields.length > 0 ? fields : ['*']);
2020-04-22 15:52:17 +02:00
}
2020-07-24 12:13:28 +02:00
public static update<M extends Model>(factory: ModelFactory<M>, data: {
2020-04-22 15:52:17 +02:00
[key: string]: any
}): ModelQuery<M> {
2020-04-22 15:52:17 +02:00
const fields = [];
for (let key in data) {
if (data.hasOwnProperty(key)) {
2020-06-14 11:43:31 +02:00
fields.push(new UpdateFieldValue(key, data[key], false));
2020-04-22 15:52:17 +02:00
}
}
2020-07-24 12:13:28 +02:00
return new ModelQuery(QueryType.UPDATE, factory, fields);
2020-04-22 15:52:17 +02:00
}
2020-07-24 12:13:28 +02:00
public static delete<M extends Model>(factory: ModelFactory<M>): ModelQuery<M> {
return new ModelQuery(QueryType.DELETE, factory);
2020-04-22 15:52:17 +02:00
}
private readonly type: QueryType;
2020-07-24 12:13:28 +02:00
private readonly factory: ModelFactory<M>;
2020-04-22 15:52:17 +02:00
private readonly table: string;
2020-09-05 15:33:51 +02:00
private readonly fields: SelectFields;
2020-06-14 11:43:31 +02:00
private _leftJoin?: string;
2020-09-05 15:33:51 +02:00
private _leftJoinAlias?: string;
2020-06-14 11:43:31 +02:00
private _leftJoinOn: WhereFieldValue[] = [];
2020-09-04 22:15:55 +02:00
private _where: (WhereFieldValue | WhereFieldValueGroup)[] = [];
2020-04-22 15:52:17 +02:00
private _limit?: number;
private _offset?: number;
private _sortBy?: string;
private _sortDirection?: 'ASC' | 'DESC';
private readonly relations: string[] = [];
private _pivot?: string[];
private _recursiveRelation?: RelationDatabaseProperties;
2020-04-22 15:52:17 +02:00
2020-09-05 15:33:51 +02:00
private constructor(type: QueryType, factory: ModelFactory<M>, fields?: SelectFields) {
2020-04-22 15:52:17 +02:00
this.type = type;
2020-07-24 12:13:28 +02:00
this.factory = factory;
this.table = factory.table;
2020-04-22 15:52:17 +02:00
this.fields = fields || [];
}
2020-09-05 15:33:51 +02:00
public leftJoin(table: string, alias?: string): this {
2020-06-14 11:43:31 +02:00
this._leftJoin = table;
2020-09-05 15:33:51 +02:00
this._leftJoinAlias = alias;
2020-04-22 15:52:17 +02:00
return this;
}
2020-06-14 11:43:31 +02:00
public on(field1: string, field2: string, test: WhereTest = WhereTest.EQ, operator: WhereOperator = WhereOperator.AND): this {
this._leftJoinOn.push(new WhereFieldValue(field1, field2.split('.').map(v => `\`${v}\``).join('.'), true, test, operator));
2020-06-14 11:43:31 +02:00
return this;
2020-04-22 15:52:17 +02:00
}
public where(field: string, value: string | Date | ModelQuery<any> | any, test: WhereTest = WhereTest.EQ, operator: WhereOperator = WhereOperator.AND): this {
2020-06-14 11:43:31 +02:00
this._where.push(new WhereFieldValue(field, value, false, test, operator));
return this;
2020-04-22 15:52:17 +02:00
}
2020-09-04 22:15:55 +02:00
public groupWhere(setter: (query: WhereFieldConsumer<M>) => void, operator: WhereOperator = WhereOperator.AND): this {
this._where.push(new WhereFieldValueGroup(this.collectWheres(setter), operator));
return this;
}
private collectWheres(setter: (query: WhereFieldConsumer<M>) => void): (WhereFieldValue | WhereFieldValueGroup)[] {
const query = this;
const wheres: (WhereFieldValue | WhereFieldValueGroup)[] = [];
setter({
where(field: string, value: string | Date | ModelQuery<any> | any, test: WhereTest = WhereTest.EQ, operator: WhereOperator = WhereOperator.AND) {
wheres.push(new WhereFieldValue(field, value, false, test, operator));
return this;
},
groupWhere(setter: (query: WhereFieldConsumer<M>) => void, operator: WhereOperator = WhereOperator.AND) {
wheres.push(new WhereFieldValueGroup(query.collectWheres(setter), operator))
return this;
}
});
return wheres;
}
2020-06-14 11:43:31 +02:00
public limit(limit: number, offset: number = 0): this {
2020-04-22 15:52:17 +02:00
this._limit = limit;
this._offset = offset;
return this;
}
2020-06-14 11:43:31 +02:00
public sortBy(field: string, direction: 'ASC' | 'DESC' = 'ASC'): this {
this._sortBy = field.split('.').map(v => v.startsWith('`') ? v : `\`${v}\``).join('.');
2020-04-22 15:52:17 +02:00
this._sortDirection = direction;
return this;
}
/**
* @param relation The relation field name to eagerload
*/
public with(relation: string): this {
this.relations.push(relation);
return this;
}
public pivot(...fields: string[]): this {
this._pivot = fields;
2020-04-22 15:52:17 +02:00
return this;
}
public recursive(relation: RelationDatabaseProperties): this {
if (this.type !== QueryType.SELECT) throw new Error('Recursive queries are only implemented with SELECT.');
this._recursiveRelation = relation;
return this;
}
2020-04-22 15:52:17 +02:00
public toString(final: boolean = false): string {
let query = '';
2020-09-05 15:33:51 +02:00
if (this._pivot) this.fields.push(...this._pivot);
// Prevent wildcard and fields from conflicting
2020-09-05 15:33:51 +02:00
let fieldArray = this.fields.map(f => {
let field = f.toString();
2020-09-05 15:33:51 +02:00
if (field.startsWith('(')) return f; // Skip sub-queries
2020-09-05 15:33:51 +02:00
let parts = field.split('.');
if (parts.length === 1) parts = [this.table, field]; // Add table disambiguation
return parts.map(v => v.startsWith('`') || v === '*' ? v : `\`${v}\``).join('.');
});
let fields = fieldArray.join(',');
2020-04-22 15:52:17 +02:00
2020-06-14 11:43:31 +02:00
let join = '';
if (this._leftJoin) {
join = ` LEFT JOIN \`${this._leftJoin}\``
+ (this._leftJoinAlias ? ` AS \`${this._leftJoinAlias}\`` : '')
+ ` ON ${this._leftJoinOn[0]}`;
2020-06-14 11:43:31 +02:00
for (let i = 1; i < this._leftJoinOn.length; i++) {
join += this._leftJoinOn[i].toString(false);
}
}
2020-04-22 15:52:17 +02:00
let where = '';
if (this._where.length > 0) {
where = ` WHERE ${this._where[0]}`;
2020-04-22 15:52:17 +02:00
for (let i = 1; i < this._where.length; i++) {
where += this._where[i].toString(false);
}
}
let limit = '';
if (typeof this._limit === 'number') {
limit = ` LIMIT ${this._limit}`;
2020-04-22 15:52:17 +02:00
if (typeof this._offset === 'number' && this._offset !== 0) {
limit += ` OFFSET ${this._offset}`;
}
}
let orderBy = '';
if (typeof this._sortBy === 'string') {
orderBy = ` ORDER BY ${this._sortBy}`
2020-09-06 10:47:24 +02:00
+ (this._sortDirection ? ' ' + this._sortDirection : '');
2020-04-22 15:52:17 +02:00
}
const table = `\`${this.table}\``;
2020-04-22 15:52:17 +02:00
switch (this.type) {
case QueryType.SELECT:
if (this._recursiveRelation) {
const cteFields = fields.replace(RegExp(`${table}`, 'g'), 'o');
query = `WITH RECURSIVE cte AS (`
+ `SELECT ${fields} FROM ${table}${where}`
+ ` UNION `
+ `SELECT ${cteFields} FROM ${table} AS o, cte AS c WHERE o.\`${this._recursiveRelation.foreignKey}\`=c.\`${this._recursiveRelation.localKey}\``
+ `) SELECT * FROM cte${join}${orderBy}${limit}`;
} else {
query = `SELECT ${fields} FROM ${table}${join}${where}${orderBy}${limit}`;
}
2020-04-22 15:52:17 +02:00
break;
case QueryType.UPDATE:
query = `UPDATE ${table} SET ${fields}${where}${orderBy}${limit}`;
2020-04-22 15:52:17 +02:00
break;
case QueryType.DELETE:
query = `DELETE FROM ${table}${where}${orderBy}${limit}`;
2020-04-22 15:52:17 +02:00
break;
}
return final ? query : `(${query})`;
}
public build(): string {
return this.toString(true);
}
public get variables(): any[] {
const variables: any[] = [];
this.fields?.filter(v => v instanceof FieldValue)
.flatMap(v => (<FieldValue>v).variables)
.forEach(v => variables.push(v));
2020-09-04 22:15:55 +02:00
this._where.flatMap(v => this.getVariables(v))
2020-04-22 15:52:17 +02:00
.forEach(v => variables.push(v));
return variables;
}
2020-09-04 22:15:55 +02:00
private getVariables(where: WhereFieldValue | WhereFieldValueGroup): any[] {
return where instanceof WhereFieldValueGroup ?
where.fields.flatMap(v => this.getVariables(v)) :
where.variables;
}
public async execute(connection?: Connection): Promise<QueryResult> {
return await query(this.build(), this.variables, connection);
2020-04-22 15:52:17 +02:00
}
public async get(connection?: Connection): Promise<ModelQueryResult<M>> {
const queryResult = await this.execute(connection);
const models: ModelQueryResult<M> = [];
models.originalData = [];
if (this._pivot) models.pivot = [];
// Eager loading init
const relationMap: { [p: string]: ModelRelation<any, any, any>[] } = {};
for (const relation of this.relations) {
relationMap[relation] = [];
}
for (const result of queryResult.results) {
const modelData: any = {};
for (const field of Object.keys(result)) {
modelData[field.split('.')[1] || field] = result[field];
}
const model = this.factory.create(modelData, false);
models.push(model);
models.originalData.push(modelData);
if (this._pivot) {
const pivotData: any = {};
for (const field of this._pivot) {
pivotData[field] = result[field.split('.')[1]];
}
models.pivot!.push(pivotData);
}
// Eager loading init map
for (const relation of this.relations) {
if (model[relation] === undefined) throw new Error(`Relation ${relation} doesn't exist on ${model.constructor.name}.`);
relationMap[relation].push(model[relation]);
}
}
// Eager loading execute
for (const relationName of this.relations) {
const relations = relationMap[relationName];
2020-06-27 17:11:31 +02:00
if (relations.length > 0) {
const allModels = await relations[0].eagerLoad(relations);
await Promise.all(relations.map(r => r.populate(allModels)));
}
}
return models;
}
public async paginate(page: number, perPage: number, connection?: Connection): Promise<ModelQueryResult<M>> {
this.limit(perPage, (page - 1) * perPage);
const result = await this.get(connection);
result.pagination = new Pagination<M>(result, page, perPage, await this.count(true, connection));
return result;
}
public async first(): Promise<M | null> {
const models = await this.limit(1).get();
return models.length > 0 ? models[0] : null;
}
public async count(removeLimit: boolean = false, connection?: Connection): Promise<number> {
if (removeLimit) {
this._limit = undefined;
this._offset = undefined;
2020-04-22 15:52:17 +02:00
}
this._sortBy = undefined;
this._sortDirection = undefined;
this.fields.splice(0, this.fields.length);
2020-09-04 18:51:41 +02:00
this.fields.push(new SelectFieldValue('_count', 'COUNT(*)', true));
let queryResult = await this.execute(connection);
2020-09-04 18:51:41 +02:00
return queryResult.results[0]['_count'];
2020-04-22 15:52:17 +02:00
}
}
export interface ModelQueryResult<M extends Model> extends Array<M> {
originalData?: any[];
pagination?: Pagination<M>;
pivot?: { [p: string]: any }[];
}
2020-04-22 15:52:17 +02:00
export enum QueryType {
SELECT,
UPDATE,
DELETE,
}
2020-06-14 11:43:31 +02:00
export enum WhereOperator {
2020-04-22 15:52:17 +02:00
AND = 'AND',
OR = 'OR',
}
2020-06-14 11:43:31 +02:00
export enum WhereTest {
EQ = '=',
NE = '!=',
GT = '>',
GE = '>=',
LT = '<',
LE = '<=',
2020-04-22 15:52:17 +02:00
IN = ' IN ',
}
class FieldValue {
protected readonly field: string;
protected value: any;
2020-06-14 11:43:31 +02:00
protected raw: boolean;
2020-04-22 15:52:17 +02:00
2020-06-14 11:43:31 +02:00
constructor(field: string, value: any, raw: boolean) {
2020-04-22 15:52:17 +02:00
this.field = field;
this.value = value;
2020-06-14 11:43:31 +02:00
this.raw = raw;
2020-04-22 15:52:17 +02:00
}
public toString(first: boolean = true): string {
2020-09-04 18:51:41 +02:00
const valueStr = (this.raw || this.value === null || this.value instanceof ModelQuery) ? this.value :
(Array.isArray(this.value) ? `(${'?'.repeat(this.value.length).split('').join(',')})` : '?');
let field = this.field.split('.').map(p => `\`${p}\``).join('.');
return `${first ? '' : ','}${field}${this.test}${valueStr}`;
2020-04-22 15:52:17 +02:00
}
protected get test(): string {
return '=';
}
public get variables(): any[] {
if (this.value instanceof ModelQuery) return this.value.variables;
if (this.raw || this.value === null) return [];
if (Array.isArray(this.value)) return this.value;
return [this.value];
2020-04-22 15:52:17 +02:00
}
}
2020-09-05 15:33:51 +02:00
export class SelectFieldValue extends FieldValue {
2020-04-22 15:52:17 +02:00
public toString(first: boolean = true): string {
2020-09-04 18:51:41 +02:00
return `(${this.value instanceof ModelQuery ? this.value : (this.raw ? this.value : '?')}) AS \`${this.field}\``;
2020-04-22 15:52:17 +02:00
}
}
class UpdateFieldValue extends FieldValue {
}
class WhereFieldValue extends FieldValue {
private readonly _test: WhereTest;
2020-06-14 11:43:31 +02:00
private readonly operator: WhereOperator;
2020-04-22 15:52:17 +02:00
2020-06-14 11:43:31 +02:00
constructor(field: string, value: any, raw: boolean, test: WhereTest, operator: WhereOperator) {
super(field, value, raw);
2020-04-22 15:52:17 +02:00
this._test = test;
2020-06-14 11:43:31 +02:00
this.operator = operator;
2020-04-22 15:52:17 +02:00
}
public toString(first: boolean = true): string {
return (!first ? ` ${this.operator} ` : '') + super.toString(true);
}
protected get test(): string {
if (this.value === null) {
if (this._test === WhereTest.EQ) {
return ' IS ';
} else if (this._test === WhereTest.NE) {
return ' IS NOT ';
}
}
2020-04-22 15:52:17 +02:00
return this._test;
}
2020-09-04 22:15:55 +02:00
}
class WhereFieldValueGroup {
public readonly fields: (WhereFieldValue | WhereFieldValueGroup)[];
public readonly operator: WhereOperator;
public constructor(fields: (WhereFieldValue | WhereFieldValueGroup)[], operator: WhereOperator) {
this.fields = fields;
this.operator = operator;
}
public toString(first: boolean = true): string {
let str = `${first ? '' : ` ${this.operator} `}(`;
let firstField = true;
for (const field of this.fields) {
str += field.toString(firstField);
firstField = false;
}
str += ')';
return str;
}
}
interface WhereFieldConsumer<M extends Model> {
where(field: string, value: string | Date | ModelQuery<any> | any, test?: WhereTest, operator?: WhereOperator): this;
groupWhere(setter: (query: WhereFieldConsumer<M>) => void, operator?: WhereOperator): this;
}
2020-09-05 15:33:51 +02:00
export type SelectFields = (string | SelectFieldValue | UpdateFieldValue)[];