47 lines
1.2 KiB
TypeScript
47 lines
1.2 KiB
TypeScript
import { QueryExecuterService } from "../../query/executer/query.executer.service";
|
|
import { Query } from "../../query/entities/query.entity";
|
|
import { Plugin } from "../plugin.class";
|
|
|
|
export class QueryPlugin extends Plugin {
|
|
constructor(
|
|
name: string,
|
|
private query: Query,
|
|
private QueryExecuterService: QueryExecuterService
|
|
) {
|
|
super(name, ["run"]);
|
|
}
|
|
|
|
static async init(query: Query, queryExecuterService: QueryExecuterService) {
|
|
return new QueryPlugin("query", query, queryExecuterService);
|
|
}
|
|
|
|
async run(id: string, data: any): Promise<any> {
|
|
const query = await this.QueryExecuterService.queryRepository.findOne({
|
|
where: { id: this.query.id },
|
|
});
|
|
|
|
if (!query) {
|
|
throw new Error(`Query with id ${this.query.id} not found`);
|
|
}
|
|
|
|
if (query.isCommand && !this.query.isCommand) {
|
|
throw new Error(
|
|
`Query with id ${this.query.id} is a command and cannot be called from query`
|
|
);
|
|
}
|
|
|
|
return await this.QueryExecuterService.runQuery(
|
|
id,
|
|
data,
|
|
this.callStack + 1,
|
|
this.headers,
|
|
this.cookies,
|
|
this.log
|
|
);
|
|
}
|
|
|
|
onFinish() {
|
|
// No resources to clean up
|
|
}
|
|
}
|