DB
This commit is contained in:
@ -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>) {
|
||||
|
||||
Reference in New Issue
Block a user