This commit is contained in:
Boris D
2025-09-22 18:35:02 +03:00
parent 51f8b0d773
commit fbbbd61838
23 changed files with 282 additions and 67 deletions

View File

@ -19,6 +19,84 @@ export class QueryHandlerService {
private readonly projectService: ProjectService
) {}
private async createOrGetVmModule(
name: string,
sourcePath: string
): Promise<VMModule> {
const vmModule = await this.moduleRepository.findOne({
where: {
name,
},
});
if (vmModule) {
return vmModule;
}
const newModule = this.moduleRepository.create({
sourcePath,
name,
});
return this.moduleRepository.save(newModule);
}
private async createOrGetPlugin(
name: string,
className: string,
query: Query,
config: Record<string, any>
): Promise<VmPlugin> {
const plugin = await this.pluginRepository.findOne({
where: {
name,
query: {
id: query.id,
},
},
});
if (plugin) {
return plugin;
}
const newPlugin = this.pluginRepository.create({
name,
class: className,
config: JSON.stringify(config),
query,
});
return this.pluginRepository.save(newPlugin);
}
// TODO: make it nice
async createDefaults(query: Query): Promise<void> {
const asyncCallModule = await this.createOrGetVmModule(
"asyncCall",
"dist/vm/modules/async.js"
);
const squelModule = await this.createOrGetVmModule(
"query",
"node_modules/squel/dist/squel.min.js"
);
query.modules = [asyncCallModule, squelModule];
const dbPlugin = await this.createOrGetPlugin("db", "DATABASE", query, {
// TODO: take it from database handler
host: process.env.DB_HOST,
user: process.env.DB_USERNAME,
port: process.env.DB_PORT,
password: process.env.DB_PASSWORD,
database: "test",
});
query.plugins = [dbPlugin];
await this.queryRepository.save(query);
}
async createQuery(queryData: { projectToken: string; source: string }) {
const project = await this.projectService.findByToken(
queryData.projectToken
@ -32,7 +110,11 @@ export class QueryHandlerService {
delete queryData.projectToken;
const query = this.queryRepository.create(queryData);
return this.queryRepository.save(query);
await this.queryRepository.save(query);
await this.createDefaults(query);
return query;
}
async updateQuery(id: string, updateData: Partial<Query>) {